-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3acb36
commit 3ec4fa3
Showing
17 changed files
with
459 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/fuin/objects4j/common/HasPublicStaticIsValidMethodValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.fuin.objects4j.common; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
|
||
/** | ||
* Determines if the annotated class has: | ||
* - a public static method with the given name | ||
* - a parameter with the given type | ||
* defined in the annotation and the return type of that method is {@literal boolean}. | ||
*/ | ||
public class HasPublicStaticIsValidMethodValidator implements ConstraintValidator<HasPublicStaticIsValidMethod, Object> { | ||
|
||
private String methodName; | ||
|
||
private Class<?> paramClass; | ||
|
||
@Override | ||
public void initialize(HasPublicStaticIsValidMethod annotation) { | ||
this.methodName = annotation.method(); | ||
this.paramClass = annotation.param(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object obj, ConstraintValidatorContext context) { | ||
try { | ||
final Method method = obj.getClass().getMethod(methodName, paramClass); | ||
final int modifiers = method.getModifiers(); | ||
if (!Modifier.isStatic(modifiers)) { | ||
error(context, "Method '" + methodName + "' is not static (#1)"); | ||
return false; | ||
} | ||
if (method.getReturnType() != boolean.class) { | ||
error(context, "Method '" + methodName + "' does not return 'boolean', but: " + method.getReturnType().getName() + " (#3)"); | ||
return false; | ||
} | ||
final boolean valid = (boolean) method.invoke(obj, (Object) null); | ||
if (!valid) { | ||
error(context, "Method '" + methodName + "' is expected to return 'true' on 'null' argument, but was 'false' (#4)"); | ||
return false; | ||
} | ||
return true; | ||
} catch (final NoSuchMethodException ex) { | ||
error(context, "The method '" + methodName + "' is undefined or it is not public (#2)"); | ||
return false; | ||
} catch (final IllegalAccessException | InvocationTargetException ex) { | ||
throw new IllegalStateException("Failed to execute method", ex); | ||
} | ||
|
||
} | ||
|
||
private void error(ConstraintValidatorContext context, String message) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/fuin/objects4j/common/HasPublicStaticIsValidMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.fuin.objects4j.common; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Collection of {@link HasPublicStaticIsValidMethod} annotations. | ||
*/ | ||
@Documented | ||
@Target(ElementType.TYPE) | ||
@Inherited | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface HasPublicStaticIsValidMethods { | ||
|
||
/** | ||
* Returns a list of {@link HasPublicStaticIsValidMethod} instances. | ||
* | ||
* @return Array of annotations. | ||
*/ | ||
HasPublicStaticIsValidMethod[] value(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/fuin/objects4j/common/HasPublicStaticValueOfMethodValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.fuin.objects4j.common; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
|
||
/** | ||
* Determines if the annotated class has: | ||
* - a public static method with the given name | ||
* - a parameter with the given type | ||
* defined in the annotation and the return type of that method is the same as the annotated class. | ||
*/ | ||
public class HasPublicStaticValueOfMethodValidator implements ConstraintValidator<HasPublicStaticValueOfMethod, Object> { | ||
|
||
private String methodName; | ||
|
||
private Class<?> paramClass; | ||
|
||
@Override | ||
public void initialize(HasPublicStaticValueOfMethod annotation) { | ||
this.methodName = annotation.method(); | ||
this.paramClass = annotation.param(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object obj, ConstraintValidatorContext context) { | ||
try { | ||
final Method method = obj.getClass().getMethod(methodName, paramClass); | ||
final int modifiers = method.getModifiers(); | ||
if (!Modifier.isStatic(modifiers)) { | ||
error(context, "Method '" + methodName + "' is not static (#1)"); | ||
return false; | ||
} | ||
if (method.getReturnType() != obj.getClass()) { | ||
error(context, "Method '" + methodName + "' does not return '" + obj.getClass().getName() + "', but: " + method.getReturnType().getName() + " (#3)"); | ||
return false; | ||
} | ||
final Object value = method.invoke(obj, (Object) null); | ||
if (value != null) { | ||
error(context, "Method '" + methodName + "' is expected to return 'true' on 'null' argument, but was: " + value + " (#4)"); | ||
return false; | ||
} | ||
return true; | ||
} catch (final NoSuchMethodException ex) { | ||
error(context, "The method '" + methodName + "' is undefined or it is not public (#2)"); | ||
return false; | ||
} catch (final IllegalAccessException | InvocationTargetException ex) { | ||
throw new IllegalStateException("Failed to execute method", ex); | ||
} | ||
|
||
} | ||
|
||
private void error(ConstraintValidatorContext context, String message) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/main/java/org/fuin/objects4j/common/IsValidCapables.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.