private String usingStaticFactoryMethod(List<String> item) { int size = item != null ? item.size() : 0; checkFactoryMethodArguments(size); if (size == 0) { return typeDef.getName() + "." + staticFactoryMethod + "()"; } return typeDef.getName() + "." + staticFactoryMethod + "(" + item.stream().collect(Collectors.joining(", ")) + ")"; }
/** * Converts a reference from the source type, to the target type by using the builder. * @param ref The ref. * @param source The source type of the reference.. * @param target The target type. * @param targetBuilder The target type builder. * @return */ private static String convertReference(String ref, TypeDef source, TypeDef target, TypeDef targetBuilder) { StringBuilder sb = new StringBuilder(); sb.append("new ").append(targetBuilder.getName()).append("(").append(convertReference(ref,source,target)).append(")"); return sb.toString(); }
/** * Returns the fully qualified name of the type. */ public String getFullyQualifiedName() { StringBuilder sb = new StringBuilder(); if (packageName != null && !packageName.isEmpty()) { sb.append(getPackageName()).append("."); } if (outerType != null) { sb.append(outerType.getName()).append("."); } sb.append(getName()); return sb.toString(); }
private String getClassPrefix(Property property) { TypeDef memberOf = property.getAttribute(OUTER_CLASS); if (memberOf != null) { return memberOf.getName() + ".this."; } else return ""; }
/** * Create a mapping from class name to {@link ClassRef}. */ private Map<String, ClassRef> getReferenceMap() { Map<String, ClassRef> mapping = new HashMap<String, ClassRef>(); List<ClassRef> refs = getReferences(); //It's best to have predictable order, so that we can generate uniform code. Collections.sort(refs, new Comparator<ClassRef>() { @Override public int compare(ClassRef o1, ClassRef o2) { return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName()); } }); for (ClassRef ref : refs) { String key = ref.getDefinition().getName(); if (!mapping.containsKey(key)) { mapping.put(key, ref); } } return mapping; }
public boolean isAssignableFrom(TypeRef o) { if (this == o) { return true; } else if (o == null) { return false; } else if (o instanceof ClassRef) { if (!((ClassRef) o).getDefinition().getPackageName().equals(JAVA_LANG)) { return false; } if (!((ClassRef) o).getDefinition().getName().toUpperCase().startsWith(name.toUpperCase())) { return false; } return true; } if (o == null || getClass() != o.getClass()) return false; PrimitiveRef that = (PrimitiveRef) o; if (dimensions != that.dimensions) return false; return name != null ? name.equals(that.name) : that.name == null; }
private Map<String, String> getReferenceMapInternal() { Map<String, String> mapping = new HashMap<String, String>(); List<ClassRef> refs = new ArrayList<ClassRef>(); for (TypeDef typeDef : getDefinitions()) { refs.add(typeDef.toInternalReference()); } //It's best to have predictable order, so that we can generate uniform code. Collections.sort(refs, new Comparator<ClassRef>() { @Override public int compare(ClassRef o1, ClassRef o2) { return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName()); } }); for (ClassRef classRef : refs) { String key = classRef.getDefinition().getName(); if (!mapping.containsKey(key)) { mapping.put(key, classRef.getDefinition().getFullyQualifiedName()); } } mapping.putAll(custom); return mapping; }
public Set<String> getImports() { final Set<String> imports = new LinkedHashSet<String>(); for (ClassRef ref : getReferenceMap().values()) { TypeDef definition = ref.getDefinition(); if (definition.getPackageName() == null || definition.getPackageName().isEmpty() || definition.getPackageName().equals(packageName) || definition.getName().equals(name)) { continue; } else { imports.add(ref.getDefinition().getFullyQualifiedName()); } } return imports; }
/** * 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."); } }
/** * Checks that a constructor with the required number of arguments is found. * @param arguments */ private void checkConstructorArguments(int arguments) { if (arguments == 0 && (typeDef.getConstructors() == null || typeDef.getConstructors().isEmpty())) { return; } for (Method m : typeDef.getConstructors()) { int a = m.getArguments() != null ? m.getArguments().size() : 0; if (a == arguments) { return; } } throw new IllegalArgumentException("No constructor found for " + typeDef.getName() + " with " + arguments + " arguments."); }
public String getName() { if (requiresFullyQualifiedName()) { return getDefinition().getFullyQualifiedName(); } return getDefinition().getName(); }
public boolean isAssignableFrom(TypeRef other) { if (this == other) { return true; } else if (other == null) { return false; } else if (other instanceof PrimitiveRef) { if (getDefinition() == null) { return false; } if (getDefinition() != null && !JAVA_LANG.equals(getDefinition().getPackageName())) { return false; } if (!getDefinition().getName().toUpperCase().startsWith(((PrimitiveRef) other).getName().toUpperCase())) { return false; } return true; } if (!(other instanceof ClassRef)) { return false; } if (this == other || this.equals(other)) { return true; } return definition.isAssignableFrom(((ClassRef) other).getDefinition()); }
public TypeDef apply(TypeDef item) { List<TypeParamDef> parameters = new ArrayList<TypeParamDef>(item.getParameters()); parameters.add(getNextGeneric(item)); return new TypeDefBuilder(item) .withKind(Kind.INTERFACE) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(item.getName() + "Fluent") .withParameters(parameters) .withInnerTypes() .build(); } };
public TypeDef apply(TypeDef item) { return new TypeDefBuilder(item) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(item.getName() + "Builder") .withInnerTypes() .build(); } };
public TypeDef getInlineableInterface(Inline inline) { return new TypeDefBuilder(inlineableBase) .withKind(Kind.INTERFACE) .withPackageName(builderPackage) .withName(inline.prefix() + (!inline.name().isEmpty() ? inline.name() : INLINEABLE.getName()) + inline.suffix()) .withParameters(INLINEABLE.getParameters()) .addNewMethod() .withReturnType(TypeUtils.newTypeParamRef("T")) .withName(inline.value()) .and() .build(); }
public TypeDef apply(TypeDef item) { TypeDef builder = SHALLOW_BUILDER.apply(item); TypeDef fluent = FLUENT_IMPL.apply(item); List<TypeRef> parameters = new ArrayList<TypeRef>(); for (TypeParamDef param : item.getParameters()) { parameters.add(param.toReference()); } parameters.add(builder.toInternalReference()); return new TypeDefBuilder(item) .withKind(Kind.CLASS) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(item.getName() + "Builder") .withParameters(item.getParameters()) .withInnerTypes() .withExtendsList(fluent.toReference(parameters)) .withImplementsList(BuilderContextManager.getContext().getVisitableBuilderInterface().toReference(item.toInternalReference(), builder.toInternalReference())) .build(); } };
public TypeDef apply(TypeDef item) { List<TypeParamDef> parameters = new ArrayList<TypeParamDef>(); for (TypeParamDef generic : item.getParameters()) { parameters.add(generic); } return new TypeDefBuilder(item) .withKind(Kind.CLASS) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("Editable" + item.getName()) .withParameters(parameters) .withExtendsList(item.toInternalReference()) .withImplementsList(BuilderContextManager.getContext().getEditableInterface().toReference(SHALLOW_BUILDER.apply(item).toInternalReference())) .withInnerTypes() .withProperties() .withMethods() .withConstructors() .build(); } };
public TypeDefFluentImpl(TypeDef instance){ this.withKind(instance.getKind()); this.withPackageName(instance.getPackageName()); this.withName(instance.getName()); this.withComments(instance.getComments()); this.withAnnotations(instance.getAnnotations()); this.withExtendsList(instance.getExtendsList()); this.withImplementsList(instance.getImplementsList()); this.withParameters(instance.getParameters()); this.withProperties(instance.getProperties()); this.withConstructors(instance.getConstructors()); this.withMethods(instance.getMethods()); this.withOuterType(instance.getOuterType()); this.withInnerTypes(instance.getInnerTypes()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); }
public TypeDefBuilder(TypeDef instance,Boolean validationEnabled){ this.fluent = this; this.withKind(instance.getKind()); this.withPackageName(instance.getPackageName()); this.withName(instance.getName()); this.withComments(instance.getComments()); this.withAnnotations(instance.getAnnotations()); this.withExtendsList(instance.getExtendsList()); this.withImplementsList(instance.getImplementsList()); this.withParameters(instance.getParameters()); this.withProperties(instance.getProperties()); this.withConstructors(instance.getConstructors()); this.withMethods(instance.getMethods()); this.withOuterType(instance.getOuterType()); this.withInnerTypes(instance.getInnerTypes()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; }
public TypeDefBuilder(TypeDefFluent<?> fluent,TypeDef instance,Boolean validationEnabled){ this.fluent = fluent; fluent.withKind(instance.getKind()); fluent.withPackageName(instance.getPackageName()); fluent.withName(instance.getName()); fluent.withComments(instance.getComments()); fluent.withAnnotations(instance.getAnnotations()); fluent.withExtendsList(instance.getExtendsList()); fluent.withImplementsList(instance.getImplementsList()); fluent.withParameters(instance.getParameters()); fluent.withProperties(instance.getProperties()); fluent.withConstructors(instance.getConstructors()); fluent.withMethods(instance.getMethods()); fluent.withOuterType(instance.getOuterType()); fluent.withInnerTypes(instance.getInnerTypes()); fluent.withModifiers(instance.getModifiers()); fluent.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; } public TypeDefBuilder(TypeDef instance){