/** * Return a set of executable types contained in {@link ValidateOnExecution} annotation belonging to the {@code element}. * * @param element element to be examined for {@link ValidateOnExecution}. * @return set of executable types or an empty set if the element is not annotated with {@link ValidateOnExecution}. */ private Set<ExecutableType> getExecutableTypes(final AnnotatedElement element) { final ValidateOnExecution validateExecutable = element.getAnnotation(ValidateOnExecution.class); return validateExecutable != null ? Arrays.stream(validateExecutable.type()).collect(Collectors.toSet()) : Collections.emptySet(); }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.ALL) public interface DeliveryService { public void createDelivery(@NotNull String name); }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.GETTER_METHODS) public interface OrderService { public Order placeOrder(@NotNull String name); @NotNull public Order getOrder(); }
protected static ExecutableType[] getExecutableTypesOnMethod(Method method) { ValidateOnExecution voe = method.getAnnotation(ValidateOnExecution.class); if (voe == null || voe.type().length == 0) { return null; } ExecutableType[] types = voe.type(); if (types == null || types.length == 0) { return null; } return types; }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.ALL) public interface ShipmentService { public void findShipment(@NotNull String id); @ValidateOnExecution(type = ExecutableType.GETTER_METHODS) @NotNull public Shipment getShipment(); @NotNull public Shipment getAnotherShipment(); }
private EnumSet<ExecutableType> commonExecutableTypeChecks(ValidateOnExecution validateOnExecutionAnnotation) { if ( validateOnExecutionAnnotation == null ) { return EnumSet.noneOf( ExecutableType.class ); } EnumSet<ExecutableType> executableTypes = EnumSet.noneOf( ExecutableType.class ); if ( validateOnExecutionAnnotation.type().length == 0 ) { // HV-757 executableTypes.add( ExecutableType.NONE ); } else { Collections.addAll( executableTypes, validateOnExecutionAnnotation.type() ); } // IMPLICIT cannot be mixed 10.1.2 of spec - Mixing IMPLICIT and other executable types is illegal if ( executableTypes.contains( ExecutableType.IMPLICIT ) && executableTypes.size() > 1 ) { throw log.getMixingImplicitWithOtherExecutableTypesException(); } // NONE can be removed 10.1.2 of spec - A list containing NONE and other types of executables is equivalent to a // list containing the types of executables without NONE. if ( executableTypes.contains( ExecutableType.NONE ) && executableTypes.size() > 1 ) { executableTypes.remove( ExecutableType.NONE ); } // 10.1.2 of spec - A list containing ALL and other types of executables is equivalent to a list containing only ALL if ( executableTypes.contains( ExecutableType.ALL ) ) { executableTypes = ALL_EXECUTABLE_TYPES; } return executableTypes; }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.IMPLICIT) public class AnotherShipmentService1stInHierarchyImpl implements AnotherShipmentService1stInHierarchy { @Override public Shipment getShipment() { return null; } }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.NONE) public class OrderLine { public OrderLine(@NotNull String item) { } public void setItem(@NotNull String item) { } }
/** * Return a set of executable types contained in {@link ValidateOnExecution} annotation belonging to the {@code element}. * * @param element element to be examined for {@link ValidateOnExecution}. * @return set of executable types or an empty set if the element is not annotated with {@link ValidateOnExecution}. */ private Set<ExecutableType> getExecutableTypes(final AnnotatedElement element) { final ValidateOnExecution validateExecutable = element.getAnnotation(ValidateOnExecution.class); return validateExecutable != null ? Arrays.stream(validateExecutable.type()).collect(Collectors.toSet()) : Collections.emptySet(); }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.ALL) public class CalendarService { public Event createEvent(@NotNull String title) { return new Event(); } @NotNull public Event getEvent() { return null; } }
private EnumSet<ExecutableType> commonExecutableTypeChecks(ValidateOnExecution validateOnExecutionAnnotation) { if ( validateOnExecutionAnnotation == null ) { return EnumSet.noneOf( ExecutableType.class ); } EnumSet<ExecutableType> executableTypes = EnumSet.noneOf( ExecutableType.class ); if ( validateOnExecutionAnnotation.type().length == 0 ) { // HV-757 executableTypes.add( ExecutableType.NONE ); } else { Collections.addAll( executableTypes, validateOnExecutionAnnotation.type() ); } // IMPLICIT cannot be mixed 10.1.2 of spec - Mixing IMPLICIT and other executable types is illegal if ( executableTypes.contains( ExecutableType.IMPLICIT ) && executableTypes.size() > 1 ) { throw log.getMixingImplicitWithOtherExecutableTypesException(); } // NONE can be removed 10.1.2 of spec - A list containing NONE and other types of executables is equivalent to a // list containing the types of executables without NONE. if ( executableTypes.contains( ExecutableType.NONE ) && executableTypes.size() > 1 ) { executableTypes.remove( ExecutableType.NONE ); } // 10.1.2 of spec - A list containing ALL and other types of executables is equivalent to a list containing only ALL if ( executableTypes.contains( ExecutableType.ALL ) ) { executableTypes = ALL_EXECUTABLE_TYPES; } return executableTypes; }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.NONE) public class LineItem { @ValidLineItem public LineItem(@NotNull String name) { } @NotNull public String setName(@NotNull String name) { return null; } }
private <T> boolean computeIsConstructorValidated(Class<T> targetClass, Constructor<T> ctor) { final AnnotatedType<T> annotatedType = CDI.current().getBeanManager().createAnnotatedType(ctor.getDeclaringClass()); final ValidateOnExecution annotation = annotatedType.getConstructors().stream().filter(ac -> ctor.equals(ac.getJavaMember())).findFirst() .map(ac -> ac.getAnnotation(ValidateOnExecution.class)) .orElseGet(() -> ctor.getAnnotation(ValidateOnExecution.class)); final Set<ExecutableType> validatedExecutableTypes = annotation == null ? classConfiguration : ExecutableTypes.interpret(annotation.type()); return validatedExecutableTypes.contains(ExecutableType.CONSTRUCTORS); }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.GETTER_METHODS) public class ClassLevelAnnotatedCalendarService { public Event createEvent(@NotNull String name) { return new Event(); } @NotNull public Event getEvent() { return null; } }
private <T> boolean computeIsConstructorValidated(Class<T> targetClass, Constructor<T> ctor) { final AnnotatedType<T> annotatedType = CDI.current().getBeanManager().createAnnotatedType(ctor.getDeclaringClass()); final ValidateOnExecution annotation = annotatedType.getConstructors().stream().filter(ac -> ctor.equals(ac.getJavaMember())).findFirst() .map(ac -> ac.getAnnotation(ValidateOnExecution.class)) .orElseGet(() -> ctor.getAnnotation(ValidateOnExecution.class)); final Set<ExecutableType> validatedExecutableTypes = annotation == null ? classConfiguration : ExecutableTypes.interpret(annotation.type()); return validatedExecutableTypes.contains(ExecutableType.CONSTRUCTORS); }
/** * @author Gunnar Morling */ @ValidateOnExecution(type = ExecutableType.NONE) public class AnnotatedCalendarService { @ValidateOnExecution(type = ExecutableType.NON_GETTER_METHODS) public void createEvent(@NotNull String name) { } @ValidateOnExecution(type = ExecutableType.GETTER_METHODS) public Event createEvent(@Min(0) int duration) { return new Event(); } }
@Override public void handleMessage(Message message) throws Fault { final Object theServiceObject = getServiceObject(message); if (theServiceObject == null) { return; } final Method method = getServiceMethod(message); if (method == null) { return; } ValidateOnExecution validateOnExec = method.getAnnotation(ValidateOnExecution.class); if (validateOnExec != null) { ExecutableType[] execTypes = validateOnExec.type(); if (execTypes.length == 1 && execTypes[0] == ExecutableType.NONE) { return; } } final List< Object > arguments = MessageContentsList.getContentsList(message); handleValidation(message, theServiceObject, method, arguments); }
/** * @author Gunnar Morling */ @ValidateOnExecution public class OrderServiceImpl implements OrderService { @Override public Order placeOrder(String name) { return new Order(); } @Override public Order getOrder() { return null; } }
@Override public void handleMessage(Message message) throws Fault { final Object theServiceObject = getServiceObject(message); if (theServiceObject == null) { return; } final Method method = getServiceMethod(message); if (method == null) { return; } ValidateOnExecution validateOnExec = method.getAnnotation(ValidateOnExecution.class); if (validateOnExec != null) { ExecutableType[] execTypes = validateOnExec.type(); if (execTypes.length == 1 && execTypes[0] == ExecutableType.NONE) { return; } } final List< Object > arguments = MessageContentsList.getContentsList(message); handleValidation(message, theServiceObject, method, arguments); }