Attempt to handle references to anonymous inner classes

This commit is contained in:
Mumfrey committed 2015-01-24 00:39:00 +00:00
1 parent 5048a431bc
commit dcd44b91d2
7 files changed
+305 -12

No files matched your search

@@ -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();
@@ -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);
}
/**
@@ -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;
}
/**
@@ -0,0 +1,167 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered.org <http://www.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<Element> children = new ArrayList<Element>();
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<? extends AnnotationMirror> getAnnotationMirrors() {
return Collections.<AnnotationMirror>emptyList();
}
@Override
public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
return null;
}
@Override
public Set<Modifier> getModifiers() {
return ImmutableSet.<Modifier>of(
Modifier.PRIVATE
);
}
@Override
public Name getSimpleName() {
return this.name;
}
@Override
public Element getEnclosingElement() {
return this.parent;
}
@Override
public List<? extends Element> getEnclosedElements() {
return this.children;
}
@Override
public <R, P> R accept(ElementVisitor<R, P> 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<? extends TypeMirror> getInterfaces() {
return null;
}
@Override
public List<? extends TypeParameterElement> getTypeParameters() {
return null;
}
}
@@ -0,0 +1,70 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered.org <http://www.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, P> R accept(TypeVisitor<R, P> v, P p) {
return null;
}
@Override
public Element asElement() {
return this.element;
}
@Override
public TypeMirror getEnclosingType() {
return this.element.getEnclosingElement().asType();
}
@Override
public List<? extends TypeMirror> getTypeArguments() {
return Collections.<TypeMirror>emptyList();
}
}
@@ -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);
}
}