#644 Only delegate to the Eclipse-specific workaround when the compiler is actually identified as Eclipse JDT

This commit is contained in:
Andreas Gudian 2015-11-16 20:32:41 +01:00
parent 76603416e7
commit dde84306ab
5 changed files with 44 additions and 10 deletions

View File

@ -53,13 +53,13 @@ public class DefaultModelElementProcessorContext implements ProcessorContext {
public DefaultModelElementProcessorContext(ProcessingEnvironment processingEnvironment, Options options) { public DefaultModelElementProcessorContext(ProcessingEnvironment processingEnvironment, Options options) {
this.processingEnvironment = processingEnvironment; this.processingEnvironment = processingEnvironment;
this.messager = new DelegatingMessager( processingEnvironment.getMessager() ); 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( this.typeFactory = new TypeFactory(
processingEnvironment.getElementUtils(), processingEnvironment.getElementUtils(),
delegatingTypes delegatingTypes
); );
this.options = options; this.options = options;
this.versionInformation = DefaultVersionInformation.fromProcessingEnvironment( processingEnvironment );
} }
@Override @Override

View File

@ -37,21 +37,28 @@ import org.mapstruct.ap.internal.version.VersionInformation;
*/ */
public class DefaultVersionInformation implements VersionInformation { public class DefaultVersionInformation implements VersionInformation {
private static final String JAVAC_PE_CLASS = "com.sun.tools.javac.processing.JavacProcessingEnvironment"; 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 = private static final String JDT_IDE_PE_CLASS =
"org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeBuildProcessingEnvImpl"; "org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeBuildProcessingEnvImpl";
private static final String JDT_BATCH_PE_CLASS = private static final String JDT_BATCH_PE_CLASS =
"org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl"; "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 static final String MAP_STRUCT_VERSION = initMapStructVersion();
private final String runtimeVersion; private final String runtimeVersion;
private final String runtimeVendor; private final String runtimeVendor;
private final String compiler; private final String compiler;
private final boolean eclipseJDT;
private final boolean javac;
DefaultVersionInformation(String runtimeVersion, String runtimeVendor, String compiler) { DefaultVersionInformation(String runtimeVersion, String runtimeVendor, String compiler) {
this.runtimeVersion = runtimeVersion; this.runtimeVersion = runtimeVersion;
this.runtimeVendor = runtimeVendor; this.runtimeVendor = runtimeVendor;
this.compiler = compiler; this.compiler = compiler;
this.eclipseJDT = compiler.startsWith( COMPILER_NAME_ECLIPSE_JDT );
this.javac = compiler.startsWith( COMPILER_NAME_JAVAC );
} }
@Override @Override
@ -74,6 +81,16 @@ public class DefaultVersionInformation implements VersionInformation {
return this.compiler; return this.compiler;
} }
@Override
public boolean isEclipseJDTCompiler() {
return eclipseJDT;
}
@Override
public boolean isJavacCompiler() {
return javac;
}
static DefaultVersionInformation fromProcessingEnvironment(ProcessingEnvironment processingEnv) { static DefaultVersionInformation fromProcessingEnvironment(ProcessingEnvironment processingEnv) {
String runtimeVersion = System.getProperty( "java.version" ); String runtimeVersion = System.getProperty( "java.version" );
String runtimeVendor = System.getProperty( "java.vendor" ); String runtimeVendor = System.getProperty( "java.vendor" );
@ -87,16 +104,17 @@ public class DefaultVersionInformation implements VersionInformation {
String className = processingEnv.getClass().getName(); String className = processingEnv.getClass().getName();
if ( className.equals( JAVAC_PE_CLASS ) ) { if ( className.equals( JAVAC_PE_CLASS ) ) {
return "javac"; return COMPILER_NAME_JAVAC;
} }
if ( className.equals( JDT_IDE_PE_CLASS ) ) { if ( className.equals( JDT_IDE_PE_CLASS ) ) {
// the processing environment for the IDE integrated APT is in a different bundle than the APT classes // 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 ) ) { 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 ); return processingEnv.getClass().getSimpleName() + " from " + getLibraryName( processingEnv.getClass(), false );

View File

@ -27,6 +27,8 @@ import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
import javax.lang.model.util.Types; import javax.lang.model.util.Types;
import org.mapstruct.ap.internal.version.VersionInformation;
/** /**
* Contains workarounds for various quirks in specific compilers. * Contains workarounds for various quirks in specific compilers.
* *
@ -121,7 +123,8 @@ public class SpecificCompilerWorkarounds {
* @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=382590">Eclipse Bug 382590</a> * @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=382590">Eclipse Bug 382590</a>
* @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=481555">Eclipse Bug 481555</a> * @see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=481555">Eclipse Bug 481555</a>
*/ */
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; TypeMirror result = null;
Exception lastException = null; Exception lastException = null;
try { try {
@ -130,13 +133,15 @@ public class SpecificCompilerWorkarounds {
} }
catch ( IllegalArgumentException e ) { catch ( IllegalArgumentException e ) {
lastException = e; lastException = e;
if ( versionInformation.isEclipseJDTCompiler() ) {
result = EclipseAsMemberOfWorkaround.asMemberOf( env, containing, element ); result = EclipseAsMemberOfWorkaround.asMemberOf( env, containing, element );
}
} }
} }
catch ( Exception e ) { catch ( Exception e ) {
lastException = e; lastException = e;
} }
if ( null == result ) { if ( null == result ) {
throw new RuntimeException( "Fallback implementation of asMemberOf didn't work for " throw new RuntimeException( "Fallback implementation of asMemberOf didn't work for "
+ element + " in " + containing, lastException ); + element + " in " + containing, lastException );

View File

@ -34,6 +34,8 @@ import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.WildcardType; import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Types; 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 * Replaces the usage of {@link Types} within MapStruct by delegating to the original implementation or to our specific
* workarounds if necessary. * workarounds if necessary.
@ -43,10 +45,12 @@ import javax.lang.model.util.Types;
public class TypesDecorator implements Types { public class TypesDecorator implements Types {
private final Types delegate; private final Types delegate;
private final ProcessingEnvironment processingEnv; private final ProcessingEnvironment processingEnv;
private final VersionInformation versionInformation;
public TypesDecorator(ProcessingEnvironment processingEnv) { public TypesDecorator(ProcessingEnvironment processingEnv, VersionInformation versionInformation) {
this.delegate = processingEnv.getTypeUtils(); this.delegate = processingEnv.getTypeUtils();
this.processingEnv = processingEnv; this.processingEnv = processingEnv;
this.versionInformation = versionInformation;
} }
@Override @Override
@ -141,6 +145,11 @@ public class TypesDecorator implements Types {
@Override @Override
public TypeMirror asMemberOf(DeclaredType containing, Element element) { public TypeMirror asMemberOf(DeclaredType containing, Element element) {
return SpecificCompilerWorkarounds.asMemberOf( delegate, processingEnv, containing, element ); return SpecificCompilerWorkarounds.asMemberOf(
delegate,
processingEnv,
versionInformation,
containing,
element );
} }
} }

View File

@ -28,4 +28,6 @@ public interface VersionInformation {
String getRuntimeVendor(); String getRuntimeVendor();
String getMapStructVersion(); String getMapStructVersion();
String getCompiler(); String getCompiler();
boolean isEclipseJDTCompiler();
boolean isJavacCompiler();
} }