Search in sources :

Example 46 with MemberDetails

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

the class SecurityAuthorizationsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting the annotated entity type
    JavaType annotatedService = governorPhysicalTypeMetadata.getType();
    // Getting the service details
    MemberDetails serviceDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(annotatedService));
    Map<MethodMetadata, String> preAuthorizationMethods = new LinkedHashMap<MethodMetadata, String>();
    // Get methods defined in each annotation @RooSecurityAuthorization
    AnnotationMetadata annotationAuthorizations = serviceDetails.getAnnotation(RooJavaType.ROO_SECURITY_AUTHORIZATIONS);
    AnnotationAttributeValue<?> attributeAuthorizations = annotationAuthorizations.getAttribute("authorizations");
    List<?> values = (List<?>) attributeAuthorizations.getValue();
    if (values != null && !values.isEmpty()) {
        Iterator<?> valuesIt = values.iterator();
        while (valuesIt.hasNext()) {
            NestedAnnotationAttributeValue authorizationAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
            // Get attributes from the annotation @RooSecurityAuthorization
            String methodName = (String) authorizationAnnotation.getValue().getAttribute("method").getValue();
            List<?> methodParameters = (List<?>) authorizationAnnotation.getValue().getAttribute("parameters").getValue();
            List<MethodMetadata> methods = serviceDetails.getMethods(new JavaSymbolName(methodName));
            for (MethodMetadata method : methods) {
                // check the parameters to get corresponding method
                if (checkParameters(method, methodParameters)) {
                    String roles = null;
                    if (authorizationAnnotation.getValue().getAttribute("roles") != null) {
                        roles = (String) authorizationAnnotation.getValue().getAttribute("roles").getValue();
                    }
                    String usernames = null;
                    if (authorizationAnnotation.getValue().getAttribute("usernames") != null) {
                        usernames = (String) authorizationAnnotation.getValue().getAttribute("usernames").getValue();
                    }
                    // Create the content based on defined roles and
                    // usernames to include it in the security annotation
                    String secAnnotationValue = getSecurityOperations().getSpringSecurityAnnotationValue(roles, usernames);
                    preAuthorizationMethods.put(method, secAnnotationValue);
                    break;
                }
            }
        }
    }
    return new SecurityAuthorizationsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, preAuthorizationMethods);
}
Also used : AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) LinkedHashMap(java.util.LinkedHashMap) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) List(java.util.List) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Example 47 with MemberDetails

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

the class PushInCommands method getAllPossibleMethods.

/**
 * Method that returns all defined methods for provided class on --class parameter.
 *
 * @param context context
 *            ShellContext used to obtain specified parameters
 * @return List with available methods. Empty List if class has not been specified.
 */
@CliOptionAutocompleteIndicator(command = "push-in", param = "method", validate = false, help = "Provides possible methods names if some class parameter has been specified")
public List<String> getAllPossibleMethods(ShellContext context) {
    List<String> allPossibleMethods = new ArrayList<String>();
    // Getting all introduces parameters
    Map<String, String> specifiedParameters = context.getParameters();
    String specifiedClass = specifiedParameters.get("class");
    // Check if class parameter has been specified
    if (StringUtils.isNotEmpty(specifiedClass)) {
        JavaType klass = getJavaTypeConverter().convertFromText(specifiedClass, JavaType.class, PROJECT);
        // TODO: Class details should be cached to prevent load MemberDetails everytime.
        // The problem is that if some element is cached, and then, new  method is added
        // to .aj file, this parameter will not autocomplete it.
        MemberDetails klassDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), typeLocationService.getTypeDetails(klass));
        if (klassDetails != null) {
            List<MethodMetadata> definedMethods = klassDetails.getMethods();
            for (MethodMetadata method : definedMethods) {
                // Check if method has been defined on current class and check
                // if current method has been pushed before.
                String declaredByMetadataID = method.getDeclaredByMetadataId();
                if (StringUtils.isNotBlank(declaredByMetadataID) && declaredByMetadataID.split("\\?").length > 1 && declaredByMetadataID.split("\\#").length > 0 && !declaredByMetadataID.split("\\#")[0].equals("MID:org.springframework.roo.classpath.PhysicalTypeIdentifier") && declaredByMetadataID.split("\\?")[1].equals(klass.getFullyQualifiedTypeName())) {
                    String methodName = method.getMethodName().getSymbolName();
                    List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
                    methodName = methodName.concat("(");
                    for (int i = 0; i < parameterTypes.size(); i++) {
                        String paramType = parameterTypes.get(i).getJavaType().getSimpleTypeName();
                        methodName = methodName.concat(paramType).concat(",");
                    }
                    if (!parameterTypes.isEmpty()) {
                        methodName = methodName.substring(0, methodName.length() - 1).concat(")");
                    } else {
                        methodName = methodName.concat(")");
                    }
                    allPossibleMethods.add(methodName);
                }
            }
        }
    }
    return allPossibleMethods;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 48 with MemberDetails

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

