Skip to content

Commit 0143a21

Browse files
committed
[GR-70821] Proper resolution of interface methods
PullRequest: graal/22445
2 parents 72524fa + 9822485 commit 0143a21

File tree

27 files changed

+879
-462
lines changed

27 files changed

+879
-462
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.truffle.espresso.shared.lookup;
26+
27+
import com.oracle.truffle.espresso.classfile.Constants;
28+
import com.oracle.truffle.espresso.shared.meta.ModifiersProvider;
29+
30+
/**
31+
* Provides a simple way of filtering elements with {@linkplain ModifiersProvider modifiers} (such
32+
* as classes, methods or fields), based on their declared flags.
33+
*/
34+
public final class LookupMode {
35+
public static final LookupMode ALL = new LookupMode(0, 0);
36+
public static final LookupMode INSTANCE_ONLY = new LookupMode(0, Constants.ACC_STATIC);
37+
public static final LookupMode STATIC_ONLY = new LookupMode(Constants.ACC_STATIC, 0);
38+
public static final LookupMode NON_STATIC_NON_PRIVATE = new LookupMode(0, Constants.ACC_STATIC | Constants.ACC_PRIVATE);
39+
public static final LookupMode PUBLIC_NON_STATIC = new LookupMode(Constants.ACC_PUBLIC, Constants.ACC_STATIC);
40+
41+
private final int positives;
42+
private final int negatives;
43+
44+
/**
45+
* Creates a new element filter.
46+
*
47+
* @param positives The flags that need to be set
48+
* @param negatives The flags that must not be set.
49+
*/
50+
public LookupMode(int positives, int negatives) {
51+
assert (positives & negatives) == 0 : "Empty mode: +" + Integer.toHexString(positives) + " and -" + Integer.toHexString(negatives);
52+
this.positives = positives;
53+
this.negatives = negatives;
54+
}
55+
56+
/**
57+
* Returns whether the given element is accepted by {@code this} filter. If {@code m} is
58+
* {@code null}, then this method returns {@code false}.
59+
*/
60+
public boolean include(ModifiersProvider m) {
61+
if (m == null) {
62+
return false;
63+
}
64+
int flags = m.getModifiers();
65+
return hasPositives(flags) && hasNoNegatives(flags);
66+
}
67+
68+
/**
69+
* Combines this filter with another.
70+
* <p>
71+
* Calling the resulting {@link #include(ModifiersProvider e)} is equivalent to
72+
* {@code this.include(e) && other.include(e)}.
73+
*/
74+
public LookupMode combine(LookupMode other) {
75+
int newPositives = this.positives | other.negatives;
76+
int newNegatives = this.negatives | other.negatives;
77+
if (newPositives == positives && newNegatives == negatives) {
78+
return this;
79+
}
80+
if (newPositives == other.positives && newNegatives == other.negatives) {
81+
return other;
82+
}
83+
return new LookupMode(newPositives, newNegatives);
84+
}
85+
86+
private boolean hasPositives(int flags) {
87+
return (flags & positives) == positives;
88+
}
89+
90+
private boolean hasNoNegatives(int flags) {
91+
return (flags & negatives) == 0;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.truffle.espresso.shared.lookup;
26+
27+
import java.io.Serial;
28+
29+
import com.oracle.truffle.espresso.shared.meta.MethodAccess;
30+
31+
/**
32+
* Checked exception thrown by the {@link MethodLookup} helper class when method lookup succeeds,
33+
* but direct invocation of the resulting method should trigger
34+
* {@link IncompatibleClassChangeError}.
35+
* <p>
36+
* Use of a checked exceptions should ensure that the result is not accidentally passed along or
37+
* invoked without consideration.
38+
*/
39+
public final class LookupSuccessInvocationFailure extends Exception {
40+
@Serial private static final long serialVersionUID = 1794882806666028197L;
41+
42+
LookupSuccessInvocationFailure(MethodAccess<?, ?, ?> m) {
43+
super(null, null);
44+
this.m = m;
45+
}
46+
47+
private final transient MethodAccess<?, ?, ?> m;
48+
49+
/**
50+
* Obtain the result of method lookup.
51+
*/
52+
@SuppressWarnings("unchecked")
53+
public <M extends MethodAccess<?, ?, ?>> M getResult() {
54+
return (M) m;
55+
}
56+
57+
@SuppressWarnings("sync-override")
58+
@Override
59+
public Throwable fillInStackTrace() {
60+
return this;
61+
}
62+
}

0 commit comments

Comments
 (0)