#1551 Use javax.annotation.processing.Generated if it is available and source version is at least RELEASE_9

This commit is contained in:
Filip Hrisafov 2018-10-13 10:24:22 +02:00
parent 0e0fd313e5
commit 6d5243dc2f
4 changed files with 29 additions and 6 deletions

View File

@ -283,8 +283,8 @@ If the `@Generated` annotation is not available, MapStruct will detect this situ
[NOTE]
=====
In Java 9 `java.annotation.processing.Generated` was added, which is considered as a general purpose annotation for any code generators
and is part of the `java.compiler` module. Support for it is planned within https://github.com/mapstruct/mapstruct/issues/1551[#1551]
In Java 9 `java.annotation.processing.Generated` was added (part of the `java.compiler` module),
if this annotation is available then it will be used.
=====
[[defining-mapper]]

View File

@ -74,12 +74,18 @@ public abstract class GeneratedType extends ModelElement {
this.versionInformation = versionInformation;
this.accessibility = accessibility;
this.generatedTypeAvailable = typeFactory.isTypeAvailable( "javax.annotation.Generated" );
if ( generatedTypeAvailable ) {
if ( versionInformation.isSourceVersionAtLeast9() &&
typeFactory.isTypeAvailable( "javax.annotation.processing.Generated" ) ) {
this.generatedType = typeFactory.getType( "javax.annotation.processing.Generated" );
this.generatedTypeAvailable = true;
}
else if ( typeFactory.isTypeAvailable( "javax.annotation.Generated" ) ) {
this.generatedType = typeFactory.getType( "javax.annotation.Generated" );
this.generatedTypeAvailable = true;
}
else {
this.generatedType = null;
this.generatedTypeAvailable = false;
}
this.constructor = constructor;

View File

@ -11,6 +11,7 @@ import java.net.URL;
import java.util.jar.Manifest;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.SourceVersion;
import org.mapstruct.ap.internal.version.VersionInformation;
@ -37,15 +38,19 @@ public class DefaultVersionInformation implements VersionInformation {
private final String runtimeVersion;
private final String runtimeVendor;
private final String compiler;
private final boolean sourceVersionAtLeast9;
private final boolean eclipseJDT;
private final boolean javac;
DefaultVersionInformation(String runtimeVersion, String runtimeVendor, String compiler) {
DefaultVersionInformation(String runtimeVersion, String runtimeVendor, String compiler,
SourceVersion sourceVersion) {
this.runtimeVersion = runtimeVersion;
this.runtimeVendor = runtimeVendor;
this.compiler = compiler;
this.eclipseJDT = compiler.startsWith( COMPILER_NAME_ECLIPSE_JDT );
this.javac = compiler.startsWith( COMPILER_NAME_JAVAC );
// If the difference between the source version and RELEASE_6 is more that 2 than we are at least on 9
this.sourceVersionAtLeast9 = sourceVersion.compareTo( SourceVersion.RELEASE_6 ) > 2;
}
@Override
@ -68,6 +73,11 @@ public class DefaultVersionInformation implements VersionInformation {
return this.compiler;
}
@Override
public boolean isSourceVersionAtLeast9() {
return sourceVersionAtLeast9;
}
@Override
public boolean isEclipseJDTCompiler() {
return eclipseJDT;
@ -84,7 +94,12 @@ public class DefaultVersionInformation implements VersionInformation {
String compiler = getCompiler( processingEnv );
return new DefaultVersionInformation( runtimeVersion, runtimeVendor, compiler );
return new DefaultVersionInformation(
runtimeVersion,
runtimeVendor,
compiler,
processingEnv.getSourceVersion()
);
}
private static String getCompiler(ProcessingEnvironment processingEnv) {

View File

@ -19,6 +19,8 @@ public interface VersionInformation {
String getCompiler();
boolean isSourceVersionAtLeast9();
boolean isEclipseJDTCompiler();
boolean isJavacCompiler();