Search in sources :

Example 46 with JavaType

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

the class ToStringMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.

@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
    final JavaType javaType = ToStringMetadata.getJavaType(metadataIdentificationString);
    final LogicalPath path = ToStringMetadata.getPath(metadataIdentificationString);
    return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
Also used : JavaType(org.springframework.roo.model.JavaType) RooJavaType(org.springframework.roo.model.RooJavaType) LogicalPath(org.springframework.roo.project.LogicalPath)

Example 47 with JavaType

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

the class JpaEntityMetadata method getAddValueMethod.

/**
 * Create add method to handle referenced relation
 *
 * @param addMethodName
 * @param field
 * @param cardinality
 * @param childType
 * @param mappedBy
 * @param removeMethodName
 * @param importResolver
 * @return method metadata or null if method already in class
 */
private MethodMetadata getAddValueMethod(final JavaSymbolName addMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, final JavaSymbolName removeMethodName, ImportRegistrationResolver importResolver) {
    // Identify parameters type and name
    final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
    if (cardinality == Cardinality.ONE_TO_ONE) {
        parameterTypes.add(childType);
        parameterNames.add(field.getFieldName());
    } else {
        parameterTypes.add(JavaType.iterableOf(childType));
        parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + ADD_PARAMETER_SUFFIX));
    }
    // See if the type itself declared the method
    MethodMetadata existingMethod = getGovernorMethod(addMethodName, parameterTypes);
    if (existingMethod != null) {
        return existingMethod;
    }
    final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
    if (cardinality == Cardinality.ONE_TO_ONE) {
        buildAddOneToOneBody(field, mappedBy, parameterNames.get(0), childType, removeMethodName, builder);
    } else if (cardinality == Cardinality.ONE_TO_MANY) {
        buildAddOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
    } else {
        buildAddManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
    }
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, addMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) 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)

Example 48 with JavaType

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

the class JpaEntityMetadata method getRemoveMethod.

/**
 * Create remove method to handle referenced relation
 *
 * @param removeMethodName
 * @param field
 * @param cardinality
 * @param childType
 * @param mappedBy
 * @param importResolver
 * @return method metadata or null if method already in class
 */
private MethodMetadata getRemoveMethod(final JavaSymbolName removeMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, ImportRegistrationResolver importResolver) {
    // Identify parameters types and names (if any)
    final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
    if (cardinality != Cardinality.ONE_TO_ONE) {
        parameterTypes.add(JavaType.iterableOf(childType));
        parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + REMOVE_PARAMETER_SUFFIX));
    }
    // See if the type itself declared the method
    MethodMetadata existingMethod = getGovernorMethod(removeMethodName, parameterTypes);
    if (existingMethod != null) {
        return existingMethod;
    }
    final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
    if (cardinality == Cardinality.ONE_TO_ONE) {
        buildRemoveOneToOneBody(field, mappedBy, builder);
    } else if (cardinality == Cardinality.ONE_TO_MANY) {
        buildRemoveOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
    } else {
        // ManyToMany
        buildRemoveManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
    }
    return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, removeMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) 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)

Example 49 with JavaType

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

the class JpaEntityMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.

@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
    final JavaType javaType = getType(metadataIdentificationString);
    final LogicalPath path = PhysicalTypeIdentifierNamingUtils.getPath(PROVIDES_TYPE_STRING, metadataIdentificationString);
    return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) LogicalPath(org.springframework.roo.project.LogicalPath)

Example 50 with JavaType

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

the class JavaParserUtils method getJavaType.

/**
 * Resolves the effective {@link JavaType} a {@link NameExpr} represents.
 * <p>
 * You should use {@link #getJavaType(CompilationUnitServices, Type, Set)}
 * where possible so that type arguments are preserved (a {@link NameExpr}
 * does not contain type arguments).
 * <p>
 * A name expression can be either qualified or unqualified.
 * <p>
 * If a name expression is qualified and the qualification starts with a
 * lowercase letter, that represents the fully-qualified name. If the
 * qualification starts with an uppercase letter, the package name is
 * prepended to the qualifier.
 * <p>
 * If a name expression is unqualified, the imports are scanned. If the
 * unqualified name expression is found in the imports, that import
 * declaration represents the fully-qualified name. If the unqualified name
 * expression is not found in the imports, it indicates the name to find is
 * either in the same package as the qualified name expression, or the type
 * relates to a member of java.lang. If part of java.lang, the fully
 * qualified name is treated as part of java.lang. Otherwise the compilation
 * unit package plus unqualified name expression represents the fully
 * qualified name expression.
 *
 * @param compilationUnitServices for package management (required)
 * @param nameToFind to locate (required)
 * @param typeParameters names to consider type parameters (can be null if
 *            there are none)
 * @return the effective Java type (never null)
 */
