Announcement Announcement Module
Collapse
No announcement yet.
How to add a method to an existing class source code in Spring Roo Addon? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to add a method to an existing class source code in Spring Roo Addon?

    I am writing a Roo Addon to add new methods to an existing class (The class source code was created by another Roo addon).

    I looked at Roo source code, I can get
    Code:
    ClassOrInterfaceTypeDetails mgrCls = typeLocationService.findClassOrInterface(propertiesManagerType);
    Then I can get the declared methods... however, how do I add a method to it?

    Thank you.

  • #2
    You need to have an add-on which creates the method in its own ITD which in turn adds the method to the class type during project compilation. To see how this works you can simply create a dummy add-on with the 'addon create advanced' command. This add-on illustrates how an add-on can create and manage its own ITD. If you look at the Roo sources you will find many add-ons (of varying complexity) which perform this task.

    HTH,
    Stefan

    Comment


    • #3
      Thank you Stefan for your reply.
      I am new at writing Roo Addon, I am still learning ITD stuff...

      Basically, in my OperationImpl class, I have this method, which is called when the user issues the command in Roo shell:

      Code:
      	/** {@inheritDoc} */
      	public void addString(String name, String value) {
      		Assert.notNull(name, "property name cannot be null.");
      
      		JavaPackage topLevelPackage = getProjectMetadata().getTopLevelPackage();		
      		JavaType propertiesManagerType = new JavaType(topLevelPackage.getFullyQualifiedPackageName()+".utils.SayHello");
      		 
      		String mgrIdentifier = typeLocationService.getPhysicalLocationCanonicalPath(propertiesManagerType, Path.SRC_MAIN_JAVA);
      
      		String declaredByMetadataId = null;
      
      		if (fileManager.exists(mgrIdentifier)) {
      			ClassOrInterfaceTypeDetails mgrCls = typeLocationService.findClassOrInterface(propertiesManagerType);
      			//System.out.println("Metadata ID: "+mgrCls.getDeclaredByMetadataId());
                              //Question here: how do I add a simple method, i.e. void sayHappy(); to mgrCls.
        
      		}else{
       
      			declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(propertiesManagerType, Path.SRC_MAIN_JAVA);
      			ClassOrInterfaceTypeDetailsBuilder typeDetailsBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, propertiesManagerType, PhysicalTypeCategory.CLASS);
      			  
      			List<MethodMetadataBuilder> methods = new ArrayList<MethodMetadataBuilder>();
      			 
      			InvocableMemberBodyBuilder bodyBuilder=new InvocableMemberBodyBuilder();
      			bodyBuilder.appendIndent();
      			bodyBuilder.appendFormalLine("return \"hello world!\";");
      			 
      			MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(declaredByMetadataId, Modifier.PUBLIC, new JavaSymbolName("sayHello"), JavaType.STRING_OBJECT, bodyBuilder);
      			methods.add(methodBuilder);
      			
      			typeDetailsBuilder.setDeclaredMethods(methods);
      			
      			typeManagementService.generateClassFile(typeDetailsBuilder.build());
      		}
             }
      I am stuck at the if branch, the else part works. I hope to add a new method to SayHello class through Roo shell after the SayHello was created.

      Also, note that the class I try to modify (utils.SayHello) is not an entity or anything. It is simply a POJO class, it has no annotations.

      Which Roo Addon will be a good example for me to study? Thanks again.
      Last edited by webhub; Jul 1st, 2011, 09:35 AM.

      Comment


      • #4
        What you are trying to do here is to add your method to the actual .java source which most Roo add-ons avoid. You should have your addon create an ITD instead which adds the method transparently. For this you need to take a look at the Metadata types that the 'addon create advanced' command generates. A good starting point for this kind of add-on would be the json addon http://git.springsource.org/roo/roo/...ter/addon-json which has operations, commands, metadata (ITD) and a trigger annotation.

        Comment

        Working...
        X