/** * The method adapts constructor method to the current class. It unsets any name that may be * presetn in the method. It also sets as a return type a reference to the current type. */ private static List<Method> adaptConstructors(List<Method> methods, TypeDef target) { List<Method> adapted = new ArrayList<Method>(); for (Method m : methods) { adapted.add(new MethodBuilder(m) .withName(null) .withReturnType(target.toUnboundedReference()) .build()); } return adapted; }
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 Method apply(ExecutableElement executableElement) { MethodBuilder methodBuilder = new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(executableElement.getModifiers())) .withName(executableElement.getSimpleName().toString()) .withReturnType(MIRROR_TO_TYPEREF.apply(executableElement.getReturnType())); //Populate constructor parameters for (VariableElement variableElement : executableElement.getParameters()) { methodBuilder = methodBuilder.addToArguments(PROPERTY.apply(variableElement)); List<ClassRef> exceptionRefs = new ArrayList<ClassRef>(); for (TypeMirror thrownType : executableElement.getThrownTypes()) { if (thrownType instanceof ClassRef) { exceptionRefs.add((ClassRef) thrownType); } } methodBuilder = methodBuilder.withExceptions(exceptionRefs); } List<ClassRef> annotationRefs = new ArrayList<ClassRef>(); for (AnnotationMirror annotationMirror : executableElement.getAnnotationMirrors()) { methodBuilder.withAnnotations(ANNOTATION_REF.apply(annotationMirror)); } return methodBuilder.build(); } };
Method getter = new MethodBuilder() .withComments(comments) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(getterName) .withReturnType(property.getTypeRef()) .withArguments() .withNewBlock() .addNewStringStatementStatement(body) .endBlock() .addToAttributes(ALSO_IMPORT, isBuildable ? Arrays.asList(BuilderContextManager.getContext().getVisitableBuilderInterface().toInternalReference()) : Collections.EMPTY_LIST) .build(); methods.add(new MethodBuilder(getter) .removeFromAnnotations(DEPRECATED_ANNOTATION) .withComments() .withName(builderName) .build()); methods.add(new MethodBuilder() .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("build" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped) .addToArguments(INDEX) .withNewBlock() .withStatements(new StringStatement("return this." + property.getName() + ".get(index).build();")) .endBlock() .build());
methods.add(new MethodBuilder() .withName(method.getName()) .withModifiers(method.getModifiers()) .withReturnType(TYPEREF.apply(method.getReturnType())) .withArguments(arguments) .withParameters(parameters) .withAnnotations(annotationRefs) .build());
Method base = new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(rewraped) .withName(methodName) .withNewBlock() .addNewStringStatementStatement(statement) .endBlock() .build(); methods.add(new MethodBuilder(base) .withArguments(INDEX) .withName("edit" + suffix) .editBlock() .withStatements( new StringStatement("if (" + property.getName() + ".size() <= index) throw new RuntimeException(\"Can't edit " + property.getName() + ". Index exceeds size.\");"), .build()); methods.add(new MethodBuilder(base) .withName("editFirst" + suffix) .withArguments() .editBlock() .withStatements( new StringStatement("if (" + property.getName() + ".size() == 0) throw new RuntimeException(\"Can't edit first " + property.getName() + ". The list is empty.\");"), new StringStatement("return setNew" + suffix + "Like(0, build" + suffix + "(0));")) .endBlock() .build()); methods.add(new MethodBuilder(base)
methods.add(new MethodBuilder() .withName(methodDeclaration.getName()) .withModifiers(methodDeclaration.getModifiers()) .withParameters(typeParamDefs) .withVarArgPreferred(preferVarArg) .withReturnType(returnType) .withExceptions(exceptions) .withArguments(arguments) .withBlock(BLOCK.apply(methodDeclaration.getBody())) .build()); .build()); constructors.add(new MethodBuilder() .withModifiers(constructorDeclaration.getModifiers()) .withExceptions(exceptions) .withArguments(arguments) .withBlock(BLOCK.apply(constructorDeclaration.getBlock())) .build());
Method targetMethod = new MethodBuilder(sourceMethod) .withAnnotations(annotations) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(paremeterType.toReference()) .withName(methodName) .build();
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(); } }
@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(); } };
public MethodBuilder edit(){ return new MethodBuilder(this); }
if (returnType instanceof ClassRef) { TypeDef toUnwrap = ((ClassRef)returnType).getDefinition(); methods.add(new MethodBuilder(m).withReturnType(Generics.UNWRAP.apply(toUnwrap).toInternalReference()).build()); } else if (returnType.getAttributes().containsKey(ORIGINAL_REF)) { methods.add(new MethodBuilder(m).withReturnType((TypeRef) returnType.getAttributes().get(ORIGINAL_REF)).build()); } else { methods.add(new MethodBuilder(m).withReturnType(returnType).build()); methods.add(new MethodBuilder(m).withReturnType(current.toUnboundedReference()).build());
public Method buildMethod(int index){ return this.methods.get(index).build(); }
private void visitMethodBuilder(MethodBuilder builder) { if (builder.getReturnType() instanceof ClassRef) { ClassRefBuilder classRefBuilder = new ClassRefBuilder((ClassRef) builder.getReturnType()); if (classRefBuilder.getDefinition() != null && classRefBuilder.getDefinition().getPackageName() != null && classRefBuilder.getDefinition().getPackageName().equals(target)) { classRefBuilder.editDefinition().withPackageName(replacement).endDefinition(); } builder.withReturnType(classRefBuilder.accept(this).build()); } }
Method getter = new MethodBuilder() .withComments(comments) .withAnnotations(annotations) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(getterName) .withReturnType(property.getTypeRef()) .withArguments(new Property[]{}) .withNewBlock() .withStatements(statements) .endBlock() .build(); methods.add(new MethodBuilder(getter) .removeFromAnnotations(DEPRECATED_ANNOTATION) .withComments() .withName("build" + property.getNameCapitalized()) .build()); methods.add(new MethodBuilder() .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("build" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped) .addToArguments(INDEX) .withNewBlock() .withStatements(new StringStatement("return this." + property.getName() + ".get(index).build();")) .endBlock() .build());
constructors.add(new MethodBuilder() .withName(constructor.getName()) .withModifiers(constructor.getModifiers()) .withArguments(arguments) .withParameters(parameters) .withAnnotations(annotationRefs) .build());
getters.add(new MethodBuilder(Getter.forProperty(field)) .withModifiers(modifiersToInt(Modifier.PUBLIC)) .withNewBlock() .withStatements(new StringStatement("return this." + StringUtils.toFieldName(name) + ";")) .endBlock() .build()); Method emptyConstructor = new MethodBuilder() .withModifiers(modifiersToInt(Modifier.PUBLIC)) .build(); Method buildableConstructor = new MethodBuilder() .withModifiers(modifiersToInt(Modifier.PUBLIC)) .withArguments(constructorArgs) .withNewBlock() .withStatements(statements) .endBlock() .accept(new TypedVisitor<PropertyBuilder>() { @Override public void visit(PropertyBuilder b) { .build(); Method staticBuilder = new MethodBuilder() .withModifiers(modifiersToInt(Modifier.PUBLIC, Modifier.STATIC)) .withName(extendsList.isEmpty() ? "newBuilder" : "new" + pojoBuilder.getName()) //avoid clashes in case of inheritance .withReturnType(pojoBuilder.toInternalReference()) .withNewBlock() .addNewStringStatementStatement("return new "+ pojoBuilder.getFullyQualifiedName()+"();")
public A removeFromConstructors(Method... items){ for (Method item : items) {MethodBuilder builder = new MethodBuilder(item);_visitables.remove(builder);if (this.constructors != null) {this.constructors.remove(builder);}} return (A)this; }
public Method buildConstructor(int index){ return this.constructors.get(index).build(); }
public static final Method forProperty(Property property) { return new MethodBuilder() .withName(name(property)) .withReturnType(property.getTypeRef()) .withNewBlock() .addNewStringStatementStatement("return this." + property + ";") .endBlock() .build(); }