Search in sources :

Example 6 with PhysicalTypeMetadata

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

the class AbstractItdMetadataProvider method get.

public final MetadataItem get(final String metadataIdentificationString) {
    Validate.isTrue(MetadataIdentificationUtils.getMetadataClass(metadataIdentificationString).equals(MetadataIdentificationUtils.getMetadataClass(getProvidesType())), "Unexpected request for '%s' to this provider (which uses '%s')", metadataIdentificationString, getProvidesType());
    // Remove the upstream dependencies for this instance (we'll be
    // recreating them later, if needed)
    getMetadataDependencyRegistry().deregisterDependencies(metadataIdentificationString);
    // Compute the identifier for the Physical Type Metadata we're
    // correlated with
    final String governorPhysicalTypeIdentifier = getGovernorPhysicalTypeIdentifier(metadataIdentificationString);
    // Obtain the physical type
    final PhysicalTypeMetadata governorPhysicalTypeMetadata = (PhysicalTypeMetadata) getMetadataService().get(governorPhysicalTypeIdentifier);
    if (governorPhysicalTypeMetadata == null || !governorPhysicalTypeMetadata.isValid()) {
        // abort (the ITD will be deleted by ItdFileDeletionService)
        return null;
    }
    // Flag to indicate whether we'll even try to create this metadata
    boolean produceMetadata = false;
    // Determine if we should generate the metadata on the basis of it
    // containing a trigger annotation
    final ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    if (cid != null) {
        // metadata triggers
        for (final JavaType trigger : metadataTriggers) {
            if (cid.getAnnotation(trigger) != null) {
                produceMetadata = true;
                break;
            }
        }
    }
    // Fall back to ignoring trigger annotations
    if (ignoreTriggerAnnotations) {
        produceMetadata = true;
    }
    // aren't available
    if (dependsOnGovernorTypeDetailAvailability && cid == null) {
        produceMetadata = false;
    }
    // only wants to know about classes
    if (cid != null && dependsOnGovernorBeingAClass && cid.getPhysicalTypeCategory() != PhysicalTypeCategory.CLASS) {
        produceMetadata = false;
    }
    final String itdFilename = governorPhysicalTypeMetadata.getItdCanonicalPath(this);
    if (!produceMetadata && isGovernor(cid) && getFileManager().exists(itdFilename)) {
        // We don't seem to want metadata anymore, yet the ITD physically
        // exists, so get rid of it
        // This might be because the trigger annotation has been removed,
        // the governor is missing a class declaration, etc.
        deleteItd(metadataIdentificationString, itdFilename, "not required for governor " + cid.getName(), true);
        return null;
    }
    if (produceMetadata) {
        // This type contains an annotation we were configured to detect, or
        // there is an ITD (which may need deletion), so we need to produce
        // the metadata
        final JavaType aspectName = governorPhysicalTypeMetadata.getItdJavaType(this);
        final ItdTypeDetailsProvidingMetadataItem metadata = getMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, itdFilename);
        if (metadata == null || !metadata.isValid()) {
            // The metadata couldn't be created properly
            deleteItd(metadataIdentificationString, itdFilename, "", false);
            return null;
        }
        // By this point we have a valid MetadataItem, but it might not
        // contain any members for the resulting ITD etc
        // Handle the management of the ITD file
        boolean deleteItdFile = false;
        final ItdTypeDetails itdTypeDetails = metadata.getMemberHoldingTypeDetails();
        if (itdTypeDetails == null) {
            // The ITD has no members
            deleteItdFile = true;
        }
        if (!deleteItdFile) {
            // We have some members in the ITD, so decide if we're to write
            // something to disk
            final ItdSourceFileComposer itdSourceFileComposer = new ItdSourceFileComposer(metadata.getMemberHoldingTypeDetails());
            // is physical content to write
            if (itdSourceFileComposer.isContent()) {
                // We have content to write
                getItdDiscoveryService().addItdTypeDetails(itdTypeDetails);
                final String itd = itdSourceFileComposer.getOutput();
                getFileManager().createOrUpdateTextFileIfRequired(itdFilename, itd, false);
            } else {
                // We don't have content to write
                deleteItdFile = true;
            }
        }
        if (deleteItdFile) {
            deleteItd(metadataIdentificationString, itdFilename, null, false);
        }
        // Eagerly notify that the metadata has been updated; this also
        // registers the metadata hash code in the superclass' cache to
        // avoid
        // unnecessary subsequent notifications if it hasn't changed
        notifyIfRequired(metadata);
        return metadata;
    }
    return null;
}
Also used : JavaType(org.springframework.roo.model.JavaType) ItdTypeDetails(org.springframework.roo.classpath.details.ItdTypeDetails) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 7 with PhysicalTypeMetadata

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

