Maven / Retrolambda: how to determine Java 8 class dependencies

Background:

We have a maven based java project that targets JRE 1.7, but the source code uses lambdas, so we use retrolambda to convert Java 8 source code to Java 7 . We also use the StreamSupport backport library when we need streams, a function. *, Optional, etc.

Using retrolambda involves setting the level of both source and target languages ​​per project to 1.8.

Everything works fine if there are no dependencies on java8 classes or methods (for example, java.util.stream.* , java.util.Optional or methods introduced in java8 , for example Collection.forEach ). If there are such ways to use, then build passages, but it does not work at run time, when running under Java 8 JVM.

Question:

My goal is to fail the assembly when such dependencies exist. Is there a way to detect dependencies on new Java 8 classes / methods at build time?

I thought of two possible options, but I'm not sure if any of them is doable:

  • Some kind of bytecode analyzer for detecting depdencies on predefined classes and methods. Are there such maven tools / plugins?
  • Lint Rules ( lint4j ). Not sure if class / method dependency can be defined with lint
+8
java maven java-8 lint retrolambda
source share
1 answer

You can use Animal Sniffer Maven Plugin for this . This allows you to verify that your code uses only APIs from a specified baseline (called a "signature"). In your case, you should use the signature org.codehaus.mojo.signature: java17: 1.0.

As others have pointed out, you can also set up the bootstrap bootstrap path, but for a) you need to configure JDK 7 and b) makes the build more complicated as you need to point out installing JDK 7. Animal Sniffer is much easier to work with my experience .

+5
source share

All Articles