Search in sources :

Example 31 with JavaType

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

the class JpaDataOnDemandCreator method getEntityFactory.

@Override
public JavaType getEntityFactory(JavaType entity) {
    Set<ClassOrInterfaceTypeDetails> dataOnDemandCids = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY);
    JavaType typeToReturn = null;
    for (ClassOrInterfaceTypeDetails cid : dataOnDemandCids) {
        if (entity.equals((JavaType) cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY).getAttribute("entity").getValue())) {
            typeToReturn = cid.getName();
            break;
        }
    }
    return typeToReturn;
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 32 with JavaType

use of org.springframework.roo.model.JavaType 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 33 with JavaType

use of org.springframework.roo.model.JavaType 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 34 with JavaType

use of org.springframework.roo.model.JavaType 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)

Example 35 with JavaType

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

the class JavaBeanMetadata method getInterfaceMethod.

/**
 * Obtains a valid MethodMetadataBuilder with necessary configuration
 *
 * @param method
 * @return MethodMetadataBuilder
 */
private MethodMetadataBuilder getInterfaceMethod(final MethodMetadata method) {
    // Compute the method name
    final JavaSymbolName methodName = method.getMethodName();
    // See if the type itself declared the accessor
    if (governorHasMethod(methodName)) {
        return null;
    }
    // Getting return type
    JavaType returnType = method.getReturnType();
    // Generating body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // If return type is not primitive, return null
    if (returnType.isPrimitive()) {
        JavaType baseType = returnType.getBaseType();
        if (baseType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return false;");
        } else if (baseType.equals(JavaType.BYTE_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.SHORT_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.INT_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.LONG_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.FLOAT_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0;");
        } else if (baseType.equals(JavaType.DOUBLE_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return 0.00;");
        } else if (baseType.equals(JavaType.CHAR_PRIMITIVE)) {
            bodyBuilder.appendFormalLine("return '\0';");
        }
    } else {
        bodyBuilder.appendFormalLine("return null;");
    }
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, bodyBuilder);
}
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

JavaType (org.springframework.roo.model.JavaType)411 RooJavaType (org.springframework.roo.model.RooJavaType)212 ArrayList (java.util.ArrayList)142 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)133 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)129 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)114 JdkJavaType (org.springframework.roo.model.JdkJavaType)110 SpringJavaType (org.springframework.roo.model.SpringJavaType)101 JpaJavaType (org.springframework.roo.model.JpaJavaType)83 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)78 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)76 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)72 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)70 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)65 LogicalPath (org.springframework.roo.project.LogicalPath)62 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)60 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)59 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)38 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)35 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)30