Don't raise no-obf error for @Inject if remappable ats exist, fixes #80

This commit is contained in:
Mumfrey committed 2015-11-26 17:08:04 +00:00
1 parent 5814ea1957
commit 9bad1f5c97
5 files changed
+156 -26

No files matched your search

@@ -56,6 +56,7 @@ import org.spongepowered.asm.util.ConstraintViolationException;
import org.spongepowered.asm.util.InvalidConstraintException;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass;
import org.spongepowered.tools.obfuscation.struct.Message;
/**
@@ -504,12 +505,17 @@ class AnnotatedMixin {
/**
* Register a {@link org.spongepowered.asm.mixin.injection.Inject} method
*
* @param method Callback method
* @param inject Inject annotation
* @param remap
* @return
*/
public void registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) {
public Message registerInjector(ExecutableElement method, AnnotationMirror inject, boolean remap) {
String originalReference = MirrorUtils.<String>getAnnotationValue(inject, "method");
MemberInfo targetMember = MemberInfo.parse(originalReference);
if (targetMember.name == null) {
return;
return null;
}
try {
@@ -518,68 +524,71 @@ class AnnotatedMixin {
this.mixins.printMessage(Kind.ERROR, ex.getMessage(), method, inject);
}
String type = "@" + inject.getAnnotationType().asElement().getSimpleName() + "";
if (targetMember.desc != null) {
this.validateReferencedTarget(method, inject, targetMember, "@Inject");
this.validateReferencedTarget(method, inject, targetMember, type);
}
if (!remap || !this.validateSingleTarget("@Inject", method)) {
return;
if (!remap || !this.validateSingleTarget(type, method)) {
return null;
}
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",
this.mixins.printMessage(Kind.WARNING, type + " target requires method signature because enclosing type information is unavailable",
method, inject);
} else if (!Constants.INIT.equals(targetMember.name)) {
this.mixins.printMessage(Kind.WARNING, "Unable to determine signature for @Inject target method", method, inject);
this.mixins.printMessage(Kind.WARNING, "Unable to determine signature for " + type + " target method", method, inject);
}
return;
return null;
}
MethodData obfMethod = this.mixins.getObfMethod(new MethodData(this.targetRef + "/" + targetMember.name, desc));
if (obfMethod == null) {
Kind error = Constants.INIT.equals(targetMember.name) ? Kind.WARNING : Kind.ERROR;
this.mixins.printMessage(error, "No obfuscation mapping for @Inject target " + targetMember.name, method, inject);
return;
return new Message(error, "No obfuscation mapping for " + type + " target " + targetMember.name, method, inject);
}
String obfName = obfMethod.name.substring(obfMethod.name.lastIndexOf('/') + 1);
MemberInfo remappedReference = new MemberInfo(obfName, this.targetRef, obfMethod.sig, false);
this.mixins.getReferenceMapper().addMapping(this.classRef, originalReference, remappedReference.toString());
return null;
}
/**
* Register a {@link org.spongepowered.asm.mixin.injection.At} annotation
* and process the references
*/
public void registerInjectionPoint(Element element, AnnotationMirror inject, AnnotationMirror at) {
public int registerInjectionPoint(Element element, AnnotationMirror inject, AnnotationMirror at) {
if (!AnnotatedMixins.getRemapValue(at)) {
return;
return 0;
}
String type = MirrorUtils.<String>getAnnotationValue(at, "value");
String target = MirrorUtils.<String>getAnnotationValue(at, "target");
this.remapReference(type + ".<target>", target, element, inject, at);
int remapped = this.remapReference(type + ".<target>", target, element, inject, at) ? 1 : 0;
// Pattern for replacing references in args, not used yet
// if ("SOMETYPE".equals(type)) {
// Map<String, String> args = AnnotatedMixin.getAtArgs(at);
// this.remapReference(type + ".args[target]", args.get("target"));
// }
return remapped;
}
private void remapReference(String key, String target, Element element, AnnotationMirror inject, AnnotationMirror at) {
private boolean remapReference(String key, String target, Element element, AnnotationMirror inject, AnnotationMirror at) {
if (target == null) {
return;
return false;
}
MemberInfo targetMember = MemberInfo.parse(target);
if (!targetMember.isFullyQualified()) {
String missing = "missing " + (targetMember.owner == null ? (targetMember.desc == null ? "owner and signature" : "owner") : "signature");
this.mixins.printMessage(Kind.ERROR, "@At(" + key + ") is not fully qualified, " + missing, element, inject);
return;
return false;
}
try {
@@ -593,7 +602,7 @@ class AnnotatedMixin {
String obfField = this.mixins.getObfField(targetMember.toSrg());
if (obfField == null) {
this.mixins.printMessage(Kind.WARNING, "Cannot find field mapping for @At(" + key + ") '" + target + "'", element, inject);
return;
return false;
}
remappedReference = MemberInfo.fromSrgField(obfField, targetMember.desc);
} else {
@@ -602,12 +611,13 @@ class AnnotatedMixin {
if (targetMember.owner == null || !targetMember.owner.startsWith("java/lang/")) {
this.mixins.printMessage(Kind.WARNING, "Cannot find method mapping for @At(" + key + ") '" + target + "'", element, inject);
}
return;
return false;
}
remappedReference = targetMember.remapUsing(obfMethod, false);
}
this.mixins.getReferenceMapper().addMapping(this.classRef, target, remappedReference.toString());
return true;
}
/**
@@ -65,6 +65,7 @@ import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper;
import org.spongepowered.asm.util.ITokenProvider;
import org.spongepowered.tools.MirrorUtils;
import org.spongepowered.tools.obfuscation.IMixinValidator.ValidationPass;
import org.spongepowered.tools.obfuscation.struct.Message;
import org.spongepowered.tools.obfuscation.validation.ParentValidator;
import org.spongepowered.tools.obfuscation.validation.TargetValidator;
@@ -497,12 +498,14 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
public void registerInjector(TypeElement mixinType, ExecutableElement method, AnnotationMirror inject) {
AnnotatedMixin mixinClass = this.getMixin(mixinType);
if (mixinClass == null) {
this.printMessage(Kind.ERROR, "Found @Inject annotation on a non-mixin method", method);
String type = "@" + inject.getAnnotationType().asElement().getSimpleName() + "";
this.printMessage(Kind.ERROR, "Found " + type + " annotation on a non-mixin method", method);
return;
}
boolean remap = this.shouldRemap(mixinClass, inject);
mixinClass.registerInjector(method, inject, remap);
Message errorMessage = mixinClass.registerInjector(method, inject, remap);
int remappedAts = 0;
if (remap) {
Object ats = MirrorUtils.getAnnotationValue(inject, "at");
@@ -514,21 +517,28 @@ class AnnotatedMixins implements Messager, ITokenProvider, IOptionProvider {
at = ((AnnotationValue)at).getValue();
}
if (at instanceof AnnotationMirror) {
mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)at);
remappedAts += mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)at);
} else {
this.printMessage(Kind.WARNING, "No annotation mirror on " + at.getClass().getName());
}
}
} else if (ats instanceof AnnotationMirror) {
mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)ats);
remappedAts += mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)ats);
} else if (ats instanceof AnnotationValue) {
// Fix for JDT
Object mirror = ((AnnotationValue)ats).getValue();
if (mirror instanceof AnnotationMirror) {
mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)mirror);
remappedAts += mixinClass.registerInjectionPoint(method, inject, (AnnotationMirror)mirror);
}
}
}
// The annotation was *not* marked as remap=false and failed so we
// checked for remappable @At annotations, if that failed as well then
// we raise the original error.
if (remappedAts == 0 && errorMessage != null) {
errorMessage.sendTo(this);
}
}
/**
@@ -53,7 +53,7 @@ public class TargetMap extends HashMap<TypeReference, Set<TypeReference>> {
private final String sessionId;
/**
* Create a new TargetMap with a session ID based on the current system time
* Create a new TargetMap with a session ID based on the current system time
*/
private TargetMap() {
this(String.valueOf(System.currentTimeMillis()));
@@ -208,7 +208,7 @@ public class TargetMap extends HashMap<TypeReference, Set<TypeReference>> {
} catch (Exception e) {
e.printStackTrace();
} finally {
if(objectinputstream != null){
if (objectinputstream != null) {
try {
objectinputstream .close();
} catch (IOException ex) {
@@ -79,7 +79,7 @@ public class TypeReference implements Serializable, Comparable<TypeReference> {
*/
public TypeHandle getHandle(ProcessingEnvironment processingEnv) {
if (this.handle == null) {
TypeElement element = processingEnv.getElementUtils().getTypeElement(getClassName());
TypeElement element = processingEnv.getElementUtils().getTypeElement(this.getClassName());
try {
this.handle = new TypeHandle(element);
} catch (Exception ex) {
@@ -0,0 +1,110 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://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.struct;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
public class Message {
private Diagnostic.Kind kind;
private CharSequence msg;
private final Element element;
private final AnnotationMirror annotation;
private final AnnotationValue value;
public Message(Diagnostic.Kind kind, CharSequence msg) {
this(kind, msg, null, null, null);
}
public Message(Diagnostic.Kind kind, CharSequence msg, Element element) {
this(kind, msg, element, null, null);
}
public Message(Diagnostic.Kind kind, CharSequence msg, Element element, AnnotationMirror annotation) {
this(kind, msg, element, annotation, null);
}
public Message(Diagnostic.Kind kind, CharSequence msg, Element element, AnnotationMirror annotation, AnnotationValue value) {
this.kind = kind;
this.msg = msg;
this.element = element;
this.annotation = annotation;
this.value = value;
}
public Message sendTo(Messager messager) {
if (this.value != null) {
messager.printMessage(this.kind, this.msg, this.element, this.annotation, this.value);
} else if (this.annotation != null) {
messager.printMessage(this.kind, this.msg, this.element, this.annotation);
} else if (this.element != null) {
messager.printMessage(this.kind, this.msg, this.element);
} else {
messager.printMessage(this.kind, this.msg);
}
return this;
}
public Diagnostic.Kind getKind() {
return this.kind;
}
public Message setKind(Diagnostic.Kind kind) {
this.kind = kind;
return this;
}
public CharSequence getMsg() {
return this.msg;
}
public Message setMsg(CharSequence msg) {
this.msg = msg;
return this;
}
public Element getElement() {
return this.element;
}
public AnnotationMirror getAnnotation() {
return this.annotation;
}
public AnnotationValue getValue() {
return this.value;
}
}