the class AbstractItdMetadataProvider method getMemberDetails.

/**
 * Returns details of the given Java type's members
 *
 * @param type the type for which to get the members (required)
 * @return <code>null</code> if the member details are unavailable
 */
protected MemberDetails getMemberDetails(final JavaType type) {
    TypeLocationService typeLocationService = getTypeLocationService();
    Validate.notNull(typeLocationService, "TypeLocationService is required");
    final String physicalTypeIdentifier = typeLocationService.getPhysicalTypeIdentifier(type);
    if (physicalTypeIdentifier == null) {
        return null;
    }
    // We need to lookup the metadata we depend on
    final PhysicalTypeMetadata physicalTypeMetadata = (PhysicalTypeMetadata) getMetadataService().get(physicalTypeIdentifier);
    return getMemberDetails(physicalTypeMetadata);
}
Also used : PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) TypeLocationService(org.springframework.roo.classpath.TypeLocationService)

Example 8 with PhysicalTypeMetadata

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

the class JpaFieldCreatorProvider method createEmbeddedField.

@Override
public void createEmbeddedField(JavaType typeName, JavaType fieldType, JavaSymbolName fieldName, boolean permitReservedWords, List<AnnotationMetadataBuilder> extraAnnotations) {
    // Check if the requested entity is a JPA @Entity
    final ClassOrInterfaceTypeDetails javaTypeDetails = typeLocationService.getTypeDetails(typeName);
    Validate.notNull(javaTypeDetails, "The type specified, '%s', doesn't exist", typeName);
    final String physicalTypeIdentifier = javaTypeDetails.getDeclaredByMetadataId();
    final PhysicalTypeMetadata targetTypeMetadata = (PhysicalTypeMetadata) metadataService.get(physicalTypeIdentifier);
    Validate.notNull(targetTypeMetadata, "The specified target '--class' does not exist or can not be found. Please create this type first.");
    final PhysicalTypeDetails targetPtd = targetTypeMetadata.getMemberHoldingTypeDetails();
    Validate.isInstanceOf(MemberHoldingTypeDetails.class, targetPtd);
    final ClassOrInterfaceTypeDetails targetTypeCid = (ClassOrInterfaceTypeDetails) targetPtd;
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(this.getClass().getName(), targetTypeCid);
    Validate.isTrue(memberDetails.getAnnotation(ENTITY) != null || memberDetails.getAnnotation(PERSISTENT) != null, "The field embedded command is only applicable to JPA @Entity or Spring Data @Persistent target types.");
    final EmbeddedField fieldDetails = new EmbeddedField(physicalTypeIdentifier, fieldType, fieldName);
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    insertField(fieldDetails, permitReservedWords, false);
}
Also used : EmbeddedField(org.springframework.roo.classpath.operations.jsr303.EmbeddedField) PhysicalTypeDetails(org.springframework.roo.classpath.PhysicalTypeDetails) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 9 with PhysicalTypeMetadata

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

the class JavaParserClassOrInterfaceTypeDetailsBuilder method build.

