public List<ObjectProperty> parseManagedTypesProperties(JavaClassSource javaClassSource, ClassTypeResolver classTypeResolver) throws ModelDriverException { List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields(); List<ObjectProperty> properties = new ArrayList<ObjectProperty>(); ObjectProperty property; for (FieldSource<JavaClassSource> field : fields) { if (DriverUtils.isManagedType(field.getType(), classTypeResolver)) { property = parseProperty(field, classTypeResolver); properties.add(property); } else { logger.debug("field: " + field + "with fieldName: " + field.getName() + " won't be loaded by the diver because type: " + field.getType().getName() + " isn't a managed type."); } } return properties; }
public boolean isManagedField(FieldSource<JavaClassSource> field, ClassTypeResolver classTypeResolver) throws Exception { if (!field.isFinal() && !field.isStatic()) { //finally we can check if the field type is a managed type. //if not, the field should remain untouched return DriverUtils.isManagedType(field.getType(), classTypeResolver); } return false; }
private boolean hasChangedToCollectionType(FieldSource<JavaClassSource> field, ObjectProperty property, ClassTypeResolver classTypeResolver) throws Exception { return DriverUtils.isManagedType(field.getType(), classTypeResolver) && !DriverUtils.equalsType(field.getType(), property.getClassName(), property.isMultiple(), property.getBag(), classTypeResolver) && property.isMultiple(); }
@Override public List<PropertySource<O>> getProperties(Class<?> type) { final Set<String> propertyNames = new LinkedHashSet<>(); for (MethodSource<O> method : getMethods()) { if ((isAccessor(method) || isMutator(method)) && method.getReturnType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(extractPropertyName(method)); } } for (FieldSource<O> field : getFields()) { if (!field.isStatic() && field.getType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(field.getName()); } } final List<PropertySource<O>> result = new ArrayList<>(propertyNames.size()); for (String name : propertyNames) { result.add(new PropertyImpl<>(name, getOrigin())); } return result; }
@Override public List<PropertySource<O>> getProperties(Class<?> type) { final Set<String> propertyNames = new LinkedHashSet<String>(); for (MethodSource<O> method : getMethods()) { if ((isAccessor(method) || isMutator(method)) && method.getReturnType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(extractPropertyName(method)); } } for (FieldSource<O> field : getFields()) { if (!field.isStatic() && field.getType().getQualifiedName().equals(type.getCanonicalName())) { propertyNames.add(field.getName()); } } final List<PropertySource<O>> result = new ArrayList<PropertySource<O>>(propertyNames.size()); for (String name : propertyNames) { result.add(new PropertyImpl<O>(name, getOrigin())); } return result; }
private void createCRUDRepository(JavaClassSource entitySource, Project project) { // build interface declaration final String name = entitySource.getName(); final String idFieldType = entitySource.getFields().stream() .filter(field -> field.hasAnnotation(Id.class)) .findFirst() .orElseThrow(IllegalArgumentException::new) .getType() .getSimpleName(); final String repoInterfaceDeclaration = "public interface " + name + "Repository extends CrudRepository<" + name + "," + idFieldType + "> {}"; JavaInterfaceSource repoSource = Roaster.parse(JavaInterfaceSource.class, repoInterfaceDeclaration) .setPackage(entitySource.getPackage()); repoSource.addImport("org.springframework.data.repository.CrudRepository"); Roaster.format(repoSource.toString()); // Create Java Classes Greeting and GreetingProperties JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class); facet.saveJavaSource(repoSource); }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
/** * Takes care of field and the corresponding setter/getter removal. */ public void removeField(JavaClassSource javaClassSource, String fieldName, ClassTypeResolver classTypeResolver) throws Exception { logger.debug("Removing field: " + fieldName + ", from class: " + javaClassSource.getName()); FieldSource<JavaClassSource> field; GenerationTools genTools = new GenerationTools(); String methodName; field = javaClassSource.getField(fieldName); if (field != null) { //check if the class has a setter/getter for the given field. Class<?> fieldClass = classTypeResolver.resolveType(field.getType().getName()); methodName = genTools.toJavaGetter(fieldName, fieldClass.getName()); removeMethodByParamsClass(javaClassSource, methodName); methodName = genTools.toJavaSetter(fieldName); removeMethodByParamsClass(javaClassSource, methodName, fieldClass); //finally remove the field. javaClassSource.removeField(field); } else { logger.debug("Field field: " + fieldName + " was not found in class: " + javaClassSource.getName()); } }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
/** * Generates a getXXX and setXXX method for the supplied field * * @param clazz * @param field */ public static void createGetterAndSetter(final JavaClassSource clazz, final FieldSource<JavaClassSource> field) { if (!clazz.hasField(field)) { throw new IllegalArgumentException("Entity did not contain the given field [" + field + "]"); } clazz.getMethods(); String fieldName = field.getName(); String methodNameSuffix = Strings.capitalize(fieldName); clazz.addMethod().setReturnType(field.getType().toString()).setName("get" + methodNameSuffix) .setPublic() .setBody("return this." + fieldName + ";"); if (!field.isFinal()) { clazz.addMethod().setReturnTypeVoid().setName("set" + methodNameSuffix).setPublic() .setParameters("final " + field.getType().toString() + " " + fieldName) .setBody("this." + fieldName + " = " + fieldName + ";"); } }
@Override public Type<O> getType() { if (isAccessible()) { return getAccessor().getReturnType(); } if (isMutable()) { return getMutator().getParameters().get(0).getType(); } if (hasField()) { return getField().getType(); } return null; }
@Override public Type<O> getType() { if (isAccessible()) { return getAccessor().getReturnType(); } if (isMutable()) { return getMutator().getParameters().get(0).getType(); } if (hasField()) { return getField().getType(); } return null; }
@Override public Type<O> getType() { if (isAccessible()) { return getAccessor().getReturnType(); } if (isMutable()) { return getMutator().getParameters().get(0).getType(); } if (hasField()) { return getField().getType(); } return null; }
@Override public MethodSource<O> getMutator() { final Type<O> type; if (hasField()) { type = getField().getType(); } else if (isAccessible()) { type = getAccessor().getReturnType(); } else { type = null; } for (MethodSource<O> method : getOrigin().getMethods()) { if (isMutator(method)) { if (type == null || Strings.areEqual(type.getQualifiedName(), method.getParameters().get(0).getType() .getQualifiedName())) { return method; } } } return null; }
public void visit(FieldSource<? extends JavaSource> fieldSource) { Type fieldType = fieldSource.getType(); String fieldClassName;
@Override public MethodSource<O> getMutator() { final Type<O> type; if (hasField()) { type = getField().getType(); } else if (isAccessible()) { type = getAccessor().getReturnType(); } else { type = null; } for (MethodSource<O> method : getOrigin().getMethods()) { if (isMutator(method)) { if (type == null || Strings.areEqual(type.getQualifiedName(), method.getParameters().get(0).getType() .getQualifiedName())) { return method; } } } return null; }
@Override public MethodSource<O> getMutator() { final Type<O> type; if (hasField()) { type = getField().getType(); } else if (isAccessible()) { type = getAccessor().getReturnType(); } else { type = null; } for (MethodSource<O> method : getOrigin().getMethods()) { if (isMutator(method)) { if (type == null || Strings.areEqual(type.getQualifiedName(), method.getParameters().get(0).getType() .getQualifiedName())) { return method; } } } return null; }
public void visit(FieldSource<? extends JavaSource> fieldSource) { Type fieldType = fieldSource.getType(); String fieldClassName; // the javadoc for Named.getName() is misleading: // the FieldSource.getName() (which is implemented by FieldImpl.getName()) // returns the (fully-qualified!) name of the field String fieldName = fieldSource.getName(); resParts.addPart(fieldName, PartType.FIELD); if (fieldType.isPrimitive()) { fieldClassName = fieldType.getName(); } else { fieldClassName = fieldType.getQualifiedName(); } addJavaResourceReference(fieldClassName); // Field annotations for (AnnotationSource annoSource : fieldSource.getAnnotations()) { visit(annoSource); } }