public A withDefinition(TypeDef definition) { this.definition = definition; this.fullyQualifiedName = definition.getFullyQualifiedName(); return (A) this; }
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 static boolean hasOrInherits(TypeDef clazz, Property property) { TypeDef current = clazz; //Iterate parent objects and check for properties with setters but not ctor arguments. while (current!= null && !current.equals(TypeDef.OBJECT)) { for (Method method : current.getMethods()) { if (isApplicable(method, property)) { return true; } } if (!current.getExtendsList().isEmpty()) { String fqn = current.getExtendsList().iterator().next().getDefinition().getFullyQualifiedName(); current = DefinitionRepository.getRepository().getDefinition(fqn); } else { current = null; } } return false; } }
public static Set<ClassRef> extractInterfacesFromType(TypeDef type) { Set<ClassRef> result = new LinkedHashSet<ClassRef>(); if (type.getExtendsList().isEmpty()) { result.add(type.toInternalReference()); } else { for (ClassRef interfaceType : type.getExtendsList()) { result.addAll(extractInterfacesFromClassRef(interfaceType)); } } return result; }
/** * 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(); }
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; }
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(); } };
String pojoName = StringUtils.toPojoName(item.getName(), "Default", ""); String relativePath = "."; for (AnnotationRef r : item.getAnnotations()) { if (r.getClassRef().getFullyQualifiedName().equals(Buildable.class.getTypeName())) { if (!annotationRefs.contains(r)) { String prefix = String.valueOf(r.getParameters().getOrDefault("prefix", "")); String suffix = String.valueOf(r.getParameters().getOrDefault("suffix", "")); pojoName = StringUtils.toPojoName(item.getName(), prefix, suffix); } else if (params.containsKey("relativePath")) { pojoName = item.getName(); ClassRef superClassRef = superClass.toInternalReference(); extendsList.add(superClassRef); BuilderContextManager.getContext().getBuildableRepository().register(superClassRef.getDefinition()); if (item.isInterface()) { implementsList.add(item.toInternalReference()); .map(n -> DefinitionRepository.getRepository().getDefinition(n)) .filter(d -> d != null) .map(d -> d.toInternalReference()) .forEach(ref -> implementsList.add(ref)); for (Method method : t.getMethods()) { if (Getter.is(method) || t.equals(item)) {
/** * Checks if the ref needs to be done by fully qualified name. Why? Because an other reference * to a class with the same name but different package has been made already. */ private boolean requiresFullyQualifiedName() { String currentPackage = PackageScope.get(); if (currentPackage != null) { if (definition != null && definition.getPackageName() != null && definition.getFullyQualifiedName() != null) { String conflictingFQCN = getDefinition().getFullyQualifiedName().replace(definition.getPackageName(), currentPackage); if (!conflictingFQCN.equals(getFullyQualifiedName()) && DefinitionRepository.getRepository().getDefinition(conflictingFQCN) != null) { return true; } } } Map<String, String> referenceMap = DefinitionRepository.getRepository().getReferenceMap(); if (referenceMap != null && referenceMap.containsKey(definition.getName())) { String fqn = referenceMap.get(definition.getName()); if (!getDefinition().getFullyQualifiedName().equals(fqn)) { return true; } } return false; }
try { double percentage = 100 * (count++) / total; System.err.println(Math.round(percentage)+"%: " + typeDef.getFullyQualifiedName()); if (typeDef.isInterface() || typeDef.isAnnotation()) { continue; Constants.DEFAULT_SOURCEFILE_TEMPLATE_LOCATION); if (typeDef.isAbstract()) { continue; if (typeDef.getAttributes().containsKey(EDIATABLE_ENABLED) && (Boolean) typeDef.getAttributes().get(EDIATABLE_ENABLED)) { generateFromResources(ClazzAs.EDITABLE_BUILDER.apply(typeDef), Constants.DEFAULT_SOURCEFILE_TEMPLATE_LOCATION); Buildable buildable = typeDef.getAttribute(BUILDABLE); ExternalBuildables externalBuildables = typeDef.getAttribute(EXTERNAL_BUILDABLE); if (buildable != null) { for (final Inline inline : buildable.inline()) {
private static String staticAdapterBody(TypeDef pojo, TypeDef pojoBuilder, TypeDef source, boolean returnBuilder) { StringBuilder sb = new StringBuilder(); sb.append("return new ").append(pojoBuilder.getName()).append("()"); for (Method m : source.getMethods()) { String trimmedName = StringUtils.deCapitalizeFirst(m.getName().replaceAll("^get", "").replaceAll("^is", "")); if (m.getReturnType() instanceof ClassRef) { ClassRef ref = (ClassRef) m.getReturnType(); Boolean hasSuperClass = pojo.getProperties() .stream() .filter(p -> p.getTypeRef() instanceof ClassRef && p.getName().equals(Getter.propertyNameSafe(m))) .map(p -> ((ClassRef)p.getTypeRef()).getDefinition()) .flatMap(c -> c.getExtendsList().stream()) .count() > 0; if (ref.getDefinition().isAnnotation()) { TypeDef generatedType = TypeUtils.allProperties(pojo) .stream() .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)) } else { sb.append(".with").append(StringUtils.capitalizeFirst(trimmedName)).append("(") .append("new ").append(generatedType.getName()).append("(") .append(ctor.getArguments().stream() .map(p -> Getter.find(((ClassRef) m.getReturnType()).getDefinition(), p, true))
TypeDef propertyTypeDef = BuilderContextManager.getContext().getDefinitionRepository().getDefinition((baseType).getDefinition().getFullyQualifiedName()); if (propertyTypeDef != null) { baseType = propertyTypeDef.toInternalReference(); TypeDef nestedTypeImpl = PropertyAs.NESTED_CLASS_TYPE.apply(property); List<TypeParamDef> parameters = baseType.getDefinition().getParameters(); List<TypeRef> typeArguments = new ArrayList<TypeRef>(); for (TypeRef arg : baseType.getArguments()) { ClassRef rewraped = nestedType.toReference(typeArguments); ClassRef rewrapedImpl = nestedTypeImpl.toReference(typeArguments);
private static List<Statement> toEquals(TypeDef type, Collection<Property> properties) { List<Statement> statements = new ArrayList<Statement>(); String simpleName = type.getName(); ClassRef superClass = type.getExtendsList().isEmpty() ? TypeDef.OBJECT_REF : type.getExtendsList().iterator().next(); statements.add(new StringStatement("if (this == o) return true;")); statements.add(new StringStatement("if (o == null || getClass() != o.getClass()) return false;")); //If base fluent is the superclass just skip. if (!Constants.BASE_FLUENT.getFullyQualifiedName().equals(superClass.getDefinition().getFullyQualifiedName())) { statements.add(new StringStatement("if (!super.equals(o)) return false;")); } statements.add(new StringStatement(new StringBuilder().append(simpleName).append(" that = (").append(simpleName).append(") o;").toString())); for (Property property : properties) { String name = property.getName(); if (TypeUtils.isPrimitive(property.getTypeRef())) { statements.add(new StringStatement(new StringBuilder().append("if (").append(name).append(" != ").append("that.").append(name).append(") return false;").toString())); } else if (property.getTypeRef() instanceof ClassRef && Descendants.isDescendant(type, ((ClassRef) property.getTypeRef()).getDefinition())) { statements.add(new StringStatement(new StringBuilder() .append("if (").append(name).append(" != null &&").append(name).append(" != this ? !").append(name).append(".equals(that.").append(name).append(") :") .append("that.").append(name).append(" != null &&").append(name).append(" != this ) return false;").append("\n") .toString())); } else { statements.add(new StringStatement(new StringBuilder().append("if (").append(name).append(" != null ? !").append(name).append(".equals(that.").append(name).append(") :") .append("that.").append(name).append(" != null) return false;").toString())); } } statements.add(new StringStatement("return true;")); return statements; }
public static void visitParents(TypeDef type, List<TypeDef> types, List<TypeDef> visited) { if (type == null || JAVA_LANG_OBJECT.equals(type.getFullyQualifiedName())) { return; } if (visited.contains(type)) { return; } visited.add(type); List<TypeRef> allRefs = new ArrayList<>(); allRefs.addAll(type.getImplementsList()); allRefs.addAll(type.getExtendsList()); for (TypeRef ref : allRefs) { TypeDef parent = DefinitionRepository.getRepository().getDefinition(ref); visitParents(parent, types, visited); } types.add(type); } }
static TypeDef inlineableOf(BuilderContext ctx, TypeDef type, Inline inline) { final String inlineableName = !inline.name().isEmpty() ? inline.name() : inline.prefix() + type.getName() + inline.suffix(); TypeDef inlineType = BuilderUtils.getInlineType(ctx, inline); TypeDef returnType = BuilderUtils.getInlineReturnType(ctx, inline, type); final ClassRef inlineTypeRef = inlineType.toReference(returnType.toReference()); TypeRef functionType = ctx.getFunctionInterface().toReference(type.toInternalReference(), returnType.toInternalReference()); .withTypeRef(TypeAs.BUILDER.apply(type).toInternalReference()) .withName(BUILDER) .withModifiers(TypeUtils.modifiersToInt(Modifier.PRIVATE, Modifier.FINAL)) .withReturnType(returnType.toInternalReference()) .withName(inline.value()) .withNewBlock() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withNewBlock() .addNewStringStatementStatement(String.format(NEW_BULDER_AND_SET_FUNCTION_FORMAT, builderType.getName())) .endBlock() .build()); .addNewArgument() .withName(ITEM) .withTypeRef(type.toInternalReference()) .and() .addNewArgument()
public boolean isAssignableFrom(TypeDef o) { if (this == o || this.equals(o)) { return true; } if (packageName == null && "java.lang".equals(o.packageName) && name.equalsIgnoreCase(o.name)) { return true; } if (o.packageName == null && "java.lang".equals(packageName) && name.equalsIgnoreCase(o.name)) { return true; } for (ClassRef e : o.getExtendsList()) { if (isAssignableFrom(e.getDefinition())) { return true; } } for (ClassRef i : o.getImplementsList()) { if (isAssignableFrom(i.getDefinition())) { return true; } } return false; }
final TypeParamDef genericType = fluentImplType.getParameters().get(fluentImplType.getParameters().size() - 1); .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .addNewArgument() .withTypeRef(item.toInternalReference()) .withName("instance").and() .withNewBlock() constructors.add(instanceConstructor); for (final Property property : item.getProperties()) { final TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(property.getTypeRef()); .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(ClassTo.TYPEREF.apply(boolean.class)) .addNewArgument().withName("o").withTypeRef(io.sundr.codegen.Constants.OBJECT.toReference()).endArgument() .withName("equals") .withBlock(new Block(new Provider<List<Statement>>() {
public static Set<String> allGenericsOf(TypeDef clazz) { Set<String> result = new HashSet<String>(); for (TypeParamDef paramDef : clazz.getParameters()) { result.add(paramDef.getName()); } for (Property property : clazz.getProperties()) { result.addAll(allGenericsOf(property)); } for (Method method : clazz.getMethods()) { result.addAll(allGenericsOf(method)); } return result; }
public Set<TypeDef> apply(TypeDef item) { if (item.equals(TypeDef.OBJECT)) { return new LinkedHashSet<TypeDef>(); } Set<TypeDef> result = new LinkedHashSet<TypeDef>(); BuilderContext ctx = BuilderContextManager.getContext(); BuildableRepository repository = ctx.getBuildableRepository(); for (TypeDef type : repository.getBuildables()) { if (type.getKind() == Kind.CLASS && !type.isAbstract() && isDescendant(type, item) && !type.equals(item) && !type.hasAttribute(GENERATED)) { result.add(type); } } return result; } });
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()); }