@Override
public ClassOrInterfaceTypeDetails build() {
    Validate.notEmpty(compilationUnit.getTypes(), "No types in compilation unit, so unable to continue parsing");
    ClassOrInterfaceDeclaration clazz = null;
    EnumDeclaration enumClazz = null;
    final StringBuilder sb = new StringBuilder(compilationUnit.getPackage().getName().toString());
    if (name.getEnclosingType() != null) {
        sb.append(".").append(name.getEnclosingType().getSimpleTypeName());
    }
    compilationUnitPackage = new JavaPackage(sb.toString());
    // Determine the type name, adding type parameters if possible
    final JavaType newName = JavaParserUtils.getJavaType(compilationUnitServices, typeDeclaration);
    // Revert back to the original type name (thus avoiding unnecessary
    // inferences about java.lang types; see ROO-244)
    name = new JavaType(newName.getFullyQualifiedTypeName(), newName.getEnclosingType(), newName.getArray(), newName.getDataType(), newName.getArgName(), newName.getParameters(), name.getModule());
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId);
    physicalTypeCategory = PhysicalTypeCategory.CLASS;
    if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
        clazz = (ClassOrInterfaceDeclaration) typeDeclaration;
        if (clazz.isInterface()) {
            physicalTypeCategory = PhysicalTypeCategory.INTERFACE;
        }
    } else if (typeDeclaration instanceof EnumDeclaration) {
        enumClazz = (EnumDeclaration) typeDeclaration;
        physicalTypeCategory = PhysicalTypeCategory.ENUMERATION;
    }
    Validate.notNull(physicalTypeCategory, "%s (%s for %s)", UNSUPPORTED_MESSAGE_PREFIX, typeDeclaration.getClass().getSimpleName(), name);
    cidBuilder.setName(name);
    cidBuilder.setPhysicalTypeCategory(physicalTypeCategory);
    imports = compilationUnit.getImports();
    if (imports == null) {
        imports = new ArrayList<ImportDeclaration>();
        compilationUnit.setImports(imports);
    }
    // Verify the package declaration appears to be correct
    if (compilationUnitPackage.equals(name.getPackage()) != true) {
        String warningStr = "[Warning] Compilation unit package '" + compilationUnitPackage + "' unexpected for type '" + name.getPackage() + "', it may be a nested class.";
        LOGGER.log(Level.WARNING, warningStr);
    }
    for (final ImportDeclaration importDeclaration : imports) {
        if (importDeclaration.getName() instanceof QualifiedNameExpr) {
            final String qualifier = ((QualifiedNameExpr) importDeclaration.getName()).getQualifier().toString();
            final String simpleName = importDeclaration.getName().getName();
            final String fullName = qualifier + "." + simpleName;
            // We want to calculate these...
            final JavaType type = new JavaType(fullName);
            final JavaPackage typePackage = importDeclaration.isAsterisk() ? new JavaPackage(fullName) : type.getPackage();
            // Process any comments for the import
            final CommentStructure commentStructure = new CommentStructure();
            JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, importDeclaration);
            final ImportMetadataBuilder newImport = new ImportMetadataBuilder(declaredByMetadataId, 0, typePackage, type, importDeclaration.isStatic(), importDeclaration.isAsterisk());
            newImport.setCommentStructure(commentStructure);
            cidBuilder.add(newImport.build());
        }
    }
    // Convert Java Parser modifier into JDK modifier
    cidBuilder.setModifier(JavaParserUtils.getJdkModifier(typeDeclaration.getModifiers()));
    // Type parameters
    final Set<JavaSymbolName> typeParameterNames = new HashSet<JavaSymbolName>();
    for (final JavaType param : name.getParameters()) {
        final JavaSymbolName arg = param.getArgName();
        // Fortunately type names can only appear at the top-level
        if (arg != null && !JavaType.WILDCARD_NEITHER_ARG.equals(arg) && !JavaType.WILDCARD_EXTENDS_ARG.equals(arg) && !JavaType.WILDCARD_SUPER_ARG.equals(arg)) {
            typeParameterNames.add(arg);
        }
    }
    List<ClassOrInterfaceType> implementsList;
    List<AnnotationExpr> annotationsList = null;
    List<BodyDeclaration> members = null;
    if (clazz != null) {
        final List<ClassOrInterfaceType> extendsList = clazz.getExtends();
        if (extendsList != null) {
            for (final ClassOrInterfaceType candidate : extendsList) {
                final JavaType javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, candidate, typeParameterNames);
                cidBuilder.addExtendsTypes(javaType);
            }
        }
        final List<JavaType> extendsTypes = cidBuilder.getExtendsTypes();
        // Obtain the superclass, if this is a class and one is available
        if (physicalTypeCategory == PhysicalTypeCategory.CLASS && extendsTypes.size() == 1) {
            final JavaType superclass = extendsTypes.get(0);
            final String superclassId = typeLocationService.getPhysicalTypeIdentifier(superclass);
            PhysicalTypeMetadata superPtm = null;
            if (superclassId != null) {
                superPtm = (PhysicalTypeMetadata) metadataService.get(superclassId);
            }
            if (superPtm != null && superPtm.getMemberHoldingTypeDetails() != null) {
                cidBuilder.setSuperclass(superPtm.getMemberHoldingTypeDetails());
            }
        }
        implementsList = clazz.getImplements();
        if (implementsList != null) {
            for (final ClassOrInterfaceType candidate : implementsList) {
                final JavaType javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, candidate, typeParameterNames);
                cidBuilder.addImplementsType(javaType);
            }
        }
        annotationsList = typeDeclaration.getAnnotations();
        members = clazz.getMembers();
    }
    if (enumClazz != null) {
        final List<EnumConstantDeclaration> constants = enumClazz.getEntries();
        if (constants != null) {
            for (final EnumConstantDeclaration enumConstants : constants) {
                cidBuilder.addEnumConstant(new JavaSymbolName(enumConstants.getName()));
            }
        }
        implementsList = enumClazz.getImplements();
        annotationsList = enumClazz.getAnnotations();
        members = enumClazz.getMembers();
    }
    if (annotationsList != null) {
        for (final AnnotationExpr candidate : annotationsList) {
            final AnnotationMetadata md = JavaParserAnnotationMetadataBuilder.getInstance(candidate, compilationUnitServices).build();
            final CommentStructure commentStructure = new CommentStructure();
            JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, candidate);
            md.setCommentStructure(commentStructure);
            cidBuilder.addAnnotation(md);
        }
    }
    if (members != null) {
        // type in the signature of the enclosing type
        for (final BodyDeclaration bodyDeclaration : members) {
            if (bodyDeclaration instanceof TypeDeclaration) {
                // Found a type
                innerTypes.add((TypeDeclaration) bodyDeclaration);
            }
        }
        for (final BodyDeclaration member : members) {
            if (member instanceof FieldDeclaration) {
                final FieldDeclaration castMember = (FieldDeclaration) member;
                for (final VariableDeclarator var : castMember.getVariables()) {
                    final FieldMetadata field = JavaParserFieldMetadataBuilder.getInstance(declaredByMetadataId, castMember, var, compilationUnitServices, typeParameterNames).build();
                    final CommentStructure commentStructure = new CommentStructure();
                    JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
                    field.setCommentStructure(commentStructure);
                    cidBuilder.addField(field);
                }
            }
            if (member instanceof MethodDeclaration) {
                final MethodDeclaration castMember = (MethodDeclaration) member;
                final MethodMetadata method = JavaParserMethodMetadataBuilder.getInstance(declaredByMetadataId, castMember, compilationUnitServices, typeParameterNames).build();
                final CommentStructure commentStructure = new CommentStructure();
                JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
                method.setCommentStructure(commentStructure);
                cidBuilder.addMethod(method);
            }
            if (member instanceof ConstructorDeclaration) {
                final ConstructorDeclaration castMember = (ConstructorDeclaration) member;
                final ConstructorMetadata constructor = JavaParserConstructorMetadataBuilder.getInstance(declaredByMetadataId, castMember, compilationUnitServices, typeParameterNames).build();
                final CommentStructure commentStructure = new CommentStructure();
                JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
                constructor.setCommentStructure(commentStructure);
                cidBuilder.addConstructor(constructor);
            }
            if (member instanceof TypeDeclaration) {
                final TypeDeclaration castMember = (TypeDeclaration) member;
                final JavaType innerType = new JavaType(castMember.getName(), name);
                final String innerTypeMetadataId = PhysicalTypeIdentifier.createIdentifier(innerType, PhysicalTypeIdentifier.getPath(declaredByMetadataId));
                final ClassOrInterfaceTypeDetails cid = new JavaParserClassOrInterfaceTypeDetailsBuilder(compilationUnit, compilationUnitServices, castMember, innerTypeMetadataId, innerType, metadataService, typeLocationService).build();
                cidBuilder.addInnerType(cid);
            }
        }
    }
    return cidBuilder.build();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) JavaPackage(org.springframework.roo.model.JavaPackage) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) FieldDeclaration(com.github.antlrjavaparser.api.body.FieldDeclaration) VariableDeclarator(com.github.antlrjavaparser.api.body.VariableDeclarator) EnumConstantDeclaration(com.github.antlrjavaparser.api.body.EnumConstantDeclaration) QualifiedNameExpr(com.github.antlrjavaparser.api.expr.QualifiedNameExpr) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ConstructorDeclaration(com.github.antlrjavaparser.api.body.ConstructorDeclaration) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ImportMetadataBuilder(org.springframework.roo.classpath.details.ImportMetadataBuilder) HashSet(java.util.HashSet) MethodDeclaration(com.github.antlrjavaparser.api.body.MethodDeclaration) EnumDeclaration(com.github.antlrjavaparser.api.body.EnumDeclaration) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) ImportDeclaration(com.github.antlrjavaparser.api.ImportDeclaration) BodyDeclaration(com.github.antlrjavaparser.api.body.BodyDeclaration) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) TypeDeclaration(com.github.antlrjavaparser.api.body.TypeDeclaration)

