public PropertyFluentImpl(Property instance){ this.withAnnotations(instance.getAnnotations()); this.withTypeRef(instance.getTypeRef()); this.withName(instance.getName()); this.withModifiers(instance.getModifiers()); this.withAttributes(instance.getAttributes()); }
public Method apply(Property property) { TypeRef returnType = property.hasAttribute(GENERIC_TYPE_REF) ? property.getAttribute(GENERIC_TYPE_REF) : T_REF; TypeRef mapType = property.getTypeRef(); Property mapProperty = new PropertyBuilder().withName("map").withTypeRef(mapType).build(); String methodName = "addTo" + property.getNameCapitalized(); return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(methodName) .withReturnType(returnType) .withArguments(mapProperty) .withNewBlock() .addNewStringStatementStatement("if(this."+property.getName()+" == null && map != null) { this." + property.getName() + " = " + property.getAttribute(INIT_FUNCTION).apply(Collections.emptyList()) + "; }") .addNewStringStatementStatement("if(map != null) { this." + property.getName() + ".putAll(map);} return (" + returnType + ")this;") .endBlock() .build(); } });
@Override public String toString() { StringBuilder sb = new StringBuilder(); if (isPublic()) { sb.append(PUBLIC).append(SPACE); } else if (isProtected()) { sb.append(PROTECTED).append(SPACE); } else if (isPrivate()) { sb.append(PRIVATE).append(SPACE); } if (isStatic()) { sb.append(STATIC).append(SPACE); } if (isFinal()) { sb.append(FINAL).append(SPACE); } sb.append(typeRef).append(SPACE); sb.append(name); return sb.toString(); } }
private void writeField(Writer writer, Property field, String block) throws IOException { if (field != null) { writer.append(field.toString()); if (field.getAttribute(INIT) != null) { writer.append(" = ").append(field.getAttribute(INIT)); } } writer.append(";"); } }
/** * Returns true if method is a getter of property. * In strict mode it will not strip non-alphanumeric characters. */ private static boolean isApplicable(Method method, Property property, boolean strict) { if (method.getArguments().size() != 1) { return false; } if (!method.getArguments().get(0).getTypeRef().equals(property.getTypeRef())) { return false; } String capitalized = capitalizeFirst(property.getName()); if (method.getName().endsWith("set" + capitalized)) { return true; } if (!strict && method.getName().endsWith("set" + property.getNameCapitalized())) { return true; } return false; }
public Method apply(Property property) { TypeDef originTypeDef = property.getAttribute(Constants.ORIGIN_TYPEDEF); String methodName = "end" + BuilderUtils.fullyQualifiedNameDiff(property.getTypeRef(), originTypeDef) + capitalizeFirst(IS_COLLECTION.apply(property.getTypeRef()) ? Singularize.FUNCTION.apply(property.getNameCapitalized()) : property.getNameCapitalized()); return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withReturnType(N_REF) .withName(methodName) .withNewBlock() .addNewStringStatementStatement("return and();") .endBlock() .build(); } });
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 String prefix(Property property) { return TypeUtils.isBoolean(property.getTypeRef()) ? IS_PREFIX : GET_PREFIX; } }
/** * 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(); }
@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(); } };
public static boolean isNestingFiltered(Property property, ClassRef classRef) { for (AnnotationRef ref : property.getAnnotations()) { if (ref.getClassRef().getFullyQualifiedName().equals(FilterDescendants.class.getName())) { Map<String, Object> parameters = ref.getParameters(); Object value = parameters == null ? null : parameters.get(VALUE); if (value instanceof String && property.getTypeRef() instanceof ClassRef) { Pattern p = Pattern.compile((String) value); if (p.matcher(classRef.getFullyQualifiedName()).matches()) { return false; } } return true; } } return false; }
Method getter = Getter.find(clazz, property); if (getter != null) { String cast = property.getTypeRef() instanceof TypeParamRef ? "(" + property.getTypeRef().toString() + ")" : ""; statements.add(new StringStatement(new StringBuilder().append(ref).append(".with").append(property.getNameCapitalized()).append("(").append(cast).append("instance.").append(getter.getName()).append("()); ").toString())); } else { throw new IllegalStateException("Could not find getter for property:" + property + " in class:" + clazz); for (Property property : target.getProperties()) { if (!hasBuildableConstructorWithArgument(target, property) && Setter.has(target, property)) { String withName = "with" + property.getNameCapitalized(); String getterName = Getter.find(target, property).getName(); statements.add(new StringStatement(new StringBuilder().append(ref).append(".").append(withName).append("(instance.").append(getterName).append("());\n").toString()));
public TypeDef apply(Property item) { TypeDef nested = new TypeDefBuilder(SHALLOW_NESTED_TYPE.apply(item)).withOuterType(item.getAttribute(OUTER_INTERFACE)).build(); TypeDef outerInterface = item.getAttribute(OUTER_INTERFACE); TypeRef typeRef = TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(item.getTypeRef());
final TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(property.getTypeRef()); if (property.isStatic()) { continue; 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));
public String apply(Property item) { return item.getName(); } }, ", ") + ");")
public Method apply(Property property) { String methodName = "set" + property.getNameCapitalized(); return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(methodName) .withReturnType(VOID) .withArguments() .withNewBlock() .addNewStringStatementStatement("this." + property.getName() + "=" + property.getName() + ";") .endBlock() .build(); } });
public Method apply(Property property) { TypeRef returnType = property.hasAttribute(GENERIC_TYPE_REF) ? property.getAttribute(GENERIC_TYPE_REF) : T_REF; String methodName = "with" + property.getNameCapitalized(); List<ClassRef> alsoImport = new ArrayList<ClassRef>(); return new MethodBuilder() .withModifiers(TypeUtils.modifiersToInt(Modifier.PUBLIC)) .withName(methodName) .withReturnType(returnType) .withArguments(property) .withVarArgPreferred(true) .withNewBlock() .withStatements(getStatements(property, alsoImport)) .endBlock() .addToAttributes(Attributeable.ALSO_IMPORT, alsoImport) .build(); }
public Set<ClassRef> getReferences() { Set<ClassRef> refs = new LinkedHashSet<ClassRef>(); for (AnnotationRef annotationRef : annotations) { refs.addAll(annotationRef.getReferences()); } if (typeRef instanceof ClassRef) { ClassRef classRef = (ClassRef) typeRef; refs.addAll(classRef.getReferences()); } for (AnnotationRef a : getAnnotations()) { refs.addAll(a.getClassRef().getReferences()); } if (getAttributes().containsKey(ALSO_IMPORT)) { Object obj = getAttributes().get(ALSO_IMPORT); if (obj instanceof ClassRef) { refs.add((ClassRef) obj); } else if (obj instanceof Collection) { refs.addAll((Collection<? extends ClassRef>) obj); } } return refs; }
public static boolean isNestingIgnored(Property property) { for (AnnotationRef ref : property.getAnnotations()) { if (ref.getClassRef().getFullyQualifiedName().equals(IgnoreDescendants.class.getName())) { return true; } } return false; }
/** * Return the getter name for the specified {@link Property}. * @param property The property. * @return The name. */ public static String name(Property property) { return prefix(property) + property.getNameCapitalized(); }