/** * Find the getter of the specified property in the type. * @param clazz The class. * @param property The property. * @return The getter method if found. Throws exception if no getter is matched. */ public static Method find(TypeDef clazz, Property property) { return find(clazz, property, false); }
/** * Checks if the specified method is a getter. * @param method The specified method. * @return True if getter, false otherwise. */ public static boolean is(Method method) { return is(method, false); }
public String apply(Property item) { return "fluent." + Getter.name(item) + "()"; } }, ","))
for (Method method : t.getMethods()) { if (Getter.is(method) || t.equals(item)) { String name = Getter.propertyNameSafe(method); TypeRef returnType = method.getReturnType(); getters.add(new MethodBuilder(Getter.forProperty(field)) .withModifiers(modifiersToInt(Modifier.PUBLIC)) .withNewBlock() if (Getter.is(method)) { for (Method m : getters) { if (m.getName().equals(method.getName())) {
Boolean hasSuperClass = pojo.getProperties() .stream() .filter(p -> p.getTypeRef() instanceof ClassRef && p.getName().equals(Getter.propertyNameSafe(m))) .map(p -> ((ClassRef)p.getTypeRef()).getDefinition()) .flatMap(c -> c.getExtendsList().stream()) .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("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 -> "instance."+m.getName()+"()."+s+"()").collect(Collectors.joining(", ")))
/** * Return the getter name for the specified {@link Property}. * @param property The property. * @return The name. */ public static String name(Property property) { return prefix(property) + property.getNameCapitalized(); }
private static boolean isApplicable(Method method, Property property) { return isApplicable(method, property, false, false); }
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; }
if (isApplicable(method, property, true, false)) { return method; if (isApplicable(method, property, false, false)) { return method; if (isApplicable(method, property, false, true)) { return method;
if (!hasBuildableConstructorWithArgument(c, property) && Setter.has(c, property)) { String setterName = "set" + property.getNameCapitalized(); String getterName = Getter.name(property); statements.add(new StringStatement(new StringBuilder() .append("buildable.").append(setterName).append("(fluent.").append(getterName).append("());")
/** * Return the property name for the specified getter {@link Method method}. * @param method The method. * @return The name. */ public static String propertyName(Method method) { if (!is(method)) { throw new IllegalArgumentException("Method: " + method + " is not a real getter."); } String name = method.getName(); if (name.startsWith(GET_PREFIX)) { return name.substring(GET_PREFIX.length()); } else if (name.startsWith(IS_PREFIX)) { return name.substring(IS_PREFIX.length()); } else if (name.startsWith(SHOULD_PREFIX)) { name = name.substring(SHOULD_PREFIX.length()); } else { throw new IllegalStateException("Method: " + method + " is a getter but couldn't find a valid prefix."); } if (name.length() == 1) { return name.toUpperCase(); } return name.substring(0, 1).toUpperCase() + name.substring(1); }
Method getter = Getter.find(clazz, property); if (getter != null) { String cast = property.getTypeRef() instanceof TypeParamRef ? "(" + property.getTypeRef().toString() + ")" : ""; 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()));
public static final Method forProperty(Property property) { return new MethodBuilder() .withName(name(property)) .withReturnType(property.getTypeRef()) .withNewBlock() .addNewStringStatementStatement("return this." + property + ";") .endBlock() .build(); }
/** * Return the property name for the specified getter {@link Method method}. * This method will not check if the method is an actual getter and will return the method name if not. * @param method The method. * @return The name, or the method name if method is not a typical getter.. */ public static String propertyNameSafe(Method method) { if (!is(method)) { return method.getName(); } String name = method.getName(); if (name.startsWith(GET_PREFIX)) { return name.substring(GET_PREFIX.length()); } else if (name.startsWith(IS_PREFIX)) { return name.substring(IS_PREFIX.length()); } else if (name.startsWith(SHOULD_PREFIX)) { name = name.substring(SHOULD_PREFIX.length()); } if (name.length() == 1) { return name.toUpperCase(); } return name.substring(0, 1).toUpperCase() + name.substring(1); }
List<String> comments = new ArrayList<String>(); String getterName = Getter.name(property); String builderName = "build" + property.getNameCapitalized(); TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_ARRAY_OF).apply(property.getTypeRef());