diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java index 72f2acd30..238edaa6f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultModelElementProcessorContext.java @@ -53,13 +53,13 @@ public class DefaultModelElementProcessorContext implements ProcessorContext { public DefaultModelElementProcessorContext(ProcessingEnvironment processingEnvironment, Options options) { this.processingEnvironment = processingEnvironment; this.messager = new DelegatingMessager( processingEnvironment.getMessager() ); - this.delegatingTypes = new TypesDecorator( processingEnvironment ); + this.versionInformation = DefaultVersionInformation.fromProcessingEnvironment( processingEnvironment ); + this.delegatingTypes = new TypesDecorator( processingEnvironment, versionInformation ); this.typeFactory = new TypeFactory( processingEnvironment.getElementUtils(), delegatingTypes ); this.options = options; - this.versionInformation = DefaultVersionInformation.fromProcessingEnvironment( processingEnvironment ); } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java index bbbb7f2d0..59216cda3 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/DefaultVersionInformation.java @@ -37,21 +37,28 @@ import org.mapstruct.ap.internal.version.VersionInformation; */ public class DefaultVersionInformation implements VersionInformation { private static final String JAVAC_PE_CLASS = "com.sun.tools.javac.processing.JavacProcessingEnvironment"; + private static final String COMPILER_NAME_JAVAC = "javac"; + private static final String JDT_IDE_PE_CLASS = "org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeBuildProcessingEnvImpl"; private static final String JDT_BATCH_PE_CLASS = "org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl"; + private static final String COMPILER_NAME_ECLIPSE_JDT = "Eclipse JDT"; private static final String MAP_STRUCT_VERSION = initMapStructVersion(); private final String runtimeVersion; private final String runtimeVendor; private final String compiler; + private final boolean eclipseJDT; + private final boolean javac; DefaultVersionInformation(String runtimeVersion, String runtimeVendor, String compiler) { this.runtimeVersion = runtimeVersion; this.runtimeVendor = runtimeVendor; this.compiler = compiler; + this.eclipseJDT = compiler.startsWith( COMPILER_NAME_ECLIPSE_JDT ); + this.javac = compiler.startsWith( COMPILER_NAME_JAVAC ); } @Override @@ -74,6 +81,16 @@ public class DefaultVersionInformation implements VersionInformation { return this.compiler; } + @Override + public boolean isEclipseJDTCompiler() { + return eclipseJDT; + } + + @Override + public boolean isJavacCompiler() { + return javac; + } + static DefaultVersionInformation fromProcessingEnvironment(ProcessingEnvironment processingEnv) { String runtimeVersion = System.getProperty( "java.version" ); String runtimeVendor = System.getProperty( "java.vendor" ); @@ -87,16 +104,17 @@ public class DefaultVersionInformation implements VersionInformation { String className = processingEnv.getClass().getName(); if ( className.equals( JAVAC_PE_CLASS ) ) { - return "javac"; + return COMPILER_NAME_JAVAC; } if ( className.equals( JDT_IDE_PE_CLASS ) ) { // the processing environment for the IDE integrated APT is in a different bundle than the APT classes - return "Eclipse JDT (IDE) " + getLibraryName( processingEnv.getTypeUtils().getClass(), true ); + return COMPILER_NAME_ECLIPSE_JDT + " (IDE) " + + getLibraryName( processingEnv.getTypeUtils().getClass(), true ); } if ( className.equals( JDT_BATCH_PE_CLASS ) ) { - return "Eclipse JDT (Batch) " + getLibraryName( processingEnv.getClass(), true ); + return COMPILER_NAME_ECLIPSE_JDT + " (Batch) " + getLibraryName( processingEnv.getClass(), true ); } return processingEnv.getClass().getSimpleName() + " from " + getLibraryName( processingEnv.getClass(), false ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/SpecificCompilerWorkarounds.java b/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/SpecificCompilerWorkarounds.java index 920154269..de1dd6095 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/SpecificCompilerWorkarounds.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/SpecificCompilerWorkarounds.java @@ -27,6 +27,8 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; +import org.mapstruct.ap.internal.version.VersionInformation; + /** * Contains workarounds for various quirks in specific compilers. * @@ -121,7 +123,8 @@ public class SpecificCompilerWorkarounds { * @see Eclipse Bug 382590 * @see Eclipse Bug 481555 */ - static TypeMirror asMemberOf(Types typeUtils, ProcessingEnvironment env, DeclaredType containing, Element element) { + static TypeMirror asMemberOf(Types typeUtils, ProcessingEnvironment env, VersionInformation versionInformation, + DeclaredType containing, Element element) { TypeMirror result = null; Exception lastException = null; try { @@ -130,13 +133,15 @@ public class SpecificCompilerWorkarounds { } catch ( IllegalArgumentException e ) { lastException = e; - - result = EclipseAsMemberOfWorkaround.asMemberOf( env, containing, element ); + if ( versionInformation.isEclipseJDTCompiler() ) { + result = EclipseAsMemberOfWorkaround.asMemberOf( env, containing, element ); + } } } catch ( Exception e ) { lastException = e; } + if ( null == result ) { throw new RuntimeException( "Fallback implementation of asMemberOf didn't work for " + element + " in " + containing, lastException ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/TypesDecorator.java b/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/TypesDecorator.java index 265309a6e..c8843f44f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/TypesDecorator.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/workarounds/TypesDecorator.java @@ -34,6 +34,8 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.type.WildcardType; import javax.lang.model.util.Types; +import org.mapstruct.ap.internal.version.VersionInformation; + /** * Replaces the usage of {@link Types} within MapStruct by delegating to the original implementation or to our specific * workarounds if necessary. @@ -43,10 +45,12 @@ import javax.lang.model.util.Types; public class TypesDecorator implements Types { private final Types delegate; private final ProcessingEnvironment processingEnv; + private final VersionInformation versionInformation; - public TypesDecorator(ProcessingEnvironment processingEnv) { + public TypesDecorator(ProcessingEnvironment processingEnv, VersionInformation versionInformation) { this.delegate = processingEnv.getTypeUtils(); this.processingEnv = processingEnv; + this.versionInformation = versionInformation; } @Override @@ -141,6 +145,11 @@ public class TypesDecorator implements Types { @Override public TypeMirror asMemberOf(DeclaredType containing, Element element) { - return SpecificCompilerWorkarounds.asMemberOf( delegate, processingEnv, containing, element ); + return SpecificCompilerWorkarounds.asMemberOf( + delegate, + processingEnv, + versionInformation, + containing, + element ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java b/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java index 4d6ff6858..a2eecc0bc 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/version/VersionInformation.java @@ -28,4 +28,6 @@ public interface VersionInformation { String getRuntimeVendor(); String getMapStructVersion(); String getCompiler(); + boolean isEclipseJDTCompiler(); + boolean isJavacCompiler(); }