public String apply(List<ClassRef> types) { Iterator<ClassRef> iterator = types.iterator(); if (iterator.hasNext()) { return iterator.next().getDefinition().getPackageName(); } return ""; } };
/** * 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 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; }
/** * Convert a {@link Collection} of {@link javax.lang.model.element.ExecutableElement}s to a {@link java.util.Set} of {@link io.sundr.codegen.model.TypeDef}es. * * @param context The context of the operation. * @param elements The target elements. * @return A set of {@link io.sundr.codegen.model.TypeDef} that describes the interfaces. */ public static Set<TypeDef> executablesToInterfaces(DslContext context, Collection<ExecutableElement> elements) { Map<String, TypeDef> byName = new LinkedHashMap<String, TypeDef>(); for (ExecutableElement current : elements) { TypeDef clazz = executableToInterface(context, current); InterfaceName interfaceName = current.getAnnotation(InterfaceName.class); String name = interfaceName != null ? clazz.getPackageName() + "." + interfaceName.value() : clazz.getFullyQualifiedName(); if (byName.containsKey(name)) { TypeDef other = byName.remove(name); byName.put(name, Merge.CLASSES.apply(new TypeDef[]{other, clazz})); } else { byName.put(name, clazz); } } return new LinkedHashSet<TypeDef>(byName.values()); }
private static List<TypeDef> setOuterType(List<TypeDef> types, TypeDef outer) { List<TypeDef> updated = new ArrayList<TypeDef>(); for (TypeDef typeDef : types) { if (outer.equals(typeDef.getOuterType())) { updated.add(typeDef); } else { updated.add(new TypeDefBuilder(typeDef).withOuterType(outer).withPackageName(outer.getPackageName()).build()); } } return updated; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); TypeDef definition = getDefinition(); if (definition == null) { sb.append(UNKNOWN); } else { if (requiresFullyQualifiedName()) { sb.append(definition.getPackageName()).append(DOT); } if (definition.getOuterType() != null) { sb.append(definition.getOuterType().getName()).append(DOT).append(definition.getName()); } else { sb.append(definition.getName()); } } if (arguments.size() > 0) { sb.append(LT); sb.append(StringUtils.join(arguments, COMA)); sb.append(GT); } for (int i = 0; i < dimensions; i++) { sb.append(BRACKETS); } return sb.toString(); } }
private void visitTypeDefBuilder(TypeDefBuilder builder) { if (target.equals(builder.getPackageName())) { builder.withPackageName(replacement); } if (builder.getAttributes().containsKey(TypeDef.ALSO_IMPORT)) { Set<ClassRef> updatedImports = new LinkedHashSet<ClassRef>(); for (ClassRef classRef : (Set<ClassRef>) builder.getAttributes().get(TypeDef.ALSO_IMPORT)) { if (target.equals(classRef.getDefinition().getPackageName())) { updatedImports.add(new ClassRefBuilder(classRef).accept(this).build()); } else { updatedImports.add(classRef); } } builder.getAttributes().put(TypeDef.ALSO_IMPORT, updatedImports); } }
private void visitClassRefBuilder(ClassRefBuilder builder) { builder.withFullyQualifiedName(builder.getFullyQualifiedName().replace(target, replacement)); if (builder.getDefinition() != null && builder.getDefinition().getPackageName() != null && builder.getDefinition().getPackageName().equals(target)) { builder.editDefinition().withPackageName(replacement).endDefinition(); } List<TypeRef> updatedArguments = new ArrayList<TypeRef>(); for (TypeRef arg : builder.getArguments()) { if (arg instanceof ClassRef && ((ClassRef) arg).getDefinition().getPackageName().equals(target)) { updatedArguments.add(new ClassRefBuilder((ClassRef) arg).editDefinition().withPackageName(replacement).endDefinition().build()); } else { updatedArguments.add(arg); } } builder.withArguments(updatedArguments); }
/** * 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; }
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()); } }
private void visitPropertyBuilder(PropertyBuilder builder) { if (builder.getTypeRef() instanceof ClassRef) { ClassRefBuilder classRefBuilder = new ClassRefBuilder((ClassRef) builder.getTypeRef()); if (classRefBuilder.getDefinition() != null && classRefBuilder.getDefinition().getPackageName() != null && classRefBuilder.getDefinition().getPackageName().equals(target)) { classRefBuilder.editDefinition().withPackageName(replacement).endDefinition(); } builder.withTypeRef(classRefBuilder.accept(this).build()); } }
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 static String fullyQualifiedNameDiff(TypeRef typeRef, TypeDef originType) { Map<String, String> map = DefinitionRepository.getRepository().getReferenceMap(); String currentPackage = originType != null ? originType.getPackageName() : null; if (typeRef instanceof ClassRef) { TypeRef unwrapped = TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(typeRef); if (unwrapped instanceof ClassRef) { ClassRef classRef = (ClassRef) unwrapped; String candidateFqn = classRef.getFullyQualifiedName().replace(classRef.getDefinition().getPackageName(), currentPackage); //If classRef is inside the current package. if (candidateFqn.equals(classRef.getFullyQualifiedName())) { return ""; } //If candidate is imported and different that the actual name, do a diff if (originType.getImports().contains(candidateFqn) && !classRef.getDefinition().getFullyQualifiedName().equals(candidateFqn)) { return capitalizeFirst(TypeUtils.fullyQualifiedNameDiff(candidateFqn, classRef.getFullyQualifiedName())); } //If not then we compare against what has been found in the map. String fqn = map.get(classRef.getDefinition().getName()); if (fqn == null) { System.out.println("Warning: Expected to find class with name:" + classRef.getDefinition().getName()); } else if (!classRef.getDefinition().getFullyQualifiedName().equals(fqn)) { return capitalizeFirst(TypeUtils.fullyQualifiedNameDiff(fqn, classRef.getFullyQualifiedName())); } } } return ""; }
public void generate() throws IOException { if (model instanceof TypeDef) { TypeDef typeDef = (TypeDef) model; PackageScope.set(typeDef.getPackageName()); GeneratorUtils.generate(context.getVelocityContext(), writer, getTemplate()); PackageScope.clear(); } else { GeneratorUtils.generate(context.getVelocityContext(), writer, getTemplate()); } } }
public TypeDef apply(Property property) { TypeDef originTypeDef = property.getAttribute(Constants.ORIGIN_TYPEDEF); TypeRef typeRef = TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(property.getTypeRef()); TypeDef typeDef = BuilderContextManager.getContext().getDefinitionRepository().getDefinition(typeRef); if (typeDef == null) { if (typeRef instanceof ClassRef) { typeDef = ((ClassRef)typeRef).getDefinition(); } else { throw new IllegalStateException("Could not find definition from property: ["+property+"] neither in the repo nor via the object tree."); } } TypeDef outerInterface = property.getAttribute(OUTER_INTERFACE); List<TypeParamDef> parameters = new ArrayList<TypeParamDef>(); for (TypeParamDef generic : typeDef.getParameters()) { parameters.add(generic); } parameters.add(N); return new TypeDefBuilder(typeDef) .withPackageName(outerInterface.getPackageName()) .withName(BuilderUtils.fullyQualifiedNameDiff(property.getTypeRef(), originTypeDef) + property.getNameCapitalized() + "Nested") .withParameters(parameters) .build(); } };
public TypeDef apply(TypeDef item) { BuilderContext ctx = BuilderContextManager.getContext(); TypeDef fluent = SHALLOW_FLUENT.apply(item); List<TypeParamDef> parameters = new ArrayList<TypeParamDef>(item.getParameters()); List<TypeRef> superClassParameters = new ArrayList<TypeRef>(); TypeParamDef nextParameter = getNextGeneric(item, parameters); ClassRef builableSuperClassRef = findBuildableSuperClassRef(item); if (builableSuperClassRef != null) { superClassParameters.addAll(builableSuperClassRef.getArguments()); } TypeParamDef parameterFluent = new TypeParamDefBuilder(nextParameter).addToBounds(fluent.toInternalReference()).build(); parameters.add(parameterFluent); superClassParameters.add(parameterFluent.toReference()); TypeDef buildableSuperClass = findBuildableSuperClass(item); TypeDef superClass = buildableSuperClass != null ? FLUENT_IMPL.apply(buildableSuperClass) : ctx.getBaseFluentClass(); return new TypeDefBuilder(item) .withKind(Kind.CLASS) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(item.getName() + "FluentImpl") .withPackageName(item.getPackageName()) .withParameters(parameters) .withExtendsList(superClass.toReference(superClassParameters)) .withImplementsList(SHALLOW_FLUENT.apply(item).toInternalReference()) .withInnerTypes() .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(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){
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; }