/** * Checks if method has a specific argument. * @param method The method. * @param property The argument. * @return True if matching argument if found. */ public static boolean methodHasArgument(Method method, Property property) { for (Property candidate : method.getArguments()) { if (candidate.equals(property)) { return true; } } return false; }
/** * Converts a reference from the source type, to the target. * @param ref The ref. * @param source The source type of the reference.. * @param target The target type. * @return */ private static String convertReference(String ref, TypeDef source, TypeDef target) { Method ctor = BuilderUtils.findBuildableConstructor(target); String arguments = ctor.getArguments().stream() .map(p -> readProperty(ref, source, p)) .collect(joining(", ")); StringBuilder sb = new StringBuilder(); sb.append("new ").append(target.getFullyQualifiedName()).append("(").append(arguments).append(")"); return sb.toString(); }
/** * Checks that a factory method with the required number of arguments is found. * @param arguments */ private void checkFactoryMethodArguments(int arguments) { for (Method m : typeDef.getMethods()) { int a = m.getArguments() != null ? m.getArguments().size() : 0; if (m.getName().equals(staticFactoryMethod) && a == arguments && m.isStatic()) { return; } } throw new IllegalArgumentException("No static method found on " + typeDef.getName() + " with name " + staticFactoryMethod + " and " + arguments + " arguments."); } }
public static boolean isInlineable(Method method) { if (method.getArguments().size() == 0 || method.getArguments().size() > 5) { return false; } if (method.isPrivate()) { return false; } for (Property argument : method.getArguments()) { if (!(argument.getTypeRef() instanceof ClassRef)) { continue; } else if (((ClassRef)argument.getTypeRef()).getDefinition().getFullyQualifiedName().startsWith("java.lang")) { continue; } else { return false; } } return true; }
private static final Method getterOf(TypeDef source, Property property) { Method result = source.getMethods().stream() .filter(m -> m.isPublic() && m.getArguments().size() == 0 && Getter.propertyNameSafe(m).equals(property.getName())) .findFirst() .orElse(null); return result; }
public static Collection<String> allGenericsOf(Method method) { Set<String> result = new HashSet<String>(allGenericsOf(method.getReturnType())); for (Property property : method.getArguments()) { result.addAll(allGenericsOf(property)); } return result; }
/** * 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."); }
/** * 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; }
/** * Returns true if method is a getter of property. * In strict mode it will not strip non-alphanumeric characters. */ private static boolean isApplicable(Method method, Property property, boolean strict) { if (method.getArguments().size() != 1) { return false; } if (!method.getArguments().get(0).getTypeRef().equals(property.getTypeRef())) { return false; } String capitalized = capitalizeFirst(property.getName()); if (method.getName().endsWith("set" + capitalized)) { return true; } if (!strict && method.getName().endsWith("set" + property.getNameCapitalized())) { 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()+"]."); } }
for (Property property : constructor.getArguments()) { Method getter = Getter.find(clazz, property); if (getter != null) {
.append(StringUtils.join(constructor.getArguments(), new Function<Property, String>() { public String apply(Property item) { return "fluent." + Getter.name(item) + "()";
.append(".stream().map(i ->") .append("new ").append(generatedType.getName()).append("(") .append(ctor.getArguments().stream() .map(p -> Getter.find(((ClassRef) m.getReturnType()).getDefinition(), p, true)) .map(g -> g.getName()) sb.append(".with").append(StringUtils.capitalizeFirst(trimmedName)).append("(") .append("new ").append(generatedType.getName()).append("(") .append(ctor.getArguments().stream() .map(p -> Getter.find(((ClassRef) m.getReturnType()).getDefinition(), p, true)) .map(g -> g.getName())
if (!method.getArguments().isEmpty()) { return false;
private static Method superConstructorOf(Method constructor, TypeDef constructorType) { List<AnnotationRef> annotations = new ArrayList<AnnotationRef>(); for (AnnotationRef candidate : constructor.getAnnotations()) { if (!candidate.getClassRef().equals(BUILDABLE_ANNOTATION.getClassRef())) { annotations.add(candidate); } } return new MethodBuilder(constructor) .withAnnotations(annotations) .withReturnType(constructorType.toReference()) .withNewBlock() .addNewStringStatementStatement("super(" + StringUtils.join(constructor.getArguments(), new Function<Property, String>() { public String apply(Property item) { return item.getName(); } }, ", ") + ");") .endBlock() .build(); } }
Method constructor = findBuildableConstructor(superClass); if (constructor != null) { constructorArgs.addAll(constructor.getArguments()); for (Property p : constructor.getArguments()) { String name = StringUtils.toFieldName(p.getName()); for (Property f : fields) { sb.append(StringUtils.join(constructor.getArguments(), new Function<Property, String>(){ @Override public String apply(Property item) {
String delegateName = delegatePrefix + property.getNameCapitalized(); String args = StringUtils.join(constructor.getArguments(), new Function<Property, String>() { public String apply(Property item) { return item.getName(); .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(returnType) .withArguments(constructor.getArguments()) .withName(ownName) .withParameters(baseType.getParameters())
public MethodFluentImpl(Method instance){ this.withComments(instance.getComments()); this.withAnnotations(instance.getAnnotations()); this.withParameters(instance.getParameters()); this.withName(instance.getName()); this.withReturnType(instance.getReturnType()); this.withArguments(instance.getArguments()); this.withVarArgPreferred(instance.isVarArgPreferred()); this.withExceptions(instance.getExceptions()); this.withBlock(instance.getBlock()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); }
public MethodBuilder(Method instance,Boolean validationEnabled){ this.fluent = this; this.withComments(instance.getComments()); this.withAnnotations(instance.getAnnotations()); this.withParameters(instance.getParameters()); this.withName(instance.getName()); this.withReturnType(instance.getReturnType()); this.withArguments(instance.getArguments()); this.withVarArgPreferred(instance.isVarArgPreferred()); this.withExceptions(instance.getExceptions()); this.withBlock(instance.getBlock()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; }
public MethodBuilder(MethodFluent<?> fluent,Method instance,Boolean validationEnabled){ this.fluent = fluent; fluent.withComments(instance.getComments()); fluent.withAnnotations(instance.getAnnotations()); fluent.withParameters(instance.getParameters()); fluent.withName(instance.getName()); fluent.withReturnType(instance.getReturnType()); fluent.withArguments(instance.getArguments()); fluent.withVarArgPreferred(instance.isVarArgPreferred()); fluent.withExceptions(instance.getExceptions()); fluent.withBlock(instance.getBlock()); fluent.withModifiers(instance.getModifiers()); fluent.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; } public MethodBuilder(Method instance){