@Override public int compare(JavaClazz left, JavaClazz right) { return left.getType().getFullyQualifiedName().compareTo(right.getType().getFullyQualifiedName()); } });
/** * Finds all the descendants of a type that are buildable. * @param item The type. * @return */ public static Set<JavaType> getBuildableDescendants(JavaType item) { if (item.getFullyQualifiedName().equals(Constants.OBJECT.getFullyQualifiedName())) { return new LinkedHashSet<JavaType>(); } Set<JavaType> result = new LinkedHashSet<JavaType>(); BuilderContext ctx = BuilderContextManager.getContext(); BuildableRepository repository = ctx.getRepository(); for (TypeElement element : repository.getBuildables()) { JavaType type = ctx.getToType().apply(element.toString()); if (isDescendant(type, item)) { result.add(type); } } return result; }
/** * Checks if type has any descendants that are "buildable" * @param item The type. * @return true if a buildable ancestor is found. */ public static boolean hasBuildableDescendants(JavaType item) { if (item.getFullyQualifiedName().equals(Constants.OBJECT.getFullyQualifiedName())) { return false; } BuilderContext ctx = BuilderContextManager.getContext(); BuildableRepository repository = ctx.getRepository(); for (TypeElement element : repository.getBuildables()) { JavaType type = ctx.getToType().apply(element.toString()); if (isDescendant(type, item)) { return true; } } return false; }
/** * Convert a {@link Collection} of {@link javax.lang.model.element.ExecutableElement}s to a {@link java.util.Set} of {@link io.sundr.codegen.model.JavaClazz}es. * * @param context The context of the operation. * @param elements The target elements. * @return A set of {@link io.sundr.codegen.model.JavaClazz} that describes the interfaces. */ public static Set<JavaClazz> executablesToInterfaces(DslProcessorContext context, Collection<ExecutableElement> elements) { Map<String, JavaClazz> byName = new LinkedHashMap<String, JavaClazz>(); for (ExecutableElement current : elements) { JavaClazz clazz = executableToInterface(context, current); InterfaceName interfaceName = current.getAnnotation(InterfaceName.class); String name = interfaceName != null ? clazz.getType().getPackageName() + "." + interfaceName.value() : clazz.getType().getFullyQualifiedName(); if (byName.containsKey(name)) { JavaClazz other = byName.remove(name); byName.put(name, Merge.CLASSES.apply(new JavaClazz[]{other, clazz})); } else { byName.put(name, clazz); } } return new LinkedHashSet<JavaClazz>(byName.values()); }
@Override public JavaType apply(String fullName) { JavaType type = delegate.apply(fullName); if (type == null) { return null; } TypeElement typeElement = elements.getTypeElement(fullName); boolean isBuildable = false; if (fullName.endsWith("[]")) { typeElement = elements.getTypeElement(fullName.substring(0, fullName.length() - 2)); isBuildable = isBuildable(typeElement); } else if (type.isCollection()) { for (JavaType genericType : type.getGenericTypes()) { isBuildable = isBuildable(elements.getTypeElement(genericType.getFullyQualifiedName())); } } else { isBuildable = isBuildable(typeElement); } Map<String, Object> attributes = new HashMap<String, Object>(type.getAttributes()); attributes.put(BUILDABLE, isBuildable); return new JavaTypeBuilder(type).withAttributes(attributes).build(); }
@Override public JavaClazz apply(JavaProperty item) { BuilderContext context = BuilderContextManager.getContext(); Elements elements = context.getElements(); JavaType type = TypeAs.UNWRAP_COLLECTION_OF.apply(item.getType()); TypeElement typeElement = elements.getTypeElement(type.getFullyQualifiedName()); return BuilderContextManager.getContext().getToClazz().apply(typeElement); } };