Skip to content

Commit 4a28b0b

Browse files
Allow adding both a static and non-static substitution for a method
This is useful when a method changes its static status.
1 parent 6fecfdf commit 4a28b0b

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/jni/JniEnv.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,7 @@ public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer Tr
22182218
}
22192219

22202220
Symbol<Type> classType = clazz.getMirrorKlass(getMeta()).getType();
2221-
getSubstitutions().registerRuntimeSubstitution(classType, name, signature, factory, true);
2221+
getSubstitutions().registerRuntimeSubstitution(classType, name, signature, targetMethod.isStatic(), factory, true);
22222222
return JNI_OK;
22232223
}
22242224

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/libs/Lib.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static List<MethodKey> getRefs(EspressoContext ctx, JavaSubstitution.Fac
118118
Symbol<Name> methodName = ctx.getNames().getOrCreate(mName);
119119
Symbol<Type> holderType = ctx.getTypes().fromClassGetName(holderName);
120120

121-
refs.add(new MethodKey(holderType, methodName, signature));
121+
refs.add(new MethodKey(holderType, methodName, signature, !factory.hasReceiver()));
122122
}
123123

124124
return refs;

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/runtime/MethodKey.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ public final class MethodKey {
3535
private final Symbol<Type> clazz;
3636
private final Symbol<Name> methodName;
3737
private final Symbol<Signature> signature;
38+
private final boolean isStatic;
3839
private final int hash;
3940

4041
public MethodKey(Method m) {
4142
this(m, m.getRawSignature());
4243
}
4344

4445
public MethodKey(Method m, Symbol<Signature> signature) {
45-
this(m.getDeclaringKlass().getType(), m.getName(), signature);
46+
this(m.getDeclaringKlass().getType(), m.getName(), signature, m.isStatic());
4647
}
4748

48-
public MethodKey(Symbol<Type> clazz, Symbol<Name> methodName, Symbol<Signature> signature) {
49+
public MethodKey(Symbol<Type> clazz, Symbol<Name> methodName, Symbol<Signature> signature, boolean isStatic) {
4950
assert clazz != null && methodName != null && signature != null;
5051
this.clazz = clazz;
5152
this.methodName = methodName;
5253
this.signature = signature;
53-
this.hash = Objects.hash(clazz, methodName, signature);
54+
this.isStatic = isStatic;
55+
this.hash = Objects.hash(clazz, methodName, signature, isStatic);
5456
}
5557

5658
@Override
@@ -64,7 +66,8 @@ public boolean equals(Object obj) {
6466
MethodKey other = (MethodKey) obj;
6567
return clazz == other.clazz &&
6668
methodName == other.methodName &&
67-
signature == other.signature;
69+
signature == other.signature &&
70+
isStatic == other.isStatic;
6871
}
6972

7073
@Override
@@ -74,7 +77,7 @@ public int hashCode() {
7477

7578
@Override
7679
public String toString() {
77-
return TypeSymbols.binaryName(clazz) + "#" + methodName + signature;
80+
return (isStatic ? "static " : "") + TypeSymbols.binaryName(clazz) + "#" + methodName + signature;
7881
}
7982

8083
public Symbol<Type> getHolderType() {
@@ -88,4 +91,8 @@ public Symbol<Name> getName() {
8891
public Symbol<Signature> getSignature() {
8992
return signature;
9093
}
94+
95+
public boolean isStatic() {
96+
return isStatic;
97+
}
9198
}

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/substitutions/Substitutions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ private static void registerStaticSubstitution(JavaSubstitution.Factory substitu
175175
}
176176

177177
private static void registerStaticSubstitution(Symbol<Type> type, Symbol<Name> methodName, Symbol<Signature> signature, JavaSubstitution.Factory factory, boolean throwIfPresent) {
178-
MethodKey key = new MethodKey(type, methodName, signature);
178+
MethodKey key = new MethodKey(type, methodName, signature, !factory.hasReceiver());
179179
if (throwIfPresent && STATIC_SUBSTITUTIONS.containsKey(key)) {
180180
throw EspressoError.shouldNotReachHere("substitution already registered" + key);
181181
}
182182
STATIC_SUBSTITUTIONS.put(key, factory);
183183
}
184184

185-
public void registerRuntimeSubstitution(Symbol<Type> type, Symbol<Name> methodName, Symbol<Signature> signature, EspressoRootNodeFactory factory, boolean throwIfPresent) {
186-
MethodKey key = new MethodKey(type, methodName, signature);
185+
public void registerRuntimeSubstitution(Symbol<Type> type, Symbol<Name> methodName, Symbol<Signature> signature, boolean isStatic, EspressoRootNodeFactory factory, boolean throwIfPresent) {
186+
MethodKey key = new MethodKey(type, methodName, signature, isStatic);
187187

188188
if (STATIC_SUBSTITUTIONS.containsKey(key)) {
189189
getLogger().log(Level.FINE, "Runtime substitution shadowed by static one: " + key);

0 commit comments

Comments
 (0)