public static int modifiersToInt(Modifier... modifiers) { return modifiersToInt(Arrays.asList(modifiers)); }
TypeDef generatedType = TypeUtils.allProperties(pojo) .stream() .filter(p -> p.getName().equals(m.getName())) if (TypeUtils.hasProperty(pojo, trimmedName)) { sb.append(".").append(withMethod).append("(").append("instance.").append(m.getName()).append("())");
public Method apply(final Property property) { String prefix = "has"; String methodName = prefix + property.getNameCapitalized(); List<Statement> statements = new ArrayList<Statement>(); if (isPrimitive(property.getTypeRef())) { statements.add(new StringStatement("return true;")); } else if (isList(property.getTypeRef()) || isSet(property.getTypeRef())) { statements.add(new StringStatement("return " + property.getName() + " != null && !" + property.getName() + ".isEmpty();")); } else if (isOptional(property.getTypeRef())|| isOptionalInt(property.getTypeRef()) || isOptionalLong(property.getTypeRef()) || isOptionalDouble(property.getTypeRef())) { statements.add(new StringStatement("return " + property.getName() + " != null && " + property.getName() + ".isPresent();")); } else { statements.add(new StringStatement("return this." + property.getName() + " != null;")); } return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(methodName) .withReturnType(BOOLEAN_REF) .withArguments() .withNewBlock() .withStatements(statements) .endBlock() .build(); } });
public Method apply(Property property) { String classPrefix = getClassPrefix(property); boolean isArray = TypeUtils.isArray(property.getTypeRef()); boolean isList = TypeUtils.isList(property.getTypeRef()); boolean isSet = TypeUtils.isSet(property.getTypeRef()); String prefix = isArray || isList ? "setTo" : "with"; String withMethodName = prefix + property.getNameCapitalized(); return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(N_REF) .withName("and") .withNewBlock() .addNewStringStatementStatement("return (N) " + classPrefix + withMethodName + "(" + (isArray || isList ? "index, " : "") + "builder.build());") .endBlock() .build(); }
boolean isArray = TypeUtils.isArray(toAdd.getTypeRef()); boolean isSet = TypeUtils.isSet(toAdd.getTypeRef()); boolean isList = TypeUtils.isList(toAdd.getTypeRef()); boolean isMap = TypeUtils.isMap(toAdd.getTypeRef()); boolean isAbstract = isAbstract(unwrapped); boolean isOptional = TypeUtils.isOptional(toAdd.getTypeRef()) || TypeUtils.isOptionalInt(toAdd.getTypeRef()) || TypeUtils.isOptionalDouble(toAdd.getTypeRef()) || TypeUtils.isOptionalLong(toAdd.getTypeRef()); } else if (!descendants.isEmpty()) { for (Property descendant : descendants) { if (TypeUtils.isCollection(descendant.getTypeRef())) { methods.addAll(ToMethod.ADD_TO_COLLECTION.apply(descendant)); methods.addAll(ToMethod.REMOVE_FROM_COLLECTION.apply(descendant));
TypeDef predicate = typeGenericOf(BuilderContextManager.getContext().getPredicateClass(), T); String prefix = Getter.prefix(property); String getterName = Getter.name(property); List<Statement> statements = new ArrayList<Statement>(); boolean isNested = false; boolean isMap = isMap(property.getTypeRef()); boolean isList = isList(property.getTypeRef()); boolean isSet = isSet(property.getTypeRef()); boolean isOptional = isOptional(property.getTypeRef()) || isOptionalDouble(property.getTypeRef()) || isOptionalInt(property.getTypeRef()) || isOptionalLong(property.getTypeRef()); .withComments(comments) .withAnnotations(annotations) .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(getterName) .withReturnType(property.getTypeRef()) .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("build" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped) .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("buildFirst" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped)
TypeDef predicate = typeGenericOf(BuilderContextManager.getContext().getPredicateClass(), T); String methodName = prefix + methodNameBase; String statement = isOptional(property.getTypeRef()) ? "return withNew" + methodNameBase + "Like(get" + methodNameBase + "() != null ? get" + methodNameBase + "().orElse(null) : null);" : "return withNew" + methodNameBase + "Like(get" + methodNameBase + "());"; .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(rewraped) .withName(methodName) if (isList(property.getTypeRef()) || isArray(property.getTypeRef())) { String suffix = Singularize.FUNCTION.apply(property.getNameCapitalized()); methods.add(new MethodBuilder(base)
TypeUtils.visitParents(item, types); String superClassName = TypeUtils.toClassName(r.getParameters().getOrDefault("superClass","")); if (!superClassName.isEmpty()) { superClassName = superClassName.replaceAll("\\.class$", ""); .withName(name) .withTypeRef(returnType) .withModifiers(TypeUtils.modifiersToInt()) .build()); .withName(StringUtils.toFieldName(name)) .withTypeRef(returnType) .withModifiers(mutable ? TypeUtils.modifiersToInt(Modifier.PRIVATE) : TypeUtils.modifiersToInt(Modifier.PRIVATE, Modifier.FINAL)) .build(); .withModifiers(modifiersToInt(Modifier.PUBLIC)) .withNewBlock() .withStatements(new StringStatement("return this." + StringUtils.toFieldName(name) + ";")) .withModifiers(modifiersToInt(Modifier.PUBLIC)) .build(); .withModifiers(modifiersToInt(Modifier.PUBLIC)) .withArguments(constructorArgs) .withNewBlock() .withModifiers(modifiersToInt(Modifier.PUBLIC)) .withName(pojoName)
ClassRef unwrapped = (ClassRef) TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(typeRef); ClassRef classRef = (ClassRef) typeRef; ClassRef builderType = isAbstract(unwrapped) || unwrapped.getDefinition().getKind() == Kind.INTERFACE ? TypeAs.VISITABLE_BUILDER.apply(unwrapped) : TypeAs.BUILDER.apply(unwrapped.getDefinition()).toInternalReference(); if (TypeUtils.isList(classRef)) { ClassRef listRef = Collections.ARRAY_LIST.toReference(builderType); return new PropertyBuilder(property).withTypeRef(Collections.LIST.toReference(builderType)) if (TypeUtils.isSet(classRef)) { ClassRef setRef = Collections.LINKED_HASH_SET.toReference(builderType); return new PropertyBuilder(property).withTypeRef(Collections.SET.toReference(builderType)) if (TypeUtils.isOptionalLong(classRef)) { ClassRef optionalRef = Optionals.OPTIONAL_LONG.toReference(builderType); return new PropertyBuilder(property).withTypeRef(optionalRef) if (TypeUtils.isOptionalDouble(classRef)) { ClassRef optionalRef = Optionals.OPTIONAL_DOUBLE.toReference(builderType); return new PropertyBuilder(property).withTypeRef(optionalRef) if (TypeUtils.isOptionalInt(classRef)) { ClassRef optionalRef = Optionals.OPTIONAL_INT.toReference(builderType); return new PropertyBuilder(property).withTypeRef(optionalRef) if (TypeUtils.isOptional(classRef)) { ClassRef optionalRef = Optionals.OPTIONAL.toReference(builderType); return new PropertyBuilder(property).withTypeRef(optionalRef)
isOptional(baseType) ? "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)) .withParameters(parameters) .withReturnType(rewraped)
String prepareOptionalSource=""; if (isBuildable(unwrapped) && !isAbstract(unwrapped)) { TypeDef builder = BUILDER.apply(((ClassRef) unwrapped).getDefinition()); prepareSource = builder.getName() + " b = new " + builder.getName() + "(" + fieldName + "); _visitables.add(b);"; .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(methodName) .withReturnType(returnType) methods.add( new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(methodName) .withReturnType(returnType)
public TypeRef apply(TypeRef type) { if (type instanceof ClassRef) { ClassRef classRef = (ClassRef) type; if (TypeUtils.isOptional(classRef)) { return classRef.getArguments().get(0); } if (TypeUtils.isOptionalInt(classRef)) { return new TypeDefBuilder().withPackageName("java.lang").withName("Integer").build().toReference(); } if (TypeUtils.isOptionalLong(classRef)) { return new TypeDefBuilder().withPackageName("java.lang").withName("Long").build().toReference(); } if (TypeUtils.isOptionalDouble(classRef)) { return new TypeDefBuilder().withPackageName("java.lang").withName("Double").build().toReference(); } } return type; } };
public JavaType apply(JavaType item) { return typeGenericOf(Constants.ARRAY_LIST, item); }
String builderName = "build" + property.getNameCapitalized(); TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_ARRAY_OF).apply(property.getTypeRef()); TypeDef predicate = typeGenericOf(BuilderContextManager.getContext().getPredicateClass(), T); .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(getterName) .withReturnType(property.getTypeRef()) .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("build" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped) .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("buildFirst" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped) .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName("buildLast" + Singularize.FUNCTION.apply(property.getNameCapitalized())) .withReturnType(unwrapped) .withComments() .withAnnotations() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC))
public static JSONSchemaProps from(TypeRef typeRef) { //1. Handle Collections and Arrays if (typeRef.getDimensions() > 0 || TypeUtils.isCollection(typeRef)) { return new JSONSchemaPropsBuilder() .withType("array") .withNewItems() .withSchema(from(TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF).apply(typeRef))) .and() .build(); //2. Handle Standard Types } else if (TYPE_MAP.containsKey(typeRef)) { return new JSONSchemaPropsBuilder() .withType(TYPE_MAP.get(typeRef)) .build(); //3. Handle Optionals } else if (TypeUtils.isOptional(typeRef)) { return from(TypeAs.UNWRAP_OPTIONAL_OF.apply(typeRef)); //4. Handle complex types } else if (typeRef instanceof ClassRef) { ClassRef classRef = (ClassRef) typeRef; TypeDef def = classRef.getDefinition(); return from(def); } return null; } }
JavaType visitorBase = unwrapGeneric(visitorInterface.getType()); JavaType visitableBase = unwrapGeneric(visitableInterface.getType()); .withGenericTypes(BASE_FLUENT.getGenericTypes()) .addToInterfaces(fluentInterface.getType()) .addToInterfaces(typeGenericOf(visitableInterface.getType(),T)) .and() .addNewMethod() .addToModifiers(Modifier.PUBLIC) .withName("build") .withReturnType(typeGenericOf(ARRAY_LIST, T)) .addNewArgument() .withType(typeGenericOf(LIST, typeExtends(Q, typeGenericOf(builderInterface.getType(), T)))) .withName("list") .endArgument() .addToModifiers(Modifier.PUBLIC) .withName("build") .withReturnType(typeGenericOf(LINKED_HASH_SET, T)) .addNewArgument() .withType(typeGenericOf(LINKED_HASH_SET, typeExtends(Q, typeGenericOf(builderInterface.getType(), T)))) .withName("set") .endArgument() .addToModifiers(Modifier.PUBLIC) .withName("aggregate") .withReturnType(typeGenericOf(ARRAY_LIST, T))
public static void visitParents(TypeDef type, List<TypeDef> types) { visitParents(type, types, new ArrayList<>()); }
/** * Creates the JSON schema for the particular {@TypeDef}. * @param definition The definition. * @return The schema. */ public static JSONSchemaProps from(TypeDef definition) { JSONSchemaPropsBuilder builder = new JSONSchemaPropsBuilder(); builder.withType("object"); List<String> required = new ArrayList<>(); for (Property property : TypeUtils.allProperties(definition)) { JSONSchemaProps schema = from(property.getTypeRef()); if (property.getAnnotations() .stream() .anyMatch(a -> a.getClassRef().getFullyQualifiedName().equals("javax.validation.constraints.NotNull"))) { required.add(property.getName()); } if (schema != null) { builder.addToProperties(property.getName(), from(property.getTypeRef())); } } builder.withRequired(required); return builder.build(); }
private TypeRef checkAgainstTypeParamRef(TypeRef typeRef, Collection<TypeParamDef> parameters) { TypeParamDef parameterDef = TypeUtils.getParameterDefinition(typeRef, parameters); if (parameterDef != null) { return parameterDef.toReference(); } return typeRef; } };
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 ""; }