Use more accurate coordinates for ModifyVariable context decorations

Fixes #511
This commit is contained in:
Mumfrey committed 2021-07-13 19:28:04 +01:00
1 parent fb0da4dba1
commit b0cd24bd63
2 files changed
+45 -13

No files matched your search

@@ -65,17 +65,17 @@ public class LocalVariableDiscriminator {
/**
* Ordinal value of this local variable type
*/
int ord = 0;
private int ord = -1;
/**
* Local variable name
*/
String name;
final String name;
/**
* Local variable type
*/
Type type;
final Type type;
public Local(String name, Type type) {
this.name = name;
@@ -87,6 +87,17 @@ public class LocalVariableDiscriminator {
return String.format("Local[ordinal=%d, name=%s, type=%s]", this.ord, this.name, this.type);
}
void setOrdinal(int ordinal) {
if (this.ord > -1 && this.ord != ordinal) {
throw new IllegalStateException("Attempted to reset ordinal for computed local");
}
this.ord = ordinal;
}
int getOrdinal() {
return this.ord;
}
}
/**
@@ -166,7 +177,7 @@ public class LocalVariableDiscriminator {
if (this.locals[l] != null) {
ordinal = ordinalMap.get(this.locals[l].type);
ordinalMap.put(this.locals[l].type, ordinal = Integer.valueOf(ordinal == null ? 0 : ordinal.intValue() + 1));
this.locals[l].ord = ordinal.intValue();
this.locals[l].setOrdinal(ordinal.intValue());
}
}
}
@@ -189,7 +200,7 @@ public class LocalVariableDiscriminator {
if (local != null) {
Type localType = local.type;
String localName = local.name;
int ordinal = local.ord;
int ordinal = local.getOrdinal();
String candidate = this.returnType.equals(localType) ? "YES" : "-";
printer.add("[%3d] [%3d] %30s %-50s %s", l, ordinal, SignaturePrinter.getTypeName(localType, false), localName, candidate);
} else if (l > 0) {
@@ -366,7 +377,7 @@ public class LocalVariableDiscriminator {
continue;
}
if (this.ordinal > -1) {
if (this.ordinal == local.ord) {
if (this.ordinal == local.getOrdinal()) {
return index;
}
continue;
@@ -93,11 +93,6 @@ public class ModifyVariableInjector extends Injector {
abstract boolean find(InjectionInfo info, InsnList insns, Collection<AbstractInsnNode> nodes, Target target);
}
/**
* Decorator key for Context decoration
*/
private static final String LOCALS_CONTEXT_KEY = "localcontext";
/**
* True to consider only method args
@@ -142,10 +137,27 @@ public class ModifyVariableInjector extends Injector {
}
}
/**
* Generate a key which uniquely identifies the combination of return type,
* frame type and target injection node so that injectors targetting the
* same instruction still get unique contexts.
*
* @param target Target method
* @param node Target node
* @return Key for storing/retrieving the injector context decoration
*/
protected String getTargetNodeKey(Target target, InjectionNode node) {
return String.format("localcontext(%s,%s,#%s)", this.returnType, this.discriminator.isArgsOnly() ? "argsOnly" : "fullFrame", node.getId());
}
@Override
protected void preInject(Target target, InjectionNode node) {
String key = this.getTargetNodeKey(target, node);
if (node.hasDecoration(key)) {
return; // already have a suitable context
}
Context context = new Context(this.returnType, this.discriminator.isArgsOnly(), target, node.getCurrentTarget());
node.<Context>decorate(ModifyVariableInjector.LOCALS_CONTEXT_KEY, context);
node.<Context>decorate(key, context);
}
/**
@@ -157,13 +169,22 @@ public class ModifyVariableInjector extends Injector {
throw new InvalidInjectionException(this.info, "Variable modifier target for " + this + " was removed by another injector");
}
Context context = node.<Context>getDecoration(ModifyVariableInjector.LOCALS_CONTEXT_KEY);
Context context = node.<Context>getDecoration(this.getTargetNodeKey(target, node));
if (context == null) {
throw new InjectionError(String.format(
"%s injector target is missing CONTEXT decoration for %s. PreInjection failure or illegal internal state change",
this.annotationType, this.info));
}
// If the context is being reused (because two identical injectors are targetting this node)
// then the insns SHOULD have been drained by the previous insertBefore. If the list hasn't
// been cleared for some reason then something probably went wrong during the previous inject
if (context.insns.size() > 0) {
throw new InjectionError(String.format(
"%s injector target has contaminated CONTEXT decoration for %s. Check for previous errors.",
this.annotationType, this.info));
}
if (this.discriminator.printLVT()) {
this.printLocals(target, context);
}