Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 60 additions & 53 deletions core/src/main/java/org/jruby/javasupport/binding/MethodGatherer.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ public static void eachAccessibleMethod(final Class<?> javaClass, Predicate<Meth
try {
// add methods, including static if this is the actual class,
// and replacing child methods with equivalent parent methods
PartitionedMethods filteredMethods = FILTERED_DECLARED_METHODS.get(klass);
Method[][] filteredMethods = FILTERED_DECLARED_METHODS.get(klass);

if (!classProcessor.test(filteredMethods.instanceMethods)) return;
if (!classProcessor.test(filteredMethods[INSTANCE_METHODS])) return;

if (klass == javaClass) {
if (!classProcessor.test(filteredMethods.staticMethods)) return;
if (!classProcessor.test(filteredMethods[STATIC_METHODS])) return;
}
}
catch (SecurityException e) { /* ignored */ }
Expand All @@ -198,9 +198,9 @@ public static void eachAccessibleMethod(final Class<?> javaClass, Predicate<Meth
// add methods, not including static (should be none on
// interfaces anyway) and not replacing child methods with
// parent methods
PartitionedMethods filteredMethods = FILTERED_METHODS.get(iface);
Method[][] filteredMethods = FILTERED_METHODS.get(iface);

if (!interfaceProcessor.test(filteredMethods.instanceMethods)) return;
if (!interfaceProcessor.test(filteredMethods[INSTANCE_METHODS])) return;
}
catch (SecurityException e) { /* ignored */ }
}
Expand Down Expand Up @@ -243,7 +243,7 @@ private static boolean methodsAreEquivalent(Method child, Method parent) {

final String methodName = parent.getName();
for (Class superClass : child.getDeclaringClass().getInterfaces()) {
for (Method superMethod : FILTERED_DECLARED_METHODS.get(superClass).instanceMethods) {
for (Method superMethod : FILTERED_DECLARED_METHODS.get(superClass)[INSTANCE_METHODS]) {
if (methodName.equals(superMethod.getName()) && sameTypesAndAccessModifier(superMethod, child)) {
return true; // very same (child) method only re-declared
}
Expand Down Expand Up @@ -304,10 +304,16 @@ public Method[] computeValue(Class cls) {
}
};

private static final ClassValue<PartitionedMethods> FILTERED_DECLARED_METHODS = new ClassValue<PartitionedMethods>() {
// The filtered methods are stored as arrays of JDK types only. These ClassValues are computed for JDK classes
// too, which keep their values as long as they are loaded, so a value of a JRuby type would keep the class
// loader that loaded JRuby alive after every runtime in it has been torn down.
private static final int INSTANCE_METHODS = 0;
private static final int STATIC_METHODS = 1;

private static final ClassValue<Method[][]> FILTERED_DECLARED_METHODS = new ClassValue<Method[][]>() {
@Override
public PartitionedMethods computeValue(Class cls) {
return new PartitionedMethods(DECLARED_METHODS.get(cls));
public Method[][] computeValue(Class cls) {
return partitionMethods(DECLARED_METHODS.get(cls));
}
};
private static final ClassValue<Method[]> METHODS = new ClassValue<Method[]>() {
Expand All @@ -320,10 +326,10 @@ public Method[] computeValue(Class cls) {
}
}
};
private static final ClassValue<PartitionedMethods> FILTERED_METHODS = new ClassValue<PartitionedMethods>() {
private static final ClassValue<Method[][]> FILTERED_METHODS = new ClassValue<Method[][]>() {
@Override
public PartitionedMethods computeValue(Class cls) {
return new PartitionedMethods(METHODS.get(cls));
public Method[][] computeValue(Class cls) {
return partitionMethods(METHODS.get(cls));
}
};
private static final ClassValue<Class<?>[]> INTERFACES = new ClassValue<Class<?>[]>() {
Expand Down Expand Up @@ -699,53 +705,54 @@ void installInstanceMethods(ThreadContext context, final RubyModule proxy) {
getInstanceInstallers().forEach(($, value) -> value.install(context, proxy));
}

private static class PartitionedMethods {
final Method[] instanceMethods;
final Method[] staticMethods;

PartitionedMethods(Method[] methods) {
List<Method> instanceMethods = Collections.EMPTY_LIST;
List<Method> staticMethods = Collections.EMPTY_LIST;

for (Method m : methods) {
int modifiers = m.getModifiers();
if (filterAccessible(m, modifiers)) {
if (Modifier.isStatic(modifiers)) {
if (staticMethods == Collections.EMPTY_LIST) staticMethods = new ArrayList<>();
staticMethods.add(m);
} else {
if (instanceMethods == Collections.EMPTY_LIST) instanceMethods = new ArrayList<>();
instanceMethods.add(m);
}
/**
* Partitions the accessible methods into instance methods, at {@link #INSTANCE_METHODS}, and static methods, at
* {@link #STATIC_METHODS}.
*/
private static Method[][] partitionMethods(Method[] methods) {
List<Method> instanceMethods = Collections.EMPTY_LIST;
List<Method> staticMethods = Collections.EMPTY_LIST;

for (Method m : methods) {
int modifiers = m.getModifiers();
if (filterAccessible(m, modifiers)) {
if (Modifier.isStatic(modifiers)) {
if (staticMethods == Collections.EMPTY_LIST) staticMethods = new ArrayList<>();
staticMethods.add(m);
} else {
if (instanceMethods == Collections.EMPTY_LIST) instanceMethods = new ArrayList<>();
instanceMethods.add(m);
}
}

this.instanceMethods = instanceMethods.toArray(Method[]::new);
this.staticMethods = staticMethods.toArray(Method[]::new);
}

private static boolean filterAccessible(Method method, int mod) {
// Exclude bridge methods
if ((mod & ACC_BRIDGE) != 0) return false;

if (Modifier.isPublic(mod)) {
// Include public
return true;
} else if (Modifier.isPrivate(mod)) {
// Exclude private
return false;
} else if (Modifier.isProtected(mod)) {
// Include protected if they can be set accessible
Method[][] partitioned = new Method[2][];
partitioned[INSTANCE_METHODS] = instanceMethods.toArray(Method[]::new);
partitioned[STATIC_METHODS] = staticMethods.toArray(Method[]::new);
return partitioned;
}

private static boolean filterAccessible(Method method, int mod) {
// Exclude bridge methods
if ((mod & ACC_BRIDGE) != 0) return false;

if (Modifier.isPublic(mod)) {
// Include public
return true;
} else if (Modifier.isPrivate(mod)) {
// Exclude private
return false;
} else if (Modifier.isProtected(mod)) {
// Include protected if they can be set accessible
return Java.trySetAccessible(method);
} else {
// We split this on 17 because at that level unopened non-public functions are inaccessible
if (Platform.JAVA_VERSION < 17) {
// Include package-private if they can be set accessible on Java 16 and earlier
return Java.trySetAccessible(method);
} else {
// We split this on 17 because at that level unopened non-public functions are inaccessible
if (Platform.JAVA_VERSION < 17) {
// Include package-private if they can be set accessible on Java 16 and earlier
return Java.trySetAccessible(method);
} else {
// Exclude package-private on Java 17 and later
return false;
}
// Exclude package-private on Java 17 and later
return false;
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,10 @@ public synchronized char matches(String feature, Suffix suffix, int len, boolean
private final List<IRubyObject> featurePaths;
}

private final ThreadLocal<StringWrapper> keyWrapper = ThreadLocal.withInitial(() -> new StringWrapper(null, 0, 0));
private final ThreadLocal<StringWrapper> rangeWrapper = ThreadLocal.withInitial(() -> new StringWrapper(null, 0, 0));
// Only used while holding this searcher's lock. They are not thread-locals, because a thread would keep its
// wrappers after the runtime is torn down, and with them the class loader that loaded JRuby.
private final StringWrapper keyWrapper = new StringWrapper(null, 0, 0);
private final StringWrapper rangeWrapper = new StringWrapper(null, 0, 0);

protected synchronized void addSingleFeatureToIndex(String key, int beg, int end, IRubyObject featurePath) {
Map<StringWrapper, Feature> featuresIndex = this.loadedFeaturesIndex;
Expand All @@ -556,13 +558,13 @@ private synchronized void releaseWrapper(StringWrapper wrapper) {
}

private synchronized StringWrapper keyWrapper(String key) {
StringWrapper wrapper = keyWrapper.get();
StringWrapper wrapper = keyWrapper;
wrapper.rewrap(key, 0, key.length());
return wrapper;
}

private synchronized StringWrapper rangeWrapper(String key, int beg, int len) {
StringWrapper wrapper = rangeWrapper.get();
StringWrapper wrapper = rangeWrapper;
wrapper.rewrap(key, beg, len);
return wrapper;
}
Expand Down
Loading