0

I have one aspect with using aspectJ as below:

public aspect TestAspect {

    pointcut classicPointcut(PersistenceManagerImpl object) : execution(manager.PersistenceManagerImpl.new(..)) && target(object);

    after(PersistenceManagerImpl object) : classicPointcut(object){
        System.err.println(object.getClass().getSimpleName());
    }
}

this aspect is in module aspect. this module is packaking as jar. PersistenceManagerImpl is in other module but i need use it in module aspect. For dependency management i use maven. But here is of course problem with cyclic reference. Exists some way how can a resolve this problem ?

----------EDIT----------

I get only this error:

java.lang.NoSuchMethodError:TestAspect.ajc$after$TestAspect$1$cc149106(Ljava/lang/Object;)V

When i move my aspect to same module, when is PersistenceManagerImpl i obtain correct solution(of course). But this is not, what i wanted.

bajky
  • 332
  • 6
  • 17

2 Answers2

0

Could you put the error result of the compiling code? You could try to put another module as dependency first then later put the dependency on the aspectj maven plugin at weaveDependency in pom.xml as follow:

....
<dependency>
            <groupId>com.maventest</groupId>
            <artifactId>mytest</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
....

....
<plugin>                                                           
    <groupId>org.codehaus.mojo</groupId>                           
    <artifactId>aspectj-maven-plugin</artifactId>                  
    <version>1.8</version>                                         
    <executions>                                                   
        <execution>                                                
            <goals>                                                
                <goal>compile</goal>                               
            </goals>                                               
         </execution>                                               
     </executions>                                                  
    <configuration>                                                
         <source>${maven.compiler.source}</source>                  
         <target>${maven.compiler.target}</target>                  
         <showWeaveInfo>true</showWeaveInfo>                        
        <complianceLevel>${maven.compiler.target}</complianceLevel>
        <encoding>${project.build.sourceEncoding}</encoding>       
         <weaveDependencies>                                        
             <weaveDependency>                                      
                <groupId>com.maventest</groupId>
                <artifactId>mytest</artifactId>                 
             </weaveDependency>                                     
         </weaveDependencies>                                       
     </configuration>                                               
     <dependencies>                                                 
         <dependency>                                               
             <groupId>org.aspectj</groupId>                         
             <artifactId>aspectjrt</artifactId>                     
             <version>${aspectj.version}</version>                  
         </dependency>                                              
        <dependency>                                               
            <groupId>org.aspectj</groupId>                         
             <artifactId>aspectjtools</artifactId>                  
             <version>${aspectj.version}</version>                  
         </dependency>                                              
     </dependencies>                                                
</plugin> 

ps: You could see my question post asking the same thing here

Community
  • 1
  • 1
Ihsan Haikal
  • 1,085
  • 4
  • 16
  • 42
  • This is not solution for me becouse when i have read documentation for where it's written "Note: The artifacts you reference must exist as dependencies in the project." We get again cyclic reference :) – bajky Apr 20 '16 at 10:02
  • have you include the another project as dependency on pom.xml? – Ihsan Haikal Apr 20 '16 at 12:29
  • i can't include that as you show me ... becouse i use aspects in module, which i need use in module aspect. I have cyclic reference !! – bajky Apr 21 '16 at 15:17
0

Your aspect seems to be specific for the module in which PersistenceManagerImpl is located, so that module should be a dependency of the aspect module. On the other hand, that module depends on the aspect module because it needs it as an <aspectLibrary> in the AspectJ Maven configuration. Indeed a circular dependency, but an unnecessary one. You have two options:

  • Move the aspect to the application module which it is specific for because IMO it belongs there if it explicitly uses specific classes from there. Only aspects which implement cross-cutting concerns in a way applicable to multiple modules should be in their own aspect library.
  • Following the previous thought, you could make your aspect more general, e.g. do something like this:
public aspect TestAspect {
    pointcut classicPointcut(Object object) :
        execution(*..PersistenceManagerImpl.new(..)) &&
        target(object);

    after(Object object) : classicPointcut(object){
        System.err.println(object.getClass().getSimpleName());
    }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202