Search in sources :

Example 6 with MethodMetadata

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

the class SecurityCommands method getAllMethodsRelatedWithProvidedClassForAuthorize.

@CliOptionAutocompleteIndicator(command = "security authorize", param = "method", help = "You must select a valid method from the provided class or a regular expression that match with existing methods", validate = false)
public List<String> getAllMethodsRelatedWithProvidedClassForAuthorize(ShellContext context) {
    List<String> results = new ArrayList<String>();
    String service = context.getParameters().get("class");
    JavaType type = null;
    if (service != null) {
        type = getJavaTypeConverter().convertFromText(service, JavaType.class, PROJECT);
    } else {
        type = lastUsed.getJavaType();
    }
    MemberDetails serviceDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), typeLocationService.getTypeDetails(type));
    List<MethodMetadata> methods = serviceDetails.getMethods();
    for (MethodMetadata method : methods) {
        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(")");
        }
        results.add(methodName);
    }
    return results;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) 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 7 with MethodMetadata

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

the class SecurityCommands method getAllMethodsRelatedWithProvidedClassForFiltering.

@CliOptionAutocompleteIndicator(command = "security filtering", param = "method", help = "You must select a valid method from the provided class or a regular expression that match with existing methods", validate = false)
public List<String> getAllMethodsRelatedWithProvidedClassForFiltering(ShellContext context) {
    List<String> results = new ArrayList<String>();
    String service = context.getParameters().get("class");
    JavaType type = null;
    if (service != null) {
        type = getJavaTypeConverter().convertFromText(service, JavaType.class, PROJECT);
    } else {
        type = lastUsed.getJavaType();
    }
    MemberDetails serviceDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), typeLocationService.getTypeDetails(type));
    List<MethodMetadata> methods = serviceDetails.getMethods();
    for (MethodMetadata method : methods) {
        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(")");
        }
        results.add(methodName);
    }
    return results;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) 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 8 with MethodMetadata

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

the class JpaDataOnDemandConfigurationMetadata method getDodTypeBeanCreationMethod.

/**
 * Builds and returns a method used to instance a DataOnDemand class using
 * {@link EntityManager} and `@Bean` annotation.
 *
 * @param dodType
 *            the class to inject in the Spring context.
 * @return the MethodMetadata to add to ITD.
 */
private MethodMetadata getDodTypeBeanCreationMethod(JavaType dodType) {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName(StringUtils.uncapitalize(dodType.getSimpleTypeName()));
    // Check if method exists
    MethodMetadata existingMethod = getGovernorMethod(methodName);
    if (existingMethod != null) {
        return existingMethod;
    }
    // Add body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("return new %s(%s());", getNameOfJavaType(dodType), getAccessorMethod(getEntityManagerField().build()).getMethodName());
    // Create method
    MethodMetadataBuilder method = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, dodType, bodyBuilder);
    // Add annotation
    method.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.BEAN));
    return method.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 9 with MethodMetadata

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

the class IdentifierMetadata method getMutators.

/**
 * Locates the mutator methods.
 * <p>
 * If {@link #getFieldBuilders()} returns fields created by this ITD, public
 * mutators will automatically be produced in the declaring class.
 *
 * @param fields
 * @return the mutators (never returns null)
 */