public static JavaType getJavaType(final CompilationUnitServices compilationUnitServices, final NameExpr nameToFind, final Set<JavaSymbolName> typeParameters) {
    Validate.notNull(compilationUnitServices, "Compilation unit services required");
    Validate.notNull(nameToFind, "Name to find is required");
    final JavaPackage compilationUnitPackage = compilationUnitServices.getCompilationUnitPackage();
    if (nameToFind instanceof QualifiedNameExpr) {
        final QualifiedNameExpr qne = (QualifiedNameExpr) nameToFind;
        // Handle qualified name expressions that are related to inner types
        // (eg Foo.Bar)
        final NameExpr qneQualifier = qne.getQualifier();
        final NameExpr enclosedBy = getNameExpr(compilationUnitServices.getEnclosingTypeName().getSimpleTypeName());
        if (isEqual(qneQualifier, enclosedBy)) {
            // This qualified name expression is simply an inner type
            // reference
            final String name = compilationUnitServices.getEnclosingTypeName().getFullyQualifiedTypeName() + "." + nameToFind.getName();
            return new JavaType(name, compilationUnitServices.getEnclosingTypeName());
        }
        // package (ROO-1210)
        if (qne.toString().length() > 1 && Character.isUpperCase(qne.toString().charAt(0))) {
            // First letter is uppercase, so this likely requires prepending
            // of some package name
            final ImportDeclaration importDeclaration = getImportDeclarationFor(compilationUnitServices, qne.getQualifier());
            if (importDeclaration == null) {
                if (!compilationUnitPackage.getFullyQualifiedPackageName().equals("")) {
                    // package
                    return new JavaType(compilationUnitServices.getCompilationUnitPackage().getFullyQualifiedPackageName() + "." + qne.toString());
                }
            } else {
                return new JavaType(importDeclaration.getName() + "." + qne.getName());
            }
        // This name expression (which contains a dot) had its qualifier
        // imported, so let's use the import
        } else {
            // a package
            return new JavaType(qne.toString());
        }
    }
    if ("?".equals(nameToFind.getName())) {
        return new JavaType(OBJECT.getFullyQualifiedTypeName(), 0, DataType.TYPE, JavaType.WILDCARD_NEITHER_ARG, null);
    }
    // list
    if (typeParameters != null && typeParameters.contains(new JavaSymbolName(nameToFind.getName()))) {
        return new JavaType(nameToFind.getName(), 0, DataType.VARIABLE, null, null);
    }
    // Check if we are looking for the enclosingType itself
    final NameExpr enclosingTypeName = getNameExpr(compilationUnitServices.getEnclosingTypeName().getSimpleTypeName());
    if (isEqual(enclosingTypeName, nameToFind)) {
        return compilationUnitServices.getEnclosingTypeName();
    }
    // check if the compilation unit itself declares that type
    for (final TypeDeclaration internalType : compilationUnitServices.getInnerTypes()) {
        final NameExpr nameExpr = getNameExpr(internalType.getName());
        if (isEqual(nameExpr, nameToFind)) {
            // Found, so now we need to convert the internalType to a proper
            // JavaType
            final String name = compilationUnitServices.getEnclosingTypeName().getFullyQualifiedTypeName() + "." + nameToFind.getName();
            return new JavaType(name);
        }
    }
    final ImportDeclaration importDeclaration = getImportDeclarationFor(compilationUnitServices, nameToFind);
    if (importDeclaration == null) {
        if (JdkJavaType.isPartOfJavaLang(nameToFind.getName())) {
            return new JavaType("java.lang." + nameToFind.getName());
        }
        final String name = compilationUnitPackage.getFullyQualifiedPackageName().equals("") ? nameToFind.getName() : compilationUnitPackage.getFullyQualifiedPackageName() + "." + nameToFind.getName();
        return new JavaType(name);
    }
    return new JavaType(importDeclaration.getName().toString());
}
Also used : QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) ImportDeclaration(com.github.antlrjavaparser.api.ImportDeclaration) TypeDeclaration(com.github.antlrjavaparser.api.body.TypeDeclaration) JavaPackage(org.springframework.roo.model.JavaPackage)

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