#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) {
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

View File

@ -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 );

View File

@ -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 <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>
*/
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 );

View File

@ -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 );
}
}

View File

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