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()); }
int length = method.getName().length(); if (method.isPrivate() || method.isStatic()) { return false; if (!method.getArguments().isEmpty()) { return false; if (method.getReturnType().equals(VOID)) { return false; if (method.getName().startsWith(GET_PREFIX)) { return length > GET_PREFIX.length(); if (method.getName().startsWith(IS_PREFIX) && TypeUtils.isBoolean(method.getReturnType())) { return length > IS_PREFIX.length(); if (method.getName().startsWith(SHOULD_PREFIX) && TypeUtils.isBoolean(method.getReturnType())) { return length > SHOULD_PREFIX.length();
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; }
/** * 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 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; }
/** * Returns the string representation of the code that reads a primitive array property. * @param ref The reference. * @param source The type of the reference. * @param property The property to read. * @return The code. */ private static String readPrimitiveArrayProperty(String ref, TypeDef source, Property property) { StringBuilder sb = new StringBuilder(); Method getter = getterOf(source, property); sb.append("Arrays.asList(") .append(ref).append(".").append(getter.getName()).append("())") .append(".stream()").append(".collect(Collectors.toList())).toArray(new "+getter.getReturnType().toString()+"[])"); return sb.toString(); }
if (isPublic()) { sb.append(PUBLIC).append(SPACE); } else if (isProtected()) { sb.append(PROTECTED).append(SPACE); } else if (isPrivate()) { sb.append(PRIVATE).append(SPACE); if (isSynchronized()) { sb.append(SYNCHRONIZED).append(SPACE); if (isStatic()) { sb.append(STATIC).append(SPACE); if (isAbstract()) { sb.append(ABSTRACT).append(SPACE); if (isFinal()) { sb.append(FINAL).append(SPACE);
String trimmedName = StringUtils.deCapitalizeFirst(m.getName().replaceAll("^get", "").replaceAll("^is", "")); if (m.getReturnType() instanceof ClassRef) { ClassRef ref = (ClassRef) m.getReturnType(); Boolean hasSuperClass = pojo.getProperties() .stream() TypeDef generatedType = TypeUtils.allProperties(pojo) .stream() .filter(p -> p.getName().equals(m.getName())) .map(p -> p.getTypeRef()) .filter(r -> r instanceof ClassRef) if (m.getReturnType().getDimensions() > 0) { sb.append(".addAllTo").append(StringUtils.capitalizeFirst(trimmedName)).append("(") .append("Arrays.asList(") .append("instance.").append(m.getName()).append("())") .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()) .map(s -> "i."+s+"()").collect(Collectors.joining(", "))) .append(")") 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())
/** * 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; }
/** * Returns the string representation of the code that reads an object property from a reference using a getter. * @param ref The reference. * @param source The type of the reference. * @param property The property to read. * @return The code. */ private static String readObjectProperty(String ref, TypeDef source, Property property) { return ref + "." + getterOf(source, property).getName() + "()"; }
for (AnnotationRef candidate : sourceMethod.getAnnotations()) { if (!candidate.getClassRef().getFullyQualifiedName().startsWith("io.sundr")) { annotations.add(candidate); toInterfaceName(targetMethod.getName());
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(); } }
/** * 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; }
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; }
for (AnnotationRef a : getAnnotations()) { refs.addAll(a.getClassRef().getReferences()); if (getAttributes().containsKey(ALSO_IMPORT)) { Object obj = getAttributes().get(ALSO_IMPORT); if (obj instanceof ClassRef) { refs.add((ClassRef) obj);
private static boolean hasArrayFields(TypeDef item) { return item.getMethods() .stream() .filter(m -> m.getReturnType() instanceof ClassRef && ((ClassRef)m.getReturnType()).getDimensions() > 0) .findAny().isPresent(); }
private static boolean hasBuildableAnnotation(Method method) { for (AnnotationRef annotationRef : method.getAnnotations()) { if (annotationRef.getClassRef().equals(Constants.BUILDABLE_ANNOTATION.getClassRef())) { return true; } } return false; }
/** * Returns the string representation of the code that reads an object array property. * @param ref The reference. * @param source The type of the reference. * @param property The property to read. * @return The code. */ private static String readObjectArrayProperty(String ref, TypeDef source, Property property) { StringBuilder sb = new StringBuilder(); Method getter = getterOf(source, property); TypeRef getterTypeRef = getter.getReturnType(); TypeRef propertyTypeRef = property.getTypeRef(); if (propertyTypeRef instanceof ClassRef && getterTypeRef instanceof ClassRef) { String nextRef = variables.pop(); try { TypeDef propertyType = ((ClassRef) propertyTypeRef).getDefinition(); TypeDef getterType = ((ClassRef) getterTypeRef).getDefinition(); sb.append("Arrays.asList(") .append(ref).append(".").append(getter.getName()).append("())") .append(".stream().map(").append(nextRef).append(" ->").append(convertReference(nextRef, getterType, propertyType)).append(")") .append(".collect(Collectors.toList()).toArray(new " + propertyType.getFullyQualifiedName() + "[0])"); } finally { variables.push(nextRef); } return sb.toString(); } throw new IllegalArgumentException("Expected an object property and a matching object getter!!"); }
TypeRef returnType = method.getReturnType(); if (method.getReturnType() instanceof ClassRef) { ClassRef ref = (ClassRef) method.getReturnType(); if (ref.getDefinition().isAnnotation()) { 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) { if (Getter.is(method)) { for (Method m : getters) { if (m.getName().equals(method.getName())) { getters.remove(m); sb.append(StringUtils.join(constructor.getArguments(), new Function<Property, String>(){ @Override public String apply(Property item) { item.getMethods() .stream() .filter(m -> m.getReturnType() instanceof ClassRef && ((ClassRef)m.getReturnType()).getDimensions() > 0) .findAny().ifPresent(m -> { additionalImports.add(ARRAYS);
for (Property property : constructor.getArguments()) { Method getter = Getter.find(clazz, property); if (getter != null) { String cast = property.getTypeRef() instanceof TypeParamRef ? "(" + property.getTypeRef().toString() + ")" : ""; statements.add(new StringStatement(new StringBuilder().append(ref).append(".with").append(property.getNameCapitalized()).append("(").append(cast).append("instance.").append(getter.getName()).append("()); ").toString())); } else { throw new IllegalStateException("Could not find getter for property:" + property + " in class:" + clazz); if (!hasBuildableConstructorWithArgument(target, property) && Setter.has(target, property)) { String withName = "with" + property.getNameCapitalized(); String getterName = Getter.find(target, property).getName(); statements.add(new StringStatement(new StringBuilder().append(ref).append(".").append(withName).append("(instance.").append(getterName).append("());\n").toString()));