private List<MethodMetadataBuilder> getMutators(final List<FieldMetadataBuilder> fields) {
    final List<MethodMetadataBuilder> mutators = new ArrayList<MethodMetadataBuilder>();
    // Compute the names of the mutators that will be produced
    for (final FieldMetadataBuilder field : fields) {
        final JavaSymbolName requiredMutatorName = BeanInfoUtils.getMutatorMethodName(field.getFieldName());
        final JavaType parameterType = field.getFieldType();
        final MethodMetadata mutator = getGovernorMethod(requiredMutatorName, parameterType);
        if (mutator == null) {
            mutators.add(getMutatorMethod(field.getFieldName(), field.getFieldType()));
        } else {
            Validate.isTrue(Modifier.isPublic(mutator.getModifier()), "User provided field but failed to provide a public '%s(%s)' method in '%s'", requiredMutatorName.getSymbolName(), field.getFieldName().getSymbolName(), destination.getFullyQualifiedTypeName());
            mutators.add(new MethodMetadataBuilder(mutator));
        }
    }
    return mutators;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 10 with MethodMetadata

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

the class JpaUnitTestMetadata method getRemoveFromRelationMethod.

/**
 * Creates a method to test item removes from a relationship.
 *
 * @param removeMethod the {@link MethodMetadata} to test.
 * @param relationFieldName a {@link String} with the field name
 *            representing the relation on the parent entity.
 * @param childEntity the {@link JavaType} of child entity.
 * @param field the {@link FieldMetadata} of relation field.
 * @return the {@link MethodMetadata}
 */
private MethodMetadata getRemoveFromRelationMethod(RelationInfo relationInfo) {
    // Initialize local variables
    String relationFieldName = relationInfo.fieldName;
    JavaType childEntity = relationInfo.childType;
    FieldMetadata relationField = relationInfo.fieldMetadata;
    // Create test method name using target method name, child entity name and relation field
    JavaSymbolName methodName = new JavaSymbolName(String.format("%sShouldRemoveThe%sFromThe%sRelationship", relationInfo.addMethod.getMethodName().getSymbolName(), childEntity.getSimpleTypeName(), relationFieldName));
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Create body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    // Entity entity = entityFactory.create(0);
    FieldMetadata entityFactory = entityFactories.get(this.entityFactory);
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(this.entity), this.entityVar, getAccessorMethod(entityFactory).getMethodName());
    // ChildEntity childEntity1 = childEntityFactory.create(0);
    FieldMetadata childEntityFactory = entityFactories.get(this.entityAndItsFactoryMap.get(childEntity));
    Validate.notNull(childEntityFactory, "Unable to locate the entity factory of %s in JpaUnitTestMetadata", childEntity.getSimpleTypeName());
    String childEntityVar1 = String.format("%s1", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(0);", getNameOfJavaType(childEntity), childEntityVar1, getAccessorMethod(childEntityFactory).getMethodName());
    // ChildEntity childEntity2 = childEntityFactory.create(1);
    String childEntityVar2 = String.format("%s2", StringUtils.uncapitalize(childEntity.getSimpleTypeName()));
    bodyBuilder.appendFormalLine("%s %s = %s().create(1);", getNameOfJavaType(childEntity), childEntityVar2, getAccessorMethod(childEntityFactory).getMethodName());
    // entity.ADD_METHOD(Arrays.asList(childEntity1, childEntity2));
    bodyBuilder.appendFormalLine("%s.%s(%s.asList(%s, %s));", entityVar, relationInfo.addMethod.getMethodName(), getNameOfJavaType(JavaType.ARRAYS), childEntityVar1, childEntityVar2);
    bodyBuilder.newLine();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    // entity.REMOVE_METHOD(Collections.singleton(childEntity1));
    bodyBuilder.appendFormalLine("%s.%s(%s.singleton(%s));", this.entityVar, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(JavaType.COLLECTIONS), childEntityVar1);
    bodyBuilder.newLine();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    // assertThat(childEntity1.PARENT_ENTITY_ACCESSOR()).as("Check 'REMOVE_METHOD' updates the CHILD_ENTITY relationship side")
    JavaSymbolName parentEntityAccessor = BeanInfoUtils.getAccessorMethodName(new JavaSymbolName(relationInfo.mappedBy), this.entity);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' updates the %s relationship side\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), childEntityVar1, parentEntityAccessor, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .isNull();
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".isNull();");
    bodyBuilder.indentRemove();
    // assertThat(entity.RELATION_FIELD_ACCESSOR()).as("Check 'REMOVE_METHOD' removes a CHILD_ENTITY from the relationship")
    JavaSymbolName relationFieldAccessorName = BeanInfoUtils.getAccessorMethodName(relationField);
    bodyBuilder.appendFormalLine("%s(%s.%s()).as(\"Check '%s' removes a %s from the relationship\")", getNameOfJavaType(new JavaType("org.assertj.core.api.Assertions.assertThat"), true), this.entityVar, relationFieldAccessorName, relationInfo.removeMethod.getMethodName(), getNameOfJavaType(childEntity));
    // .doesNotContain(childEntity1).contains(childEntity2);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine(".doesNotContain(%s).contains(%s);", childEntityVar1, childEntityVar2);
    bodyBuilder.reset();
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    // Add throws types
    methodBuilder.addThrowsType(JdkJavaType.EXCEPTION);
    return methodBuilder.build();
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15