Prioritise TypeHandleASM when it is available.

Fixes remapping of synthetic members in resolvable classes.

(cherry picked from commit c33a48ba5e)
This commit is contained in:
LlamaLad7 committed 2023-08-12 17:37:48 +01:00
1 parent 155314e6e9
commit 89acb8387a
1 file changed
+31 -25
@@ -73,6 +73,7 @@ import org.spongepowered.tools.obfuscation.mirror.TypeHandle;
import org.spongepowered.tools.obfuscation.mirror.TypeHandleASM;
import org.spongepowered.tools.obfuscation.mirror.TypeHandleSimulated;
import org.spongepowered.tools.obfuscation.mirror.TypeReference;
import org.spongepowered.tools.obfuscation.mirror.TypeUtils;
import org.spongepowered.tools.obfuscation.struct.InjectorRemap;
import org.spongepowered.tools.obfuscation.validation.ParentValidator;
import org.spongepowered.tools.obfuscation.validation.TargetValidator;
@@ -158,7 +159,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
this.targets = this.initTargetMap();
this.obf = new ObfuscationManager(this);
this.obf.init();
this.validators = ImmutableList.<IMixinValidator>of(
new ParentValidator(this),
new TargetValidator(this)
@@ -243,7 +244,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
public CompilerEnvironment getCompilerEnvironment() {
return this.env;
}
@Override
public Integer getToken(String token) {
if (this.tokenCache.containsKey(token)) {
@@ -590,7 +591,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
this.printMessage(type.getKind(), type.decorate(msg), element, suppressedBy);
}
}
/**
* Print a message to the AP messager
*/
@@ -648,7 +649,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
this.printMessage(type.getKind(), type.decorate(msg), element, annotation, value);
}
}
/**
* Print a message to the AP messager
*/
@@ -688,6 +689,24 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
name = name.replace('/', '.');
Elements elements = this.processingEnv.getElementUtils();
PackageElement pkg = null;
int lastDotPos = name.lastIndexOf('.');
if (lastDotPos > -1) {
String pkgName = name.substring(0, lastDotPos);
pkg = elements.getPackageElement(pkgName);
}
if (pkg != null) {
// ASM gives the most and most accurate information. Try that first.
TypeHandle asmTypeHandle = TypeHandleASM.of(pkg, name.substring(lastDotPos + 1), this);
if (asmTypeHandle != null) {
return asmTypeHandle;
}
}
// ASM may be unable to resolve the class, for example if it's currently being compiled.
// Mirror is our next best bet.
TypeElement element = this.getTypeElement(name, elements);
if (element != null) {
try {
@@ -697,28 +716,15 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
}
}
int lastDotPos = name.lastIndexOf('.');
if (lastDotPos > -1) {
String pkgName = name.substring(0, lastDotPos);
PackageElement pkg = elements.getPackageElement(pkgName);
if (pkg != null) {
// If we can resolve the package but not the class, it's possible
// we're dealing with a class that mirror can't access, such as
// an anonymous class. The class might be available via the
// classpath though, so let's attempt to read the class with ASM
TypeHandle asmTypeHandle = TypeHandleASM.of(pkg, name.substring(lastDotPos + 1), this);
if (asmTypeHandle != null) {
return asmTypeHandle;
}
// Couldn't resolve the class, so just return an imaginary handle
return new TypeHandle(pkg, name);
}
if (pkg != null) {
// Couldn't resolve the class, but could resolve the package, so just return an imaginary handle.
return new TypeHandle(pkg, name);
}
// Couldn't even resolve the package, all hope is lost.
return null;
}
/**
* Get a TypeHandle representing the supplied type in the current processing
* environment
@@ -728,11 +734,11 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider
if (type instanceof TypeHandle) {
return (TypeHandle)type;
} else if (type instanceof DeclaredType) {
return new TypeHandle((DeclaredType)type);
return this.getTypeHandle(TypeUtils.getInternalName((DeclaredType) type));
} else if (type instanceof Type) {
return this.getTypeHandle(((Type)type).getClassName());
} else if (type instanceof TypeElement) {
return new TypeHandle((DeclaredType)((TypeElement)type).asType());
return this.getTypeHandle(TypeUtils.getInternalName((TypeElement) type));
} else if (type instanceof String) {
return this.getTypeHandle(type.toString());
}