Search in sources :

Example 46 with ClassOrInterfaceTypeDetails

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

the class UpdateCompilationUnitTest method testSimpleClassAddField.

@Test
public void testSimpleClassAddField() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(SIMPLE_CLASS_DECLARED_BY_MID, Modifier.PRIVATE, new JavaSymbolName("newFieldAddedByCode"), new JavaType(String.class), "\"Create by code\"");
    final ClassOrInterfaceTypeDetails newSimpleInterfaceDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
    // Invoke
    final String result = typeParsingService.getCompilationUnitContents(newSimpleInterfaceDetails);
    // save to file for debug
    saveResult(file, result, "-addedField");
    checkSimpleClass(result);
    assertTrue(result.contains("private String newFieldAddedByCode = \"Create by code\";"));
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 47 with ClassOrInterfaceTypeDetails

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

the class UpdateCompilationUnitTest method testRegresion_ROO_1505.

@Test
public void testRegresion_ROO_1505() throws Exception {
    // Set up
    final File file = getResource(ROO1505_CLASS_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, ROO1505_CLASS_DECLARED_BY_MID, ROO1505_CLASS_TYPE);
    // Invoke
    final String result = typeParsingService.getCompilationUnitContents(simpleInterfaceDetails);
    // save to file for debug
    saveResult(file, result);
    check_ROO_1505_Class(result);
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) Test(org.junit.Test)

Example 48 with ClassOrInterfaceTypeDetails

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

the class UpdateCompilationUnitTest method testSimpleClass3NoChanges.

@Test
public void testSimpleClass3NoChanges() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS3_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    // Invoke
    final String result = typeParsingService.getCompilationUnitContents(simpleInterfaceDetails);
    // Save to file for debug
    saveResult(file, result);
    checkSimple3Class(result);
}
Also used : ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) Test(org.junit.Test)

Example 49 with ClassOrInterfaceTypeDetails

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

the class UpdateCompilationUnitTest method testSimpleClass3AddField.

@Test
public void testSimpleClass3AddField() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS3_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    final SetField fieldDetails = new SetField(SIMPLE_CLASS3_DECLARED_BY_MID, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(SIMPLE_CLASS3_TYPE)), new JavaSymbolName("children"), SIMPLE_CLASS3_TYPE, Cardinality.ONE_TO_MANY, new Cascade[] { Cascade.REMOVE }, false);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails.getFieldName(), fieldDetails.getFieldType());
    fieldBuilder.setFieldInitializer("new HashSet<SimpleClass3>()");
    final ClassOrInterfaceTypeDetails newClassDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
    // Invoke
    final String result = typeParsingService.getCompilationUnitContents(newClassDetails);
    saveResult(file, result, "-addField");
    checkSimple3Class(result);
    assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
    // Add another
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    final ReferenceField fieldDetails2 = new ReferenceField(SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS2_TYPE, new JavaSymbolName("referenceField"), Cardinality.MANY_TO_ONE, new Cascade[] { Cascade.REFRESH });
    final FieldMetadataBuilder fieldBuilder2 = new FieldMetadataBuilder(fieldDetails2.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails2.getFieldName(), fieldDetails2.getFieldType());
    final ClassOrInterfaceTypeDetails newClassDetails2 = addField(simpleInterfaceDetails2, fieldBuilder2.build());
    // Invoke
    final String result2 = typeParsingService.getCompilationUnitContents(newClassDetails2);
    // Save to file for debug
    saveResult(file, result2, "-addField2");
    checkSimple3Class(result2);
    assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
    assertTrue(result2.contains("private SimpleClass2 referenceField;"));
}
Also used : JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) File(java.io.File) ReferenceField(org.springframework.roo.classpath.operations.jsr303.ReferenceField) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 50 with ClassOrInterfaceTypeDetails

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

the class DefaultPhysicalTypeMetadataProvider method get.

