Announcement Announcement Module
Collapse
No announcement yet.
MemberDetailsDecorator example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MemberDetailsDecorator example

    Hi all,

    I'm trying to register a MemberDetailsDecorator implementation that modifies the implementation of some integration tests generated by Automated Integration Testing Addon.

    As far as I understand about MemberDetailsDecorator and based on org.springframework.roo.classpath.customdata.tagge rs.CustomDataKeyDecorator, I can program a class implementing MemberDetailsDecorator Iterface and there do my personalizations in decorate(..) method. That is what I did in following code:

    Code:
    package org.gvnix.myaddon.roo.addon;
    
    import java.lang.reflect.Modifier;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.felix.scr.annotations.Component;
    import org.apache.felix.scr.annotations.Service;
    import org.springframework.roo.classpath.customdata.tagkeys.MethodMetadataCustomDataKey;
    import org.springframework.roo.classpath.details.MemberFindingUtils;
    import org.springframework.roo.classpath.details.MemberHoldingTypeDetails;
    import org.springframework.roo.classpath.details.MethodMetadata;
    import org.springframework.roo.classpath.details.MethodMetadataBuilder;
    import org.springframework.roo.classpath.details.annotations.AnnotatedJavaType;
    import org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder;
    import org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder;
    import org.springframework.roo.classpath.scanner.MemberDetails;
    import org.springframework.roo.classpath.scanner.MemberDetailsBuilder;
    import org.springframework.roo.classpath.scanner.MemberDetailsDecorator;
    import org.springframework.roo.model.JavaSymbolName;
    import org.springframework.roo.model.JavaType;
    
    @Component
    @Service
    public class IntegrationTestsMemberDetailsDecorator implements
            MemberDetailsDecorator {
    
        public MemberDetails decorate(String requestingClass,
                MemberDetails memberDetails) {
    
            MemberDetailsBuilder memberDetailsBuilder = new MemberDetailsBuilder(
                    memberDetails);
            if (requestingClass
                    .equalsIgnoreCase("org.springframework.roo.classpath.TypeLocationServiceImpl")) {
                for (MemberHoldingTypeDetails memberHoldingTypeDetails : memberDetails
                        .getDetails()) {
                    MethodMetadata testFlushMethodMetadata = MemberFindingUtils
                            .getMethod(memberHoldingTypeDetails,
                                    new JavaSymbolName("testFlush"), null);
                    // MethodMetadata testMergeMethodMetadata = MemberFindingUtils
                    // .getMethod(memberHoldingTypeDetails,
                    // new JavaSymbolName("testMerge"), null);
    
                    // System.out.println("** ".concat(requestingClass).concat(
                    // "   ****************  ")
                    // + testFlushMethodMetadata);
    
                    if (testFlushMethodMetadata != null) {
                        memberDetailsBuilder
                                .tag(testFlushMethodMetadata,
                                        new MethodMetadataCustomDataKey(
                                                "TEST_FLUSH_METHOD"),
                                        getFlushMethodTest(testFlushMethodMetadata));
                    }
                }
            }
            return memberDetailsBuilder.build();
        }
    
        private MethodMetadata getFlushMethodTest(MethodMetadata existing) {
    
            // Prepare method signature
            JavaSymbolName methodName = existing.getMethodName();
            List<JavaType> parameters = new ArrayList<JavaType>();
    
            List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
            annotations.add(new AnnotationMetadataBuilder(existing.getAnnotations()
                    .get(0)));
    
            InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
            bodyBuilder.appendFormalLine("return;");
    
            MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(
                    existing.getDeclaredByMetadataId(), Modifier.PUBLIC,
                    methodName, JavaType.VOID_PRIMITIVE,
                    AnnotatedJavaType.convertFromJavaTypes(parameters),
                    new ArrayList<JavaSymbolName>(), bodyBuilder);
            methodBuilder.setAnnotations(annotations);
            MethodMetadata method = methodBuilder.build();
    
            return method;
        }
    }
    MemberDetailsDecorator JavaDoc says that all registered Decorators are invoked, in fact my Decorator is invoked but the modification of the method body I'm trying to do is not being persisted in the IntegationTests ITD.

    I'm misundertanding something? Do I need to invoke the MemberDetailsBuilder.build() in any other place?

    Regards.
Working...
X