Announcement Announcement Module
Collapse
No announcement yet.
How to "Push In" Roo Generated Code... Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to "Push In" Roo Generated Code...

    Dear Sirs et Madames,
    I want to edit some code which is in the Roo-generated file "Person_Roo_Entity.aj", specifically I want to edit the
    PHP Code:
    persist() 
    method:

    PHP Code:
    @Transactional
        
    public void Person.persist() {
            if (
    this.entityManager == nullthis.entityManager entityManager();
            
    this.entityManager.persist(this);
        } 
    such that it also writes something to a text file after persisting. However it is recommended that these *.aj files are not edited directly as they are Roo-managed. So what are my options in editing this method? Is it possible to "push in" just a single method? and whats the guarantee, or how do I make sure that once edited, Roo does not re-generate this method the next time I run it?

    Thanks in advance,
    Tumaini

  • #2
    While the roo shell is running it scans Person.java, so as soon as you implement a public void persist() in Person.java and save the file, the shell removes the autogenerated aspect from the *.aj file. If you remove the method from Person.java, it recreates the aspect.

    Comment


    • #3
      Originally posted by BlakkPhoenixx View Post
      [...]
      I want to edit some code which is in the Roo-generated file "Person_Roo_Entity.aj"
      [...]
      Is it possible to "push in" just a single method?[...]
      Yes.

      Within the STS, expand the .aj in order to see its methods and select the desired ones (more on that plural, later) and right-click -> Refactor -> Push in...

      You can see what is going to be moved. Next/Next or Finish will do the job.

      Two warnings:
      1) If there are compilation errors in the files involved (the aj and the java), it won't work. Worst of all, it could delete the code from the .aj but not push it in the java.

      To get around this: quit the Roo Shell, fix the compilation problems and then Refactor (push in) the Aspect. Then you can start the Roo shell again.

      2) Move all the related methods and attributes from the .aj to the .java: for instance, the integration tests use a DataOnDemand attribute. Select both of them to refactor (push in...)

      Comment


      • #4
        This sounds like a real good place to use aspects to advise all your persist methods to write something to a log file.

        Easy to change, no need to push-in roo generated code and cleaner entity code.

        Just my 0.02$

        Comment


        • #5
          I totally agree. Terrific suggestion.

          Comment


          • #6
            Originally posted by guills View Post
            This sounds like a real good place to use aspects to advise all your persist methods to write something to a log file.

            Easy to change, no need to push-in roo generated code and cleaner entity code.
            I too thought of AOP when I saw this.

            Be aware though, whether you use AOP or not, that the file system is not inherently transactional, so if something that happens after persist() rolls back the transaction, your code may need (depending on your requirements) to explicitly revert the log file to its previous state.

            Comment


            • #7
              Originally posted by jbbarquero View Post
              Yes.

              Within the STS, expand the .aj in order to see its methods and select the desired ones (more on that plural, later) and right-click -> Refactor -> Push in...

              You can see what is going to be moved. Next/Next or Finish will do the job.
              In STS I cannot even see the *.aj files (although I can in Eclipse) , so I cannot perform the above action. Can anyone tell me why I cannot see these files in STS but can in Eclipse?

              Comment


              • #8
                Originally posted by guills View Post
                This sounds like a real good place to use aspects to advise all your persist methods to write something to a log file.

                Easy to change, no need to push-in roo generated code and cleaner entity code.

                Just my 0.02$
                Ok as am new to Spring and AOP I decided in order to implement your advice I would have to at least go through one AOP tutorial. However I run into problems... I have files like so:

                PHP Code:
                package com.testing.aop;
                public class 
                DefaultFooService implements FooService {

                public 
                Foo getFoo(String nameint age) {
                    return new 
                Foo(nameage);
                }

                public 
                void getAfter() {}
                public 
                void getBefore(String myName) {}


                PHP Code:
                package com.testing.aop;

                public interface 
                FooService {
                    
                Foo getFoo(String nameint age);
                    
                    public 
                void getAfter();
                    public 
                void getBefore(String myName);

                PHP Code:
                package com.testing.aop;

                public class 
                Foo {
                    
                String name;
                    
                int age;
                    
                    public 
                Foo(String name,int age){
                        
                this.age age;
                        
                this.name name;        
                    }

                PHP Code:
                package com.testing.aop;

                public class 
                SimpleProfiler {
                    public 
                void afterMethod() throws Throwable {
                        
                System.out.println("After the method call");
                    }

                    public 
                void beforeMethod(String myName){
                        
                System.out.println("My name is "+myName);
                    }

                I also have a spring.xml file which looks like so:
                PHP Code:
                <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:aop="http://www.springframework.org/schema/aop"
                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
                >
                <!-- 
                this is the object that will be proxied by Spring's AOP infrastructure -->
                <bean id="fooService" class="DefaultFooService"/>
                <!-- this is the actual advice itself -->
                <bean id="profiler" class="SimpleProfiler"/>
                <aop:config>
                <aop:aspect ref="profiler">
                <aop:pointcut id="aopafterMethod" expression="execution(* FooService.*(..))"/>
                <aop:after pointcut-ref="aopafterMethod" method="afterMethod"/>
                <aop:pointcut id="aopBefore" expression="execution(* FooService.getBefore(String)) and args(myName)"/>
                <aop:before pointcut-ref="aopBefore" method="beforeMethod"/>
                </aop:aspect>
                </aop:config>
                </beans> 
                My main class is:

                PHP Code:
                package com.ifakara.aoptest;

                import org.springframework.beans.factory.BeanFactory;
                import org.springframework.context.ApplicationContext;
                import org.springframework.context.support.ClassPathXmlApplicationContext;

                public class 
                Boo {
                   public static 
                void main(String[] argsthrows Exception {
                      
                BeanFactory ctx = new ClassPathXmlApplicationContext("spring.xml");
                      
                FooService foo = (FooServicectx.getBean("fooService");
                      
                foo.getFoo("Donald Duck"12);
                      
                foo.getAfter();
                      
                foo.getBefore("Possum");
                    }
                 } 
                However, when I run the main class I get an exception:
                PHP Code:
                NetBeansExecuting 'mvn.bat -Dexec.classpathScope=runtime -Dexec.args=-classpath %classpath com.ifakara.aoptest.Boo -Dexec.executable=java -Dnetbeans.execution=true process-classes org.codehaus.mojo:exec-maven-plugin:1.1.1:exec'
                NetBeans:      JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_10
                Scanning 
                for projects...
                ------------------------------------------------------------------------
                Building AOPTest Java EE 6 Webapp
                   task
                -segment: [process-classesorg.codehaus.mojo:exec-maven-plugin:1.1.1:exec]
                ------------------------------------------------------------------------
                [
                resources:resources]
                [
                WARNINGUsing platform encoding (Cp1252 actuallyto copy filtered resourcesi.ebuild is platform dependent!
                Copying 0 resource
                [compiler:compile]
                Compiling 1 source file to C:\Users\Sir Tumaini Kilimba\Documents\NetBeansProjects\Spring\AOPTest\target\classes
                [exec:exec]
                May 292011 8:17:00 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
                INFO
                Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f436f5startup date [Sun May 29 20:17:00 EAT 2011]; root of context hierarchy
                May 29
                2011 8:17:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
                INFO
                Loading XML bean definitions from class path resource [spring.xml]
                Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreExceptionIOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.xmlcannot be opened because it does not exist
                        at org
                .springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
                        
                at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
                        
                at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
                        
                at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
                        
                at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
                        
                at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
                        
                at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
                        
                at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
                        
                at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
                        
                at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
                        
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
                        
                at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
                        
                at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
                        
                at com.ifakara.aoptest.Boo.main(Boo.java:9)
                Caused byjava.io.FileNotFoundException: class path resource [spring.xmlcannot be opened because it does not exist
                        at org
                .springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
                        
                at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
                        ... 
                13 more
                ------------------------------------------------------------------------
                [
                ERROR]BUILD ERROR 
                I cannot understand why it tells me spring.xml does not exist. my Main class is in :

                C:\Users\codealot\Documents\NetBeansProjects\Sprin g\AOPTest\src\main\java\com\ifakara\aoptest\Boo.ja va

                and my spring.xml is in:

                C:\Users\Sir Tumaini Kilimba\Documents\NetBeansProjects\Spring\AOPTest\ src\main\java\com\ifakara\aoptest\spring.xml

                So they are both in the same directory... why is spring.xml not being found?

                Thanks in advance...

                Comment


                • #9
                  How to show/hide ITDs

                  Originally posted by BlakkPhoenixx View Post
                  In STS I cannot even see the *.aj files (although I can in Eclipse) , so I cannot perform the above action. Can anyone tell me why I cannot see these files in STS but can in Eclipse?
                  STS (and maybe Eclipse) hides ITDs by default; you can display them as follows:
                  1. open the Package Explorer menu
                  2. choose "Filters..."
                  3. untick the option called "Hide generated Spring Roo ITDs"
                  Once you've toggled this option, it gets pinned to the Package Explorer menu so that you can toggle it more easily next time.

                  Comment


                  • #10
                    Originally posted by BlakkPhoenixx View Post
                    my Main class is in :

                    C:\Users\codealot\Documents\NetBeansProjects\Sprin g\AOPTest\src\main\java\com\ifakara\aoptest\Boo.ja va

                    and my spring.xml is in:

                    C:\Users\Sir Tumaini Kilimba\Documents\NetBeansProjects\Spring\AOPTest\ src\main\java\com\ifakara\aoptest\spring.xml

                    So they are both in the same directory... why is spring.xml not being found?

                    Thanks in advance...
                    Normally you should put resource files like your Spring config file into src\main\resources, not src\main\java. So the full path to your spring.xml file should be src\main\resources\com\ifakara\aoptest\spring.xml. Also if you are running your app from your IDE, make sure that the src\main\resources folder is on the classpath; I don't know about NetBeans, but Eclipse doesn't do this by default.

                    Comment


                    • #11
                      Originally posted by Andrew Swan View Post
                      Also if you are running your app from your IDE, make sure that the src\main\resources folder is on the classpath; I don't know about NetBeans, but Eclipse doesn't do this by default.
                      Ok thanks... sorry for sounding so green, but how do I "make sure src\main\resources folder is on the classpath" ?

                      Comment


                      • #12
                        For COC (Convention Over Configuration) MAVEN has the four folders (java and resources for tests and main) in the classpath. If you didn't modify the pom.xml, using mvn from the command line will work.

                        In Eclipse, if you have the maven plugin (STS has it), you can right-click on the project->Maven (it has a bold red icon "m2")->Update project configuration. It will do the job for you.

                        Again, in Eclipse, you can do that manually with right-click on the project->Properties->Java Build Path, find the "Source" tab where you configure the "source folders on the build path". I don't recommend you to modify manually this option, it's better to let the maven plugin to do that, but if you're using another IDE without this plugin, it should have an option like that, in order to configure the output folder for the classes and resources (properties, xmls and so on)

                        Comment


                        • #13
                          Originally posted by jbbarquero View Post
                          Yes.

                          Within the STS, expand the .aj in order to see its methods and select the desired ones (more on that plural, later) and right-click -> Refactor -> Push in...

                          You can see what is going to be moved. Next/Next or Finish will do the job.

                          )
                          Thanks, this worked fine when it came to displaying the *.aj files but and when I performed the push in on a specific method, that method disappeared from the .aj file but where it went, I have no idea, as it is NOT in the corresponding .java file :-( ... help!

                          Comment


                          • #14
                            Originally posted by BlakkPhoenixx View Post
                            Thanks, this worked fine when it came to displaying the *.aj files but and when I performed the push in on a specific method, that method disappeared from the .aj file but where it went, I have no idea, as it is NOT in the corresponding .java file :-( ... help!
                            There is an intermediate screen on the Push in...'s wizard that inform you of the target Java file.

                            Besides, you need to move all the related elements. If a method use an attribute (for instance, the *.aj integration tests uses an instance of the DataOnDemand), you need to select both the method and the dod variable and Push them in the Java file.

                            Furthermore, all the files involved in the "Push in..." operation need to be correct (I mean, they have no compilation errors) before performing the push.

                            Be careful with the Roo Shell. Sometimes it's better to operate with the shell closed, managing the files AND the Roo annotations, and then re-start the Shell.

                            Comment


                            • #15
                              Good tip

                              Originally posted by jbbarquero View Post
                              There is an intermediate screen on the Push in...'s wizard that inform you of the target Java file.

                              Besides, you need to move all the related elements. If a method use an attribute (for instance, the *.aj integration tests uses an instance of the DataOnDemand), you need to select both the method and the dod variable and Push them in the Java file.

                              Furthermore, all the files involved in the "Push in..." operation need to be correct (I mean, they have no compilation errors) before performing the push.

                              Be careful with the Roo Shell. Sometimes it's better to operate with the shell closed, managing the files AND the Roo annotations, and then re-start the Shell.
                              These are good tips. If you want to be sure that code's not going to be lost when you do a push-in, there's a "Preview" button in the refactor dialog that shows you what the pushed-in code will look like. If you only see the source .aj file in this dialog and not the target .java file, cancel the refactoring and check the things listed above.

                              Comment

                              Working...
                              X