Search in sources :

Example 1 with MemberDetailsDecorator

use of org.springframework.roo.classpath.scanner.MemberDetailsDecorator 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)1 DefaultPhysicalTypeMetadata (org.springframework.roo.classpath.details.DefaultPhysicalTypeMetadata)1 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)1 MemberDetailsBuilder (org.springframework.roo.classpath.scanner.MemberDetailsBuilder)1 MemberDetailsDecorator (org.springframework.roo.classpath.scanner.MemberDetailsDecorator)1 JavaType (org.springframework.roo.model.JavaType)1 LogicalPath (org.springframework.roo.project.LogicalPath)1