Ejemplo n.º 1
0
 public static JavaMethod findBuildableConstructor(JavaClazz clazz) {
   for (JavaMethod candidate : clazz.getConstructors()) {
     if (candidate.getArguments().length != 0) {
       return candidate;
     }
   }
   return clazz.getConstructors().iterator().next();
 }
Ejemplo n.º 2
0
 public static Set<JavaMethod> getInlineableConstructors(JavaProperty property) {
   Set<JavaMethod> result = new HashSet<JavaMethod>();
   JavaClazz clazz = PropertyAs.CLASS.apply(property);
   for (JavaMethod candidate : clazz.getConstructors()) {
     if (isInlineable(candidate)) {
       result.add(candidate);
     }
   }
   return result;
 }
Ejemplo n.º 3
0
 /**
  * Checks if there is a default constructor available.
  *
  * @param item The clazz to check.
  * @return
  */
 public static boolean hasDefaultConstructor(JavaClazz item) {
   if (item == null) {
     return false;
   } else if (item.getConstructors().isEmpty()) {
     return true;
   } else {
     for (JavaMethod constructor : item.getConstructors()) {
       if (constructor.getArguments().length == 0) {
         return true;
       }
     }
   }
   return false;
 }
Ejemplo n.º 4
0
 public static JavaMethod findGetter(JavaClazz clazz, JavaProperty property) {
   for (JavaMethod method : clazz.getMethods()) {
     if (isApplicableGetterOf(method, property)) {
       return method;
     }
   }
   return null;
 }
