mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-25 19:14:29 +00:00
Detect and compare compatible synthetic bridges, raise error on conflict
This commit is contained in:
1 parent
d1620ed602
commit
ada26ef95b
8 files changed
+522
-35
No files matched your search
@@ -46,6 +46,13 @@ public interface IMixinContext {
|
||||
*/
|
||||
public abstract String getClassRef();
|
||||
|
||||
/**
|
||||
* Get the internal name of the target class for this context
|
||||
*
|
||||
* @return internal target class name
|
||||
*/
|
||||
public abstract String getTargetClassRef();
|
||||
|
||||
/**
|
||||
* Get the reference mapper for this mixin
|
||||
*
|
||||
|
||||
@@ -1033,7 +1033,7 @@ public final class ClassInfo extends TreeInfo {
|
||||
* anywhere
|
||||
*/
|
||||
public boolean hasSuperClass(ClassInfo superClass) {
|
||||
return this.hasSuperClass(superClass, Traversal.NONE);
|
||||
return this.hasSuperClass(superClass, Traversal.NONE, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1045,11 +1045,24 @@ public final class ClassInfo extends TreeInfo {
|
||||
* anywhere
|
||||
*/
|
||||
public boolean hasSuperClass(ClassInfo superClass, Traversal traversal) {
|
||||
return this.hasSuperClass(superClass, traversal, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test whether this class has the specified superclass in its hierarchy
|
||||
*
|
||||
* @param superClass Superclass to search for in the hierarchy
|
||||
* @param traversal Traversal type to allow during this lookup
|
||||
* @param includeInterfaces True to include interfaces in the lookup
|
||||
* @return true if the specified class appears in the class's hierarchy
|
||||
* anywhere
|
||||
*/
|
||||
public boolean hasSuperClass(ClassInfo superClass, Traversal traversal, boolean includeInterfaces) {
|
||||
if (ClassInfo.OBJECT == superClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.findSuperClass(superClass.name, traversal) != null;
|
||||
|
||||
return this.findSuperClass(superClass.name, traversal, includeInterfaces) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1072,29 +1085,48 @@ public final class ClassInfo extends TreeInfo {
|
||||
* @return Matched superclass or null if not found
|
||||
*/
|
||||
public ClassInfo findSuperClass(String superClass, Traversal traversal) {
|
||||
return this.findSuperClass(superClass, traversal, false, new HashSet<String>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the specified superclass in this class's hierarchy. If found
|
||||
* returns the ClassInfo, otherwise returns null
|
||||
*
|
||||
* @param superClass Superclass name to search for
|
||||
* @param traversal Traversal type to allow during this lookup
|
||||
* @param includeInterfaces True to include interfaces in the lookup
|
||||
* @return Matched superclass or null if not found
|
||||
*/
|
||||
public ClassInfo findSuperClass(String superClass, Traversal traversal, boolean includeInterfaces) {
|
||||
if (ClassInfo.OBJECT.name == superClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.findSuperClass(superClass, traversal, new HashSet<String>());
|
||||
return this.findSuperClass(superClass, traversal, includeInterfaces, new HashSet<String>());
|
||||
}
|
||||
|
||||
private ClassInfo findSuperClass(String superClass, Traversal traversal, Set<String> traversed) {
|
||||
private ClassInfo findSuperClass(String superClass, Traversal traversal, boolean includeInterfaces, Set<String> traversed) {
|
||||
ClassInfo superClassInfo = this.getSuperClass();
|
||||
if (superClassInfo != null) {
|
||||
List<ClassInfo> targets = superClassInfo.getTargets();
|
||||
for (ClassInfo superTarget : targets) {
|
||||
for (ClassInfo superTarget : superClassInfo.getTargets()) {
|
||||
if (superClass.equals(superTarget.getName())) {
|
||||
return superClassInfo;
|
||||
}
|
||||
|
||||
ClassInfo found = superTarget.findSuperClass(superClass, traversal.next(), traversed);
|
||||
ClassInfo found = superTarget.findSuperClass(superClass, traversal.next(), includeInterfaces, traversed);
|
||||
if (found != null) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (includeInterfaces) {
|
||||
ClassInfo iface = this.findInterface(superClass);
|
||||
if (iface != null) {
|
||||
return iface;
|
||||
}
|
||||
}
|
||||
|
||||
if (traversal.canTraverse()) {
|
||||
for (MixinInfo mixin : this.mixins) {
|
||||
String mixinClassName = mixin.getClassName();
|
||||
@@ -1106,7 +1138,7 @@ public final class ClassInfo extends TreeInfo {
|
||||
if (superClass.equals(mixinClass.getName())) {
|
||||
return mixinClass;
|
||||
}
|
||||
ClassInfo targetSuper = mixinClass.findSuperClass(superClass, Traversal.ALL, traversed);
|
||||
ClassInfo targetSuper = mixinClass.findSuperClass(superClass, Traversal.ALL, includeInterfaces, traversed);
|
||||
if (targetSuper != null) {
|
||||
return targetSuper;
|
||||
}
|
||||
@@ -1116,6 +1148,20 @@ public final class ClassInfo extends TreeInfo {
|
||||
return null;
|
||||
}
|
||||
|
||||
private ClassInfo findInterface(String superClass) {
|
||||
for (String ifaceName : this.getInterfaces()) {
|
||||
ClassInfo iface = ClassInfo.forName(ifaceName);
|
||||
if (superClass.equals(ifaceName)) {
|
||||
return iface;
|
||||
}
|
||||
ClassInfo superIface = iface.findInterface(superClass);
|
||||
if (superIface != null) {
|
||||
return superIface;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks up this class's hierarchy to find the first class targetted by the
|
||||
* specified mixin. This is used during mixin application to translate a
|
||||
@@ -1647,4 +1693,110 @@ public final class ClassInfo extends TreeInfo {
|
||||
}
|
||||
return ClassInfo.forName(type.getClassName().replace('.', '/'));
|
||||
}
|
||||
|
||||
/**
|
||||
* ASM logic applied via ClassInfo, returns first common superclass of
|
||||
* classes specified by <tt>type1</tt> and <tt>type2</tt>.
|
||||
*
|
||||
* @param type1 First type
|
||||
* @param type2 Second type
|
||||
* @return common superclass info
|
||||
*/
|
||||
public static ClassInfo getCommonSuperClass(String type1, String type2) {
|
||||
if (type1 == null || type2 == null) {
|
||||
return ClassInfo.OBJECT;
|
||||
}
|
||||
return ClassInfo.getCommonSuperClass(ClassInfo.forName(type1), ClassInfo.forName(type2));
|
||||
}
|
||||
|
||||
/**
|
||||
* ASM logic applied via ClassInfo, returns first common superclass of
|
||||
* classes specified by <tt>type1</tt> and <tt>type2</tt>.
|
||||
*
|
||||
* @param type1 First type
|
||||
* @param type2 Second type
|
||||
* @return common superclass info
|
||||
*/
|
||||
public static ClassInfo getCommonSuperClass(org.spongepowered.asm.lib.Type type1, org.spongepowered.asm.lib.Type type2) {
|
||||
if (type1 == null || type2 == null
|
||||
|| type1.getSort() != org.spongepowered.asm.lib.Type.OBJECT || type2.getSort() != org.spongepowered.asm.lib.Type.OBJECT) {
|
||||
return ClassInfo.OBJECT;
|
||||
}
|
||||
return ClassInfo.getCommonSuperClass(ClassInfo.forType(type1), ClassInfo.forType(type2));
|
||||
}
|
||||
|
||||
/**
|
||||
* ASM logic applied via ClassInfo, returns first common superclass of
|
||||
* classes specified by <tt>type1</tt> and <tt>type2</tt>.
|
||||
*
|
||||
* @param type1 First type
|
||||
* @param type2 Second type
|
||||
* @return common superclass info
|
||||
*/
|
||||
private static ClassInfo getCommonSuperClass(ClassInfo type1, ClassInfo type2) {
|
||||
return ClassInfo.getCommonSuperClass(type1, type2, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* ASM logic applied via ClassInfo, returns first common superclass of
|
||||
* classes specified by <tt>type1</tt> and <tt>type2</tt>.
|
||||
*
|
||||
* @param type1 First type
|
||||
* @param type2 Second type
|
||||
* @return common superclass info
|
||||
*/
|
||||
public static ClassInfo getCommonSuperClassOrInterface(String type1, String type2) {
|
||||
if (type1 == null || type2 == null) {
|
||||
return ClassInfo.OBJECT;
|
||||
}
|
||||
return ClassInfo.getCommonSuperClassOrInterface(ClassInfo.forName(type1), ClassInfo.forName(type2));
|
||||
}
|
||||
|
||||
/**
|
||||
* ASM logic applied via ClassInfo, returns first common superclass of
|
||||
* classes specified by <tt>type1</tt> and <tt>type2</tt>.
|
||||
*
|
||||
* @param type1 First type
|
||||
* @param type2 Second type
|
||||
* @return common superclass info
|
||||
*/
|
||||
public static ClassInfo getCommonSuperClassOrInterface(org.spongepowered.asm.lib.Type type1, org.spongepowered.asm.lib.Type type2) {
|
||||
if (type1 == null || type2 == null
|
||||
|| type1.getSort() != org.spongepowered.asm.lib.Type.OBJECT || type2.getSort() != org.spongepowered.asm.lib.Type.OBJECT) {
|
||||
return ClassInfo.OBJECT;
|
||||
}
|
||||
return ClassInfo.getCommonSuperClassOrInterface(ClassInfo.forType(type1), ClassInfo.forType(type2));
|
||||
}
|
||||
|
||||
/**
|
||||
* ASM logic applied via ClassInfo, returns first common superclass or
|
||||
* interface of classes specified by <tt>type1</tt> and <tt>type2</tt>.
|
||||
*
|
||||
* @param type1 First type
|
||||
* @param type2 Second type
|
||||
* @return common superclass info
|
||||
*/
|
||||
public static ClassInfo getCommonSuperClassOrInterface(ClassInfo type1, ClassInfo type2) {
|
||||
return ClassInfo.getCommonSuperClass(type1, type2, true);
|
||||
}
|
||||
|
||||
private static ClassInfo getCommonSuperClass(ClassInfo type1, ClassInfo type2, boolean includeInterfaces) {
|
||||
if (type1.hasSuperClass(type2, Traversal.NONE, includeInterfaces)) {
|
||||
return type2;
|
||||
} else if (type2.hasSuperClass(type1, Traversal.NONE, includeInterfaces)) {
|
||||
return type1;
|
||||
} else if (type1.isInterface() || type2.isInterface()) {
|
||||
return ClassInfo.OBJECT;
|
||||
}
|
||||
|
||||
do {
|
||||
type1 = type1.getSuperClass();
|
||||
if (type1 == null) {
|
||||
return ClassInfo.OBJECT;
|
||||
}
|
||||
} while (!type2.hasSuperClass(type1, Traversal.NONE, includeInterfaces));
|
||||
|
||||
return type1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,8 +33,6 @@ import org.spongepowered.asm.lib.ClassWriter;
|
||||
*/
|
||||
public class MixinClassWriter extends ClassWriter {
|
||||
|
||||
private static final String JAVA_LANG_OBJECT = "java/lang/Object";
|
||||
|
||||
public MixinClassWriter(int flags) {
|
||||
super(flags);
|
||||
}
|
||||
@@ -49,27 +47,7 @@ public class MixinClassWriter extends ClassWriter {
|
||||
*/
|
||||
@Override
|
||||
protected String getCommonSuperClass(final String type1, final String type2) {
|
||||
ClassInfo t1 = ClassInfo.forName(type1);
|
||||
ClassInfo t2 = ClassInfo.forName(type2);
|
||||
|
||||
if (t1.hasSuperClass(t2)) {
|
||||
return type2;
|
||||
}
|
||||
if (t2.hasSuperClass(t1)) {
|
||||
return type1;
|
||||
}
|
||||
if (t1.isInterface() || t2.isInterface()) {
|
||||
return MixinClassWriter.JAVA_LANG_OBJECT;
|
||||
}
|
||||
|
||||
do {
|
||||
t1 = t1.getSuperClass();
|
||||
if (t1 == null) {
|
||||
return MixinClassWriter.JAVA_LANG_OBJECT;
|
||||
}
|
||||
} while (!t2.hasSuperClass(t1));
|
||||
|
||||
return t1.getName();
|
||||
return ClassInfo.getCommonSuperClass(type1, type2).getName();
|
||||
}
|
||||
|
||||
}
|
||||
+18
-2
@@ -56,6 +56,7 @@ import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.Bytecode.Visibility;
|
||||
import org.spongepowered.asm.util.Constants;
|
||||
import org.spongepowered.asm.util.throwables.SyntheticBridgeException;
|
||||
|
||||
/**
|
||||
* <p>Mixin bytecode pre-processor. This class is responsible for bytecode pre-
|
||||
@@ -464,8 +465,23 @@ class MixinPreProcessorStandard {
|
||||
|
||||
AnnotationNode unique = Annotations.getVisible(mixinMethod, Unique.class);
|
||||
if (unique == null || !Annotations.<Boolean>getValue(unique, "silent", Boolean.FALSE).booleanValue()) {
|
||||
MixinPreProcessorStandard.logger.warn("Discarding {} public method {} in {} because it already exists in {}", type, mixinMethod.name,
|
||||
this.mixin, context.getTarget());
|
||||
if (Bytecode.hasFlag(mixinMethod, Opcodes.ACC_BRIDGE)) {
|
||||
try {
|
||||
// will throw exception if bridge methods are incompatible
|
||||
Bytecode.compareBridgeMethods(target, mixinMethod);
|
||||
MixinPreProcessorStandard.logger.debug("Discarding sythetic bridge method {} in {} because existing method in {} is compatible",
|
||||
type, mixinMethod.name, this.mixin, context.getTarget());
|
||||
} catch (SyntheticBridgeException ex) {
|
||||
if (this.verboseLogging || this.env.getOption(Option.DEBUG_VERIFY)) {
|
||||
// Show analysis if
|
||||
ex.printAnalysis(context, target, mixinMethod);
|
||||
}
|
||||
throw new InvalidMixinException(this.mixin, ex.getMessage());
|
||||
}
|
||||
} else {
|
||||
MixinPreProcessorStandard.logger.warn("Discarding {} public method {} in {} because it already exists in {}", type, mixinMethod.name,
|
||||
this.mixin, context.getTarget());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -225,6 +225,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
}
|
||||
|
||||
void addMixinMethod(MethodNode method) {
|
||||
Annotations.setVisible(method, MixinMerged.class, "mixin", this.getClassName());
|
||||
this.getTarget().addMixinMethod(method);
|
||||
}
|
||||
|
||||
@@ -313,6 +314,7 @@ public class MixinTargetContext extends ClassContext implements IMixinContext {
|
||||
* @return the reference of the target class (only valid on single-target
|
||||
* mixins)
|
||||
*/
|
||||
@Override
|
||||
public String getTargetClassRef() {
|
||||
return this.getTarget().getClassRef();
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -50,6 +51,8 @@ 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 org.spongepowered.asm.util.throwables.SyntheticBridgeException;
|
||||
import org.spongepowered.asm.util.throwables.SyntheticBridgeException.Problem;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.primitives.Ints;
|
||||
@@ -416,7 +419,7 @@ public final class Bytecode {
|
||||
* the {@link Opcodes} class have the same value as opcodes
|
||||
*/
|
||||
public static String getOpcodeName(AbstractInsnNode node) {
|
||||
return Bytecode.getOpcodeName(node.getOpcode());
|
||||
return node != null ? Bytecode.getOpcodeName(node.getOpcode()) : "";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1105,4 +1108,53 @@ public final class Bytecode {
|
||||
return Pattern.compile(sb.append(");$").toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two synthetic bridge methods and throws an exception if they are
|
||||
* not compatible.
|
||||
*
|
||||
* @param a Incumbent method
|
||||
* @param b Incoming method
|
||||
*/
|
||||
public static void compareBridgeMethods(MethodNode a, MethodNode b) {
|
||||
ListIterator<AbstractInsnNode> ia = a.instructions.iterator();
|
||||
ListIterator<AbstractInsnNode> ib = b.instructions.iterator();
|
||||
|
||||
int index = 0;
|
||||
for (; ia.hasNext() && ib.hasNext(); index++) {
|
||||
AbstractInsnNode na = ia.next();
|
||||
AbstractInsnNode nb = ib.next();
|
||||
if (na instanceof LabelNode) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (na instanceof MethodInsnNode) {
|
||||
MethodInsnNode ma = (MethodInsnNode)na;
|
||||
MethodInsnNode mb = (MethodInsnNode)nb;
|
||||
if (!ma.name.equals(mb.name)) {
|
||||
throw new SyntheticBridgeException(Problem.BAD_INVOKE_NAME, a.name, a.desc, index, na, nb);
|
||||
} else if (!ma.desc.equals(mb.desc)) {
|
||||
throw new SyntheticBridgeException(Problem.BAD_INVOKE_DESC, a.name, a.desc, index, na, nb);
|
||||
}
|
||||
} else if (na.getOpcode() != nb.getOpcode()) {
|
||||
throw new SyntheticBridgeException(Problem.BAD_INSN, a.name, a.desc, index, na, nb);
|
||||
} else if (na instanceof VarInsnNode) {
|
||||
VarInsnNode va = (VarInsnNode)na;
|
||||
VarInsnNode vb = (VarInsnNode)nb;
|
||||
if (va.var != vb.var) {
|
||||
throw new SyntheticBridgeException(Problem.BAD_LOAD, a.name, a.desc, index, na, nb);
|
||||
}
|
||||
} else if (na instanceof TypeInsnNode) {
|
||||
TypeInsnNode ta = (TypeInsnNode)na;
|
||||
TypeInsnNode tb = (TypeInsnNode)nb;
|
||||
if (ta.getOpcode() == Opcodes.CHECKCAST && !ta.desc.equals(tb.desc)) {
|
||||
throw new SyntheticBridgeException(Problem.BAD_CAST, a.name, a.desc, index, na, nb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ia.hasNext() || ib.hasNext()) {
|
||||
throw new SyntheticBridgeException(Problem.BAD_LENGTH, a.name, a.desc, index, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
* 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.asm.util.throwables;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import org.spongepowered.asm.lib.Type;
|
||||
import org.spongepowered.asm.lib.tree.AbstractInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.MethodNode;
|
||||
import org.spongepowered.asm.lib.tree.TypeInsnNode;
|
||||
import org.spongepowered.asm.lib.tree.VarInsnNode;
|
||||
import org.spongepowered.asm.mixin.refmap.IMixinContext;
|
||||
import org.spongepowered.asm.mixin.throwables.MixinException;
|
||||
import org.spongepowered.asm.mixin.transformer.ClassInfo;
|
||||
import org.spongepowered.asm.mixin.transformer.meta.MixinMerged;
|
||||
import org.spongepowered.asm.util.Annotations;
|
||||
import org.spongepowered.asm.util.Bytecode;
|
||||
import org.spongepowered.asm.util.PrettyPrinter;
|
||||
|
||||
/**
|
||||
* Exception thrown when comparing two synthetic bridge methods fails, thrown
|
||||
* with the corresponding problem and functionality to support debugging when
|
||||
* verbosity is enabled.
|
||||
*/
|
||||
public class SyntheticBridgeException extends MixinException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* Problem types for synthetic bridge comparisons
|
||||
*/
|
||||
public enum Problem {
|
||||
|
||||
BAD_INSN("Conflicting opcodes %4$s and %5$s at offset %3$d in synthetic bridge method %1$s%2$s"),
|
||||
BAD_LOAD("Conflicting variable access at offset %3$d in synthetic bridge method %1$s%2$s"),
|
||||
BAD_CAST("Conflicting type cast at offset %3$d in synthetic bridge method %1$s%2$s"),
|
||||
BAD_INVOKE_NAME("Conflicting synthetic bridge target method name in synthetic bridge method %1$s%2$s Existing:%6$s Incoming:%7$s"),
|
||||
BAD_INVOKE_DESC("Conflicting synthetic bridge target method descriptor in synthetic bridge method %1$s%2$s Existing:%8$s Incoming:%9$s"),
|
||||
BAD_LENGTH("Mismatched bridge method length for synthetic bridge method %1$s%2$s unexpected extra opcode at offset %3$d");
|
||||
|
||||
private final String message;
|
||||
|
||||
private Problem(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
String getMessage(String name, String desc, int index, AbstractInsnNode a, AbstractInsnNode b) {
|
||||
return String.format(this.message, name, desc, index, Bytecode.getOpcodeName(a), Bytecode.getOpcodeName(a),
|
||||
Problem.getInsnName(a), Problem.getInsnName(b), Problem.getInsnDesc(a), Problem.getInsnDesc(b));
|
||||
}
|
||||
|
||||
private static String getInsnName(AbstractInsnNode node) {
|
||||
return node instanceof MethodInsnNode ? ((MethodInsnNode)node).name : "";
|
||||
}
|
||||
|
||||
private static String getInsnDesc(AbstractInsnNode node) {
|
||||
return node instanceof MethodInsnNode ? ((MethodInsnNode)node).desc : "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Problem?
|
||||
*/
|
||||
private final Problem problem;
|
||||
|
||||
/**
|
||||
* Bridge method name and descriptor
|
||||
*/
|
||||
private final String name, desc;
|
||||
|
||||
/**
|
||||
* Opcode index where the mismatch occurred
|
||||
*/
|
||||
private final int index;
|
||||
|
||||
/**
|
||||
* Mismatched nodes, a is from the target class, b is from the incoming
|
||||
* class
|
||||
*/
|
||||
private final AbstractInsnNode a, b;
|
||||
|
||||
public SyntheticBridgeException(Problem problem, String name, String desc, int index, AbstractInsnNode a, AbstractInsnNode b) {
|
||||
super(problem.getMessage(name, desc, index, a, b));
|
||||
this.problem = problem;
|
||||
this.name = name;
|
||||
this.desc = desc;
|
||||
this.index = index;
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context Incoming mixin
|
||||
* @param mda Target method
|
||||
* @param mdb Incoming method
|
||||
*/
|
||||
public void printAnalysis(IMixinContext context, MethodNode mda, MethodNode mdb) {
|
||||
PrettyPrinter printer = new PrettyPrinter();
|
||||
printer.addWrapped(100, this.getMessage()).hr();
|
||||
printer.add().kv("Method", this.name + this.desc).kv("Problem Type", this.problem).add().hr();
|
||||
String merged = Annotations.<String>getValue(Annotations.getVisible(mda, MixinMerged.class), "mixin");
|
||||
String owner = merged != null ? merged : context.getTargetClassRef().replace('/', '.');
|
||||
this.printMethod(printer.add("Existing method").add().kv("Owner", owner).add(), mda).hr();
|
||||
this.printMethod(printer.add("Incoming method").add().kv("Owner", context.getClassRef().replace('/', '.')).add(), mdb).hr();
|
||||
this.printProblem(printer, context, mda, mdb).print(System.err);
|
||||
}
|
||||
|
||||
private PrettyPrinter printMethod(PrettyPrinter printer, MethodNode method) {
|
||||
int index = 0;
|
||||
for (Iterator<AbstractInsnNode> iter = method.instructions.iterator(); iter.hasNext(); index++) {
|
||||
printer.kv(index == this.index ? ">>>>" : "", Bytecode.describeNode(iter.next()));
|
||||
}
|
||||
return printer.add();
|
||||
}
|
||||
|
||||
private PrettyPrinter printProblem(PrettyPrinter printer, IMixinContext context, MethodNode mda, MethodNode mdb) {
|
||||
Type target = Type.getObjectType(context.getTargetClassRef());
|
||||
|
||||
printer.add("Analysis").add();
|
||||
switch (this.problem) {
|
||||
case BAD_INSN:
|
||||
printer.add("The bridge methods are not compatible because they contain incompatible opcodes");
|
||||
printer.add("at index " + this.index + ":").add();
|
||||
printer.kv("Existing opcode: %s", Bytecode.getOpcodeName(this.a));
|
||||
printer.kv("Incoming opcode: %s", Bytecode.getOpcodeName(this.b)).add();
|
||||
printer.add("This implies that the bridge methods are from different interfaces. This problem");
|
||||
printer.add("may not be resolvable without changing the base interfaces.").add();
|
||||
break;
|
||||
|
||||
case BAD_LOAD:
|
||||
printer.add("The bridge methods are not compatible because they contain different variables at");
|
||||
printer.add("opcode index " + this.index + ".").add();
|
||||
|
||||
ListIterator<AbstractInsnNode> ia = mda.instructions.iterator();
|
||||
ListIterator<AbstractInsnNode> ib = mdb.instructions.iterator();
|
||||
|
||||
Type[] argsa = Type.getArgumentTypes(mda.desc);
|
||||
Type[] argsb = Type.getArgumentTypes(mdb.desc);
|
||||
for (int index = 0; ia.hasNext() && ib.hasNext(); index++) {
|
||||
AbstractInsnNode na = ia.next();
|
||||
AbstractInsnNode nb = ib.next();
|
||||
if (na instanceof VarInsnNode && nb instanceof VarInsnNode) {
|
||||
VarInsnNode va = (VarInsnNode)na;
|
||||
VarInsnNode vb = (VarInsnNode)nb;
|
||||
|
||||
Type ta = va.var > 0 ? argsa[va.var - 1] : target;
|
||||
Type tb = vb.var > 0 ? argsb[vb.var - 1] : target;
|
||||
printer.kv("Target " + index, "%8s %-2d %s", Bytecode.getOpcodeName(va), va.var, ta);
|
||||
printer.kv("Incoming " + index, "%8s %-2d %s", Bytecode.getOpcodeName(vb), vb.var, tb);
|
||||
|
||||
if (ta.equals(tb)) {
|
||||
printer.kv("", "Types match: %s", ta);
|
||||
} else if (ta.getSort() != tb.getSort()) {
|
||||
printer.kv("", "Types are incompatible");
|
||||
} else if (ta.getSort() == Type.OBJECT) {
|
||||
ClassInfo superClass = ClassInfo.getCommonSuperClassOrInterface(ta, tb);
|
||||
printer.kv("", "Common supertype: %s", superClass);
|
||||
}
|
||||
|
||||
printer.add();
|
||||
}
|
||||
}
|
||||
|
||||
printer.add("Since this probably means that the methods come from different interfaces, you");
|
||||
printer.add("may have a \"multiple inheritance\" problem, it may not be possible to implement");
|
||||
printer.add("both root interfaces");
|
||||
break;
|
||||
|
||||
case BAD_CAST:
|
||||
printer.add("Incompatible CHECKCAST encountered at opcode " + this.index + ", this could indicate that the bridge");
|
||||
printer.add("is casting down for contravariant generic types. It may be possible to coalesce the");
|
||||
printer.add("bridges by adjusting the types in the target method.").add();
|
||||
|
||||
Type ta = Type.getObjectType(((TypeInsnNode)this.a).desc);
|
||||
Type tb = Type.getObjectType(((TypeInsnNode)this.b).desc);
|
||||
printer.kv("Target type", ta);
|
||||
printer.kv("Incoming type", tb);
|
||||
printer.kv("Common supertype", ClassInfo.getCommonSuperClassOrInterface(ta, tb)).add();
|
||||
break;
|
||||
|
||||
case BAD_INVOKE_NAME:
|
||||
printer.add("Incompatible invocation targets in synthetic bridge. This is extremely unusual");
|
||||
printer.add("and implies that a remapping transformer has incorrectly remapped a method. This");
|
||||
printer.add("is an unrecoverable error.");
|
||||
break;
|
||||
|
||||
case BAD_INVOKE_DESC:
|
||||
MethodInsnNode mdna = (MethodInsnNode)this.a;
|
||||
MethodInsnNode mdnb = (MethodInsnNode)this.b;
|
||||
|
||||
Type[] arga = Type.getArgumentTypes(mdna.desc);
|
||||
Type[] argb = Type.getArgumentTypes(mdnb.desc);
|
||||
|
||||
if (arga.length != argb.length) {
|
||||
int argCount = Type.getArgumentTypes(mda.desc).length;
|
||||
String winner = arga.length == argCount ? "The TARGET" : (argb.length == argCount ? " The INCOMING" : "NEITHER");
|
||||
|
||||
printer.add("Mismatched invocation descriptors in synthetic bridge implies that a remapping");
|
||||
printer.add("transformer has incorrectly coalesced a bridge method with a conflicting name.");
|
||||
printer.add("Overlapping bridge methods should always have the same number of arguments, yet");
|
||||
printer.add("the target method has %d arguments, the incoming method has %d. This is an", arga.length, argb.length);
|
||||
printer.add("unrecoverable error. %s method has the expected arg count of %d", winner, argCount);
|
||||
break;
|
||||
}
|
||||
|
||||
Type rta = Type.getReturnType(mdna.desc);
|
||||
Type rtb = Type.getReturnType(mdnb.desc);
|
||||
|
||||
printer.add("Incompatible invocation descriptors in synthetic bridge implies that generified");
|
||||
printer.add("types are incompatible over one or more generic superclasses or interfaces. It may");
|
||||
printer.add("be possible to adjust the generic types on implemented members to rectify this");
|
||||
printer.add("problem by coalescing the appropriate generic types.").add();
|
||||
|
||||
this.printTypeComparison(printer, "return type", rta, rtb);
|
||||
for (int i = 0; i < arga.length; i++) {
|
||||
this.printTypeComparison(printer, "arg " + i, arga[i], argb[i]);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case BAD_LENGTH:
|
||||
printer.add("Mismatched bridge method length implies the bridge methods are incompatible");
|
||||
printer.add("and may originate from different superinterfaces. This is an unrecoverable");
|
||||
printer.add("error.").add();
|
||||
break;
|
||||
|
||||
default :
|
||||
break;
|
||||
|
||||
}
|
||||
return printer;
|
||||
}
|
||||
|
||||
private PrettyPrinter printTypeComparison(PrettyPrinter printer, String index, Type tpa, Type tpb) {
|
||||
printer.kv("Target " + index, "%s", tpa);
|
||||
printer.kv("Incoming " + index, "%s", tpb);
|
||||
|
||||
if (tpa.equals(tpb)) {
|
||||
printer.kv("Analysis", "Types match: %s", tpa);
|
||||
} else if (tpa.getSort() != tpb.getSort()) {
|
||||
printer.kv("Analysis", "Types are incompatible");
|
||||
} else if (tpa.getSort() == Type.OBJECT) {
|
||||
ClassInfo superClass = ClassInfo.getCommonSuperClassOrInterface(tpa, tpb);
|
||||
printer.kv("Analysis", "Common supertype: L%s;", superClass);
|
||||
}
|
||||
|
||||
return printer.add();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user