Search in sources :

Example 21 with ClassOrInterfaceTypeDetails

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

the class JpaDataOnDemandCreator method getEntityDetails.

/**
 * Returns the {@link ClassOrInterfaceTypeDetails} for the provided entity.
 *
 * @param entity
 *            the entity to lookup required
 * @return the ClassOrInterfaceTypeDetails type details (never null; throws
 *         an exception if it cannot be obtained or parsed)
 */
private ClassOrInterfaceTypeDetails getEntityDetails(final JavaType entity) {
    final ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(entity);
    Validate.notNull(cid, "Java source code details unavailable for type '%s'", entity);
    return cid;
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 22 with ClassOrInterfaceTypeDetails

use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails 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 23 with ClassOrInterfaceTypeDetails

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

the class JpaDataOnDemandCreator method newDataOnDemandClass.

/**
 * Creates a new data-on-demand provider for an entity. Silently returns
 * if the DoD class already exists.
 *
 * @param entity to produce a DoD provider for
 * @param name the name of the new DoD class
 */
private JavaType newDataOnDemandClass(JavaType entity, JavaType name) {
    Validate.notNull(entity, "Entity to produce a data on demand provider for is required");
    Validate.notNull(name, "Name of the new data on demand provider is required");
    final LogicalPath path = LogicalPath.getInstance(Path.SRC_TEST_JAVA, name.getModule());
    Validate.notNull(path, "Location of the new data on demand provider is required");
    // Add javax validation dependency
    projectOperations.addDependency(name.getModule(), VALIDATION_API_DEPENDENCY);
    // Verify the requested entity actually exists as a class and is not
    // abstract
    final ClassOrInterfaceTypeDetails cid = getEntityDetails(entity);
    Validate.isTrue(cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS, "Type %s is not a class", entity.getFullyQualifiedTypeName());
    Validate.isTrue(!Modifier.isAbstract(cid.getModifier()), "Type %s is abstract", entity.getFullyQualifiedTypeName());
    // Check if the requested entity is a JPA @Entity
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(JpaDataOnDemandCreator.class.getName(), cid);
    final AnnotationMetadata entityAnnotation = memberDetails.getAnnotation(ENTITY);
    Validate.isTrue(entityAnnotation != null, "Type %s must be a JPA entity type", entity.getFullyQualifiedTypeName());
    // Everything is OK to proceed
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, path);
    if (metadataService.get(declaredByMetadataId) != null) {
        // The file already exists
        return new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId).getName();
    }
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    final List<AnnotationAttributeValue<?>> dodConfig = new ArrayList<AnnotationAttributeValue<?>>();
    dodConfig.add(new ClassAttributeValue(new JavaSymbolName("entity"), entity));
    annotations.add(new AnnotationMetadataBuilder(RooJavaType.ROO_JPA_DATA_ON_DEMAND, dodConfig));
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.CLASS);
    cidBuilder.setAnnotations(annotations);
    // Write changes on disk
    final ClassOrInterfaceTypeDetails dodClassCid = cidBuilder.build();
    typeManagementService.createOrUpdateTypeOnDisk(dodClassCid);
    return cid.getName();
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) LogicalPath(org.springframework.roo.project.LogicalPath) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 24 with ClassOrInterfaceTypeDetails

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

