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

@@ -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());
}
}