Search in sources :

Example 21 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getDataField.

/**
 * @return the "data" field to use, which is either provided by the user or
 *         produced on demand (never returns null)
 */
private FieldMetadataBuilder getDataField() {
    final List<JavaType> parameterTypes = Arrays.asList(entity);
    final JavaType listType = new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, parameterTypes);
    int index = -1;
    while (true) {
        // Compute the required field name
        index++;
        // The type parameters to be used by the field type
        final JavaSymbolName fieldName = new JavaSymbolName("data" + StringUtils.repeat("_", index));
        dataFieldName = fieldName;
        final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
        if (candidate != null) {
            // Verify if candidate is suitable
            if (!Modifier.isPrivate(candidate.getModifier())) {
                // next possible name)
                continue;
            }
            if (!candidate.getFieldType().equals(listType)) {
                // The equals method also verifies type params are present
                continue;
            }
            // we assume the user knows what they're doing and have made one
            return new FieldMetadataBuilder(candidate);
        }
        // Candidate not found, so let's create one
        FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldName, listType);
        CommentStructure comment = new CommentStructure();
        comment.addComment(new JavadocComment("List of created entities."), CommentLocation.BEGINNING);
        fieldBuilder.setCommentStructure(comment);
        return fieldBuilder;
    }
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 22 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getSizeField.

/**
 * Creates size field.
 *
 * @return {@link FieldMetadataBuilder} for building field into ITD.
 */
private FieldMetadata getSizeField() {
    // Create field
    FieldMetadataBuilder sizeField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(SIZE_VAR), JavaType.INT_PRIMITIVE);
    CommentStructure comment = new CommentStructure();
    comment.addComment(new JavadocComment("Number of elements to create and persist."), CommentLocation.BEGINNING);
    sizeField.setCommentStructure(comment);
    return sizeField.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 23 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getSpecificEntityMethod.

private MethodMetadata getSpecificEntityMethod() {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName(JpaEntityFactoryMetadata.SPECIFIC_METHOD_PREFIX + this.entity.getSimpleTypeName());
    final JavaType parameterType = JavaType.INT_PRIMITIVE;
    final List<JavaSymbolName> parameterNames = Arrays.asList(INDEX_SYMBOL);
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName, parameterType);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
        this.specificEntityMethod = userMethod;
        return userMethod;
    }
    // Create method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("init();");
    bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " < 0) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(INDEX_VAR + " = 0;");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine("if (" + INDEX_VAR + " > (" + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1)) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(INDEX_VAR + " = " + getAccessorMethod(getDataField().build()).getMethodName() + "().size() - 1;");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.appendFormalLine("return %s().get(%s);", getAccessorMethod(getDataField().build()).getMethodName(), INDEX_VAR);
    final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add(String.format("%s the position of the {@link %s} to return", INDEX_VAR, this.entity.getSimpleTypeName()));
    JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a given index.", this.entity.getSimpleTypeName()), paramsInfo, String.format("%1$s the specific {@link %1$s}", this.entity.getSimpleTypeName()), null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    this.specificEntityMethod = methodBuilder.build();
    return this.specificEntityMethod;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 24 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JavaBeanMetadata method generateGettersAndSetters.

protected void generateGettersAndSetters(final Map<FieldMetadata, JavaSymbolName> declaredFields, List<? extends MethodMetadata> interfaceMethods) {
    // Add getters and setters
    for (final Entry<FieldMetadata, JavaSymbolName> entry : declaredFields.entrySet()) {
        final FieldMetadata field = entry.getKey();
        final MethodMetadataBuilder accessorMethod = getDeclaredGetter(field);
        final MethodMetadataBuilder mutatorMethod = getDeclaredSetter(field);
        // Check to see if GAE is interested
        if (entry.getValue() != null) {
            JavaSymbolName hiddenIdFieldName;
            if (field.getFieldType().isCommonCollectionType()) {
                hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Keys");
                builder.getImportRegistrationResolver().addImport(GAE_DATASTORE_KEY_FACTORY);
                builder.addField(getMultipleEntityIdField(hiddenIdFieldName));
            } else {
                hiddenIdFieldName = governorTypeDetails.getUniqueFieldName(field.getFieldName().getSymbolName() + "Id");
                builder.addField(getSingularEntityIdField(hiddenIdFieldName));
            }
            processGaeAnnotations(field);
            InvocableMemberBodyBuilder gaeAccessorBody = getGaeAccessorBody(field, hiddenIdFieldName);
            accessorMethod.setBodyBuilder(gaeAccessorBody);
            InvocableMemberBodyBuilder gaeMutatorBody = getGaeMutatorBody(field, hiddenIdFieldName);
            mutatorMethod.setBodyBuilder(gaeMutatorBody);
        }
        // Add to mutators and accesors list and build
        if (accessorMethod != null) {
            this.accesorMethods.put(field.getFieldName(), accessorMethod.build());
            builder.addMethod(accessorMethod);
        }
        if (mutatorMethod != null) {
            this.mutatorMethods.put(field.getFieldName(), mutatorMethod.build());
            builder.addMethod(mutatorMethod);
        }
    }
    // Implements interface methods if exists
    implementsInterfaceMethods(interfaceMethods);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 25 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JavaBeanMetadata method getDeclaredSetter.

/**
 * Obtains the specific mutator method that is either contained within the
 * normal Java compilation unit or will be introduced by this add-on via an
 * ITD.
 *
 * @param field
 *            that already exists on the type either directly or via
 *            introduction (required; must be declared by this type to be
 *            located)
 * @return the method corresponding to a mutator, or null if not found
 */
private MethodMetadataBuilder getDeclaredSetter(final FieldMetadata field) {
    Validate.notNull(field, "Field required");
    // Compute the mutator method name
    final JavaSymbolName methodName = BeanInfoUtils.getMutatorMethodName(field);
    // Compute the mutator method parameters
    final JavaType parameterType = field.getFieldType();
    // See if the type itself declared the mutator
    if (governorHasMethod(methodName, parameterType)) {
        return null;
    }
    // Compute the mutator method parameter names
    final List<JavaSymbolName> parameterNames = Arrays.asList(field.getFieldName());
    // final fields as per ROO-36)
    if (annotationValues.isSettersByDefault() && !Modifier.isTransient(field.getModifier()) && !Modifier.isStatic(field.getModifier()) && !Modifier.isFinal(field.getModifier())) {
        final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
        bodyBuilder.appendFormalLine("this." + field.getFieldName().getSymbolName() + " = " + field.getFieldName().getSymbolName() + ";");
        return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterType), parameterNames, bodyBuilder);
    }
    return null;
}
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) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25