Ejemplo n.º 5
0
  public BuilderContext(
      Elements elements,
      Boolean generateBuilderPackage,
      String builderPackage,
      Inline... inlineables) {
    this.elements = elements;
    this.generateBuilderPackage = generateBuilderPackage;
    this.builderPackage = builderPackage;
    this.inlineables = inlineables;

    stringJavaTypeFunction = new ToBuildableJavaType(elements);
    variableElementJavaPropertyFunction = new ToBuildableJavaProperty(stringJavaTypeFunction);
    executableElementToJavaMethod =
        new ExecutableElementToJavaMethod(
            stringJavaTypeFunction, variableElementJavaPropertyFunction);
    typeElementToJavaClazz =
        new TypeElementToJavaClazz(
            elements,
            stringJavaTypeFunction,
            executableElementToJavaMethod,
            variableElementJavaPropertyFunction);

    repository = new BuildableRepository();

    visitorInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(VISITOR.getClassName())
            .withGenericTypes(VISITOR.getGenericTypes())
            .and()
            .addNewMethod()
            .addToModifiers(Modifier.PUBLIC)
            .withReturnType(VOID)
            .withName("visit")
            .addNewArgument()
            .withName("item")
            .withType(V)
            .endArgument()
            .and()
            .build();

    JavaType visitorBase = unwrapGeneric(visitorInterface.getType());

    visitableInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(VISITABLE.getClassName())
            .withGenericTypes(new JavaType[] {V})
            .and()
            .addNewMethod()
            .addToModifiers(Modifier.PUBLIC)
            .withReturnType(V)
            .withName("accept")
            .addNewArgument()
            .withName("visitor")
            .withType(visitorBase)
            .endArgument()
            .and()
            .build();

    JavaType visitableBase = unwrapGeneric(visitableInterface.getType());

    builderInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withPackageName(builderPackage)
            .withKind(JavaKind.INTERFACE)
            .withClassName(BUILDER.getClassName())
            .withGenericTypes(BUILDER.getGenericTypes())
            .and()
            .addNewMethod()
            .withReturnType(T)
            .withName("build")
            .and()
            .build();

    fluentInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(FLUENT.getClassName())
            .withGenericTypes(FLUENT.getGenericTypes())
            .and()
            .build();

    baseFluentClass =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.CLASS)
            .withPackageName(builderPackage)
            .withClassName(BASE_FLUENT.getClassName())
            .withGenericTypes(BASE_FLUENT.getGenericTypes())
            .addToInterfaces(fluentInterface.getType())
            .addToInterfaces(typeGenericOf(visitableInterface.getType(), T))
            .and()
            .addNewMethod()
            .addToTypeParameters(T)
            .addToModifiers(Modifier.PUBLIC)
            .withName("build")
            .withReturnType(typeGenericOf(ARRAY_LIST, T))
            .addNewArgument()
            .withType(
                typeGenericOf(LIST, typeExtends(Q, typeGenericOf(builderInterface.getType(), T))))
            .withName("list")
            .endArgument()
            .addToAttributes(BODY, loadResourceQuietly(BUILD_LIST_SNIPPET))
            .and()
            .addNewMethod()
            .addToTypeParameters(T)
            .addToModifiers(Modifier.PUBLIC)
            .withName("build")
            .withReturnType(typeGenericOf(LINKED_HASH_SET, T))
            .addNewArgument()
            .withType(
                typeGenericOf(
                    LINKED_HASH_SET, typeExtends(Q, typeGenericOf(builderInterface.getType(), T))))
            .withName("set")
            .endArgument()
            .addToAttributes(BODY, loadResourceQuietly(BUILD_SET_SNIPPET))
            .and()
            .addNewMethod()
            .addToTypeParameters(T)
            .addToModifiers(Modifier.PUBLIC)
            .withName("aggregate")
            .withReturnType(typeGenericOf(ARRAY_LIST, T))
            .addNewArgument()
            .withType(typeGenericOf(LIST, typeExtends(Q, T)))
            .withName("...lists")
            .endArgument()
            .addToAttributes(BODY, loadResourceQuietly(AGGREGATE_LIST_SNIPPET))
            .and()
            .addNewMethod()
            .addToTypeParameters(T)
            .addToModifiers(Modifier.PUBLIC)
            .withName("aggregate")
            .withReturnType(typeGenericOf(LINKED_HASH_SET, T))
            .addNewArgument()
            .withType(typeGenericOf(SET, typeExtends(Q, T)))
            .withName("...sets")
            .endArgument()
            .addToAttributes(BODY, loadResourceQuietly(AGGREGATE_SET_SNIPPET))
            .and()
            .addNewMethod()
            .addToModifiers(Modifier.PUBLIC)
            .withName("accept")
            .withReturnType(T)
            .addNewArgument()
            .withType(visitorBase)
            .withName("visitor")
            .endArgument()
            .addToAttributes(BODY, loadResourceQuietly(ACCEPT_VISITOR_SNIPPET))
            .and()
            .addNewField()
            .addToModifiers(Modifier.PUBLIC)
            .addToModifiers(Modifier.FINAL)
            .withName("_visitables")
            .withType(
                new JavaTypeBuilder(typeGenericOf(LIST, visitableBase))
                    .withDefaultImplementation(typeGenericOf(ARRAY_LIST, visitableBase))
                    .build())
            .and()
            .build();

    nestedInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(NESTED.getClassName())
            .withGenericTypes(NESTED.getGenericTypes())
            .and()
            .addNewMethod()
            .withReturnType(N)
            .withName("and")
            .and()
            .build();

    editableInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(EDITABLE.getClassName())
            .withGenericTypes(EDITABLE.getGenericTypes())
            .and()
            .addNewMethod()
            .withReturnType(T)
            .withName("edit")
            .and()
            .build();

    visitableBuilderInterface =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(VISITABLE_BUILDER.getClassName())
            .addToInterfaces(visitableInterface.getType())
            .addToInterfaces(builderInterface.getType())
            .withGenericTypes(VISITABLE_BUILDER.getGenericTypes())
            .and()
            .build();

    inlineableBase =
        new JavaClazzBuilder()
            .withNewType()
            .withKind(JavaKind.INTERFACE)
            .withPackageName(builderPackage)
            .withClassName(INLINEABLE.getClassName())
            .withGenericTypes(INLINEABLE.getGenericTypes())
            .and()
            .addNewMethod()
            .withReturnType(T)
            .withName("inline")
            .and()
            .build();
  }