use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method constraintDeclarations.
private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(ArrayList<Type> allTypes) {
// Find all constraints and flatten them into an iterable
Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {
@Override
public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
Constraints constraints = Annotations.annotationOn(type, Constraints.class);
if (constraints == null) {
return empty();
} else {
return iterable(constraints.value());
}
}
};
Iterable<Class<? extends Constraint<?, ?>>> flatten = flattenIterables(map(function, allTypes));
return toList(flatten);
}
use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method buildComposite.
protected void buildComposite(AssemblyHelper helper, StateDeclarations stateDeclarations) {
this.stateDeclarations = stateDeclarations;
this.helper = helper;
for (Class<?> compositeType : types) {
metaInfo = new MetaInfo(metaInfo).withAnnotations(compositeType);
addAnnotationsMetaInfo(compositeType, metaInfo);
}
immutable = metaInfo.get(Immutable.class) != null;
propertiesModel = new PropertiesModel();
stateModel = createStateModel();
mixinsModel = createMixinsModel();
compositeMethodsModel = new CompositeMethodsModel(mixinsModel);
// Implement composite methods
Iterable<Class<? extends Constraint<?, ?>>> constraintClasses = constraintDeclarations(this.types);
Iterable<Class<?>> concernClasses = Iterables.flatten(concerns, concernDeclarations(this.types));
Iterable<Class<?>> sideEffectClasses = Iterables.flatten(sideEffects, sideEffectDeclarations(this.types));
Iterable<Class<?>> mixinClasses = Iterables.flatten(mixins, mixinDeclarations(this.types));
implementMixinType(types, constraintClasses, concernClasses, sideEffectClasses, mixinClasses);
// Add state from methods and fields
addState(constraintClasses);
}
use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method constraintsFor.
protected ValueConstraintsModel constraintsFor(Iterable<Annotation> constraintAnnotations, Type valueType, String name, boolean optional, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses, AccessibleObject accessor) {
valueType = wrapperClass(valueType);
List<AbstractConstraintModel> constraintModels = new ArrayList<AbstractConstraintModel>();
nextConstraint: for (Annotation constraintAnnotation : filter(translate(type(), hasAnnotation(ConstraintDeclaration.class)), constraintAnnotations)) {
// Check composite declarations first
Class<? extends Annotation> annotationType = constraintAnnotation.annotationType();
for (Class<? extends Constraint<?, ?>> constraint : constraintClasses) {
if (helper.appliesTo(constraint, annotationType, valueType)) {
constraintModels.add(new ConstraintModel(constraintAnnotation, constraint));
continue nextConstraint;
}
}
// Check the annotation itself
Constraints constraints = annotationType.getAnnotation(Constraints.class);
if (constraints != null) {
for (Class<? extends Constraint<?, ?>> constraintClass : constraints.value()) {
if (helper.appliesTo(constraintClass, annotationType, valueType)) {
constraintModels.add(new ConstraintModel(constraintAnnotation, constraintClass));
continue nextConstraint;
}
}
}
// No implementation found!
// Check if if it's a composite constraints
Iterable<Annotation> annotations = iterable(annotationType.getAnnotations());
if (Iterables.matchesAny(translate(type(), hasAnnotation(ConstraintDeclaration.class)), annotations)) {
ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, valueType, name, optional, constraintClasses, accessor);
CompositeConstraintModel compositeConstraintModel = new CompositeConstraintModel(constraintAnnotation, valueConstraintsModel);
constraintModels.add(compositeConstraintModel);
continue nextConstraint;
}
throw new InvalidCompositeException("Cannot find implementation of constraint @" + annotationType.getSimpleName() + " for " + valueType + " in method " + ((Member) accessor).getName() + " of composite " + types);
}
return new ValueConstraintsModel(constraintModels, name, optional);
}
use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method constraintDeclarations.
private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(Iterable<? extends Class<?>> typess) {
// Find constraint declarations
List<Type> allTypes = new ArrayList<Type>();
for (Class<?> type : typess) {
Iterable<Type> types = typesOf(type);
Iterables.addAll(allTypes, types);
}
// Find all constraints and flatten them into an iterable
Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {
@Override
public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
Constraints constraints = Annotations.annotationOn(type, Constraints.class);
if (constraints == null) {
return Iterables.empty();
} else {
return iterable(constraints.value());
}
}
};
Iterable<Class<? extends Constraint<?, ?>>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
return Iterables.toList(flatten);
}
use of org.qi4j.api.constraint.Constraint in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method constraintsFor.
// Model
private ConstraintsModel constraintsFor(Method method, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
List<ValueConstraintsModel> parameterConstraintModels = Collections.emptyList();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Type[] parameterTypes = method.getGenericParameterTypes();
boolean constrained = false;
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] parameterAnnotation = parameterAnnotations[i];
Name nameAnnotation = (Name) Iterables.first(Iterables.filter(isType(Name.class), iterable(parameterAnnotation)));
String name = nameAnnotation == null ? "param" + (i + 1) : nameAnnotation.value();
boolean optional = Iterables.first(Iterables.filter(isType(Optional.class), iterable(parameterAnnotation))) != null;
ValueConstraintsModel parameterConstraintsModel = constraintsFor(Arrays.asList(parameterAnnotation), parameterTypes[i], name, optional, constraintClasses, method);
if (parameterConstraintsModel.isConstrained()) {
constrained = true;
}
if (parameterConstraintModels.isEmpty()) {
parameterConstraintModels = new ArrayList<ValueConstraintsModel>();
}
parameterConstraintModels.add(parameterConstraintsModel);
}
if (!constrained) {
return new ConstraintsModel(Collections.<ValueConstraintsModel>emptyList());
} else {
return new ConstraintsModel(parameterConstraintModels);
}
}
Aggregations