public MetadataItem get(final String metadataIdentificationString) {
    if (fileManager == null) {
        fileManager = getFileManager();
    }
    Validate.notNull(fileManager, "FileManager is required");
    if (metadataDependencyRegistry == null) {
        metadataDependencyRegistry = getMetadataDependencyRegistry();
    }
    Validate.notNull(metadataDependencyRegistry, "MetadataDependencyRegistry is required");
    if (projectOperations == null) {
        projectOperations = getProjectOperations();
    }
    Validate.notNull(projectOperations, "ProjectOperations is required");
    if (typeLocationService == null) {
        typeLocationService = getTypeLocationService();
    }
    Validate.notNull(typeLocationService, "TypeLocationService is required");
    if (typeParsingService == null) {
        typeParsingService = getTypeParsingService();
    }
    Validate.notNull(typeParsingService, "TypeParsingService is required");
    Validate.isTrue(PhysicalTypeIdentifier.isValid(metadataIdentificationString), "Metadata id '%s' is not valid for this metadata provider", metadataIdentificationString);
    final String canonicalPath = typeLocationService.getPhysicalTypeCanonicalPath(metadataIdentificationString);
    if (StringUtils.isBlank(canonicalPath)) {
        return null;
    }
    metadataDependencyRegistry.deregisterDependencies(metadataIdentificationString);
    if (!fileManager.exists(canonicalPath)) {
        // that was found but could not be parsed
        return null;
    }
    final JavaType javaType = PhysicalTypeIdentifier.getJavaType(metadataIdentificationString);
    final ClassOrInterfaceTypeDetails typeDetails = typeParsingService.getTypeAtLocation(canonicalPath, metadataIdentificationString, javaType);
    if (typeDetails == null) {
        return null;
    }
    final PhysicalTypeMetadata result = new DefaultPhysicalTypeMetadata(metadataIdentificationString, canonicalPath, typeDetails);
    final ClassOrInterfaceTypeDetails details = result.getMemberHoldingTypeDetails();
    if (details != null && details.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS && details.getExtendsTypes().size() == 1) {
        // This is a class, and it extends another class
        if (details.getSuperclass() != null) {
            // We have a dependency on the superclass, and there is metadata
            // available for the superclass
            // We won't implement the full MetadataNotificationListener
            // here, but rely on MetadataService's fallback
            // (which is to evict from cache and call get again given
            // JavaParserMetadataProvider doesn't implement
            // MetadataNotificationListener, then notify everyone we've
            // changed)
            final String superclassId = details.getSuperclass().getDeclaredByMetadataId();
            metadataDependencyRegistry.registerDependency(superclassId, result.getId());
        } else {
            // type change, in the hope we discover our parent someday
            for (final LogicalPath sourcePath : projectOperations.getPathResolver().getSourcePaths()) {
                final String possibleSuperclass = PhysicalTypeIdentifier.createIdentifier(details.getExtendsTypes().get(0), sourcePath);
                metadataDependencyRegistry.registerDependency(possibleSuperclass, result.getId());
            }
        }
    }
    MemberDetails memberDetails = new MemberDetailsBuilder(Arrays.asList(details)).build();
    // Loop until such time as we complete a full loop where no changes are
    // made to the result
    boolean additionalLoopRequired = true;
    while (additionalLoopRequired) {
        additionalLoopRequired = false;
        for (final MemberDetailsDecorator decorator : decorators) {
            final MemberDetails newResult = decorator.decorateTypes(DefaultPhysicalTypeMetadataProvider.class.getName(), memberDetails);
            Validate.isTrue(newResult != null, "Decorator '%s' returned an illegal result", decorator.getClass().getName());
            if (!newResult.equals(memberDetails)) {
                additionalLoopRequired = true;
                memberDetails = newResult;
            }
        }
    }
    return new DefaultPhysicalTypeMetadata(metadataIdentificationString, canonicalPath, (ClassOrInterfaceTypeDetails) memberDetails.getDetails().get(0));
}
Also used : JavaType(org.springframework.roo.model.JavaType) DefaultPhysicalTypeMetadata(org.springframework.roo.classpath.details.DefaultPhysicalTypeMetadata) DefaultPhysicalTypeMetadata(org.springframework.roo.classpath.details.DefaultPhysicalTypeMetadata) LogicalPath(org.springframework.roo.project.LogicalPath) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) MemberDetailsBuilder(org.springframework.roo.classpath.scanner.MemberDetailsBuilder) MemberDetailsDecorator(org.springframework.roo.classpath.scanner.MemberDetailsDecorator)

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