the class PushInOperationsImpl method pushIn.

@Override
public List<Object> pushIn(JavaPackage specifiedPackage, JavaType klass, String method, boolean writeOnDisk) {
    List<Object> pushedElements = new ArrayList<Object>();
    // Getting all JavaTypes on current project
    Collection<JavaType> allDeclaredTypes = new ArrayList<JavaType>();
    for (String moduleName : getProjectOperations().getModuleNames()) {
        allDeclaredTypes.addAll(getTypeLocationService().getTypesForModule(getProjectOperations().getPomFromModuleName(moduleName)));
    }
    // Checking current class
    if (klass != null) {
        ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(klass);
        Validate.notNull(classDetails, String.format("ERROR: Provided class '%s' doesn't exist on current project.", klass.getSimpleTypeName()));
        // If --class parameter is provided, --package will be ignored
        specifiedPackage = klass.getPackage();
        // class
        if (method != null) {
            boolean methodExists = false;
            MemberDetails classMemberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
            for (MethodMetadata classMethod : classMemberDetails.getMethods()) {
                if (methodMatch(classMethod, method)) {
                    pushedElements.addAll(pushInMethod(klass, classMethod, writeOnDisk));
                    methodExists = true;
                }
            }
            Validate.isTrue(methodExists, String.format("ERROR: No methods found on class '%s' that matches with '%s' expression.", klass.getSimpleTypeName(), method));
        } else {
            // If method is not specified, push-in entire class elements
            pushedElements.addAll(pushInClass(klass, writeOnDisk, true));
        }
    } else if (specifiedPackage != null && method != null) {
        // Check method on specified package
        boolean methodExists = false;
        for (JavaType declaredType : allDeclaredTypes) {
            // Check only classes on specified package
            if (declaredType.getPackage().equals(specifiedPackage)) {
                ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(declaredType);
                MemberDetails classMemberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
                for (MethodMetadata classMethod : classMemberDetails.getMethods()) {
                    if (methodMatch(classMethod, method)) {
                        pushedElements.addAll(pushInMethod(declaredType, classMethod, writeOnDisk));
                        methodExists = true;
                    }
                }
            }
            // Scan and update files status
            getFileManager().scan();
        }
        Validate.isTrue(methodExists, String.format("ERROR: No methods found on package '%s' that matches with '%s' expression.", specifiedPackage.getFullyQualifiedPackageName(), method));
    } else if (method != null) {
        // Check that exists some method that match with provided method
        boolean methodExists = false;
        for (JavaType declaredType : allDeclaredTypes) {
            ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(declaredType);
            MemberDetails classMemberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
            for (MethodMetadata classMethod : classMemberDetails.getMethods()) {
                if (methodMatch(classMethod, method)) {
                    pushedElements.addAll(pushInMethod(declaredType, classMethod, writeOnDisk));
                    methodExists = true;
                }
            }
            // Scan and update files status
            getFileManager().scan();
        }
        Validate.isTrue(methodExists, String.format("ERROR: No methods found on entire project that matches with '%s' expression.", method));
    } else if (specifiedPackage != null) {
        for (JavaType declaredType : allDeclaredTypes) {
            if (declaredType.getPackage().equals(specifiedPackage)) {
                pushedElements.addAll(pushInClass(declaredType, writeOnDisk, true));
            }
            // Scan and update files status
            getFileManager().scan();
        }
    } else {
        LOGGER.log(Level.WARNING, "ERROR: You must specify at least one parameter. ");
    }
    return pushedElements;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 49 with MemberDetails

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

the class PushInOperationsImpl method pushInMethod.

/**
 * Makes push-in of a method defined on a provided class
 *
 * @param klass
 *            class to make the push-in operation
 * @param weiteOnDisk
 *            indicates if pushed elements should be writed on .java file
 *
 * @return list of objects with all the pushed elements.
 */
public List<Object> pushInMethod(JavaType klass, MethodMetadata method, boolean writeOnDisk) {
    List<Object> pushedElements = new ArrayList<Object>();
    // Check if current klass exists
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    // Getting class details
    ClassOrInterfaceTypeDetails classDetails = getTypeLocationService().getTypeDetails(klass);
    Validate.notNull(klass, "ERROR: You must specify a valid class to continue with push-in action");
    Validate.notNull(method, "ERROR: You must provide a valid method");
    // Getting member details
    MemberDetails memberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), classDetails);
    // Check if the provided class is a test to be able to select valid
    // class path
    Path path = classDetails.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST) == null ? Path.SRC_MAIN_JAVA : Path.SRC_TEST_JAVA;
    // Getting current class .java file metadata ID
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(klass, getPathResolver().getPath(klass.getModule(), path));
    // Getting detailsBuilder
    ClassOrInterfaceTypeDetailsBuilder detailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(classDetails);
    // Avoid AspectJ error when push-in from *RepositoryImpl classes
    AnnotationMetadata rooRepositoryCustomImplAnnotation = classDetails.getAnnotation(RooJavaType.ROO_REPOSITORY_JPA_CUSTOM_IMPL);
    JavaType relatedRepositoryCustom = null;
    if (rooRepositoryCustomImplAnnotation != null) {
        AnnotationAttributeValue<Object> attribute = rooRepositoryCustomImplAnnotation.getAttribute("repository");
        Validate.notNull(attribute, "Unable to find 'repository' attribute of @RooJpaRepositoryCustomImpl on '%s'", classDetails.getType().getSimpleTypeName());
        relatedRepositoryCustom = (JavaType) attribute.getValue();
    }
    // Getting all details
    for (final MemberHoldingTypeDetails memberHoldingTypeDetails : memberDetails.getDetails()) {
        // Avoid AspectJ error when push-in from *RepositoryImpl classes
        if (rooRepositoryCustomImplAnnotation != null && memberHoldingTypeDetails.getImplementsTypes().contains(relatedRepositoryCustom)) {
            detailsBuilder.addImplementsType(relatedRepositoryCustom);
            pushedElements.add(relatedRepositoryCustom);
        }
        // this .java file
        if (!memberHoldingTypeDetails.getType().equals(classDetails.getType())) {
            continue;
        }
        // Getting all declared methods (including declared on ITDs
        // and .java files)
        List<MethodMetadata> allDeclaredMethods = memberHoldingTypeDetails.getMethods();
        // Checking if is necessary to make push-in for all declared methods
        for (MethodMetadata declaredMethod : allDeclaredMethods) {
            // If method exists on .aj file, add it!
            if (!method.getDeclaredByMetadataId().equals(declaredByMetadataId) && declaredMethod.equals(method)) {
                // Add method to .java file
                MethodMetadata newMethod = getNewMethod(declaredByMetadataId, method);
                detailsBuilder.addMethod(newMethod);
                // Save changes to be pushed
                pushedElements.add(newMethod);
            }
        }
        // Getting all imports registered on .aj file to move to .java file
        Set<ImportMetadata> allRegisteredImports = memberHoldingTypeDetails.getImports();
        detailsBuilder.addImports(allRegisteredImports);
        // Save imports to be pushed only if some method has been pushed
        if (!pushedElements.isEmpty()) {
            pushedElements.addAll(allRegisteredImports);
        }
    }
    // Updating .java file if write on disdk
    if (writeOnDisk) {
        getTypeManagementService().createOrUpdateTypeOnDisk(detailsBuilder.build());
    }
    return pushedElements;
}
Also used : Path(org.springframework.roo.project.Path) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ImportMetadata(org.springframework.roo.classpath.details.ImportMetadata)

