Search in sources :

Example 86 with AnnotationMetadata

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

the class DbreDatabaseListenerImpl method hasStandardEntityAnnotations.

/**
 * Indicates whether the given entity has the standard annotations applied
 * by Roo, and no others.
 *
 * @param entity the entity to check (required)
 * @return <code>false</code> if any of the standard ones are missing or any
 *         extra ones have been added
 */
private boolean hasStandardEntityAnnotations(final ClassOrInterfaceTypeDetails entity) {
    final List<? extends AnnotationMetadata> typeAnnotations = entity.getAnnotations();
    // RooEntity or RooJpaEntity
    if (typeAnnotations.size() != 4) {
        return false;
    }
    // There are exactly four - check for any non-standard ones
    for (final AnnotationMetadata annotation : typeAnnotations) {
        final JavaType annotationType = annotation.getAnnotationType();
        final boolean entityAnnotation = ROO_JPA_ENTITY.equals(annotationType);
        if (!entityAnnotation && !ROO_DB_MANAGED.equals(annotationType) && !ROO_JAVA_BEAN.equals(annotationType) && !ROO_TO_STRING.equals(annotationType)) {
            return false;
        }
    }
    return true;
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 87 with AnnotationMetadata

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

the class DbreDatabaseListenerImpl method getManagedIdentifiers.

private Set<ClassOrInterfaceTypeDetails> getManagedIdentifiers() {
    final Set<ClassOrInterfaceTypeDetails> managedIdentifierTypes = new LinkedHashSet<ClassOrInterfaceTypeDetails>();
    final Set<ClassOrInterfaceTypeDetails> identifierTypes = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(ROO_IDENTIFIER);
    for (final ClassOrInterfaceTypeDetails managedIdentifierType : identifierTypes) {
        final AnnotationMetadata identifierAnnotation = managedIdentifierType.getTypeAnnotation(ROO_IDENTIFIER);
        final AnnotationAttributeValue<?> attrValue = identifierAnnotation.getAttribute(DB_MANAGED);
        if (attrValue != null && (Boolean) attrValue.getValue()) {
            managedIdentifierTypes.add(managedIdentifierType);
        }
    }
    return managedIdentifierTypes;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 88 with AnnotationMetadata

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

the class DbreMetadata method hasField.

private boolean hasField(final JavaSymbolName fieldName, final List<AnnotationMetadata> annotations) {
    // Check governor for field
    if (governorTypeDetails.getField(fieldName) != null) {
        return true;
    }
    // Check @Column and @JoinColumn annotations on fields in governor with
    // the same 'name' as the generated field
    final List<FieldMetadata> governorFields = governorTypeDetails.getFieldsWithAnnotation(COLUMN);
    governorFields.addAll(governorTypeDetails.getFieldsWithAnnotation(JOIN_COLUMN));
    for (final FieldMetadata governorField : governorFields) {
        governorFieldAnnotations: for (final AnnotationMetadata governorFieldAnnotation : governorField.getAnnotations()) {
            if (governorFieldAnnotation.getAnnotationType().equals(COLUMN) || governorFieldAnnotation.getAnnotationType().equals(JOIN_COLUMN)) {
                final AnnotationAttributeValue<?> name = governorFieldAnnotation.getAttribute(new JavaSymbolName(NAME));
                if (name == null) {
                    continue governorFieldAnnotations;
                }
                for (final AnnotationMetadata annotationMetadata : annotations) {
                    final AnnotationAttributeValue<?> columnName = annotationMetadata.getAttribute(new JavaSymbolName(NAME));
                    if (columnName != null && columnName.equals(name)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 89 with AnnotationMetadata

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

the class DbreTypeUtils method getAnnotationAttribute.

/**
 * Returns the value of the given attribute of the given annotation on the
 * given type
 *
 * @param <T> the expected annotation value type
 * @param type the type whose annotations to read (required)
 * @param annotationType the annotation to read (required)
 * @param attributeName the annotation attribute to read (required)
 * @return
 */
@SuppressWarnings("unchecked")
private static <T> T getAnnotationAttribute(final MemberHoldingTypeDetails type, final JavaType annotationType, final JavaSymbolName attributeName) {
    final AnnotationMetadata typeAnnotation = type.getTypeAnnotation(annotationType);
    if (typeAnnotation == null) {
        return null;
    }
    final AnnotationAttributeValue<?> attributeValue = typeAnnotation.getAttribute(attributeName);
    if (attributeValue == null) {
        return null;
    }
    return (T) attributeValue.getValue();
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 90 with AnnotationMetadata

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

the class FinderOperationsImpl method installFinder.

public void installFinder(final JavaType entity, final JavaSymbolName finderName, JavaType formBean, JavaType returnType) {
    Validate.notNull(entity, "ERROR: Entity type required to generate finder.");
    Validate.notNull(finderName, "ERROR: Finder name required to generate finder.");
    final String id = getTypeLocationService().getPhysicalTypeIdentifier(entity);
    if (id == null) {
        LOGGER.warning("Cannot locate source for '" + entity.getFullyQualifiedTypeName() + "'");
        return;
    }
    // Check if provided entity is annotated with @RooJpaEntity
    ClassOrInterfaceTypeDetails entityDetails = getTypeLocationService().getTypeDetails(entity);
    AnnotationMetadata entityAnnotation = entityDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY);
    Validate.notNull(entityAnnotation, "ERROR: Provided entity must be annotated with @RooJpaEntity");
    // Getting repository that manages current entity
    ClassOrInterfaceTypeDetails repository = getRepositoryJpaLocator().getRepository(entity);
    // Entity must have a repository that manages it, if not, shows an error
    Validate.notNull(repository, "ERROR: You must generate a repository to the provided entity before to add new finder. You could use 'repository jpa' commands.");
    // Check if provided formBean contains the field names and types indicated as finder params
    if (formBean != null) {
        MemberDetails entityMemberDetails = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), getTypeLocationService().getTypeDetails(entity));
        PartTree finderPartTree = new PartTree(finderName.getSymbolName(), entityMemberDetails);
        checkDtoFieldsForFinder(getTypeLocationService().getTypeDetails(formBean), finderPartTree, repository.getType());
    }
    // Get repository annotation
    AnnotationMetadata repositoryAnnotation = repository.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA);
    AnnotationMetadataBuilder repositoryAnnotationBuilder = new AnnotationMetadataBuilder(repositoryAnnotation);
    // Create list that will include finders to add
    List<AnnotationAttributeValue<?>> finders = new ArrayList<AnnotationAttributeValue<?>>();
    // Check if new finder name to be included already exists in @RooRepositoryJpa annotation
    AnnotationAttributeValue<?> currentFinders = repositoryAnnotation.getAttribute("finders");
    if (currentFinders != null) {
        List<?> values = (List<?>) currentFinders.getValue();
        Iterator<?> it = values.iterator();
        while (it.hasNext()) {
            NestedAnnotationAttributeValue finder = (NestedAnnotationAttributeValue) it.next();
            if (finder.getValue() != null && finder.getValue().getAttribute("value") != null) {
                if (finder.getValue().getAttribute("value").getValue().equals(finderName.getSymbolName())) {
                    LOGGER.log(Level.WARNING, String.format("ERROR: Finder '%s' already exists on entity '%s'", finderName.getSymbolName(), entity.getSimpleTypeName()));
                    return;
                }
                finders.add(finder);
            }
        }
    }
    // Create @RooFinder
    AnnotationMetadataBuilder singleFinderAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_FINDER);
    // Add finder attribute
    singleFinderAnnotation.addStringAttribute("value", finderName.getSymbolName());
    // Add returnType attribute
    singleFinderAnnotation.addClassAttribute("returnType", returnType);
    // Prevent errors validating if the return type contains a valid module
    if (returnType.getModule() != null) {
        getProjectOperations().addModuleDependency(repository.getName().getModule(), returnType.getModule());
    }
    // Add formBean attribute
    if (formBean != null) {
        singleFinderAnnotation.addClassAttribute("formBean", formBean);
        getProjectOperations().addModuleDependency(repository.getName().getModule(), formBean.getModule());
    }
    NestedAnnotationAttributeValue newFinder = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), singleFinderAnnotation.build());
    // If not exists current finder, include it
    finders.add(newFinder);
    // Add finder list to currentFinders
    ArrayAttributeValue<AnnotationAttributeValue<?>> newFinders = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("finders"), finders);
    // Include finder name to finders attribute
    repositoryAnnotationBuilder.addAttribute(newFinders);
    // Update @RooRepositoryJpa annotation
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(repository);
    // Update annotation
    cidBuilder.updateTypeAnnotation(repositoryAnnotationBuilder);
    // Save changes on disk
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Aggregations

AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)109 JavaType (org.springframework.roo.model.JavaType)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)52 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)47 ArrayList (java.util.ArrayList)40 RooJavaType (org.springframework.roo.model.RooJavaType)34 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)30 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)24 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)21 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)20 List (java.util.List)19 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)17 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)16 SpringJavaType (org.springframework.roo.model.SpringJavaType)15 JdkJavaType (org.springframework.roo.model.JdkJavaType)12 JpaJavaType (org.springframework.roo.model.JpaJavaType)12 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)11 LinkedHashMap (java.util.LinkedHashMap)10