diff --git a/src/ap/java/org/spongepowered/tools/MirrorUtils.java b/src/ap/java/org/spongepowered/tools/MirrorUtils.java index 126eadb5..62ea0a15 100644 --- a/src/ap/java/org/spongepowered/tools/MirrorUtils.java +++ b/src/ap/java/org/spongepowered/tools/MirrorUtils.java @@ -187,7 +187,10 @@ public abstract class MirrorUtils { } public static String getInternalName(DeclaredType type) { - TypeElement elem = (TypeElement)type.asElement(); + return MirrorUtils.getInternalName((TypeElement)type.asElement()); + } + + public static String getInternalName(TypeElement elem) { StringBuilder reference = new StringBuilder(); reference.append(elem.getSimpleName()); Element parent = elem.getEnclosingElement(); diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java index 3ae8662d..0646776d 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixin.java @@ -176,10 +176,10 @@ class AnnotatedMixin { this.mixin = type; this.classRef = type.getQualifiedName().toString().replace('.', '/'); - TypeMirror primaryTarget = this.initTargets(); + TypeElement primaryTarget = this.initTargets(); if (primaryTarget != null) { - this.targetRef = MirrorUtils.getInternalName((DeclaredType)primaryTarget); - this.targetType = (TypeElement)((DeclaredType)primaryTarget).asElement(); + this.targetRef = MirrorUtils.getInternalName(primaryTarget); + this.targetType = primaryTarget; } else { this.targetRef = null; this.targetType = null; @@ -194,8 +194,8 @@ class AnnotatedMixin { } } - private TypeMirror initTargets() { - TypeMirror primaryTarget = null; + private TypeElement initTargets() { + TypeElement primaryTarget = null; // Public targets, referenced by class try { @@ -208,7 +208,7 @@ class AnnotatedMixin { } this.targets.add(element); if (primaryTarget == null) { - primaryTarget = target; + primaryTarget = element; } } } catch (Exception ex) { @@ -231,10 +231,9 @@ class AnnotatedMixin { this.mixins.printMessage(Kind.ERROR, "Mixin target " + privateTarget + " is public and must be specified in value", this); return null; } - TypeMirror target = element.asType(); this.targets.add(element); if (primaryTarget == null) { - primaryTarget = target; + primaryTarget = element; } } } catch (Exception ex) { @@ -339,7 +338,8 @@ class AnnotatedMixin { return; } - this.addMethodMapping(mcpName, obfMethod.name, mcpSignature, obfMethod.sig); + String obfName = obfMethod.name.substring(obfMethod.name.lastIndexOf('/') + 1); + this.addMethodMapping(mcpName, obfName, mcpSignature, obfMethod.sig); } /** diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java index 216f2553..91542835 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/AnnotatedMixins.java @@ -40,8 +40,10 @@ import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; +import javax.lang.model.util.Elements; import javax.tools.Diagnostic; import javax.tools.Diagnostic.Kind; import javax.tools.FileObject; @@ -465,7 +467,55 @@ class AnnotatedMixins implements Messager { * environment */ public TypeElement getTypeElement(String name) { - return this.processingEnv.getElementUtils().getTypeElement(name); + name = name.replace('/', '.'); + + Elements elements = this.processingEnv.getElementUtils(); + TypeElement element = elements.getTypeElement(name); + if (element != null) { + return element; + } + + int lastDotPos = name.lastIndexOf('.'); + if (lastDotPos > -1) { + String pkg = name.substring(0, lastDotPos); + PackageElement packageElement = elements.getPackageElement(pkg); + if (packageElement != null) { + element = (TypeElement)this.findTypeElement(packageElement, name.substring(lastDotPos + 1).split("[/\\.\\x24]"), 0); + } + } + + return element; + } + + /** + * When we can't find a TypeElement by directly looking it up (probably + * because the element is private or anonymous) recurse as far down the tree + * as possible and then fake out the elements which can't be navigated to. + * + * @param search Element to search within + * @param parts Path parts following the package declaration, eg. for the + * class name com.foo.bar.Baz$1 will contain the elements "Baz" and "1" + * @param index Position within the parts array, each time we recurse down + * the tree this value is incremented + * @return found or faked element + */ + private Element findTypeElement(Element search, String[] parts, int index) { + if (index >= parts.length) { + return null; + } + + for (Element element : search.getEnclosedElements()) { + if (element.getSimpleName().contentEquals(parts[index])) { + return this.findTypeElement(element, parts, index + 1); + } + } + + FakeElement el = new FakeElement(search, parts[index]); + for (index++; index < parts.length; index++) { + el = el.addChild(parts[index]); + } + + return el; } /** diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/FakeElement.java b/src/ap/java/org/spongepowered/tools/obfuscation/FakeElement.java new file mode 100644 index 00000000..2a88bd3c --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/FakeElement.java @@ -0,0 +1,167 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * 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; + +import java.lang.annotation.Annotation; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.ElementVisitor; +import javax.lang.model.element.Modifier; +import javax.lang.model.element.Name; +import javax.lang.model.element.NestingKind; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.TypeParameterElement; +import javax.lang.model.type.TypeMirror; + +import com.google.common.collect.ImmutableSet; + + +public class FakeElement implements TypeElement { + + private final NestingKind nesting; + + private final Element parent; + + private final Name name; + + private final List children = new ArrayList(); + + private final TypeMirror typeMirror; + + public FakeElement(Element parent, final String name) { + this.nesting = name.matches("^[0-9].*$") ? NestingKind.ANONYMOUS : NestingKind.MEMBER; + this.parent = parent; + this.name = new Name() { + @Override + public int length() { + return name.length(); + } + + @Override + public char charAt(int index) { + return name.charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return name.substring(start, end); + } + + @Override + public boolean contentEquals(CharSequence cs) { + return name.equals(cs); + } + }; + + if (parent instanceof FakeElement) { + ((FakeElement)parent).children.add(this); + } + + this.typeMirror = new FakeType(this); + } + + public FakeElement addChild(String name) { + return new FakeElement(this, name); + } + + @Override + public TypeMirror asType() { + return this.typeMirror; + } + + @Override + public ElementKind getKind() { + return ElementKind.CLASS; + } + + @Override + public List getAnnotationMirrors() { + return Collections.emptyList(); + } + + @Override + public A getAnnotation(Class annotationType) { + return null; + } + + @Override + public Set getModifiers() { + return ImmutableSet.of( + Modifier.PRIVATE + ); + } + + @Override + public Name getSimpleName() { + return this.name; + } + + @Override + public Element getEnclosingElement() { + return this.parent; + } + + @Override + public List getEnclosedElements() { + return this.children; + } + + @Override + public R accept(ElementVisitor v, P p) { + return null; + } + + @Override + public NestingKind getNestingKind() { + return this.nesting; + } + + @Override + public Name getQualifiedName() { + return null; + } + + @Override + public TypeMirror getSuperclass() { + return null; + } + + @Override + public List getInterfaces() { + return null; + } + + @Override + public List getTypeParameters() { + return null; + } + +} diff --git a/src/ap/java/org/spongepowered/tools/obfuscation/FakeType.java b/src/ap/java/org/spongepowered/tools/obfuscation/FakeType.java new file mode 100644 index 00000000..27373ea0 --- /dev/null +++ b/src/ap/java/org/spongepowered/tools/obfuscation/FakeType.java @@ -0,0 +1,70 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * 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; + +import java.util.Collections; +import java.util.List; + +import javax.lang.model.element.Element; +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 javax.lang.model.type.TypeVisitor; + + +public class FakeType implements DeclaredType { + + private final TypeElement element; + + public FakeType(TypeElement element) { + this.element = element; + } + + @Override + public TypeKind getKind() { + return TypeKind.DECLARED; + } + + @Override + public R accept(TypeVisitor v, P p) { + return null; + } + + @Override + public Element asElement() { + return this.element; + } + + @Override + public TypeMirror getEnclosingType() { + return this.element.getEnclosingElement().asType(); + } + + @Override + public List getTypeArguments() { + return Collections.emptyList(); + } +} 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 98b72fcf..5d599ada 100644 --- a/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java +++ b/src/ap/java/org/spongepowered/tools/obfuscation/validation/TargetValidator.java @@ -31,6 +31,8 @@ import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; +import org.spongepowered.tools.obfuscation.FakeType; + public class TargetValidator extends MixinValidator { @@ -44,7 +46,7 @@ public class TargetValidator extends MixinValidator { for (TypeElement target : targets) { TypeMirror targetType = target.asType(); - if (!this.processingEnv.getTypeUtils().isAssignable(targetType, superClass)) { + if (!(targetType instanceof FakeType) && !this.processingEnv.getTypeUtils().isAssignable(targetType, superClass)) { this.error("Superclass " + superClass + " of " + mixin + " was not found in the hierarchy of target class " + targetType, mixin); } } diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java index c636a73d..e5357f67 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinInfo.java @@ -201,6 +201,7 @@ class MixinInfo extends TreeInfo implements Comparable, IMixinInfo { */ private void readTargets(List outTargets, List inTargets, boolean suppressPlugin, boolean checkPublic) { for (String targetClassName : inTargets) { + targetClassName = targetClassName.replace('/', '.'); if (this.plugin == null || suppressPlugin || this.plugin.shouldApplyMixin(targetClassName, this.className)) { ClassInfo targetInfo = ClassInfo.forName(targetClassName); if (targetInfo.isInterface()) {