Example 50 with MemberDetails

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

the class ThymeleafViewGeneratorServiceImpl method addFinderListView.

@Override
public void addFinderListView(String moduleName, JpaEntityMetadata entityMetadata, MemberDetails entity, ThymeleafMetadata viewMetadata, JavaType formBean, JavaType returnType, String finderName, List<ThymeleafMetadata> detailsControllers, ViewContext<ThymeleafMetadata> ctx) {
    // Getting returnType details
    MemberDetails returnTypeDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(returnType));
    // Getting entity fields that should be included on view
    List<FieldMetadata> returnFields = getPersistentFields(returnTypeDetails.getFields());
    List<FieldItem> fields = getFieldViewItems(entityMetadata, returnFields, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
    // Getting formBean details
    MemberDetails formBeanDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), getTypeLocationService().getTypeDetails(formBean));
    // Getting entity fields that should be included on view
    List<FieldMetadata> formBeanFieldsMetadata = getPersistentFields(formBeanDetails.getFields());
    List<FieldItem> formBeanFields = getFieldViewItems(entityMetadata, formBeanFieldsMetadata, ctx.getEntityName(), true, ctx, TABLE_SUFFIX);
    // Process elements to generate
    Document newDoc = null;
    // Getting new viewName
    String viewName = getViewsFolder(moduleName).concat(ctx.getControllerPath()).concat("/").concat(finderName).concat(getViewsExtension());
    EntityItem entityItem = createEntityItem(entityMetadata, ctx, TABLE_SUFFIX);
    List<List<DetailEntityItem>> detailsLevels = new ArrayList<List<DetailEntityItem>>();
    if (detailsControllers != null && !detailsControllers.isEmpty()) {
        List<DetailEntityItem> details = new ArrayList<DetailEntityItem>();
        for (ThymeleafMetadata detailController : detailsControllers) {
            DetailEntityItem detailItem = createDetailEntityItem(detailController, entity, entityMetadata, ctx.getEntityName(), ctx, DETAIL_SUFFIX, entityItem);
            details.add(detailItem);
        }
        // Sort details by path
        Collections.sort(details, new Comparator<DetailEntityItem>() {

            @Override
            public int compare(DetailEntityItem o1, DetailEntityItem o2) {
                return o1.getPathString().compareTo(o2.getPathString());
            }
        });
        // levels
        for (DetailEntityItem detail : details) {
            // Create group until item level
            while (detailsLevels.size() < detail.getLevel()) {
                detailsLevels.add(new ArrayList<DetailEntityItem>());
            }
            // Include detail in its group
            detailsLevels.get(detail.getLevel() - 1).add(detail);
            if (detail.getLevel() < 1) {
                // Nothing more to do with detail
                continue;
            }
            // look for parent
            for (DetailEntityItem parent : details) {
                if (detail.isTheParentEntity(parent)) {
                    // set parent
                    detail.setParentEntity(parent);
                    break;
                }
            }
        }
    }
    ctx.addExtraParameter("detailsLevels", detailsLevels);
    final JavaType searchController = viewMetadata.getDestination();
    Map<String, MethodMetadata> finderDatatablesMethods = viewMetadata.getFinderDatatableMethods();
    MethodMetadata finderDtMethod = finderDatatablesMethods.get(finderName);
    Map<String, MethodMetadata> finderListMethods = viewMetadata.getFinderListMethods();
    MethodMetadata finderListMethod = finderListMethods.get(finderName);
    Map<String, MethodMetadata> finderFormMethods = viewMetadata.getFinderFormMethods();
    MethodMetadata finderFormMethod = finderFormMethods.get(finderName);
    ctx.addExtraParameter("entity", entityItem);
    ctx.addExtraParameter("fields", fields);
    // Adding formBean fields
    ctx.addExtraParameter("formbeanfields", formBeanFields);
    // Check if new view to generate exists or not
    if (existsFile(viewName)) {
        Document existingDoc = loadExistingDoc(viewName);
        if (!isUserManagedDocument(existingDoc)) {
            newDoc = mergeListView("finderList", existingDoc, ctx, entityItem, fields, detailsLevels);
        }
    } else {
        newDoc = process("finderList", ctx);
    }
    // Write newDoc on disk
    writeDoc(newDoc, viewName);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem) ArrayList(java.util.ArrayList) List(java.util.List) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) EntityItem(org.springframework.roo.addon.web.mvc.views.components.EntityItem) DetailEntityItem(org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)

Aggregations

MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)56 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)36 JavaType (org.springframework.roo.model.JavaType)35 RooJavaType (org.springframework.roo.model.RooJavaType)31 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)29 ArrayList (java.util.ArrayList)25 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)20 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)14 SpringJavaType (org.springframework.roo.model.SpringJavaType)12 List (java.util.List)10 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)9 DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)8 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)7 FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)7 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)6 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)6