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; }
/** * Unrolls the hierararchy of a specified type. * @param typeDef The specified type. * @return A set that contains all the hierarching (including the specified type). */ public static Set<TypeDef> unrollHierarchy(TypeDef typeDef) { if (OBJECT.equals(typeDef)) { return new HashSet<>(); } Set<TypeDef> hierarchy = new HashSet<>(); hierarchy.add(typeDef); hierarchy.addAll(typeDef.getExtendsList().stream().flatMap(s -> unrollHierarchy(s.getDefinition()).stream()).collect(Collectors.toSet())); return hierarchy; }
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); } }
public static ClassRef findBuildableSuperClassRef(TypeDef clazz) { BuildableRepository repository = BuilderContextManager.getContext().getBuildableRepository(); for (ClassRef superClass : clazz.getExtendsList()) { if (repository.isBuildable(superClass)) { return superClass; } } return null; }
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; } }
if (!target.getExtendsList().isEmpty()) { target = BuilderContextManager.getContext().getBuildableRepository().getBuildable(target.getExtendsList().iterator().next()); } else { return statements;
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; }
public static TypeDef findBuildableSuperClass(TypeDef clazz) { BuildableRepository repository = BuilderContextManager.getContext().getBuildableRepository(); for (ClassRef superClass : clazz.getExtendsList()) { if (repository.isBuildable(superClass)) { return repository.getBuildable(superClass); } } return null; }
.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;
public TypeDef apply(TypeDef... 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) { TypeDefBuilder builder = new TypeDefBuilder(items[0]); for (ClassRef classRef : items[1].getExtendsList()) { builder = builder.addToExtendsList(classRef); } for (TypeParamDef type : items[1].getParameters()) { if (!items[0].getParameters().contains(type)) { builder = builder.addToParameters(type); } } return builder.build(); } else { TypeDef[] rest = new TypeDef[items.length - 1]; System.arraycopy(items, 1, rest, 0, rest.length); return TYPES.apply(new TypeDef[]{items[0], TYPES.apply(rest)}); } } };
if (!current.getExtendsList().iterator().hasNext()) { break; String fqn = current.getExtendsList().iterator().next().getDefinition().getFullyQualifiedName(); current = DefinitionRepository.getRepository().getDefinition(fqn);
/** * Checks if a {@link TypeDef} is an instance of an other {@link TypeDef}. * @param type The type to compare. * @param targetType The target type. * @param function * @return true if match, false otherwise. */ public static boolean isInstanceOf(TypeRef type, TypeDef targetType, Function<TypeRef, Boolean> function) { if (type instanceof ClassRef) { ClassRef classRef = (ClassRef) type; TypeDef definition = classRef.getDefinition(); if (definition.getFullyQualifiedName().equals(targetType.getFullyQualifiedName())) { return true; } for (TypeRef i : definition.getImplementsList()) { if (function.apply(i)) { return true; } } for (TypeRef e : definition.getExtendsList()) { if (function.apply(e)) { return true; } } } return false; }
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 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){