the class JavaBeanMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    final JavaBeanAnnotationValues annotationValues = new JavaBeanAnnotationValues(governorPhysicalTypeMetadata);
    if (!annotationValues.isAnnotationFound()) {
        return null;
    }
    ClassOrInterfaceTypeDetails currentClassDetails = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    final Map<FieldMetadata, JavaSymbolName> declaredFields = new LinkedHashMap<FieldMetadata, JavaSymbolName>();
    final List<FieldMetadata> declaredFieldsList = new ArrayList<FieldMetadata>();
    for (final FieldMetadata field : currentClassDetails.getDeclaredFields()) {
        declaredFields.put(field, getIdentifierAccessorMethodName(field, metadataIdentificationString));
        declaredFieldsList.add(field);
    }
    // In order to handle switching between GAE and JPA produced MIDs need
    // to be remembered so they can be regenerated on JPA <-> GAE switch
    producedMids.add(metadataIdentificationString);
    // Getting implements
    List<JavaType> interfaces = currentClassDetails.getImplementsTypes();
    List<? extends MethodMetadata> interfaceMethods = null;
    if (!interfaces.isEmpty()) {
        for (JavaType currentInterface : interfaces) {
            ClassOrInterfaceTypeDetails currentInterfaceDetails = getTypeLocationService().getTypeDetails(currentInterface);
            if (currentInterfaceDetails != null) {
                interfaceMethods = currentInterfaceDetails.getDeclaredMethods();
            }
        }
    }
    final JavaType target = governorPhysicalTypeMetadata.getType();
    // Prepare Equals
    final EqualsAnnotationValues equalsAnnotationValues = new EqualsAnnotationValues(governorPhysicalTypeMetadata);
    List<FieldMetadata> equalsFields = null;
    FieldMetadata identifier = null;
    if (equalsAnnotationValues.isAnnotationFound()) {
        EqualsMetadataProvider equalsProvider = getEqualsMetadataProvider();
        equalsFields = equalsProvider.locateFields(target, equalsAnnotationValues.getExcludeFields(), declaredFieldsList, metadataIdentificationString);
        identifier = equalsProvider.getIdentifier(governorPhysicalTypeMetadata);
    }
    // Prepare ToString
    final ToStringAnnotationValues toStringAnnotationValues = new ToStringAnnotationValues(governorPhysicalTypeMetadata);
    List<FieldMetadata> toStringFields = null;
    if (toStringAnnotationValues.isAnnotationFound()) {
        ToStringMetadataProvider toStringProvider = getToStringMetadataProvider();
        toStringFields = toStringProvider.getToStringFields(governorPhysicalTypeMetadata, declaredFieldsList);
    }
    return new JavaBeanMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, declaredFields, interfaceMethods, getMemberDetailsScanner(), equalsAnnotationValues, equalsFields, identifier, toStringAnnotationValues, toStringFields);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 25 with ClassOrInterfaceTypeDetails

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

the class JpaCommands method getIdentifierTypePossibleValues.

@CliOptionAutocompleteIndicator(command = "entity jpa", param = "identifierType", help = "--identifierType option should be a wrapper of a primitive type or an embeddable class.")
public List<String> getIdentifierTypePossibleValues(ShellContext shellContext) {
    String currentText = shellContext.getParameters().get("identifierType");
    List<String> allPossibleValues = new ArrayList<String>();
    // Add java-lang and java-number classes
    allPossibleValues.add(Number.class.getName());
    allPossibleValues.add(Short.class.getName());
    allPossibleValues.add(Byte.class.getName());
    allPossibleValues.add(Integer.class.getName());
    allPossibleValues.add(Long.class.getName());
    allPossibleValues.add(Float.class.getName());
    allPossibleValues.add(Double.class.getName());
    allPossibleValues.add(BigDecimal.class.getName());
    allPossibleValues.add(BigInteger.class.getName());
    // Getting all existing embeddable classes
    Set<ClassOrInterfaceTypeDetails> embeddableClassesInProject = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(JpaJavaType.EMBEDDABLE);
    for (ClassOrInterfaceTypeDetails embeddableClass : embeddableClassesInProject) {
        String name = replaceTopLevelPackageString(embeddableClass, currentText);
        if (!allPossibleValues.contains(name)) {
            allPossibleValues.add(name);
        }
    }
    return allPossibleValues;
}
Also used : ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Aggregations

ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)232 JavaType (org.springframework.roo.model.JavaType)114 ArrayList (java.util.ArrayList)87 RooJavaType (org.springframework.roo.model.RooJavaType)86 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)52 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)43 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)42 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)39 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)36 JpaJavaType (org.springframework.roo.model.JpaJavaType)34 SpringJavaType (org.springframework.roo.model.SpringJavaType)28 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)24 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)23 JdkJavaType (org.springframework.roo.model.JdkJavaType)22 Test (org.junit.Test)20 File (java.io.File)19 List (java.util.List)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)17