Example 10 with PhysicalTypeMetadata

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

the class ClasspathOperationsImpl method focus.

@Override
public void focus(final JavaType type) {
    Validate.notNull(type, "Specify the type to focus on");
    final String physicalTypeIdentifier = typeLocationService.getPhysicalTypeIdentifier(type);
    Validate.notNull(physicalTypeIdentifier, "Cannot locate the type %s", type.getFullyQualifiedTypeName());
    final PhysicalTypeMetadata ptm = (PhysicalTypeMetadata) metadataService.get(physicalTypeIdentifier);
    Validate.notNull(ptm, "Class %s does not exist", PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier));
}
Also used : PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata)

Aggregations

PhysicalTypeMetadata (org.springframework.roo.classpath.PhysicalTypeMetadata)11 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 JavaType (org.springframework.roo.model.JavaType)5 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)4 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)3 ArrayList (java.util.ArrayList)2 PhysicalTypeDetails (org.springframework.roo.classpath.PhysicalTypeDetails)2 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)2 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)2 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)2 EmbeddedField (org.springframework.roo.classpath.operations.jsr303.EmbeddedField)2 JdkJavaType (org.springframework.roo.model.JdkJavaType)2 ImportDeclaration (com.github.antlrjavaparser.api.ImportDeclaration)1 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)1 ClassOrInterfaceDeclaration (com.github.antlrjavaparser.api.body.ClassOrInterfaceDeclaration)1 ConstructorDeclaration (com.github.antlrjavaparser.api.body.ConstructorDeclaration)1 EnumConstantDeclaration (com.github.antlrjavaparser.api.body.EnumConstantDeclaration)1 EnumDeclaration (com.github.antlrjavaparser.api.body.EnumDeclaration)1 FieldDeclaration (com.github.antlrjavaparser.api.body.FieldDeclaration)1 MethodDeclaration (com.github.antlrjavaparser.api.body.MethodDeclaration)1