From c33a48ba5ec71ec736d5ef6b782a78a1fc60295d 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. --- .../tools/obfuscation/AnnotatedMixins.java | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 8e5c6d684..7a83477a5 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.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; @@ -688,6 +689,24 @@ public TypeHandle getTypeHandle(String name) { 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,25 +716,12 @@ public TypeHandle getTypeHandle(String name) { } } - 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; } @@ -728,11 +734,11 @@ public TypeHandle getTypeHandle(Object type) { 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()); }