public TypeDef getBuildable(TypeRef type) { if (type instanceof ClassRef) { return buildables.get(((ClassRef)type).getDefinition().getFullyQualifiedName()); } return null; }
public ClassRefBuilder(ClassRef instance, Boolean validationEnabled) { this.fluent = this; this.withDefinition(instance.getDefinition()); this.withFullyQualifiedName(instance.getFullyQualifiedName()); this.withDimensions(instance.getDimensions()); this.withArguments(instance.getArguments()); this.withAttributes(instance.getAttributes()); this.validationEnabled = validationEnabled; }
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 String getName() { if (requiresFullyQualifiedName()) { return getDefinition().getFullyQualifiedName(); } return getDefinition().getName(); }
ClassRef builderType = TypeAs.SHALLOW_BUILDER.apply(unwrappedClassRef.getDefinition()).toReference(); TypeDef propertyTypeDef = BuilderContextManager.getContext().getDefinitionRepository().getDefinition((baseType).getDefinition().getFullyQualifiedName()); if (propertyTypeDef != null) { baseType = propertyTypeDef.toInternalReference(); TypeDef nestedType = PropertyAs.NESTED_INTERFACE_TYPE.apply(property); List<TypeParamDef> parameters = baseType.getDefinition().getParameters(); List<TypeRef> typeArguments = new ArrayList<TypeRef>(); for (TypeRef ignore : baseType.getArguments()) { typeArguments.add(Q); ? "return withNew" + methodNameBase + "Like(get" + methodNameBase + "() != null && get"+methodNameBase+"().isPresent() ? get" + methodNameBase + "().get() : new " + builderType.getName() + "().build());" : "return withNew" + methodNameBase + "Like(get" + methodNameBase + "() != null ? get" + methodNameBase + "(): new " + builderType.getName() + "().build());"; return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC))
TypeDef propertyTypeDef = BuilderContextManager.getContext().getDefinitionRepository().getDefinition(unwrappedClassRef.getDefinition().getFullyQualifiedName()); if (propertyTypeDef != null) { baseType = propertyTypeDef.toInternalReference(); TypeDef nestedType = PropertyAs.NESTED_INTERFACE_TYPE.apply(property); List<TypeParamDef> parameters = baseType.getDefinition().getParameters(); List<TypeRef> typeArguments = new ArrayList<TypeRef>(); for (TypeRef ignore : baseType.getArguments()) { typeArguments.add(Q);
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 ""; }
String targetClass = targetType.getName(); parameters.addAll(targetType.getDefinition().getParameters()); String builderClass = targetClass + "Builder"; alsoImport.add(TypeAs.BUILDER.apply(targetType.getDefinition()).toInternalReference()); statements.add(new StringStatement("for (" + targetClass + " item : items) {" + builderClass + " builder = new " + builderClass + "(item);_visitables.remove(builder);if (this." + propertyName + " != null) {this."+propertyName+".remove(builder);}} return (" + returnType + ")this;")); } else if (!descendants.isEmpty()) { final ClassRef targetType = (ClassRef) unwrapped; parameters.addAll(targetType.getDefinition().getParameters()); statements.add(new StringStatement("for (" + targetType.toString() + " item : items) {" + StringUtils.join(descendants, new Function<Property, String>() { public String apply(Property item) { TypeRef itemRef = TypeAs.combine(UNWRAP_COLLECTION_OF, ARRAY_OF).apply(item.getTypeRef()); statements.add(createRemoveFromDescendantsFallback(targetType.getName(), property.getName())); statements.add(new StringStatement( "} return (" + returnType + ")this;"));
@Override public int compare(ClassRef o1, ClassRef o2) { return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName()); } });
String key = ref.getName(); definitionRepository.putCustomMapping(builderKey, ref.getDefinition().getFullyQualifiedName() + "Builder"); definitionRepository.putCustomMapping(editableKey, "Editable" + ref.getDefinition().getFullyQualifiedName()); definitionRepository.putCustomMapping(fluentKey, ref.getDefinition().getFullyQualifiedName() + "Fluent"); definitionRepository.putCustomMapping(fluentImplKey, ref.getDefinition().getFullyQualifiedName() + "FluentImpl");
if (r.getClassRef().getFullyQualifiedName().equals(Buildable.class.getTypeName())) { if (!annotationRefs.contains(r)) { annotationRefs.add(r); if (r.getClassRef().getFullyQualifiedName().equals(Pojo.class.getTypeName())) { pojoRef = r; Map<String, Object> params = r.getParameters(); ClassRef superClassRef = superClass.toInternalReference(); extendsList.add(superClassRef); BuilderContextManager.getContext().getBuildableRepository().register(superClassRef.getDefinition()); BuilderUtils.findBuildableReferences(superClassRef) .stream() .forEach(b -> BuilderContextManager.getContext().getBuildableRepository().register(b.getDefinition())); if (ref.getDefinition().isAnnotation()) { TypeDef p = hasPojoAnnotation(ref.getDefinition()) ? POJO.apply(ref.getDefinition()) : POJO.apply(new TypeDefBuilder(ref.getDefinition()) .withAnnotations(annotationRefs) .addToAnnotations(inheritedPojoRef) .withDimensions(ref.getDimensions()) .build(); item.getMethods() .stream() .filter(m -> m.getReturnType() instanceof ClassRef && ((ClassRef)m.getReturnType()).getDimensions() > 0)
public TypeRef apply(TypeRef type) { if (type instanceof ClassRef) { ClassRef classRef = (ClassRef) type; if (Collections.IS_COLLECTION.apply(classRef)) { return classRef.getArguments().get(0); } } return type; } };
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; AnnotationRef that = (AnnotationRef) o; if (!classRef.equals(that.classRef)) return false; return true; }
@Override public Method apply(Property property) { Method method = WITH_NEW_LIKE_NESTED.apply(property); if (!(property.getTypeRef() instanceof ClassRef)) { throw new IllegalStateException("Expected Nestable / Buildable type and found:" + property.getTypeRef()); } ClassRef baseType = (ClassRef) TypeAs.UNWRAP_COLLECTION_OF.apply(property.getTypeRef()); TypeRef returnType = property.hasAttribute(GENERIC_TYPE_REF) ? property.getAttribute(GENERIC_TYPE_REF) : T_REF; TypeDef nestedTypeImpl = PropertyAs.NESTED_CLASS_TYPE.apply(property); List<TypeRef> typeArguments = new ArrayList<TypeRef>(); for (TypeRef ignore : baseType.getArguments()) { typeArguments.add(Q); } typeArguments.add(returnType); ClassRef rewrapedImpl = nestedTypeImpl.toReference(typeArguments); return new MethodBuilder(method) .addToArguments(0, INDEX) .withName(method.getName().replaceFirst("add", "set")) .editBlock() .withStatements(new StringStatement("return new " + rewrapedImpl.getName() + "(index, item);")) .endBlock() .build(); } };
private static boolean hasArrayFields(TypeDef item) { return item.getMethods() .stream() .filter(m -> m.getReturnType() instanceof ClassRef && ((ClassRef)m.getReturnType()).getDimensions() > 0) .findAny().isPresent(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(AT).append(classRef.toString()); if (parameters != null && parameters.size() > 0) { sb.append(OP); boolean first = true; for (Map.Entry<String, Object> entry : parameters.entrySet()) { Object value = entry.getValue(); if (first) { first = false; } else { sb.append(SPACE).append(COMA); } sb.append(entry.getKey()).append(SPACE).append(EQ).append(SPACE).append(toString(value)); } sb.append(CP); } return sb.toString(); } }
public static TypeParamDef getParameterDefinition(TypeRef typeRef, Collection<TypeParamDef> parameters) { String name; if (typeRef instanceof ClassRef) { name = ((ClassRef)typeRef).getName(); } else if (typeRef instanceof TypeParamRef) { name = ((TypeParamRef)typeRef).getName(); } else if (typeRef instanceof PrimitiveRef) { name = ((PrimitiveRef)typeRef).getName(); } else { name = typeRef.toString(); } for (TypeParamDef parameter : parameters) { if (parameter.getName().equals(name)) { return parameter; } } return null; }
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()) { typeArguments.add(arg); .withName(methodName) .withNewBlock() .addNewStringStatementStatement("return new " + rewrapedImpl.getName() + "();") .endBlock() .build();
List<TypeRef> arguments = ((ClassRef) typeRef).getArguments(); targetType = isAbstract(unwarppedClassRef) || unwarppedClassRef.getDefinition().getKind() == Kind.INTERFACE ? TypeAs.VISITABLE_BUILDER.apply(unwarppedClassRef) : TypeAs.BUILDER.apply(unwarppedClassRef.getDefinition()).toInternalReference();
/** * 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; }