Search in sources :

Example 1 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class JSONMixinMetadata method getDeseralizerFieldFor.

private FieldMetadataBuilder getDeseralizerFieldFor(FieldMetadata field, JavaType deserializer) {
    AnnotationMetadataBuilder annotation = new AnnotationMetadataBuilder(JSON_DESERIALIZE);
    annotation.addClassAttribute("using", deserializer);
    return new FieldMetadataBuilder(getId(), Modifier.PRIVATE, Arrays.asList(annotation), field.getFieldName(), field.getFieldType());
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 2 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class ExceptionsMetadata method getLoggerField.

public FieldMetadata getLoggerField() {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.append(getNameOfJavaType(LOGGER_FACTORY_JAVA_TYPE));
    bodyBuilder.append(".getLogger(");
    bodyBuilder.append(governorPhysicalTypeMetadata.getType().getSimpleTypeName());
    bodyBuilder.append(".class)");
    final String initializer = bodyBuilder.getOutput();
    FieldMetadataBuilder field = new FieldMetadataBuilder(getId(), Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL, new JavaSymbolName("LOG"), LOGGER_JAVA_TYPE, initializer);
    return field.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 3 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method getParameterizedConstructor.

/**
 * Locates the parameterised constructor consisting of the id fields for
 * this class.
 *
 * @param fields
 * @return the constructor, never null.
 */
private ConstructorMetadataBuilder getParameterizedConstructor(final List<FieldMetadataBuilder> fields) {
    // Search for an existing constructor
    final List<JavaType> parameterTypes = new ArrayList<JavaType>();
    for (final FieldMetadataBuilder field : fields) {
        parameterTypes.add(field.getFieldType());
    }
    final ConstructorMetadata result = governorTypeDetails.getDeclaredConstructor(parameterTypes);
    if (result != null) {
        // Found an existing parameterised constructor on this class
        publicNoArgConstructor = true;
        return null;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("super();");
    for (final FieldMetadataBuilder field : fields) {
        final String fieldName = field.getFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
        parameterNames.add(field.getFieldName());
    }
    // Create the constructor
    final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
    constructorBuilder.setParameterNames(parameterNames);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    return constructorBuilder;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 4 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method getMutators.

/**
 * Locates the mutator methods.
 * <p>
 * If {@link #getFieldBuilders()} returns fields created by this ITD, public
 * mutators will automatically be produced in the declaring class.
 *
 * @param fields
 * @return the mutators (never returns null)
 */
private List<MethodMetadataBuilder> getMutators(final List<FieldMetadataBuilder> fields) {
    final List<MethodMetadataBuilder> mutators = new ArrayList<MethodMetadataBuilder>();
    // Compute the names of the mutators that will be produced
    for (final FieldMetadataBuilder field : fields) {
        final JavaSymbolName requiredMutatorName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
        final JavaType parameterType = field.getFieldType();
        final MethodMetadata mutator = getGovernorMethod(requiredMutatorName, parameterType);
        if (mutator == null) {
            mutators.add(getMutatorMethod(field.getFieldName(), field.getFieldType()));
        } else {
            Validate.isTrue(Modifier.isPublic(mutator.getModifier()), "User provided field but failed to provide a public '%s(%s)' method in '%s'", requiredMutatorName.getSymbolName(), field.getFieldName().getSymbolName(), destination.getFullyQualifiedTypeName());
            mutators.add(new MethodMetadataBuilder(mutator));
        }
    }
    return mutators;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 5 with FieldMetadataBuilder

use of org.springframework.roo.classpath.details.FieldMetadataBuilder in project spring-roo by spring-projects.

the class IdentifierMetadata method getFieldBuilders.

/**
 * Locates declared fields.
 * <p>
 * If no parent is defined, one will be located or created. All declared
 * fields will be returned.
 *
 * @return fields (never returns null)
 */
private List<FieldMetadataBuilder> getFieldBuilders() {
    // Locate all declared fields
    final List<? extends FieldMetadata> declaredFields = governorTypeDetails.getDeclaredFields();
    // Add fields to ITD from annotation
    final List<FieldMetadata> fields = new ArrayList<FieldMetadata>();
    if (identifierServiceResult != null) {
        for (final Identifier identifier : identifierServiceResult) {
            final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
            annotations.add(getColumnBuilder(identifier));
            if (identifier.getFieldType().equals(DATE)) {
                setDateAnnotations(identifier.getColumnDefinition(), annotations);
            }
            final FieldMetadata idField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, identifier.getFieldName(), identifier.getFieldType()).build();
            // Only add field to ITD if not declared on governor
            if (!hasField(declaredFields, idField)) {
                fields.add(idField);
            }
        }
    }
    fields.addAll(declaredFields);
    // Remove fields with static and transient modifiers
    for (final Iterator<FieldMetadata> iter = fields.iterator(); iter.hasNext(); ) {
        final FieldMetadata field = iter.next();
        if (Modifier.isStatic(field.getModifier()) || Modifier.isTransient(field.getModifier())) {
            iter.remove();
        }
    }
    // Remove fields with the @Transient annotation
    final List<FieldMetadata> transientAnnotatedFields = governorTypeDetails.getFieldsWithAnnotation(TRANSIENT);
    if (fields.containsAll(transientAnnotatedFields)) {
        fields.removeAll(transientAnnotatedFields);
    }
    final List<FieldMetadataBuilder> fieldBuilders = new ArrayList<FieldMetadataBuilder>();
    if (!fields.isEmpty()) {
        for (final FieldMetadata field : fields) {
            fieldBuilders.add(new FieldMetadataBuilder(field));
        }
        return fieldBuilders;
    }
    // We need to create a default identifier field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Compute the column name, as required
    final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
    columnBuilder.addStringAttribute("name", "id");
    columnBuilder.addBooleanAttribute("nullable", false);
    annotations.add(columnBuilder);
    fieldBuilders.add(new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, new JavaSymbolName("id"), LONG_OBJECT));
    return fieldBuilders;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) RooIdentifier(org.springframework.roo.addon.jpa.annotations.identifier.RooIdentifier) ArrayList(java.util.ArrayList) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)66 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)48 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)40 ArrayList (java.util.ArrayList)21 JavaType (org.springframework.roo.model.JavaType)19 JdkJavaType (org.springframework.roo.model.JdkJavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)8 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)7 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)5 LinkedHashMap (java.util.LinkedHashMap)4 Reference (org.springframework.roo.addon.dbre.addon.model.Reference)4 Table (org.springframework.roo.addon.dbre.addon.model.Table)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)4 EnumDetails (org.springframework.roo.model.EnumDetails)4