Support requiring Overwrite at the config level, fix intrinsic accessors

This commit is contained in:
Mumfrey committed 2017-07-05 10:26:30 +01:00
1 parent 41e53c4942
commit 50660894fc
6 files changed
+201 -123

No files matched your search

@@ -53,7 +53,8 @@ public abstract class AccessorGenerator {
*/
protected final MethodNode createMethod(int maxLocals, int maxStack) {
MethodNode method = this.info.getMethod();
MethodNode accessor = new MethodNode(Opcodes.ASM5, method.access & ~Opcodes.ACC_ABSTRACT, method.name, method.desc, null, null);
MethodNode accessor = new MethodNode(Opcodes.ASM5, (method.access & ~Opcodes.ACC_ABSTRACT) | Opcodes.ACC_SYNTHETIC, method.name, method.desc,
null, null);
accessor.visibleAnnotations = new ArrayList<AnnotationNode>();
accessor.visibleAnnotations.add(this.info.getAnnotation());
accessor.maxLocals = maxLocals;
@@ -329,7 +329,9 @@ public class AccessorInfo extends SpecialMethodInfo {
* @return generated accessor method
*/
public MethodNode generate() {
return this.type.getGenerator(this).generate();
MethodNode generatedAccessor = this.type.getGenerator(this).generate();
Bytecode.mergeAnnotations(this.method, generatedAccessor);
return generatedAccessor;
}
private FieldNode findTargetField() {
@@ -343,7 +343,7 @@ class MixinApplicatorStandard {
*/
protected void applyAnnotations(MixinTargetContext mixin) {
ClassNode sourceClass = mixin.getClassNode();
this.mergeAnnotations(sourceClass, this.targetClass);
Bytecode.mergeAnnotations(sourceClass, this.targetClass);
}
/**
@@ -364,7 +364,7 @@ class MixinApplicatorStandard {
FieldNode shadow = entry.getKey();
FieldNode target = this.findTargetField(shadow);
if (target != null) {
this.mergeAnnotations(shadow, target);
Bytecode.mergeAnnotations(shadow, target);
// Strip the FINAL flag from @Mutable non-private fields
if (entry.getValue().isDecoratedMutable() && !Bytecode.hasFlag(target, Opcodes.ACC_PRIVATE)) {
@@ -402,7 +402,7 @@ class MixinApplicatorStandard {
protected void applyShadowMethod(MixinTargetContext mixin, MethodNode shadow) {
MethodNode target = this.findTargetMethod(shadow);
if (target != null) {
this.mergeAnnotations(shadow, target);
Bytecode.mergeAnnotations(shadow, target);
}
}
@@ -441,6 +441,12 @@ class MixinApplicatorStandard {
return;
}
} else {
if (mixin.requireOverwriteAnnotations() && !isOverwrite) {
throw new InvalidMixinException(mixin,
String.format("%s%s in %s cannot overwrite method in %s because @Overwrite is required by the parent configuration",
method.name, method.desc, mixin, mixin.getTarget().getClassName()));
}
this.targetClass.methods.remove(target);
}
} else if (isOverwrite) {
@@ -535,10 +541,12 @@ class MixinApplicatorStandard {
throw new InvalidMixinException(mixin, "@Intrinsic method cannot be static, found " + methodName + " in " + mixin);
}
AnnotationNode renamed = Annotations.getVisible(method, MixinRenamed.class);
if (renamed == null || !Annotations.getValue(renamed, "isInterfaceMember", Boolean.FALSE)) {
throw new InvalidMixinException(mixin, "@Intrinsic method must be prefixed interface method, no rename encountered on "
+ methodName + " in " + mixin);
if (!Bytecode.hasFlag(method, Opcodes.ACC_SYNTHETIC)) {
AnnotationNode renamed = Annotations.getVisible(method, MixinRenamed.class);
if (renamed == null || !Annotations.getValue(renamed, "isInterfaceMember", Boolean.FALSE)) {
throw new InvalidMixinException(mixin, "@Intrinsic method must be prefixed interface method, no rename encountered on "
+ methodName + " in " + mixin);
}
}
if (!Annotations.getValue(intrinsic, "displace", Boolean.FALSE)) {
@@ -649,7 +657,7 @@ class MixinApplicatorStandard {
MethodNode ctor = null;
for (MethodNode mixinMethod : mixin.getMethods()) {
if (Constants.CTOR.equals(mixinMethod.name) && MixinApplicatorStandard.hasLineNumbers(mixinMethod)) {
if (Constants.CTOR.equals(mixinMethod.name) && Bytecode.methodHasLineNumbers(mixinMethod)) {
if (ctor == null) {
ctor = mixinMethod;
} else {
@@ -966,116 +974,6 @@ class MixinApplicatorStandard {
}
}
/**
* Merge annotations from the specified source ClassNode to the destination
* ClassNode, replaces annotations of the equivalent type on the target with
* annotations from the source. If the source node has no annotations then
* no action will take place, if the target node has no annotations then a
* new annotation list will be created. Annotations from the mixin package
* are not merged.
*
* @param from ClassNode to merge annotations from
* @param to ClassNode to merge annotations to
*/
protected final void mergeAnnotations(ClassNode from, ClassNode to) {
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
}
/**
* Merge annotations from the specified source MethodNode to the destination
* MethodNode, replaces annotations of the equivalent type on the target
* with annotations from the source. If the source node has no annotations
* then no action will take place, if the target node has no annotations
* then a new annotation list will be created. Annotations from the mixin
* package are not merged.
*
* @param from MethodNode to merge annotations from
* @param to MethodNode to merge annotations to
*/
protected final void mergeAnnotations(MethodNode from, MethodNode to) {
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
}
/**
* Merge annotations from the specified source FieldNode to the destination
* FieldNode, replaces annotations of the equivalent type on the target with
* annotations from the source. If the source node has no annotations then
* no action will take place, if the target node has no annotations then a
* new annotation list will be created. Annotations from the mixin package
* are not merged.
*
* @param from FieldNode to merge annotations from
* @param to FieldNode to merge annotations to
*/
protected final void mergeAnnotations(FieldNode from, FieldNode to) {
to.visibleAnnotations = this.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, from.name);
to.invisibleAnnotations = this.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, from.name);
}
/**
* Merge annotations from the source list to the target list. Returns the
* target list or a new list if the target list was null.
*
* @param from Annotations to merge
* @param to Annotation list to merge into
* @param name Name of the item being merged, for debugging purposes
* @return The merged list (or a new list if the target list was null)
*/
private List<AnnotationNode> mergeAnnotations(List<AnnotationNode> from, List<AnnotationNode> to, String name) {
try {
if (from == null) {
return to;
}
if (to == null) {
to = new ArrayList<AnnotationNode>();
}
for (AnnotationNode annotation : from) {
if (!this.isMergeableAnnotation(annotation)) {
continue;
}
for (Iterator<AnnotationNode> iter = to.iterator(); iter.hasNext();) {
if (iter.next().desc.equals(annotation.desc)) {
iter.remove();
break;
}
}
to.add(annotation);
}
} catch (Exception ex) {
this.logger.warn("Exception encountered whilst merging annotations for {}", name);
}
return to;
}
private boolean isMergeableAnnotation(AnnotationNode annotation) {
if (annotation.desc.startsWith("L" + Constants.MIXIN_PACKAGE_REF)) {
return annotation.desc.endsWith("Debug;");
}
return true;
}
/**
* Returns true if the supplied method contains any line number information
*
* @param method Method to scan
* @return true if a line number node is located
*/
private static boolean hasLineNumbers(MethodNode method) {
for (Iterator<AbstractInsnNode> iter = method.instructions.iterator(); iter.hasNext();) {
if (iter.next() instanceof LineNumberNode) {
return true;
}
}
return false;
}
/**
* Finds a method in the target class
* @param searchFor
@@ -46,6 +46,7 @@ import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.injection.Inject;
@@ -88,6 +89,9 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
@SerializedName("conformVisibility")
boolean conformAccessModifiers;
@SerializedName("requireAnnotations")
boolean requireOverwriteAnnotations;
}
/**
@@ -656,6 +660,16 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
public boolean conformOverwriteVisibility() {
return this.overwriteOptions.conformAccessModifiers;
}
/**
* Get whether {@link Overwrite} annotations are required to enable
* overwrite behaviour for mixins in this config
*
* @return true to require overwriting methods to be annotated
*/
public boolean requireOverwriteAnnotations() {
return this.overwriteOptions.requireOverwriteAnnotations;
}
// AMS - temp
public boolean select(MixinEnvironment environment) {
@@ -25,8 +25,16 @@
package org.spongepowered.asm.mixin.transformer;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
@@ -38,6 +46,7 @@ import org.spongepowered.asm.lib.tree.*;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel;
import org.spongepowered.asm.mixin.MixinEnvironment.Option;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.SoftOverride;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.gen.AccessorInfo;
@@ -388,6 +397,15 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
public InjectorGroupInfo.Map getInjectorGroups() {
return this.injectorGroups;
}
/**
* Get whether overwrite annotations are required for methods in this mixin
*
* @return true if overwrite methods must be annoated with {@link Overwrite}
*/
public boolean requireOverwriteAnnotations() {
return this.mixin.getParent().requireOverwriteAnnotations();
}
/**
* Find the corresponding class type for the supplied mixin class in this
@@ -29,10 +29,15 @@ import static org.spongepowered.asm.lib.ClassWriter.*;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.lib.ClassReader;
import org.spongepowered.asm.lib.ClassWriter;
import org.spongepowered.asm.lib.MethodVisitor;
@@ -41,6 +46,10 @@ import org.spongepowered.asm.lib.Type;
import org.spongepowered.asm.lib.tree.*;
import org.spongepowered.asm.lib.util.CheckClassAdapter;
import org.spongepowered.asm.lib.util.TraceClassVisitor;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Intrinsic;
import org.spongepowered.asm.mixin.Overwrite;
import com.google.common.base.Joiner;
import com.google.common.primitives.Ints;
@@ -89,7 +98,7 @@ public final class Bytecode {
}
}
/**
* Integer constant opcodes
*/
@@ -189,6 +198,20 @@ public final class Bytecode {
null
};
/**
* Annotations which are eligible for merge via {@link #mergeAnnotations}
*/
private static final Class<?>[] MERGEABLE_MIXIN_ANNOTATIONS = new Class<?>[] {
Overwrite.class,
Intrinsic.class,
Final.class,
Debug.class
};
private static Pattern mergeableAnnotationPattern = Bytecode.getMergeableAnnotationPattern();
private static final Logger logger = LogManager.getLogger("mixin");
private Bytecode() {
// utility class
}
@@ -429,7 +452,22 @@ public final class Bytecode {
return opcode >= 0 ? String.valueOf(opcode) : "UNKNOWN";
}
/**
* Returns true if the supplied method contains any line number information
*
* @param method Method to scan
* @return true if a line number node is located
*/
public static boolean methodHasLineNumbers(MethodNode method) {
for (Iterator<AbstractInsnNode> iter = method.instructions.iterator(); iter.hasNext();) {
if (iter.next() instanceof LineNumberNode) {
return true;
}
}
return false;
}
/**
* Returns true if the supplied method node is static
*
@@ -959,5 +997,112 @@ public final class Bytecode {
public static String getUnboxingMethod(Type type) {
return type == null ? null : Bytecode.UNBOXING_METHODS[type.getSort()];
}
/**
* Merge annotations from the specified source ClassNode to the destination
* ClassNode, replaces annotations of the equivalent type on the target with
* annotations from the source. If the source node has no annotations then
* no action will take place, if the target node has no annotations then a
* new annotation list will be created. Annotations from the mixin package
* are not merged.
*
* @param from ClassNode to merge annotations from
* @param to ClassNode to merge annotations to
*/
public static void mergeAnnotations(ClassNode from, ClassNode to) {
to.visibleAnnotations = Bytecode.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, "class", from.name);
to.invisibleAnnotations = Bytecode.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, "class", from.name);
}
/**
* Merge annotations from the specified source MethodNode to the destination
* MethodNode, replaces annotations of the equivalent type on the target
* with annotations from the source. If the source node has no annotations
* then no action will take place, if the target node has no annotations
* then a new annotation list will be created. Annotations from the mixin
* package are not merged.
*
* @param from MethodNode to merge annotations from
* @param to MethodNode to merge annotations to
*/
public static void mergeAnnotations(MethodNode from, MethodNode to) {
to.visibleAnnotations = Bytecode.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, "method", from.name);
to.invisibleAnnotations = Bytecode.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, "method", from.name);
}
/**
* Merge annotations from the specified source FieldNode to the destination
* FieldNode, replaces annotations of the equivalent type on the target with
* annotations from the source. If the source node has no annotations then
* no action will take place, if the target node has no annotations then a
* new annotation list will be created. Annotations from the mixin package
* are not merged.
*
* @param from FieldNode to merge annotations from
* @param to FieldNode to merge annotations to
*/
public static void mergeAnnotations(FieldNode from, FieldNode to) {
to.visibleAnnotations = Bytecode.mergeAnnotations(from.visibleAnnotations, to.visibleAnnotations, "field", from.name);
to.invisibleAnnotations = Bytecode.mergeAnnotations(from.invisibleAnnotations, to.invisibleAnnotations, "field", from.name);
}
/**
* Merge annotations from the source list to the target list. Returns the
* target list or a new list if the target list was null.
*
* @param from Annotations to merge
* @param to Annotation list to merge into
* @param type Type of element being merged
* @param name Name of the item being merged, for debugging purposes
* @return The merged list (or a new list if the target list was null)
*/
private static List<AnnotationNode> mergeAnnotations(List<AnnotationNode> from, List<AnnotationNode> to, String type, String name) {
try {
if (from == null) {
return to;
}
if (to == null) {
to = new ArrayList<AnnotationNode>();
}
for (AnnotationNode annotation : from) {
if (!Bytecode.isMergeableAnnotation(annotation)) {
continue;
}
for (Iterator<AnnotationNode> iter = to.iterator(); iter.hasNext();) {
if (iter.next().desc.equals(annotation.desc)) {
iter.remove();
break;
}
}
to.add(annotation);
}
} catch (Exception ex) {
Bytecode.logger.warn("Exception encountered whilst merging annotations for {} {}", type, name);
}
return to;
}
private static boolean isMergeableAnnotation(AnnotationNode annotation) {
if (annotation.desc.startsWith("L" + Constants.MIXIN_PACKAGE_REF)) {
return Bytecode.mergeableAnnotationPattern.matcher(annotation.desc).matches();
}
return true;
}
private static Pattern getMergeableAnnotationPattern() {
StringBuilder sb = new StringBuilder("^L(");
for (int i = 0; i < Bytecode.MERGEABLE_MIXIN_ANNOTATIONS.length; i++) {
if (i > 0) {
sb.append('|');
}
sb.append(Bytecode.MERGEABLE_MIXIN_ANNOTATIONS[i].getName().replace('.', '/'));
}
return Pattern.compile(sb.append(");$").toString());
}
}