public static JavaMethod findBuildableConstructor(JavaClazz clazz) { for (JavaMethod candidate : clazz.getConstructors()) { if (candidate.getArguments().length != 0) { return candidate; } } return clazz.getConstructors().iterator().next(); }
/** * 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; }
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; }
@Override public JavaClazz apply(JavaClazz item) { JavaType type = item.getType(); JavaType editableType = TypeAs.EDITABLE.apply(type); JavaType builderType = TypeAs.BUILDER.apply(type); Set<JavaMethod> constructors = new LinkedHashSet<JavaMethod>(); Set<JavaMethod> methods = new LinkedHashSet<JavaMethod>(); for (JavaMethod constructor : item.getConstructors()) { constructors.add(superConstructorOf(constructor, editableType)); } JavaMethod edit = new JavaMethodBuilder() .withReturnType(builderType) .withName("edit") .addToAttributes(BODY, "return new " + builderType.getSimpleName() + "(this);") .build(); methods.add(edit); return new JavaClazzBuilder() .withType(editableType) .withConstructors(constructors) .withMethods(methods) .build(); } }, INLINEABLE {
@Override public JavaClazz apply(JavaClazz... items) { if (items == null || items.length == 0) { throw new IllegalArgumentException("Items cannot be empty."); } else if (items.length == 1) { return items[0]; } else if (items.length == 2) { JavaType mergedType = TYPES.apply(new JavaType[]{items[0].getType(), items[1].getType()}); JavaClazzBuilder builder = new JavaClazzBuilder(items[0]).withType(mergedType); for (JavaMethod constructor : items[1].getConstructors()) { builder = builder.addToConstructors(constructor); } for (JavaMethod method : items[1].getMethods()) { builder = builder.addToMethods(method); } for (JavaProperty property : items[1].getFields()) { builder = builder.addToFields(property); } return builder.build(); } else { JavaClazz[] rest = new JavaClazz[items.length - 1]; System.arraycopy(items, 1, rest, 0, rest.length); CLASSES.apply(new JavaClazz[]{items[0], CLASSES.apply(rest)}); } return null; } };
JavaClazz inlineableOf(BuilderContext ctx, JavaClazz clazz, Inline inline) { JavaClazz base = ClazzAs.INLINEABLE.apply(clazz); JavaType baseInterface = typeGenericOf(BuilderUtils.getInlineType(ctx, inline), clazz.getType()); JavaMethod method = new JavaMethodBuilder(base.getMethods().iterator().next()).withName(inline.value()).build(); JavaType fluent = TypeAs.SHALLOW_FLUENT.apply(clazz.getType()); JavaType shallowInlineType = new JavaTypeBuilder(base.getType()) .withClassName(inline.prefix() + base.getType().getClassName() + inline.suffix()) .addToInterfaces(baseInterface) .build(); JavaType inlineType = new JavaTypeBuilder(shallowInlineType) .withSuperClass(typeGenericOf(fluent, shallowInlineType)) .build(); Set<JavaMethod> constructors = new LinkedHashSet<JavaMethod>(); for (JavaMethod constructor : base.getConstructors()) { constructors.add(new JavaMethodBuilder(constructor).withReturnType(inlineType).build()); } return new JavaClazzBuilder(base) .withType(inlineType) .withConstructors(constructors) .withMethods(new HashSet<JavaMethod>(Arrays.asList(method))) .build(); } }