From 1e1aa7fb52dec78630f3f2f53fd70a4c496a7d66 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Thu, 4 Nov 2021 20:33:03 +0000 Subject: [PATCH] Resolve anonymous inner classes in AP via classpath + ASM where possible Fixes #518 --- .../tools/obfuscation/AnnotatedMixin.java | 61 ++-- .../AnnotatedMixinElementHandler.java | 7 +- .../AnnotatedMixinElementHandlerAccessor.java | 6 +- ...AnnotatedMixinElementHandlerOverwrite.java | 7 +- ...atedMixinElementHandlerSoftImplements.java | 13 +- .../tools/obfuscation/AnnotatedMixins.java | 46 ++- .../tools/obfuscation/MixinValidator.java | 6 +- .../obfuscation/interfaces/IMessagerEx.java | 8 +- .../interfaces/IMixinValidator.java | 4 +- .../interfaces/ITypeHandleProvider.java | 9 + .../obfuscation/mirror/AnnotationHandle.java | 7 + .../tools/obfuscation/mirror/FieldHandle.java | 9 + .../{Visibility.java => FieldHandleASM.java} | 29 +- .../obfuscation/mirror/MemberHandle.java | 1 + .../obfuscation/mirror/MethodHandle.java | 10 + .../obfuscation/mirror/MethodHandleASM.java | 56 ++++ .../tools/obfuscation/mirror/TypeHandle.java | 56 +++- .../obfuscation/mirror/TypeHandleASM.java | 302 ++++++++++++++++++ .../mirror/TypeHandleSimulated.java | 2 +- .../tools/obfuscation/mirror/TypeUtils.java | 4 + .../validation/ParentValidator.java | 4 +- .../validation/TargetValidator.java | 9 +- .../spongepowered/asm/util/Annotations.java | 10 + .../asm/util/asm/IAnnotationHandle.java | 19 ++ 24 files changed, 606 insertions(+), 79 deletions(-) rename src/ap/java/org/spongepowered/tools/obfuscation/mirror/{Visibility.java => FieldHandleASM.java} (68%) create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandleASM.java create mode 100644 src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index e3487136..f3b918a8 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -33,8 +33,6 @@ import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.MixinEnvironment.Option; @@ -62,6 +60,7 @@ import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager; import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; import org.spongepowered.tools.obfuscation.mapping.IMappingConsumer; import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; +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.struct.InjectorRemap; @@ -76,7 +75,7 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { /** * Mixin annotation */ - private final AnnotationHandle annotation; + private final IAnnotationHandle annotation; /** * Messager @@ -106,7 +105,7 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { /** * Methods */ - private final List methods; + private final List methods; /** * Mixin class @@ -178,7 +177,7 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { this.messager = ap; this.mixin = type; this.handle = new TypeHandle(type); - this.methods = new ArrayList(this.handle.getEnclosedElements(ElementKind.METHOD)); + this.methods = new ArrayList(this.handle.getMethods()); this.virtual = this.handle.getAnnotation(Pseudo.class).exists(); this.annotation = this.handle.getAnnotation(Mixin.class); this.classRef = TypeUtils.getInternalName(type); @@ -212,9 +211,9 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { // Public targets, referenced by class try { - for (TypeMirror target : this.annotation.getList()) { - TypeHandle type = new TypeHandle((DeclaredType)target); - if (this.targets.contains(type)) { + for (Object target : this.annotation.getList()) { + TypeHandle type = this.typeProvider.getTypeHandle(target); + if (type == null || this.targets.contains(type)) { continue; } this.addTarget(type); @@ -237,16 +236,24 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { type = this.typeProvider.getSimulatedHandle(softTarget, this.mixin.asType()); } else if (type == null) { this.printMessage(MessageType.MIXIN_SOFT_TARGET_NOT_FOUND, "Mixin target " + softTarget + " could not be found"); - return null; + if (MessageType.MIXIN_SOFT_TARGET_NOT_FOUND.isError()) { + return null; + } + type = this.typeProvider.getSimulatedHandle(softTarget, this.mixin.asType()); } else if (type.isImaginary()) { this.printMessage(MessageType.MIXIN_SOFT_TARGET_NOT_RESOLVED, "Mixin target " + softTarget + " could not be fully resolved.", SuppressedBy.UNRESOLVABLE_TARGET); - return null; + if (MessageType.MIXIN_SOFT_TARGET_NOT_RESOLVED.isError()) { + return null; + } } else if (type.isPublic()) { SuppressedBy suppressedBy = (type.getPackage().isUnnamed()) ? SuppressedBy.DEFAULT_PACKAGE : SuppressedBy.PUBLIC_TARGET; + String must = MessageType.MIXIN_SOFT_TARGET_IS_PUBLIC.isError() ? "must" : "should"; this.printMessage(MessageType.MIXIN_SOFT_TARGET_IS_PUBLIC, "Mixin target " + softTarget - + " is public and must be specified in value", suppressedBy); - return null; + + " is public and " + must + " be specified in value", suppressedBy); + if (MessageType.MIXIN_SOFT_TARGET_IS_PUBLIC.isError()) { + return null; + } } this.addSoftTarget(type, softTarget); if (primaryTarget == null) { @@ -268,14 +275,14 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { * Print a message to the AP messager */ private void printMessage(MessageType type, CharSequence msg) { - this.messager.printMessage(type, msg, this.mixin, this.annotation.asMirror()); + this.messager.printMessage(type, msg, this.mixin, AnnotationHandle.asMirror(this.annotation)); } /** * Print a suppressible message to the AP messager */ private void printMessage(MessageType type, CharSequence msg, SuppressedBy suppressedBy) { - this.messager.printMessage(type, msg, this.mixin, this.annotation.asMirror(), suppressedBy); + this.messager.printMessage(type, msg, this.mixin, AnnotationHandle.asMirror(this.annotation), suppressedBy); } private void addSoftTarget(TypeHandle type, String reference) { @@ -296,7 +303,7 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { return this.mixin.getSimpleName().toString(); } - public AnnotationHandle getAnnotation() { + public IAnnotationHandle getAnnotation() { return this.annotation; } @@ -363,13 +370,25 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { } private void runFinalValidation() { - for (ExecutableElement method : this.methods) { + for (MethodHandle method : this.methods) { this.overwrites.registerMerge(method); } } + private void removeMethod(ExecutableElement method) { + MethodHandle handle = null; + for (MethodHandle methodHandle : this.methods) { + if (methodHandle.getElement() == method) { + handle = methodHandle; + } + } + if (handle != null) { + this.methods.remove(handle); + } + } + public void registerOverwrite(ExecutableElement method, AnnotationHandle overwrite, boolean shouldRemap) { - this.methods.remove(method); + this.removeMethod(method); this.overwrites.registerOverwrite(new AnnotatedElementOverwrite(method, overwrite, shouldRemap)); } @@ -378,12 +397,12 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { } public void registerShadow(ExecutableElement method, AnnotationHandle shadow, boolean shouldRemap) { - this.methods.remove(method); + this.removeMethod(method); this.shadows.registerShadow(this.shadows.new AnnotatedElementShadowMethod(method, shadow, shouldRemap)); } public void registerInjector(ExecutableElement method, AnnotationHandle inject, InjectorRemap remap) { - this.methods.remove(method); + this.removeMethod(method); AnnotatedElementInjector injectorElement = new AnnotatedElementInjector(method, inject, this, remap); this.injectors.registerInjector(injectorElement); @@ -426,12 +445,12 @@ class AnnotatedMixin implements IMixinContext, IAnnotatedElement { } public void registerAccessor(ExecutableElement element, AnnotationHandle accessor, boolean shouldRemap) { - this.methods.remove(element); + this.removeMethod(element); this.accessors.registerAccessor(new AnnotatedElementAccessor(element, accessor, this, shouldRemap)); } public void registerInvoker(ExecutableElement element, AnnotationHandle invoker, boolean shouldRemap) { - this.methods.remove(element); + this.removeMethod(element); this.accessors.registerAccessor(new AnnotatedElementInvoker(element, invoker, this, shouldRemap)); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java index 75928304..2e7c7415 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandler.java @@ -38,6 +38,7 @@ import org.spongepowered.asm.mixin.refmap.IMixinContext; import org.spongepowered.asm.obfuscation.mapping.IMapping; import org.spongepowered.asm.obfuscation.mapping.common.MappingField; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.asm.util.Bytecode.Visibility; import org.spongepowered.asm.util.ConstraintParser; import org.spongepowered.asm.util.ConstraintParser.Constraint; import org.spongepowered.asm.util.asm.IAnnotatedElement; @@ -55,7 +56,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.Visibility; /** * Base class for module for {@link AnnotatedMixin} which handle different @@ -194,6 +194,11 @@ abstract class AnnotatedMixinElementHandler { this.aliases = annotation.getList("aliases"); } + public AliasedElementName(MethodHandle method, AnnotationHandle annotation) { + this.originalName = method.getName(); + this.aliases = annotation.getList("aliases"); + } + public AliasedElementName setCaseSensitive(boolean caseSensitive) { this.caseSensitive = caseSensitive; return this; diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java index e6fada40..d8dc4862 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerAccessor.java @@ -304,17 +304,17 @@ class AnnotatedMixinElementHandlerAccessor extends AnnotatedMixinElementHandler } private void registerFactoryForTarget(AnnotatedElementInvoker elem, TypeHandle target) { - EquivalencyResult equivalency = TypeUtils.isEquivalentType(this.ap.getProcessingEnvironment(), elem.getReturnType(), target.getType()); + 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.getType() + " are incompatible with " + elem.getReturnType() + ". " + equivalency); + + 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.getType() + " but found " + elem.getReturnType()); + + target.getTypeMirror() + " but found " + elem.getReturnType()); return; } } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java index dc4b9d49..568648f8 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerOverwrite.java @@ -34,6 +34,7 @@ import org.spongepowered.tools.obfuscation.Mappings.MappingConflictException; import org.spongepowered.tools.obfuscation.interfaces.IMessagerEx.MessageType; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; +import org.spongepowered.tools.obfuscation.mirror.MethodHandle; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -63,8 +64,10 @@ class AnnotatedMixinElementHandlerOverwrite extends AnnotatedMixinElementHandler super(ap, mixin); } - public void registerMerge(ExecutableElement method) { - this.validateTargetMethod(method, null, new AliasedElementName(method, AnnotationHandle.MISSING), "overwrite", true, true); + public void registerMerge(MethodHandle method) { + if (!method.isImaginary()) { + this.validateTargetMethod(method.getElement(), null, new AliasedElementName(method, AnnotationHandle.MISSING), "overwrite", true, true); + } } public void registerOverwrite(AnnotatedElementOverwrite elem) { diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java index 8f9008ae..f9e7dfb2 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixinElementHandlerSoftImplements.java @@ -26,8 +26,6 @@ package org.spongepowered.tools.obfuscation; import java.util.List; -import javax.lang.model.element.ElementKind; -import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.DeclaredType; import org.spongepowered.asm.mixin.Interface.Remap; @@ -37,7 +35,6 @@ import org.spongepowered.tools.obfuscation.interfaces.IMessagerEx.MessageType; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; import org.spongepowered.tools.obfuscation.mirror.MethodHandle; -import org.spongepowered.tools.obfuscation.mirror.TypeUtils; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -99,7 +96,7 @@ class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinElementHa * @param prefix Prefix declared in the soft-implements decoration */ private void processSoftImplements(Remap remap, TypeHandle iface, String prefix) { - for (ExecutableElement method : iface.getEnclosedElements(ElementKind.METHOD)) { + for (MethodHandle method : iface.getMethods()) { this.processMethod(remap, iface, prefix, method); } @@ -118,10 +115,10 @@ class AnnotatedMixinElementHandlerSoftImplements extends AnnotatedMixinElementHa * @param prefix Prefix declared in the soft-implements decoration * @param method Interface method to search for */ - private void processMethod(Remap remap, TypeHandle iface, String prefix, ExecutableElement method) { - String name = method.getSimpleName().toString(); - String sig = TypeUtils.getJavaSignature(method); - String desc = TypeUtils.getDescriptor(method); + private void processMethod(Remap remap, TypeHandle iface, String prefix, MethodHandle method) { + String name = method.getName(); + String sig = method.getJavaSignature(); + String desc = method.getDesc(); if (remap != Remap.ONLY_PREFIXED) { MethodHandle mixinMethod = this.mixin.getHandle().findMethod(name, sig); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 7e1e7ae3..0835ead5 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -54,6 +54,7 @@ import javax.tools.Diagnostic.Kind; import javax.tools.FileObject; import javax.tools.StandardLocation; +import org.objectweb.asm.Type; import org.spongepowered.asm.launch.MixinBootstrap; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.gen.Accessor; @@ -69,6 +70,7 @@ import org.spongepowered.tools.obfuscation.interfaces.IObfuscationManager; import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; 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.struct.InjectorRemap; @@ -377,8 +379,8 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider for (TypeReference mixin : this.targets.getMixinsTargeting(targetType)) { TypeHandle handle = mixin.getHandle(this.processingEnv); - if (handle != null) { - minions.add(handle.getType()); + if (handle != null && handle.hasTypeMirror()) { + minions.add(handle.getTypeMirror()); } } @@ -697,13 +699,43 @@ final class AnnotatedMixins implements IMixinAnnotationProcessor, ITokenProvider int lastDotPos = name.lastIndexOf('.'); if (lastDotPos > -1) { - String pkg = name.substring(0, lastDotPos); - PackageElement packageElement = elements.getPackageElement(pkg); - if (packageElement != null) { - return new TypeHandle(packageElement, name); + 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); } } - + + return null; + } + + /** + * Get a TypeHandle representing the supplied type in the current processing + * environment + */ + @Override + public TypeHandle getTypeHandle(Object type) { + if (type instanceof TypeHandle) { + return (TypeHandle)type; + } else if (type instanceof DeclaredType) { + return new TypeHandle((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()); + } else if (type instanceof String) { + return this.getTypeHandle(type.toString()); + } return null; } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java index b2ba7af0..ab10529a 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/MixinValidator.java @@ -30,11 +30,11 @@ 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.mirror.AnnotationHandle; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -84,7 +84,7 @@ public abstract class MixinValidator implements IMixinValidator { * java.util.Collection) */ @Override - public final boolean validate(ValidationPass pass, TypeElement mixin, AnnotationHandle annotation, Collection targets) { + public final boolean validate(ValidationPass pass, TypeElement mixin, IAnnotationHandle annotation, Collection targets) { if (pass != this.pass) { return true; } @@ -92,7 +92,7 @@ public abstract class MixinValidator implements IMixinValidator { return this.validate(mixin, annotation, targets); } - protected abstract boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection targets); + protected abstract boolean validate(TypeElement mixin, IAnnotationHandle annotation, Collection targets); protected final Collection getMixinsTargeting(TypeMirror targetType) { return AnnotatedMixins.getMixinsForEnvironment(this.processingEnv).getMixinsTargeting(targetType); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerEx.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerEx.java index 25713f78..85df92ec 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerEx.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMessagerEx.java @@ -25,7 +25,6 @@ package org.spongepowered.tools.obfuscation.interfaces; import java.util.HashSet; -import java.util.Locale; import java.util.Set; import javax.annotation.processing.Messager; @@ -304,6 +303,13 @@ public interface IMessagerEx extends Messager { this.originalKind = this.kind = kind; } + /** + * Get whether the message is treated as an error + */ + public boolean isError() { + return this.kind == Kind.ERROR; + } + /** * Get the current message kind */ diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java index 6a4b5424..6a12b0d4 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/IMixinValidator.java @@ -28,7 +28,7 @@ import java.util.Collection; import javax.lang.model.element.TypeElement; -import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -56,6 +56,6 @@ public interface IMixinValidator { * * @return False to halt processing of further validators */ - public abstract boolean validate(ValidationPass pass, TypeElement mixin, AnnotationHandle annotation, Collection targets); + public abstract boolean validate(ValidationPass pass, TypeElement mixin, IAnnotationHandle annotation, Collection targets); } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java index c7caaa0e..e5bd62b9 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/interfaces/ITypeHandleProvider.java @@ -40,6 +40,15 @@ public interface ITypeHandleProvider { * @return A new type handle or null if the type could not be found */ public abstract TypeHandle getTypeHandle(String name); + + /** + * Get a type handle for the specified type + * + * @param type Type source, can be a String, TypeMirror, TypeElement or an + * existing TypeHandle + * @return TypeHandle + */ + public abstract TypeHandle getTypeHandle(Object type); /** * Generate a type handle for the specified type, simulate the target using diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java index 638c0ad2..6b2c9f63 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/AnnotationHandle.java @@ -164,6 +164,7 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param defaultValue default value to return if value is not present * @return value or default if not present or not set */ + @Override public boolean getBoolean(String key, boolean defaultValue) { return this.getValue(key, Boolean.valueOf(defaultValue)).booleanValue(); } @@ -195,6 +196,7 @@ public final class AnnotationHandle implements IAnnotationHandle { * @param list element duck type * @return list of values */ + @Override public List getList() { return this.getList("value"); } @@ -306,6 +308,10 @@ public final class AnnotationHandle implements IAnnotationHandle { return null; } + + public static AnnotationMirror asMirror(IAnnotationHandle handle) { + return handle instanceof AnnotationHandle ? ((AnnotationHandle)handle).asMirror() : null; + } /** * Returns a new annotation handle for the supplied annotation mirror @@ -329,4 +335,5 @@ public final class AnnotationHandle implements IAnnotationHandle { public static AnnotationHandle of(Element elem, Class annotationClass) { return new AnnotationHandle(AnnotationHandle.getAnnotation(elem, annotationClass)); } + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java index 746f8dea..b799022f 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandle.java @@ -28,6 +28,7 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import org.spongepowered.asm.obfuscation.mapping.common.MappingField; +import org.spongepowered.asm.util.Bytecode.Visibility; import com.google.common.base.Strings; @@ -63,10 +64,18 @@ public class FieldHandle extends MemberHandle { this(owner, element, rawType, TypeUtils.getName(element), TypeUtils.getInternalName(element)); } + public FieldHandle(TypeHandle owner, String name, String desc) { + this(owner, null, false, name, desc); + } + public FieldHandle(String owner, String name, String desc) { this(owner, null, false, name, desc); } + public FieldHandle(TypeHandle owner, VariableElement element, boolean rawType, String name, String desc) { + this(owner != null ? owner.getName() : null, element, rawType, name, desc); + } + private FieldHandle(String owner, VariableElement element, boolean rawType, String name, String desc) { super(owner, name, desc); this.element = element; diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/Visibility.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandleASM.java similarity index 68% rename from src/ap/java/org/spongepowered/tools/obfuscation/mirror/Visibility.java rename to src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandleASM.java index 86a2b174..bbdd20ab 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/Visibility.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/FieldHandleASM.java @@ -24,15 +24,28 @@ */ package org.spongepowered.tools.obfuscation.mirror; +import org.objectweb.asm.tree.FieldNode; +import org.spongepowered.asm.util.Bytecode; +import org.spongepowered.asm.util.Bytecode.Visibility; + /** - * Ordinal member visibility level. This is used to represent visibility of - * a member in a formal way from lowest to highest. + * A {@link FieldHandle} for classpath classes being read with ASM */ -public enum Visibility { - - PRIVATE, - PROTECTED, - PACKAGE, - PUBLIC +public class FieldHandleASM extends FieldHandle { + + /** + * The field from ASM + */ + private final FieldNode field; + + public FieldHandleASM(TypeHandle owner, FieldNode field) { + super(owner, field.name, field.desc); + this.field = field; + } + @Override + public Visibility getVisibility() { + return Bytecode.getVisibility(this.field); + } + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java index b886b723..8e7f8c38 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MemberHandle.java @@ -25,6 +25,7 @@ package org.spongepowered.tools.obfuscation.mirror; import org.spongepowered.asm.obfuscation.mapping.IMapping; +import org.spongepowered.asm.util.Bytecode.Visibility; /** * Abstract base class for element handles diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java index 29f1cfc0..f51358de 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandle.java @@ -27,6 +27,7 @@ package org.spongepowered.tools.obfuscation.mirror; import javax.lang.model.element.ExecutableElement; import org.spongepowered.asm.obfuscation.mapping.common.MappingMethod; +import org.spongepowered.asm.util.Bytecode.Visibility; import org.spongepowered.tools.obfuscation.mirror.mapping.MappingMethodResolvable; import com.google.common.base.Strings; @@ -74,6 +75,14 @@ public class MethodHandle extends MemberHandle { return this.element; } + /** + * Get the java signature (similar to descriptor but with java types in + * source form) for this element + */ + public String getJavaSignature() { + return TypeUtils.getJavaSignature(this.element); + } + @Override public Visibility getVisibility() { return TypeUtils.getVisibility(this.element); @@ -97,4 +106,5 @@ public class MethodHandle extends MemberHandle { String desc = Strings.nullToEmpty(this.getDesc()); return String.format("%s%s%s", owner, name, desc); } + } diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandleASM.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandleASM.java new file mode 100644 index 00000000..1d617148 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/MethodHandleASM.java @@ -0,0 +1,56 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.tools.obfuscation.mirror; + +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.util.Bytecode; +import org.spongepowered.asm.util.Bytecode.Visibility; + +/** + * A {@link MethodHandle} for classpath classes being read with ASM + */ +public class MethodHandleASM extends MethodHandle { + + /** + * The method from ASM + */ + private final MethodNode method; + + public MethodHandleASM(TypeHandle owner, MethodNode method) { + super(owner, method.name, method.desc); + this.method = method; + } + + @Override + public String getJavaSignature() { + return TypeUtils.getJavaSignature(this.method.desc); + } + + @Override + public Visibility getVisibility() { + return Bytecode.getVisibility(this.method); + } + +} 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 fcc7b2f7..3b92cb08 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandle.java @@ -25,6 +25,7 @@ package org.spongepowered.tools.obfuscation.mirror; import java.lang.annotation.Annotation; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -42,6 +43,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.Bytecode; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.mirror.mapping.MappingMethodResolvable; import com.google.common.collect.ImmutableList; @@ -159,14 +161,14 @@ public class TypeHandle { * @return new annotation handle, call exists on the returned * handle to determine whether the annotation is present */ - public AnnotationHandle getAnnotation(Class annotationClass) { + public IAnnotationHandle getAnnotation(Class annotationClass) { return AnnotationHandle.of(this.getTargetElement(), annotationClass); } /** * Returns enclosed elements (methods, fields, etc.) */ - public final List getEnclosedElements() { + protected final List getEnclosedElements() { return TypeHandle.getEnclosedElements(this.getTargetElement()); } @@ -176,15 +178,25 @@ public class TypeHandle { * @param kind types of element to return * @param list element type */ - public List getEnclosedElements(ElementKind... kind) { + protected List getEnclosedElements(ElementKind... kind) { return TypeHandle.getEnclosedElements(this.getTargetElement(), kind); } + /** + * Get whether the type handle can return a type mirror for the represented + * type. This is true only for types returned by mirror and is not available + * for simulated types or classpath types inaccessible via mirror (eg. anon + * classes) + */ + public boolean hasTypeMirror() { + return this.getTargetElement() != null; + } + /** * Returns the enclosed element as a type mirror, or null if this is an * imaginary type */ - public TypeMirror getType() { + public TypeMirror getTypeMirror() { return this.getTargetElement() != null ? this.getTargetElement().asType() : null; } @@ -218,9 +230,20 @@ public class TypeHandle { for (TypeMirror iface : this.getTargetElement().getInterfaces()) { list.add(new TypeHandle((DeclaredType)iface)); } - return list.build(); } + + /** + * Get methods in this type + */ + public List getMethods() { + List methods = new ArrayList(); + for (ExecutableElement method : this.getEnclosedElements(ElementKind.METHOD)) { + MethodHandle handle = new MethodHandle(this, method); + methods.add(handle); + } + return methods; + } /** * Get whether the element is probably public @@ -239,7 +262,8 @@ public class TypeHandle { } /** - * Get whether the element is imaginary (inaccessible via mirror) + * Get whether the element is imaginary (inaccessible via mirror or + * classpath) */ public boolean isImaginary() { return this.getTargetElement() == null; @@ -310,11 +334,11 @@ public class TypeHandle { * of the supplied element * * @param element Element to match - * @param caseSensitive True if case-sensitive comparison should be used + * @param matchCase True if case-sensitive comparison should be used * @return handle to the discovered field if matched or null if no match */ - public final FieldHandle findField(VariableElement element, boolean caseSensitive) { - return this.findField(element.getSimpleName().toString(), TypeUtils.getTypeName(element.asType()), caseSensitive); + public final FieldHandle findField(VariableElement element, boolean matchCase) { + return this.findField(element.getSimpleName().toString(), TypeUtils.getTypeName(element.asType()), matchCase); } /** @@ -335,16 +359,16 @@ public class TypeHandle { * * @param name Field name to search for * @param type Field descriptor (java-style) - * @param caseSensitive True if case-sensitive comparison should be used + * @param matchCase True if case-sensitive comparison should be used * @return handle to the discovered field if matched or null if no match */ - public FieldHandle findField(String name, String type, boolean caseSensitive) { + public FieldHandle findField(String name, String type, boolean matchCase) { String rawType = TypeUtils.stripGenerics(type); for (VariableElement field : this.getEnclosedElements(ElementKind.FIELD)) { - if (TypeHandle.compareElement(field, name, type, caseSensitive)) { + if (TypeHandle.compareElement(field, name, type, matchCase)) { return new FieldHandle(this.getTargetElement(), field); - } else if (TypeHandle.compareElement(field, name, rawType, caseSensitive)) { + } else if (TypeHandle.compareElement(field, name, rawType, matchCase)) { return new FieldHandle(this.getTargetElement(), field, true); } } @@ -368,11 +392,11 @@ public class TypeHandle { * type of the supplied element * * @param element Element to match - * @param caseSensitive True if case-sensitive comparison should be used + * @param matchCase True if case-sensitive comparison should be used * @return handle to the discovered method if matched or null if no match */ - public final MethodHandle findMethod(ExecutableElement element, boolean caseSensitive) { - return this.findMethod(element.getSimpleName().toString(), TypeUtils.getJavaSignature(element), caseSensitive); + public final MethodHandle findMethod(ExecutableElement element, boolean matchCase) { + return this.findMethod(element.getSimpleName().toString(), TypeUtils.getJavaSignature(element), matchCase); } /** diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java new file mode 100644 index 00000000..ff208a3c --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleASM.java @@ -0,0 +1,302 @@ +/* + * This file is part of Mixin, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.tools.obfuscation.mirror; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.annotation.processing.Filer; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.PackageElement; +import javax.lang.model.type.TypeMirror; +import javax.tools.StandardLocation; + +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.AnnotationNode; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.MethodNode; +import org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName; +import org.spongepowered.asm.util.Annotations; +import org.spongepowered.asm.util.asm.IAnnotationHandle; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; +import org.spongepowered.tools.obfuscation.interfaces.ITypeHandleProvider; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; + +/** + * The specs of JSR 269 mean that mirror will only give us things visible down + * to the method level and no further. This means that classes declared inside + * method bodies (anonymous classes) cannot be accessed via mirror. However for + * classpath classes we can at least retrieve the class bytecode via the AP + * Filer, so this class is used to wrap an ASM {@link ClassNode} up as a + * {@link TypeHandle} so that the mixin AP can use it to resolve things. It + * delegates back to {@link ITypeHandleProvider} where appropriate so that + * related classes (superclasses, interfaces, etc.) will be backed by mirror + * TypeElements wherever possible. + */ +public class TypeHandleASM extends TypeHandle { + + /** + * Cache ASM type handles to avoid unnecessary churn + */ + private static final Map cache = new HashMap(); + + /** + * 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 + * name + * + * @param pkg Package + * @param name FQ class name + */ + protected TypeHandleASM(PackageElement pkg, String name, ClassNode classNode, ITypeHandleProvider typeProvider) { + super(pkg, name); + this.classNode = classNode; + this.typeProvider = typeProvider; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getAnnotation(java.lang.Class) + */ + @Override + public IAnnotationHandle getAnnotation(Class annotationClass) { + AnnotationNode visibleAnnotation = Annotations.getVisible(this.classNode, annotationClass); + if (visibleAnnotation != null) { + return Annotations.handleOf(visibleAnnotation); + } + AnnotationNode invisibleAnnotation = Annotations.getInvisible(this.classNode, annotationClass); + if (invisibleAnnotation != null) { + return Annotations.handleOf(invisibleAnnotation); + } + return AnnotationHandle.of(null); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getEnclosedElements(javax.lang.model.element.ElementKind[]) + */ + @Override + public List getEnclosedElements(ElementKind... kind) { + return super.getEnclosedElements(kind); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #hasTypeMirror() + */ + @Override + public boolean hasTypeMirror() { + return false; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getTypeMirror() + */ + @Override + public TypeMirror getTypeMirror() { + return null; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getSuperclass() + */ + @Override + public TypeHandle getSuperclass() { + TypeHandle superClass = this.typeProvider.getTypeHandle(this.classNode.superName); + return superClass; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle + * #getInterfaces() + */ + @Override + public List getInterfaces() { + Builder list = ImmutableList.builder(); + for (String ifaceName : this.classNode.interfaces) { + TypeHandle iface = this.typeProvider.getTypeHandle(ifaceName); + if (iface != null) { + list.add(iface); + } + } + return list.build(); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#getMethods() + */ + @Override + public List getMethods() { + Builder methods = ImmutableList.builder(); + try { + for (MethodNode method : this.classNode.methods) { + if (!method.name.startsWith("<") && (method.access & Opcodes.ACC_SYNTHETIC) == 0) { + methods.add(new MethodHandleASM(this, method)); + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + return methods.build(); + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#isPublic() + */ + @Override + public boolean isPublic() { + return (this.classNode.access & Opcodes.ACC_PUBLIC) != 0; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#isImaginary() + */ + @Override + public boolean isImaginary() { + return false; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#findDescriptor + * (org.spongepowered.asm.mixin.injection.selectors.ITargetSelectorByName) + */ + @Override + public String findDescriptor(ITargetSelectorByName selector) { + String desc = selector.getDesc(); + if (desc == null) { + for (MethodNode method : this.classNode.methods) { + if (method.name.equals(selector.getName())) { + desc = method.desc; + break; + } + } + } + return desc; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#findField( + * java.lang.String, java.lang.String, boolean) + */ + @Override + public FieldHandle findField(String name, String type, boolean matchCase) { + for (FieldNode field : this.classNode.fields) { + if (TypeHandleASM.compareElement(field.name, TypeUtils.getJavaSignature(field.desc), name, type, matchCase)) { + return new FieldHandleASM(this, field); + } + } + return null; + } + + /* (non-Javadoc) + * @see org.spongepowered.tools.obfuscation.mirror.TypeHandle#findMethod( + * java.lang.String, java.lang.String, boolean) + */ + @Override + public MethodHandle findMethod(String name, String signature, boolean matchCase) { + for (MethodNode method : this.classNode.methods) { + if (TypeHandleASM.compareElement(method.name, TypeUtils.getJavaSignature(method.desc), name, signature, matchCase)) { + return new MethodHandleASM(this, method); + } + } + return null; + } + + protected static boolean compareElement(String elementName, String elementType, String name, String type, boolean matchCase) { + try { + boolean compared = matchCase ? name.equals(elementName) : name.equalsIgnoreCase(elementName); + return compared && (type.length() == 0 || type.equals(elementType)); + } catch (NullPointerException ex) { + return false; + } + } + + /** + * Attempts to read a class from the compile classpath using ASM. This at + * least lets us determine whether the class exists and inspect the methods + * and fields it contains. Returns null if the class resource could + * not be resolved via the compile classpath. + * + * @param pkg Containing package + * @param name Class name + * @param ap Annotation Processor, used to access Filer and + * TypeHandleProvider + * @return ASM TypeHandle or null if the class could not be read + */ + public static TypeHandle of(PackageElement pkg, String name, IMixinAnnotationProcessor ap) { + String fqName = pkg.getQualifiedName() + "." + name; + if (TypeHandleASM.cache.containsKey(fqName)) { + return TypeHandleASM.cache.get(fqName); + } + + InputStream is = null; + try { + Filer filer = ap.getProcessingEnvironment().getFiler(); + is = filer.getResource(StandardLocation.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()); + TypeHandleASM.cache.put(fqName, typeHandle); + return typeHandle; + } catch (FileNotFoundException fnfe) { + // This is expected if the resource doesn't exist + TypeHandleASM.cache.put(fqName, null); + } catch (Exception ex) { + // This isn't expected but there's not a lot we can do about it, so just give up + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + return null; + } + +} 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 a269d8de..e34c7298 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeHandleSimulated.java @@ -128,7 +128,7 @@ public class TypeHandleSimulated extends TypeHandle { */ @Override public FieldHandle findField(String name, String type, boolean caseSensitive) { - return new FieldHandle(null, name, type); + return new FieldHandle((String)null, name, type); } /* (non-Javadoc) 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 0c03a2cb..cbc88820 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/mirror/TypeUtils.java @@ -41,6 +41,7 @@ import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; +import org.spongepowered.asm.util.Bytecode.Visibility; import org.spongepowered.asm.util.Constants; import org.spongepowered.asm.util.SignaturePrinter; @@ -246,6 +247,9 @@ public abstract class TypeUtils { * @return java signature */ public static String getJavaSignature(Element element) { + if (element == null) { + return ""; + } if (element instanceof ExecutableElement) { ExecutableElement method = (ExecutableElement)element; StringBuilder desc = new StringBuilder().append("("); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java b/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java index 6c75b36c..ca1f8f70 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/ParentValidator.java @@ -30,10 +30,10 @@ import javax.lang.model.element.ElementKind; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.MixinValidator; import org.spongepowered.tools.obfuscation.interfaces.IMessagerEx.MessageType; import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; -import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; /** @@ -57,7 +57,7 @@ public class ParentValidator extends MixinValidator { * java.util.Collection) */ @Override - public boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection targets) { + public boolean validate(TypeElement mixin, IAnnotationHandle annotation, Collection targets) { if (mixin.getEnclosingElement().getKind() != ElementKind.PACKAGE && !mixin.getModifiers().contains(Modifier.STATIC)) { this.messager.printMessage(MessageType.PARENT_VALIDATOR, "Inner class mixin must be declared static", mixin); } 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 3fdc1bff..3db43e90 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -35,13 +35,14 @@ import javax.lang.model.type.TypeMirror; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; +import org.spongepowered.asm.util.asm.IAnnotationHandle; import org.spongepowered.tools.obfuscation.MixinValidator; import org.spongepowered.tools.obfuscation.SupportedOptions; -import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.interfaces.IMessagerEx.MessageType; +import org.spongepowered.tools.obfuscation.interfaces.IMixinAnnotationProcessor; import org.spongepowered.tools.obfuscation.mirror.AnnotationHandle; -import org.spongepowered.tools.obfuscation.mirror.TypeUtils; import org.spongepowered.tools.obfuscation.mirror.TypeHandle; +import org.spongepowered.tools.obfuscation.mirror.TypeUtils; /** * Validator which checks that the mixin targets are sane @@ -64,7 +65,7 @@ public class TargetValidator extends MixinValidator { * java.util.Collection) */ @Override - public boolean validate(TypeElement mixin, AnnotationHandle annotation, Collection targets) { + public boolean validate(TypeElement mixin, IAnnotationHandle annotation, Collection targets) { if ("true".equalsIgnoreCase(this.options.getOption(SupportedOptions.DISABLE_TARGET_VALIDATOR))) { return true; } @@ -105,7 +106,7 @@ public class TargetValidator extends MixinValidator { TypeMirror superClass = mixin.getSuperclass(); for (TypeHandle target : targets) { - TypeMirror targetType = target.getType(); + TypeMirror targetType = target.getTypeMirror(); if (targetType != null && !this.validateSuperClass(targetType, superClass)) { this.messager.printMessage(MessageType.TARGET_VALIDATOR, "Superclass " + superClass + " of " + mixin + " was not found in the hierarchy of target class " + targetType, mixin); diff --git a/src/main/java/org/spongepowered/asm/util/Annotations.java b/src/main/java/org/spongepowered/asm/util/Annotations.java index 1602c9b0..259986d8 100644 --- a/src/main/java/org/spongepowered/asm/util/Annotations.java +++ b/src/main/java/org/spongepowered/asm/util/Annotations.java @@ -122,6 +122,16 @@ public final class Annotations { return this.getValue(key, null); } + @Override + public boolean getBoolean(String key, boolean defaultValue) { + return this.getValue(key, Boolean.valueOf(defaultValue)).booleanValue(); + } + + @Override + public List getList() { + return this.getList("value"); + } + @Override public List getList(String key) { return Annotations.getValue(this.annotation, key, false); diff --git a/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java b/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java index a62bc28b..67674c82 100644 --- a/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java +++ b/src/main/java/org/spongepowered/asm/util/asm/IAnnotationHandle.java @@ -120,6 +120,25 @@ public interface IAnnotationHandle { */ public abstract T getValue(String key); + /** + * Get the primitive boolean value with the specified key or return null if + * not present or not set + * + * @param key key to fetch + * @param defaultValue default value to return if value is not present + * @return value or default if not present or not set + */ + public abstract boolean getBoolean(String key, boolean defaultValue); + + /** + * Retrieve the annotation value as a list with values of the specified + * type. Returns an empty list if the value is not present or not set. + * + * @param list element duck type + * @return list of values + */ + public abstract List getList(); + /** * Retrieve the annotation value with the specified key as a list with * values of the specified type. Returns an empty list if the value is not