/** * Checks if there is a default constructor available. * * @param item The clazz to check. * @return True if default constructor is found, false otherwise. */ public static boolean hasDefaultConstructor(TypeDef item) { if (item == null) { return false; } else if (item.getConstructors().isEmpty()) { return true; } else { for (Method constructor : item.getConstructors()) { if (constructor.getArguments().size() == 0) { return true; } } } return false; }
public static Method findBuildableConstructor(TypeDef clazz) { //1st pass go for annotated method for (Method candidate : clazz.getConstructors()) { if (hasBuildableAnnotation(candidate)) { return candidate; } } //2nd pass go for the first non-empty constructor for (Method candidate : clazz.getConstructors()) { if (candidate.getArguments().size() != 0) { return candidate; } } if (!clazz.getConstructors().isEmpty()) { return clazz.getConstructors().iterator().next(); } else { throw new IllegalStateException("Could not find buildable constructor in: ["+clazz.getFullyQualifiedName()+"]."); } }
/** * Checks that a constructor with the required number of arguments is found. * @param arguments */ private void checkConstructorArguments(int arguments) { if (arguments == 0 && (typeDef.getConstructors() == null || typeDef.getConstructors().isEmpty())) { return; } for (Method m : typeDef.getConstructors()) { int a = m.getArguments() != null ? m.getArguments().size() : 0; if (a == arguments) { return; } } throw new IllegalArgumentException("No constructor found for " + typeDef.getName() + " with " + arguments + " arguments."); }
public static Set<Method> getInlineableConstructors(Property property) { Set<Method> result = new HashSet<Method>(); TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(property.getTypeRef()); if (unwrapped instanceof ClassRef) { for (Method candidate : ((ClassRef)unwrapped).getDefinition().getConstructors()) { if (isInlineable(candidate)) { result.add(candidate); } } } //We try both types to make sure... TypeDef fromRepo = BuilderContextManager.getContext().getDefinitionRepository().getDefinition(unwrapped); if (fromRepo != null) { for (Method candidate : fromRepo.getConstructors()) { if (isInlineable(candidate)) { result.add(candidate); } } } return result; }
public TypeDef apply(TypeDef... items) { if (items == null || items.length == 0) { throw new IllegalArgumentException("Items cannot be empty."); } else if (items.length == 1) { return items[0]; } else if (items.length == 2) { TypeDef mergedType = TYPES.apply(new TypeDef[]{items[0], items[1]}); TypeDefBuilder builder = new TypeDefBuilder(mergedType); for (Method constructor : items[1].getConstructors()) { builder = builder.addToConstructors(constructor); } for (Method method : items[1].getMethods()) { builder = builder.addToMethods(method); } for (Property property : items[1].getProperties()) { builder = builder.addToProperties(property); } return builder.build(); } else { TypeDef[] rest = new TypeDef[items.length - 1]; System.arraycopy(items, 1, rest, 0, rest.length); CLASSES.apply(new TypeDef[]{items[0], CLASSES.apply(rest)}); } return null; } };
List<Method> methods = new ArrayList<Method>(); for (Method constructor : item.getConstructors()) { constructors.add(superConstructorOf(constructor, editableType));
public TypeDefBuilder(TypeDef instance,Boolean validationEnabled){ this.fluent = this; this.withKind(instance.getKind()); this.withPackageName(instance.getPackageName()); this.withName(instance.getName()); this.withComments(instance.getComments()); this.withAnnotations(instance.getAnnotations()); this.withExtendsList(instance.getExtendsList()); this.withImplementsList(instance.getImplementsList()); this.withParameters(instance.getParameters()); this.withProperties(instance.getProperties()); this.withConstructors(instance.getConstructors()); this.withMethods(instance.getMethods()); this.withOuterType(instance.getOuterType()); this.withInnerTypes(instance.getInnerTypes()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; }
public TypeDefFluentImpl(TypeDef instance){ this.withKind(instance.getKind()); this.withPackageName(instance.getPackageName()); this.withName(instance.getName()); this.withComments(instance.getComments()); this.withAnnotations(instance.getAnnotations()); this.withExtendsList(instance.getExtendsList()); this.withImplementsList(instance.getImplementsList()); this.withParameters(instance.getParameters()); this.withProperties(instance.getProperties()); this.withConstructors(instance.getConstructors()); this.withMethods(instance.getMethods()); this.withOuterType(instance.getOuterType()); this.withInnerTypes(instance.getInnerTypes()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); }
public TypeDefBuilder(TypeDefFluent<?> fluent,TypeDef instance,Boolean validationEnabled){ this.fluent = fluent; fluent.withKind(instance.getKind()); fluent.withPackageName(instance.getPackageName()); fluent.withName(instance.getName()); fluent.withComments(instance.getComments()); fluent.withAnnotations(instance.getAnnotations()); fluent.withExtendsList(instance.getExtendsList()); fluent.withImplementsList(instance.getImplementsList()); fluent.withParameters(instance.getParameters()); fluent.withProperties(instance.getProperties()); fluent.withConstructors(instance.getConstructors()); fluent.withMethods(instance.getMethods()); fluent.withOuterType(instance.getOuterType()); fluent.withInnerTypes(instance.getInnerTypes()); fluent.withModifiers(instance.getModifiers()); fluent.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; } public TypeDefBuilder(TypeDef instance){