From 89acb8387aea527ae05908040ca4fdda231e6e16 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Sun, 24 Jul 2022 13:19:19 +0100 Subject: [PATCH 1/8] 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()); } From 1cec8b137739ebe0e4e79bb66f9eb6369dfa067d Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Mon, 13 Feb 2023 19:17:32 +0000 Subject: [PATCH 2/8] AP: Fix several cases of ASM TypeHandles breaking code that relies on Mirror. (cherry picked from commit e5d9db491a4d031fe590e274f7eeb9ef4fae33f7) --- .../tools/obfuscation/AnnotatedMixin.java | 2 +- ...atedMixinElementHandlerSoftImplements.java | 2 +- .../tools/obfuscation/AnnotatedMixins.java | 22 ++++---- .../tools/obfuscation/MixinValidator.java | 12 +++-- .../obfuscation/ObfuscationEnvironment.java | 6 +-- .../tools/obfuscation/TargetMap.java | 10 ---- .../tools/obfuscation/mirror/TypeHandle.java | 49 +++++++++++++++--- .../obfuscation/mirror/TypeHandleASM.java | 29 +++++++---- .../mirror/TypeHandleSimulated.java | 22 ++++---- .../obfuscation/mirror/TypeReference.java | 9 ++-- .../validation/TargetValidator.java | 51 ++++--------------- 11 files changed, 110 insertions(+), 104 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index f3b918a8..001bbdfa 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -176,7 +176,7 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { this.mappings = this.obf.createMappingConsumer(); this.messager = ap; this.mixin = type; - this.handle = new TypeHandle(type); + this.handle = new TypeHandle(type, ap.getTypeProvider()); this.methods = new ArrayList(this.handle.getMethods()); this.virtual = this.handle.getAnnotation(Pseudo.class).exists(); this.annotation = this.handle.getAnnotation(Mixin.class); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java index f9e7dfb2..82be0f1b 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java @@ -77,7 +77,7 @@ class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinElementHa } try { - TypeHandle iface = new TypeHandle(interfaceAnnotation.getValue("iface")); + TypeHandle iface = this.ap.getTypeProvider().getTypeHandle(interfaceAnnotation.getValue("iface")); String prefix = interfaceAnnotation.getValue("prefix"); this.processSoftImplements(remap, iface, prefix); } catch (Exception ex) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 654e61dd..ec6670b7 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -371,17 +371,13 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider return this.mixins.get(mixinType); } - public Collection getMixinsTargeting(TypeMirror targetType) { - return this.getMixinsTargeting((TypeElement)((DeclaredType)targetType).asElement()); - } - - public Collection getMixinsTargeting(TypeElement targetType) { - List minions = new ArrayList(); + public Collection getMixinsTargeting(TypeHandle targetType) { + List minions = new ArrayList(); for (TypeReference mixin : this.targets.getMixinsTargeting(targetType)) { - TypeHandle handle = mixin.getHandle(this.processingEnv); - if (handle != null && handle.hasTypeMirror()) { - minions.add(handle.getTypeMirror()); + TypeHandle handle = mixin.getHandle(this); + if (handle != null) { + minions.add(handle); } } @@ -710,7 +706,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider TypeElement element = this.getTypeElement(name, elements); if (element != null) { try { - return new TypeHandle(element); + return new TypeHandle(element, this); } catch (NullPointerException ex) { // probably bad package } @@ -718,7 +714,7 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider if (pkg != null) { // Couldn't resolve the class, but could resolve the package, so just return an imaginary handle. - return new TypeHandle(pkg, name); + return new TypeHandle(pkg, name, this); } // Couldn't even resolve the package, all hope is lost. @@ -816,11 +812,11 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider String pkg = name.substring(0, lastDotPos); PackageElement packageElement = this.processingEnv.getElementUtils().getPackageElement(pkg); if (packageElement != null) { - return new TypeHandleSimulated(packageElement, name, simulatedTarget); + return new TypeHandleSimulated(packageElement, name, simulatedTarget, this); } } - return new TypeHandleSimulated(name, simulatedTarget); + return new TypeHandleSimulated(name, simulatedTarget, this); } /* (non-Javadoc) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java index ab10529a..85f68512 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java @@ -28,13 +28,13 @@ import java.util.Collection; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.TypeElement; -import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.interfaces.IMessagerSuppressible; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IMixinValidator; import org.spongepowered.tools.obfuscation.interfaces.IOptionProvider; +import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -57,7 +57,12 @@ public abstract class MixinValidator implements IMixinValidator { * Option provider */ protected final IOptionProvider options; - + + /** + * Type handle provider + */ + protected final ITypeHandleProvider typeHandleProvider; + /** * Pass to run this validator in */ @@ -73,6 +78,7 @@ public abstract class MixinValidator implements IMixinValidator { this.processingEnv = ap.getProcessingEnvironment(); this.messager = ap; this.options = ap; + this.typeHandleProvider = ap.getTypeProvider(); this.pass = pass; } @@ -94,7 +100,7 @@ public abstract class MixinValidator implements IMixinValidator { protected abstract boolean validate(TypeElement mixin, IAnnotationHandle annotation, Collection targets); - protected final Collection getMixinsTargeting(TypeMirror targetType) { + protected final Collection getMixinsTargeting(TypeHandle targetType) { return AnnotatedMixins.getMixinsForEnvironment(this.processingEnv).getMixinsTargeting(targetType); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java index 2f55a873..fa42dcb8 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/ObfuscationEnvironment.java @@ -196,13 +196,13 @@ public abstract class ObfuscationEnvironment implements IObfuscationEnvironment } // See if we can get the superclass from the reference - TypeMirror superClass = type.getElement().getSuperclass(); - if (superClass.getKind() != TypeKind.DECLARED) { + TypeHandle superClass = type.getSuperclass(); + if (superClass == null) { return null; } // Well we found it, let's inflect the class name and recurse the search - String superClassName = ((TypeElement)((DeclaredType)superClass).asElement()).getQualifiedName().toString(); + String superClassName = superClass.getSimpleName(); return this.getObfMethod(method.move(superClassName.replace('.', '/'))); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java b/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java index 1cd2fbad..80c8e214 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/TargetMap.java @@ -132,16 +132,6 @@ public final class TargetMap extends HashMap> Set mixins = this.getMixinsFor(target); mixins.add(mixin); } - - /** - * Get mixin classes which target the specified class - * - * @param target Target class - * @return Collection of mixins registered as targetting the specified class - */ - Collection getMixinsTargeting(TypeElement target) { - return this.getMixinsTargeting(new TypeHandle(target)); - } /** * Get mixin classes which target the specified class diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java index 3b92cb08..11ed760e 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java @@ -44,6 +44,7 @@ import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.Bytecode; import org.spongepowered.asm.util.asm.IAnnotationHandle; +import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; import org.spongepowered.tools.obfuscation.mirror.mapping.MappingMethodResolvable; import com.google.common.collect.ImmutableList; @@ -71,6 +72,12 @@ public class TypeHandle { * Actual type element, this is null for inaccessible classes */ private final TypeElement element; + + /** + * Type handle provider, we need this since we have to resolve type handles + * for related classes (eg. superclass) without using mirror + */ + protected final ITypeHandleProvider typeProvider; /** * Reference to this handle, for serialisation @@ -84,10 +91,11 @@ public class TypeHandle { * @param pkg Package * @param name FQ class name */ - public TypeHandle(PackageElement pkg, String name) { + public TypeHandle(PackageElement pkg, String name, ITypeHandleProvider typeProvider) { this.name = name.replace('.', '/'); this.pkg = pkg; this.element = null; + this.typeProvider = typeProvider; } /** @@ -95,10 +103,11 @@ public class TypeHandle { * * @param element ze element */ - public TypeHandle(TypeElement element) { + public TypeHandle(TypeElement element, ITypeHandleProvider typeProvider) { this.pkg = TypeUtils.getPackage(element); this.name = TypeUtils.getInternalName(element); this.element = element; + this.typeProvider = typeProvider; } /** @@ -106,8 +115,8 @@ public class TypeHandle { * * @param type type */ - public TypeHandle(DeclaredType type) { - this((TypeElement)type.asElement()); + public TypeHandle(DeclaredType type, ITypeHandleProvider typeProvider) { + this((TypeElement)type.asElement(), typeProvider); } /* (non-Javadoc) @@ -215,7 +224,7 @@ public class TypeHandle { return null; } - return new TypeHandle((DeclaredType)superClass); + return typeProvider.getTypeHandle(superClass); } /** @@ -228,7 +237,7 @@ public class TypeHandle { Builder list = ImmutableList.builder(); for (TypeMirror iface : this.getTargetElement().getInterfaces()) { - list.add(new TypeHandle((DeclaredType)iface)); + list.add(typeProvider.getTypeHandle(iface)); } return list.build(); } @@ -275,6 +284,34 @@ public class TypeHandle { public boolean isSimulated() { return false; } + + /** + * Gets whether this handle definitely does not represent an interface + */ + public boolean isNotInterface() { + TypeElement target = this.getTargetElement(); + return target != null && !target.getKind().isInterface(); + } + + /** + * Gets whether this handle is the same as or a supertype of the other handle + */ + public boolean isAssignableFrom(TypeHandle other) { + if (this.getName().equals(other.getName())) { + return true; + } + List superTypes = new ArrayList<>(); + if (other.getSuperclass() != null) { + superTypes.add(other.getSuperclass()); + } + superTypes.addAll(other.getInterfaces()); + for (TypeHandle superType : superTypes) { + if (this.isAssignableFrom(superType)) { + return true; + } + } + return false; + } /** * Get the TypeReference for this type, used for serialisation diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java index aa745b40..00ae928f 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java @@ -76,12 +76,6 @@ public class TypeHandleASM extends TypeHandle { * ClassNode used for accessing information when not accessible via mirror */ private final ClassNode classNode; - - /** - * Type handle provider, we need this since we have to resolve type handles - * for related classes (eg. superclass) without using mirror - */ - private final ITypeHandleProvider typeProvider; /** * Ctor for imaginary elements, require the enclosing package and the FQ @@ -91,9 +85,8 @@ public class TypeHandleASM extends TypeHandle { * @param name FQ class name */ protected TypeHandleASM(PackageElement pkg, String name, ClassNode classNode, ITypeHandleProvider typeProvider) { - super(pkg, name); + super(pkg, name, typeProvider); this.classNode = classNode; - this.typeProvider = typeProvider; } /* (non-Javadoc) @@ -146,8 +139,10 @@ public class TypeHandleASM extends TypeHandle { */ @Override public TypeHandle getSuperclass() { - TypeHandle superClass = this.typeProvider.getTypeHandle(this.classNode.superName); - return superClass; + if (this.classNode.superName == null) { + return null; + } + return this.typeProvider.getTypeHandle(this.classNode.superName); } /* (non-Javadoc) @@ -196,6 +191,14 @@ public class TypeHandleASM extends TypeHandle { return false; } + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#isInterface() + */ + @Override + public boolean isNotInterface() { + return (this.classNode.access & Opcodes.ACC_INTERFACE) == 0; + } + /* (non-Javadoc) * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#findDescriptor * (org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName) @@ -272,7 +275,11 @@ public class TypeHandleASM extends TypeHandle { InputStream is = null; try { Filer filer = ap.getProcessingEnvironment().getFiler(); - is = filer.getResource(StandardLocation.CLASS_PATH, pkg.getQualifiedName(), name + ".class").openInputStream(); + try { + is = filer.getResource(StandardLocation.CLASS_PATH, pkg.getQualifiedName(), name + ".class").openInputStream(); + } catch (FileNotFoundException ignored) { + is = filer.getResource(StandardLocation.PLATFORM_CLASS_PATH, pkg.getQualifiedName(), name + ".class").openInputStream(); + } ClassNode classNode = new ClassNode(); new ClassReader(is).accept(classNode, 0); TypeHandleASM typeHandle = new TypeHandleASM(pkg, fqName, classNode, ap.getTypeProvider()); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java index e34c7298..bdb44612 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java @@ -35,6 +35,7 @@ import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; import org.spongepowered.asm.util.SignaturePrinter; +import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; /** * A simulated type handle, used with virtual (pseudo) mixins. For obfuscation @@ -48,12 +49,12 @@ public class TypeHandleSimulated extends TypeHandle { private final TypeElement simulatedType; - public TypeHandleSimulated(String name, TypeMirror type) { - this(TypeUtils.getPackage(type), name, type); + public TypeHandleSimulated(String name, TypeMirror type, ITypeHandleProvider typeProvider) { + this(TypeUtils.getPackage(type), name, type, typeProvider); } - public TypeHandleSimulated(PackageElement pkg, String name, TypeMirror type) { - super(pkg, name); + public TypeHandleSimulated(PackageElement pkg, String name, TypeMirror type, ITypeHandleProvider typeProvider) { + super(pkg, name, typeProvider); this.simulatedType = (TypeElement)((DeclaredType)type).asElement(); } @@ -152,14 +153,14 @@ public class TypeHandleSimulated extends TypeHandle { String rawSignature = TypeUtils.stripGenerics(signature); // Try to locate a member anywhere in the hierarchy which matches - MethodHandle method = TypeHandleSimulated.findMethodRecursive(this, name, signature, rawSignature, true); + MethodHandle method = TypeHandleSimulated.findMethodRecursive(this, name, signature, rawSignature, true, this.typeProvider); // If we find one, return it otherwise just simulate the method return method != null ? method.asMapping(true) : super.getMappingMethod(name, desc); } - private static MethodHandle findMethodRecursive(TypeHandle target, String name, String signature, String rawSignature, boolean matchCase) { + private static MethodHandle findMethodRecursive(TypeHandle target, String name, String signature, String rawSignature, boolean matchCase, ITypeHandleProvider typeProvider) { TypeElement elem = target.getTargetElement(); if (elem == null) { return null; @@ -171,7 +172,7 @@ public class TypeHandleSimulated extends TypeHandle { } for (TypeMirror iface : elem.getInterfaces()) { - method = TypeHandleSimulated.findMethodRecursive(iface, name, signature, rawSignature, matchCase); + method = TypeHandleSimulated.findMethodRecursive(iface, name, signature, rawSignature, matchCase, typeProvider); if (method != null) { return method; } @@ -182,15 +183,14 @@ public class TypeHandleSimulated extends TypeHandle { return null; } - return TypeHandleSimulated.findMethodRecursive(superClass, name, signature, rawSignature, matchCase); + return TypeHandleSimulated.findMethodRecursive(superClass, name, signature, rawSignature, matchCase, typeProvider); } - private static MethodHandle findMethodRecursive(TypeMirror target, String name, String signature, String rawSignature, boolean matchCase) { + private static MethodHandle findMethodRecursive(TypeMirror target, String name, String signature, String rawSignature, boolean matchCase, ITypeHandleProvider typeProvider) { if (!(target instanceof DeclaredType)) { return null; } - TypeElement element = (TypeElement)((DeclaredType)target).asElement(); - return TypeHandleSimulated.findMethodRecursive(new TypeHandle(element), name, signature, rawSignature, matchCase); + return TypeHandleSimulated.findMethodRecursive(typeProvider.getTypeHandle(target), name, signature, rawSignature, matchCase, typeProvider); } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeReference.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeReference.java index e2a0b979..41ea5f45 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeReference.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeReference.java @@ -24,6 +24,8 @@ */ package org.spongepowered.tools.obfuscation.mirror; +import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; + import java.io.Serializable; import javax.annotation.processing.ProcessingEnvironment; @@ -82,15 +84,14 @@ public class TypeReference implements Serializable, Comparable { /** * Fetch or attempt to generate the type handle * - * @param processingEnv environment to create handle if it needs to be + * @param typeHandleProvider provider to create handle if it needs to be * regenerated * @return type handle */ - public TypeHandle getHandle(ProcessingEnvironment processingEnv) { + public TypeHandle getHandle(ITypeHandleProvider typeHandleProvider) { if (this.handle == null) { - TypeElement element = processingEnv.getElementUtils().getTypeElement(this.getClassName()); try { - this.handle = new TypeHandle(element); + this.handle = typeHandleProvider.getTypeHandle(this.name); } catch (Exception ex) { // Class probably doesn't exist in scope :/ ex.printStackTrace(); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java index 3db43e90..d0a3cafa 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -29,9 +29,6 @@ import java.util.Collection; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeKind; -import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; @@ -94,8 +91,7 @@ public class TargetValidator extends MixinValidator { } for (TypeHandle target : targets) { - TypeElement targetType = target.getElement(); - if (targetType != null && !(targetType.getKind() == ElementKind.INTERFACE)) { + if (target != null && target.isNotInterface()) { this.messager.printMessage(MessageType.TARGET_VALIDATOR, "Targetted type '" + target + " of " + mixin + " is not an interface", mixin); } @@ -103,54 +99,27 @@ public class TargetValidator extends MixinValidator { } private void validateClassMixin(TypeElement mixin, Collection targets) { - TypeMirror superClass = mixin.getSuperclass(); + TypeHandle superClass = this.typeHandleProvider.getTypeHandle(TypeUtils.getInternalName(mixin)).getSuperclass(); for (TypeHandle target : targets) { - TypeMirror targetType = target.getTypeMirror(); - if (targetType != null && !this.validateSuperClass(targetType, superClass)) { + if (target != null && !this.validateSuperClass(target, superClass)) { this.messager.printMessage(MessageType.TARGET_VALIDATOR, "Superclass " + superClass + " of " + mixin - + " was not found in the hierarchy of target class " + targetType, mixin); + + " was not found in the hierarchy of target class " + target, mixin); } } } - private boolean validateSuperClass(TypeMirror targetType, TypeMirror superClass) { - if (TypeUtils.isAssignable(this.processingEnv, targetType, superClass)) { - return true; - } - - return this.validateSuperClassRecursive(targetType, superClass); + private boolean validateSuperClass(TypeHandle targetType, TypeHandle superClass) { + return superClass.isAssignableFrom(targetType) || this.checkMixinsFor(targetType, superClass); } - private boolean validateSuperClassRecursive(TypeMirror targetType, TypeMirror superClass) { - if (!(targetType instanceof DeclaredType)) { - return false; - } - - if (TypeUtils.isAssignable(this.processingEnv, targetType, superClass)) { - return true; - } - - TypeElement targetElement = (TypeElement)((DeclaredType)targetType).asElement(); - TypeMirror targetSuper = targetElement.getSuperclass(); - if (targetSuper.getKind() == TypeKind.NONE) { - return false; - } - - if (this.checkMixinsFor(targetSuper, superClass)) { - return true; - } - - return this.validateSuperClassRecursive(targetSuper, superClass); - } - - private boolean checkMixinsFor(TypeMirror targetType, TypeMirror superClass) { - for (TypeMirror mixinType : this.getMixinsTargeting(targetType)) { - if (TypeUtils.isAssignable(this.processingEnv, mixinType, superClass)) { + private boolean checkMixinsFor(TypeHandle targetType, TypeHandle superClass) { + for (TypeHandle mixinType : this.getMixinsTargeting(targetType)) { + if (mixinType.isAssignableFrom(superClass)) { return true; } } - return false; + return targetType.getSuperclass() != null && this.checkMixinsFor(targetType.getSuperclass(), superClass); } } From 2e4bf962e9eaf85adee1865b1bdad9805f1befb2 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Sun, 24 Jul 2022 13:46:04 +0100 Subject: [PATCH 3/8] Fix resolution of upper bounds of intersection types. (cherry picked from commit c90298501502d9b23634345ccaf293470a0a6465) --- .../spongepowered/tools/obfuscation/mirror/TypeUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java index cbc88820..47242bbd 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java @@ -37,6 +37,7 @@ import javax.lang.model.element.TypeParameterElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.ArrayType; import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.IntersectionType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; @@ -448,6 +449,10 @@ public abstract class TypeUtils { throw new IllegalStateException("Generic symbol \"" + type + "\" is too complex, exceeded " + TypeUtils.MAX_GENERIC_RECURSION_DEPTH + " iterations attempting to determine upper bound"); } + if (type instanceof IntersectionType) { + TypeMirror first = ((IntersectionType) type).getBounds().get(0); + return TypeUtils.getUpperBound0(first, --depth); + } if (type instanceof DeclaredType) { return (DeclaredType)type; } From be3b94df6adf6b60566d4332640c6624037f9621 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Thu, 16 Feb 2023 20:10:24 +0000 Subject: [PATCH 4/8] AP: Skip superclass validation for imaginary and simulated targets. (cherry picked from commit 0fb3116c4f7143a46a0a2706cc6e7a43484aaafd) --- .../tools/obfuscation/validation/TargetValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java index d0a3cafa..d86661bd 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -110,7 +110,7 @@ public class TargetValidator extends MixinValidator { } private boolean validateSuperClass(TypeHandle targetType, TypeHandle superClass) { - return superClass.isAssignableFrom(targetType) || this.checkMixinsFor(targetType, superClass); + return targetType.isImaginary() || targetType.isSimulated() || superClass.isAssignableFrom(targetType) || this.checkMixinsFor(targetType, superClass); } private boolean checkMixinsFor(TypeHandle targetType, TypeHandle superClass) { From ebc72cf7827ff4219d417074357a2012799a0b75 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Thu, 16 Feb 2023 20:10:24 +0000 Subject: [PATCH 5/8] AP: Improve validation of mixin superclasses. Extending an already-compiled mixin no longer results in an error, and extending (a mixin that targets) your target class does result in an error. (cherry picked from commit 7f7dbc1f37c7290bb25ac4ab07f8c5fa1534f1f8) --- .../tools/obfuscation/mirror/TypeHandle.java | 9 +++------ .../validation/TargetValidator.java | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java index 11ed760e..5c2f1e6a 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java @@ -294,19 +294,16 @@ public class TypeHandle { } /** - * Gets whether this handle is the same as or a supertype of the other handle + * Gets whether this handle is a supertype of the other handle */ - public boolean isAssignableFrom(TypeHandle other) { - if (this.getName().equals(other.getName())) { - return true; - } + public boolean isSuperTypeOf(TypeHandle other) { List superTypes = new ArrayList<>(); if (other.getSuperclass() != null) { superTypes.add(other.getSuperclass()); } superTypes.addAll(other.getInterfaces()); for (TypeHandle superType : superTypes) { - if (this.isAssignableFrom(superType)) { + if (this.name.equals(superType.name) || this.isSuperTypeOf(superType)) { return true; } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java index d86661bd..00a0e41d 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -30,6 +30,7 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; +import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.util.asm.IAnnotationHandle; @@ -110,16 +111,21 @@ public class TargetValidator extends MixinValidator { } private boolean validateSuperClass(TypeHandle targetType, TypeHandle superClass) { - return targetType.isImaginary() || targetType.isSimulated() || superClass.isAssignableFrom(targetType) || this.checkMixinsFor(targetType, superClass); + return targetType.isImaginary() || targetType.isSimulated() || superClass.isSuperTypeOf(targetType) || this.checkMixinsFor(targetType, superClass); } - private boolean checkMixinsFor(TypeHandle targetType, TypeHandle superClass) { - for (TypeHandle mixinType : this.getMixinsTargeting(targetType)) { - if (mixinType.isAssignableFrom(superClass)) { + private boolean checkMixinsFor(TypeHandle targetType, TypeHandle superMixin) { + IAnnotationHandle annotation = superMixin.getAnnotation(Mixin.class); + for (Object target : annotation.getList()) { + if (this.typeHandleProvider.getTypeHandle(target).isSuperTypeOf(targetType)) { return true; } } - - return targetType.getSuperclass() != null && this.checkMixinsFor(targetType.getSuperclass(), superClass); + for (String target : annotation.getList("targets")) { + if (this.typeHandleProvider.getTypeHandle(target).isSuperTypeOf(targetType)) { + return true; + } + } + return false; } } From 74c25d25ea841e286d79369b54111264ff6466c7 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Fri, 24 Feb 2023 22:24:59 +0000 Subject: [PATCH 6/8] AP: Fix and simplify checking of factory invoker return types. We no longer use mirror. The generics are irrelevant in the first place because Mixin does not check them at runtime. (cherry picked from commit ccfb5ee089629ce9c6f4137a0ef77aa1efc101ef) --- .../AnnotatedMixinElementHandlerAccessor.java | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java index d8dc4862..d5bfd802 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java @@ -46,8 +46,6 @@ import org.spongepowered.tools.obfuscation.mirror.FieldHandle; import org.spongepowered.tools.obfuscation.mirror.MethodHandle; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; import org.spongepowered.tools.obfuscation.mirror.TypeUtils; -import org.spongepowered.tools.obfuscation.mirror.TypeUtils.Equivalency; -import org.spongepowered.tools.obfuscation.mirror.TypeUtils.EquivalencyResult; import com.google.common.base.Strings; @@ -304,19 +302,11 @@ class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementHandler } private void registerFactoryForTarget(AnnotatedElementInvoker elem, TypeHandle target) { - EquivalencyResult equivalency = TypeUtils.isEquivalentType(this.ap.getProcessingEnvironment(), elem.getReturnType(), target.getTypeMirror()); - if (equivalency.type != Equivalency.EQUIVALENT) { - if (equivalency.type == Equivalency.EQUIVALENT_BUT_RAW && equivalency.rawType == 1) { - elem.printMessage(this.ap, MessageType.INVOKER_RAW_RETURN_TYPE, "Raw return type for Factory @Invoker", SuppressedBy.RAW_TYPES); - } else if (equivalency.type == Equivalency.BOUNDS_MISMATCH) { - elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_GENERIC_ARGS, "Invalid Factory @Invoker return type, generic type args of " - + target.getTypeMirror() + " are incompatible with " + elem.getReturnType() + ". " + equivalency); - return; - } else { - elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_RETURN_TYPE, "Invalid Factory @Invoker return type, expected " - + target.getTypeMirror() + " but found " + elem.getReturnType()); - return; - } + String returnType = TypeUtils.getTypeName(elem.getReturnType()); + if (!returnType.equals(target.toString())) { + elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_RETURN_TYPE, "Invalid Factory @Invoker return type, expected " + + target + " but found " + returnType); + return; } if (!elem.isStatic()) { elem.printMessage(this.ap, MessageType.FACTORY_INVOKER_NONSTATIC, "Factory @Invoker must be static"); From d633516a277b7704be6f5e2babf429d75b403814 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Sat, 12 Aug 2023 17:42:00 +0100 Subject: [PATCH 7/8] Bump AP to Java 8. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index c73873f2..756b2699 100644 --- a/build.gradle +++ b/build.gradle @@ -94,7 +94,7 @@ sourceSets { ap { compileClasspath += main.output ext.languageVersion = 8 - ext.compatibility = '1.6' + ext.compatibility = '1.8' } fernflower { compileClasspath += main.output From 1edd5f10cbb624058b968509c6a88e5fd971d4f8 Mon Sep 17 00:00:00 2001 From: LlamaLad7 Date: Sat, 12 Aug 2023 17:54:26 +0100 Subject: [PATCH 8/8] Fix NPE when resolving fields in `TypeHandleASM`. Closes https://github.com/SpongePowered/Mixin/issues/560. --- .../org/spongepowered/tools/obfuscation/mirror/TypeUtils.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java index 47242bbd..709a6eaf 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java @@ -275,6 +275,9 @@ public abstract class TypeUtils { * @return java signature */ public static String getJavaSignature(String descriptor) { + if (!descriptor.contains("(")) { + return SignaturePrinter.getTypeName(org.objectweb.asm.Type.getType(descriptor), false, true); + } return new SignaturePrinter("", descriptor).setFullyQualified(true).toDescriptor(); }