0

I have a library with aspect which scan all statement executions and doing some logic with sql sentence.

@Pointcut("target(java.sql.Statement)")
public void statement() {}

@AfterReturning("!within(AnalyzerAspect) && statement() && args(sql)")
public void after(JoinPoint jp, String sql) throws Throwable {
  // some logic
}

When I use it for jdbc operations it works fine. I also used it with spring-jdbc like that

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <version>${aspect.compiler.plugin.version}</version>
  <configuration>
    <weaveDependencies>
      <weaveDependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
      </weaveDependency>
    </weaveDependencies>
    <aspectLibraries>
      <aspectLibrary>
        <groupId>edu.ifmo.diploma</groupId> <!-- My library -->
        <artifactId>db-analyzer</artifactId>
      </aspectLibrary>
    </aspectLibraries>
    <complianceLevel>11</complianceLevel>
    <showWeaveInfo>true</showWeaveInfo>
    <Xlint>ignore</Xlint>
    <encoding>UTF-8</encoding>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>test-compile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

But when I try to weave hibernate core dependency the same way

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <version>${aspect.compiler.plugin.version}</version>
  <configuration>
    <weaveDependencies>
      <weaveDependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-core</artifactId>
      </weaveDependency>
    </weaveDependencies>
    <aspectLibraries>
      <aspectLibrary>
        <groupId>edu.ifmo.diploma</groupId> <!-- My library -->
        <artifactId>db-analyzer</artifactId>
      </aspectLibrary>
    </aspectLibraries>
    <complianceLevel>11</complianceLevel>
    <showWeaveInfo>true</showWeaveInfo>
    <Xlint>ignore</Xlint>
    <encoding>UTF-8</encoding>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>test-compile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

I have an error on aspectj compile task.

In logs I found a lot of NPE's(264) like that

end public class org.hibernate.metamodel.mapping.ordering.ast.ParseTreeVisitor
 -- (NullPointerException) Cannot invoke "org.aspectj.weaver.ResolvedMember.getGenericParameterTypes()" because the return value of "org.aspectj.weaver.Shadow.getResolvedSignature()" is null
Cannot invoke "org.aspectj.weaver.ResolvedMember.getGenericParameterTypes()" because the return value of "org.aspectj.weaver.Shadow.getResolvedSignature()" is null
java.lang.NullPointerException: Cannot invoke "org.aspectj.weaver.ResolvedMember.getGenericParameterTypes()" because the return value of "org.aspectj.weaver.Shadow.getResolvedSignature()" is null

How to resolve that? Maybe aspectj plugin has an option to skip some weaves if they causes an error or maybe I have to put some properties to resolve that generic parameter types?

Full aspectj compiler log

I don't find any links with same problem in weaving, but there is one with closest exception: link

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Weaving should work, but the woven Hibernate classes will be part of your application JAR. If you still have the original Hibernate library on the classpath, you might get conflicts with duplicate classes. This can be solved, but it strongly depends on your actual POM or set of POMs. I cannot answer that in one sentence. Maybe instead of post-compile weaving, load-time weaving would make life simpler for you. I can help you, but I need more than a POM snippet. Please publish an [MCVE](https://stackoverflow.com/help/mcve) reproducing the problem on GitHub – kriegaex Jun 11 '23 at 18:02
  • You also seem to be using Lombok, which can lead to problems during compile time in connection with AspectJ. But I do not want to speculate. I need to see a minimal version of your project, as small as possible, just big enough to reproduce the compilation problems. Then I can analyse it and suggest a solution. – kriegaex Jun 11 '23 at 18:13
  • The library with aspect https://github.com/niallrox/db-analyzer Application with aspect dependency https://github.com/niallrox/djidjya-java-kubernetes-template – Maxim Rybalchenko Jun 12 '23 at 13:56
  • "The POM for com.djidjya.team:djidjya-boot-starter:jar:2.10 is missing, no dependency information available". Besides, your project has a weave dependency not on Hibernate but on Spring JDBC. Your question describes it differently. – kriegaex Jun 12 '23 at 14:27
  • djidjya-boot-starter is private library and it has not any db libs inside. I wrote about spring jdbc at start of question. For causing error u have to switch jdbc starter dependency to jpa starter and weave another lib as I wrote in description. I will remove my starter from dependencies fort easily reproducing – Maxim Rybalchenko Jun 12 '23 at 18:23
  • @kriegaex I update app(remove starter and include hibernate lib to weave config in pom) https://github.com/niallrox/djidjya-java-kubernetes-template – Maxim Rybalchenko Jun 12 '23 at 19:56

1 Answers1

0

Thanks for the two MCVE projects. They helped me to reproduce the problem, and it is actually an AspectJ problem. There are missing null checks for a certain method call in the weaver, which subsequently leads to errors in case of parsing method signatures in pointcuts, pointing to methods unavailable during weaving. Imagine that a call() pointcut is woven into a class, but then a dependency class is unavailable or is present in a different version with different methods. It is an edge case, but you bumped into it.

I am in the process of fixing the problem, see AspectJ issue #243.

Furthermore, I sent you two PRs for your projects:

You are using Java 17 in one of them, so you should use the AspectJ.dev version of AspectJ Maven Plugin, because the Mojohaus version still does not support Java 17 in version 1.14.0. The AspectJ.dev version is also the version recommended by the AspectJ project, because it is better maintained and has a few more features. In the PRs, I also updated the AspectJ version to the latest snapshot containing the fix for your problem, so you can easily retest. Not important here, but FYI, the snapshot also has Java 20 compilation support, while the latest release 1.9.19 only supports Java 19.


Update: In order to use an AspectJ snapshot, you need this repository:

<repositories>
    <repository>
        <id>ossrh-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thank you! Which repo include this snapshots? Now I have `The POM for org.aspectj:aspectjweaver:jar:1.9.20-SNAPSHOT is missing, no dependency information available` – Maxim Rybalchenko Jun 13 '23 at 08:34
  • I found a repo. Thank you, thats solution help me with compile, but my spring jpa repository call(PersonRepository.findAll) doesn't trigger the aspect. Do you know why this problem? Maybe I haven't weaved important dependencies? – Maxim Rybalchenko Jun 13 '23 at 09:09
  • Sorry, I forgot to add the repo to the PRs, but just now updated my answer above. I built AspectJ locally, so I did not notice that you would not be able to find the snapshots without an extra repo. As for your other question, I am too busy at the moment to look into that. I would have to analyse your aspects and how they are woven. That is a separate question, unrelated to this one. – kriegaex Jun 13 '23 at 10:01
  • I agree with you, I will create another one – Maxim Rybalchenko Jun 13 '23 at 10:11