Output warnings for unresolved targets during AP run, closes #40

This commit is contained in:
Mumfrey committed 2015-04-23 01:06:27 +01:00
1 parent 98c0b90545
commit 04ac3aac18
8 files changed
+477 -60

No files matched your search

@@ -37,7 +37,6 @@ import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
@@ -72,6 +71,8 @@ class AnnotatedMixin {
private final String originalName;
private final List<String> aliases;
private String obfuscated;
ShadowElementName(Element method, AnnotationMirror shadow) {
@@ -84,6 +85,9 @@ class AnnotatedMixin {
}
this.hasPrefix = hasPrefix;
this.obfuscated = this.baseName = name;
List<AnnotationValue> aliases = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(shadow, "aliases");
this.aliases = MirrorUtils.<String>unfold(aliases);
}
@Override
@@ -115,6 +119,14 @@ class AnnotatedMixin {
public String prefix(String name) {
return this.hasPrefix ? this.prefix + name : name;
}
public boolean hasAliases() {
return this.aliases.size() > 0;
}
public List<String> getAliases() {
return this.aliases;
}
}
/**
@@ -201,9 +213,9 @@ class AnnotatedMixin {
// Public targets, referenced by class
try {
List<AnnotationValue> publiTargets = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(this.annotation, "value",
List<AnnotationValue> publicTargets = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(this.annotation, "value",
Collections.<AnnotationValue>emptyList());
for (TypeMirror target : MirrorUtils.<TypeMirror>unfold(publiTargets)) {
for (TypeMirror target : MirrorUtils.<TypeMirror>unfold(publicTargets)) {
TypeHandle type = new TypeHandle((DeclaredType)target);
if (this.targets.contains(type)) {
continue;
@@ -319,8 +331,10 @@ class AnnotatedMixin {
/**
* Register an {@link org.spongepowered.asm.mixin.Overwrite} method
*/
public void registerOverwrite(ExecutableElement method) {
if (!this.validateSingleTarget("@Overwrite", method)) {
public void registerOverwrite(ExecutableElement method, AnnotationMirror overwrite) {
this.validateOverwriteTarget(method, overwrite);
if (!this.remap || !this.validateSingleTarget("@Overwrite", method)) {
return;
}
@@ -349,15 +363,50 @@ class AnnotatedMixin {
this.addMethodMapping(mcpName, obfName, mcpSignature, obfMethod.sig);
}
/**
* Checks whether the specified method exists in all targets and raises
* warnings where appropriate
*/
private void validateOverwriteTarget(ExecutableElement method, AnnotationMirror overwrite) {
String signature = method.asType().toString();
List<String> aliases = MirrorUtils.<String>unfold(MirrorUtils.<List<AnnotationValue>>getAnnotationValue(overwrite, "aliases"));
for (TypeHandle target : this.targets) {
if (target.isImaginary()) {
continue;
}
// Find method as-is
MethodHandle targetMethod = target.findMethod(method);
if (targetMethod != null) {
continue;
}
// Check aliases
for (String alias : aliases) {
if ((targetMethod = target.findMethod(alias, signature)) != null) {
break;
}
}
if (targetMethod == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find target for @Overwrite method in " + target, method, overwrite);
}
}
}
/**
* Register a {@link org.spongepowered.asm.mixin.Shadow} field
*/
public void registerShadow(VariableElement field, AnnotationMirror shadow) {
if (!this.validateSingleTarget("@Shadow", field)) {
public void registerShadow(VariableElement field, AnnotationMirror shadow, boolean remap) {
ShadowElementName name = new ShadowElementName(field, shadow);
this.validateShadowTarget(field, shadow, name);
if (!remap || !this.validateSingleTarget("@Shadow", field)) {
return;
}
ShadowElementName name = new ShadowElementName(field, shadow);
String obfField = this.mixins.getObfField(this.targetRef + "/" + name);
if (obfField == null) {
@@ -368,15 +417,48 @@ class AnnotatedMixin {
this.addFieldMapping(name.setObfuscatedName(obfField));
}
/**
* Checks whether the specified field exists in all targets and raises
* warnings where appropriate
*/
private void validateShadowTarget(VariableElement field, AnnotationMirror shadow, ShadowElementName name) {
String fieldType = field.asType().toString();
for (TypeHandle target : this.targets) {
if (target.isImaginary()) {
continue;
}
// Search for field
FieldHandle targetField = target.findField(field);
if (targetField != null) {
continue;
}
// Try search by alias
for (String alias : name.getAliases()) {
if ((targetField = target.findField(alias, fieldType)) != null) {
break;
}
}
if (targetField == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find target field for @Shadow field in " + target, field, shadow);
}
}
}
/**
* Register a {@link org.spongepowered.asm.mixin.Shadow} method
*/
public void registerShadow(ExecutableElement method, AnnotationMirror shadow) {
if (!this.validateSingleTarget("@Shadow", method)) {
public void registerShadow(ExecutableElement method, AnnotationMirror shadow, boolean remap) {
ShadowElementName name = new ShadowElementName(method, shadow);
this.validateShadowTarget(method, shadow, name);
if (!remap || !this.validateSingleTarget("@Shadow", method)) {
return;
}
ShadowElementName name = new ShadowElementName(method, shadow);
String mcpSignature = MirrorUtils.generateSignature(method);
MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + name, mcpSignature));
@@ -388,21 +470,62 @@ class AnnotatedMixin {
this.addMethodMapping(name.setObfuscatedName(obfMethod.name), mcpSignature, obfMethod.sig);
}
/**
* Checks whether the specified method exists in all targets and raises
* warnings where appropriate
*/
private void validateShadowTarget(ExecutableElement method, AnnotationMirror shadow, ShadowElementName name) {
String signature = TypeHandle.getElementSignature(method);
for (TypeHandle target : this.targets) {
if (target.isImaginary()) {
continue;
}
// Find method as-is
MethodHandle targetMethod = target.findMethod(method);
if (targetMethod != null) {
continue;
}
// Find method without prefix
targetMethod = target.findMethod(name.toString(), signature);
if (targetMethod != null) {
continue;
}
// Check aliases
for (String alias : name.getAliases()) {
if ((targetMethod = target.findMethod(alias, signature)) != null) {
break;
}
}
if (targetMethod == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find target method for @Shadow method in " + target, method, shadow);
}
}
}
/**
* Register a {@link org.spongepowered.asm.mixin.injection.Inject} method
*/
public void registerInjector(ExecutableElement method, AnnotationMirror inject) {
if (!this.validateSingleTarget("@Inject", method)) {
return;
}
public void registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) {
String originalReference = MirrorUtils.<String>getAnnotationValue(inject, "method");
MemberInfo targetMember = MemberInfo.parse(originalReference);
if (targetMember.name == null) {
return;
}
String desc = this.findDescriptor(this.targetType, targetMember);
if (targetMember.desc != null) {
this.validateReferencedTarget(method, inject, targetMember);
}
if (!remap || !this.validateSingleTarget("@Inject", method)) {
return;
}
String desc = this.targetType.findDescriptor(targetMember);
if (desc == null) {
if (this.targetType.isImaginary()) {
this.mixins.printMessage(Kind.WARNING, "@Inject target requires method signature because enclosing type information is unavailable",
@@ -426,6 +549,25 @@ class AnnotatedMixin {
this.mixins.getReferenceMapper().addMapping(this.classRef, originalReference, remappedReference.toString());
}
/**
* Checks whether the referenced method exists in all targets and raises
* warnings where appropriate
*/
private void validateReferencedTarget(ExecutableElement method, AnnotationMirror inject, MemberInfo reference) {
String signature = reference.toDescriptor();
for (TypeHandle target : this.targets) {
if (target.isImaginary()) {
continue;
}
MethodHandle targetMethod = target.findMethod(reference.name, signature);
if (targetMethod == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find target method for @Inject in " + target, method, inject);
}
}
}
/**
* Register a {@link org.spongepowered.asm.mixin.injection.At} annotation
* and process the references
@@ -503,23 +645,6 @@ class AnnotatedMixin {
this.methodMappings.add(mapping);
}
private String findDescriptor(TypeHandle targetType, MemberInfo memberInfo) {
String desc = memberInfo.desc;
if (desc == null) {
for (Element child : targetType.getEnclosedElements()) {
if (child.getKind() != ElementKind.METHOD) {
continue;
}
if (child.getSimpleName().toString().equals(memberInfo.name)) {
desc = MirrorUtils.generateSignature((ExecutableElement)child);
break;
}
}
}
return desc;
}
static Map<String, String> getAtArgs(AnnotationMirror at) {
Map<String, String> args = new HashMap<String, String>();
List<AnnotationValue> argv = MirrorUtils.<List<AnnotationValue>>getAnnotationValue(at, "args");
@@ -54,6 +54,7 @@ import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.struct.MemberInfo;
import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.tools.MirrorUtils;
@@ -352,9 +353,7 @@ class AnnotatedMixins implements Messager {
return;
}
if (mixinClass.remap()) {
mixinClass.registerOverwrite(method);
}
mixinClass.registerOverwrite(method, MirrorUtils.getAnnotation(method, Overwrite.class));
}
/**
@@ -371,9 +370,7 @@ class AnnotatedMixins implements Messager {
return;
}
if (this.shouldRemap(mixinClass, shadow)) {
mixinClass.registerShadow(field, shadow);
}
mixinClass.registerShadow(field, shadow, this.shouldRemap(mixinClass, shadow));
}
/**
@@ -390,9 +387,7 @@ class AnnotatedMixins implements Messager {
return;
}
if (this.shouldRemap(mixinClass, shadow)) {
mixinClass.registerShadow(method, shadow);
}
mixinClass.registerShadow(method, shadow, this.shouldRemap(mixinClass, shadow));
}
/**
@@ -410,9 +405,9 @@ class AnnotatedMixins implements Messager {
return;
}
if (this.shouldRemap(mixinClass, inject)) {
mixinClass.registerInjector(method, inject);
boolean remap = this.shouldRemap(mixinClass, inject);
mixinClass.registerInjector(method, inject, remap);
if (remap) {
Object ats = MirrorUtils.getAnnotationValue(inject, "at");
if (ats instanceof List) {
@@ -0,0 +1,52 @@
/*
* 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 javax.lang.model.element.VariableElement;
class FieldHandle {
private final VariableElement element;
private final boolean rawType;
public FieldHandle(VariableElement element) {
this(element, false);
}
public FieldHandle(VariableElement element, boolean rawType) {
this.element = element;
this.rawType = rawType;
}
public VariableElement getElement() {
return this.element;
}
public boolean isRawType() {
return this.rawType;
}
}
@@ -0,0 +1,42 @@
/*
* 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 javax.lang.model.element.ExecutableElement;
class MethodHandle {
private final ExecutableElement element;
public MethodHandle(ExecutableElement element) {
this.element = element;
}
public ExecutableElement getElement() {
return this.element;
}
}
@@ -28,12 +28,16 @@ import java.util.Collections;
import java.util.List;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
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.injection.struct.MemberInfo;
import org.spongepowered.tools.MirrorUtils;
@@ -93,6 +97,14 @@ public class TypeHandle {
this((TypeElement)type.asElement());
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.name.replace('/', '.');
}
/**
* Returns the fully qualified class name
*/
@@ -142,4 +154,132 @@ public class TypeHandle {
public boolean isImaginary() {
return this.element == null;
}
public String findDescriptor(MemberInfo memberInfo) {
String desc = memberInfo.desc;
if (desc == null) {
for (Element child : this.getEnclosedElements()) {
if (child.getKind() != ElementKind.METHOD) {
continue;
}
if (child.getSimpleName().toString().equals(memberInfo.name)) {
desc = MirrorUtils.generateSignature((ExecutableElement)child);
break;
}
}
}
return desc;
}
/**
* Find a member field in this type which matches the name and declared type
* of the supplied element
*
* @param element Element to match
* @return handle to the discovered field if matched or null if no match
*/
public FieldHandle findField(VariableElement element) {
return this.findField(element.getSimpleName().toString(), element.asType().toString());
}
/**
* Find a member field in this type which matches the name and declared type
* specified
*
* @param name Field name to search for
* @param type Field descriptor (java-style)
* @return handle to the discovered field if matched or null if no match
*/
public FieldHandle findField(String name, String type) {
String rawType = type.replaceAll("<[^>]+>", "");
for (Element element : this.getEnclosedElements()) {
if (element.getKind() != ElementKind.FIELD) {
continue;
}
VariableElement field = (VariableElement)element;
if (this.compareElement(field, name, type)) {
return new FieldHandle(field);
} else if (this.compareElement(field, name, rawType)) {
return new FieldHandle(field, true);
}
}
return null;
}
/**
* Find a member method in this type which matches the name and declared
* type of the supplied element
*
* @param element Element to match
* @return handle to the discovered method if matched or null if no match
*/
public MethodHandle findMethod(ExecutableElement element) {
return this.findMethod(element.getSimpleName().toString(), TypeHandle.getElementSignature(element));
}
/**
* Find a member method in this type which matches the name and signature
* specified
*
* @param name Method name to search for
* @param signature Method signature
* @return handle to the discovered method if matched or null if no match
*/
public MethodHandle findMethod(String name, String signature) {
String rawSignature = signature.replaceAll("<[^>]+>", "");
for (Element element : this.getEnclosedElements()) {
switch (element.getKind()) {
case CONSTRUCTOR:
case METHOD:
ExecutableElement method = (ExecutableElement)element;
if (this.compareElement(method, name, signature) || this.compareElement(method, name, rawSignature)) {
return new MethodHandle(method);
}
break;
// case STATIC_INIT: // TODO?
// break;
default:
break;
}
}
return null;
}
private boolean compareElement(Element elem, String name, String type) {
try {
String elementName = elem.getSimpleName().toString();
String elementType = TypeHandle.getElementSignature(elem);
return name.equals(elementName) && (type.length() == 0 || type.equals(elementType));
} catch (NullPointerException ex) {
return false;
}
}
static String getElementSignature(Element element) {
if (element instanceof ExecutableElement) {
ExecutableElement method = (ExecutableElement)element;
StringBuilder desc = new StringBuilder().append("(");
boolean extra = false;
for (VariableElement arg : method.getParameters()) {
if (extra) {
desc.append(',');
}
desc.append(arg.asType().toString());
extra = true;
}
desc.append(')').append(method.getReturnType().toString());
return desc.toString();
}
return element.asType().toString();
}
}