Search in sources :

Example 31 with JavaSymbolName

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

the class EqualsOperationsImpl method addEqualsAndHashCodeMethods.

public void addEqualsAndHashCodeMethods(final JavaType javaType, final boolean appendSuper, final Set<String> excludeFields) {
    // Add @RooEquals annotation to class if not yet present
    final ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(javaType);
    if (cid == null || cid.getTypeAnnotation(ROO_EQUALS) != null) {
        return;
    }
    final AnnotationMetadataBuilder annotationBuilder = new AnnotationMetadataBuilder(ROO_EQUALS);
    if (appendSuper) {
        annotationBuilder.addBooleanAttribute("appendSuper", appendSuper);
    }
    if (!CollectionUtils.isEmpty(excludeFields)) {
        final List<StringAttributeValue> attributes = new ArrayList<StringAttributeValue>();
        for (final String excludeField : excludeFields) {
            attributes.add(new StringAttributeValue(new JavaSymbolName("value"), excludeField));
        }
        annotationBuilder.addAttribute(new ArrayAttributeValue<StringAttributeValue>(new JavaSymbolName("excludeFields"), attributes));
    }
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(cid);
    cidBuilder.addAnnotation(annotationBuilder.build());
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 32 with JavaSymbolName

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

the class JpaEntityMetadata method getIterableToRemoveCantBeNullConstant.

/**
 * This method obtains the constant that contains the message
 * "The given Iterable of items to remove can't be null!".
 *
 * @return name of the generated constant
 */
private FieldMetadata getIterableToRemoveCantBeNullConstant() {
    // Check if iterableCantBeNullConstant already exists
    if (iterableToRemoveCantBeNullConstant != null) {
        return iterableToRemoveCantBeNullConstant;
    }
    // If not exists, generate a new one and include into builder
    FieldMetadataBuilder constant = new FieldMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName("ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE"), JavaType.STRING, "\"The given Iterable of items to add can't be null!\"");
    iterableToRemoveCantBeNullConstant = constant.build();
    return iterableToRemoveCantBeNullConstant;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 33 with JavaSymbolName

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

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

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

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