From 89acb8387aea527ae05908040ca4fdda231e6e16 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Sun, 24 Jul 2022 13:19:19 +0100 Subject: [PATCH] Prioritise TypeHandleASM when it is available. Fixes remapping of synthetic members in resolvable classes. (cherry picked from commit c33a48ba5ec71ec736d5ef6b782a78a1fc60295d) --- .../tools/obfuscation/AnnotatedMixins.java | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 0835ead5..654e61dd 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -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.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()); }