/** * 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() + "()"; }
/** * Check if method exists on the specified type. * @param typeDef The type. * @param method The method name. * @return True if method is found, false otherwise. */ public static boolean hasMethod(TypeDef typeDef, String method) { return unrollHierarchy(typeDef) .stream() .flatMap(h -> h.getMethods().stream()) .filter(m -> method.equals(m.getName())) .findAny() .isPresent(); }
/** * 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); }
/** * 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(); }
/** * 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); }
int length = method.getName().length(); 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();
/** * 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."); } }
/** * 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!!"); }
if (method.getName().endsWith(GET_PREFIX + capitalized)) { return true; if (method.getName().endsWith(IS_PREFIX + capitalized)) { return true; if (method.getName().endsWith(SHOULD_PREFIX + capitalized)) { return true; if (acceptPrefixless && method.getName().endsWith(property.getName())) { return true; if (method.getName().endsWith(GET_PREFIX + property.getNameCapitalized())) { return true; if (method.getName().endsWith(IS_PREFIX + property.getNameCapitalized())) { return true; if (method.getName().endsWith(GET_PREFIX + property.getName()) && !Character.isAlphabetic(property.getName().charAt(0))) { return true; if (method.getName().endsWith(IS_PREFIX + property.getName()) && !Character.isAlphabetic(property.getName().charAt(0))) { return true; if (method.getName().endsWith(SHOULD_PREFIX + property.getName()) && !Character.isAlphabetic(property.getName().charAt(0))) { return true;
/** * 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; }
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()));
String trimmedName = StringUtils.deCapitalizeFirst(m.getName().replaceAll("^get", "").replaceAll("^is", "")); if (m.getReturnType() instanceof ClassRef) { ClassRef ref = (ClassRef) m.getReturnType(); TypeDef generatedType = TypeUtils.allProperties(pojo) .stream() .filter(p -> p.getName().equals(m.getName())) .map(p -> p.getTypeRef()) .filter(r -> r instanceof ClassRef) 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(")") .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(", "))) .append(")") .append(")"); sb.append(".").append(withMethod).append("(").append("instance.").append(m.getName()).append("())");
/** * Checks if {@link ClassRef} is buildable. * @param ref The reference. * @return True if buildable repository contains the ref or builder for the reference is present. */ public static boolean isBuildable(ClassRef ref) { if (BuilderContextManager.getContext().getBuildableRepository().isBuildable(ref)) { return true; } String builderFQCN = ref.getDefinition().getFullyQualifiedName() + "Builder"; TypeDef builder = BuilderContextManager.getContext().getDefinitionRepository().getDefinition(builderFQCN); if (builder == null) { return false; } return builder.getMethods() .stream() .filter(m -> "build".equals(m.getName())) .filter(m -> m.getReturnType().isAssignableFrom(ref)) .count() > 0; }
@Override public Method apply(Property property) { Method method = WITH_NEW_LIKE_NESTED.apply(property); if (!(property.getTypeRef() instanceof ClassRef)) { throw new IllegalStateException("Expected Nestable / Buildable type and found:" + property.getTypeRef()); } ClassRef baseType = (ClassRef) TypeAs.UNWRAP_COLLECTION_OF.apply(property.getTypeRef()); TypeRef returnType = property.hasAttribute(GENERIC_TYPE_REF) ? property.getAttribute(GENERIC_TYPE_REF) : T_REF; TypeDef nestedTypeImpl = PropertyAs.NESTED_CLASS_TYPE.apply(property); List<TypeRef> typeArguments = new ArrayList<TypeRef>(); for (TypeRef ignore : baseType.getArguments()) { typeArguments.add(Q); } typeArguments.add(returnType); ClassRef rewrapedImpl = nestedTypeImpl.toReference(typeArguments); return new MethodBuilder(method) .addToArguments(0, INDEX) .withName(method.getName().replaceFirst("add", "set")) .editBlock() .withStatements(new StringStatement("return new " + rewrapedImpl.getName() + "(index, item);")) .endBlock() .build(); } };
toInterfaceName(targetMethod.getName());
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; }
/** * Returns the string representation of the code that given a reference of the specified type, reads the specified property. * @param ref The reference. * @param source The type of the reference. * @param property The property to read. * @return The code. */ private static String readProperty(String ref, TypeDef source, Property property) { TypeRef propertyTypeRef = property.getTypeRef(); Method getter = getterOf(source, property); if (getter == null) { return "null"; } TypeRef getterTypeRef = getter.getReturnType(); if (propertyTypeRef.getDimensions() == getterTypeRef.getDimensions() && propertyTypeRef.isAssignableFrom(getterTypeRef)) { return readObjectProperty(ref, source, property); } if (property.getTypeRef().getDimensions() > 0) { return readArrayProperty(ref, source, property); } if (property.getTypeRef() instanceof ClassRef && ((ClassRef)getterTypeRef).getDefinition().isAnnotation()) { return readAnnotationProperty(ref + "."+getter.getName()+"()", ((ClassRef) getterTypeRef).getDefinition(), property); } return readObjectProperty(ref, source, property); }
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){