Search in sources :

Example 21 with JavaType

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

the class JpaFieldCreatorProvider method createCollectionEnumeration.

/**
 * Create a field for a List or Set of a enumeration
 *
 * @param parentCid
 * @param fieldType
 * @param fieldName
 * @param permitReservedWords
 * @param sizeMin
 * @param sizeMax
 * @param comment
 * @param notNull
 * @param isList
 */
private void createCollectionEnumeration(final ClassOrInterfaceTypeDetails parentCid, JavaType fieldType, JavaSymbolName fieldName, boolean permitReservedWords, Integer sizeMin, Integer sizeMax, String comment, boolean notNull, boolean isList) {
    SetField parentFieldDetails;
    if (isList) {
        parentFieldDetails = new SetField(parentCid.getDeclaredByMetadataId(), new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, null, null, false);
    } else {
        parentFieldDetails = new ListField(parentCid.getDeclaredByMetadataId(), new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(fieldType)), fieldName, fieldType, null, null, false);
    }
    parentFieldDetails.setNotNull(notNull);
    if (comment != null) {
        parentFieldDetails.setComment(comment);
    }
    if (sizeMin != null) {
        parentFieldDetails.setSizeMin(sizeMin);
    }
    if (sizeMax != null) {
        parentFieldDetails.setSizeMax(sizeMax);
    }
    // Handle enumeration Set
    insertField(parentFieldDetails, permitReservedWords, false, true);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ListField(org.springframework.roo.classpath.operations.jsr303.ListField) SetField(org.springframework.roo.classpath.operations.jsr303.SetField)

Example 22 with JavaType

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

the class JpaFieldCreatorProvider method createChildEntityFactory.

/**
 * Creates the Factory class of the child entity of relationship, only if parent entity has JPA unit tests.
 *
 * @param childType
 * @param parentType
 */
private void createChildEntityFactory(JavaType childType, JavaType parentType) {
    // Find current JPA unit tests
    Set<JavaType> unitTestTypes = typeLocationService.findTypesWithAnnotation(RooJavaType.ROO_JPA_UNIT_TEST);
    for (JavaType unitTestType : unitTestTypes) {
        // Get the annotation @RooJpaUnitTest
        ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(unitTestType);
        AnnotationMetadata rooUnitTestAnnotation = cid.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST);
        // Check if parent entity has JPA unit test class
        AnnotationAttributeValue<Object> targetClass = rooUnitTestAnnotation.getAttribute("targetClass");
        Validate.notNull(targetClass, String.format("'targetClass' attribute can't be found for annotation @RooJpaUnitTest in class %s", unitTestType.getSimpleTypeName()));
        if (parentType.equals(targetClass.getValue())) {
            if (jpaEntityFactoryLocator.getFirstJpaEntityFactoryForEntity(childType) == null) {
                // Create factory class for child entity if doesn't exist
                dataOnDemandCreatorProvider.createEntityFactory(childType);
            }
            break;
        }
    }
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 23 with JavaType

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

the class IdentifierMetadata method getParameterizedConstructor.

/**
 * Locates the parameterised constructor consisting of the id fields for
 * this class.
 *
 * @param fields
 * @return the constructor, never null.
 */
private ConstructorMetadataBuilder getParameterizedConstructor(final List<FieldMetadataBuilder> fields) {
    // Search for an existing constructor
    final List<JavaType> parameterTypes = new ArrayList<JavaType>();
    for (final FieldMetadataBuilder field : fields) {
        parameterTypes.add(field.getFieldType());
    }
    final ConstructorMetadata result = governorTypeDetails.getDeclaredConstructor(parameterTypes);
    if (result != null) {
        // Found an existing parameterised constructor on this class
        publicNoArgConstructor = true;
        return null;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("super();");
    for (final FieldMetadataBuilder field : fields) {
        final String fieldName = field.getFieldName().getSymbolName();
        bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
        parameterNames.add(field.getFieldName());
    }
    // Create the constructor
    final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
    constructorBuilder.setParameterNames(parameterNames);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    return constructorBuilder;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 24 with JavaType

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

the class IdentifierMetadata method getMutators.

/**
 * Locates the mutator methods.
 * <p>
 * If {@link #getFieldBuilders()} returns fields created by this ITD, public
 * mutators will automatically be produced in the declaring class.
 *
 * @param fields
 * @return the mutators (never returns null)
 */
private List<MethodMetadataBuilder> getMutators(final List<FieldMetadataBuilder> fields) {
    final List<MethodMetadataBuilder> mutators = new ArrayList<MethodMetadataBuilder>();
    // Compute the names of the mutators that will be produced
    for (final FieldMetadataBuilder field : fields) {
        final JavaSymbolName requiredMutatorName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
        final JavaType parameterType = field.getFieldType();
        final MethodMetadata mutator = getGovernorMethod(requiredMutatorName, parameterType);
        if (mutator == null) {
            mutators.add(getMutatorMethod(field.getFieldName(), field.getFieldType()));
        } else {
            Validate.isTrue(Modifier.isPublic(mutator.getModifier()), "User provided field but failed to provide a public '%s(%s)' method in '%s'", requiredMutatorName.getSymbolName(), field.getFieldName().getSymbolName(), destination.getFullyQualifiedTypeName());
            mutators.add(new MethodMetadataBuilder(mutator));
        }
    }
    return mutators;
}
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) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 25 with JavaType

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

the class JpaTestCreator method createUnitTest.

@Override
public void createUnitTest(final JavaType entity) {
    Validate.notNull(entity, "Class to produce an unit test class for is required");
    // Check if provided JavaType is a Repository
    ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(entity);
    Validate.notNull(cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY), "Type must be a Roo JPA Entity type.");
    // Create JPA DataOnDemand artifacts
    List<DataOnDemandCreatorProvider> dodCreators = getValidDataOnDemandCreatorsForType(entity);
    for (DataOnDemandCreatorProvider dodCreator : dodCreators) {
        dodCreator.createDataOnDemand(entity);
    }
    final JavaType name = new JavaType(entity + "Test", entity.getModule());
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, Path.SRC_TEST_JAVA.getModulePathId(entity.getModule()));
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return;
    }
    // Add @RooUnitTest to source file
    AnnotationMetadataBuilder rooUnitTestAnnotation = new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_UNIT_TEST);
    rooUnitTestAnnotation.addClassAttribute("targetClass", entity);
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.addAnnotation(rooUnitTestAnnotation);
    typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) DataOnDemandCreatorProvider(org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

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