Skip to content

Commit

Permalink
Merge pull request jruby#8045 from headius/reduce_invoker_exceptions
Browse files Browse the repository at this point in the history
Try to find class resource to avoid exception
  • Loading branch information
headius authored Dec 21, 2023
2 parents b576c9a + 793a9a5 commit 4365e40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

import java.io.PrintWriter;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -263,11 +264,11 @@ public Class getAnnotatedMethodClass(List<JavaMethodDescriptor> descs) {

Class superclass = determineSuperclass(info);

Class c = tryClass(generatedClassName, desc1.declaringClass, superclass);
Class c = tryClass(generatedClassName, generatedClassPath, desc1.declaringClass, superclass);
if (c == null) {
synchronized (syncObject) {
// try again
c = tryClass(generatedClassName, desc1.declaringClass, superclass);
c = tryClass(generatedClassName, generatedClassPath, desc1.declaringClass, superclass);
if (c == null) {
if (DEBUG) LOG.debug("Generating " + generatedClassName + ", min: " + info.getMin() + ", max: " + info.getMax() + ", hasBlock: " + info.isBlock() + ", rest: " + info.isRest());

Expand Down Expand Up @@ -631,14 +632,15 @@ private static void loadReceiver(String typePath, JavaMethodDescriptor desc, Ski
}
}

private Class tryClass(String name, Class targetClass, Class expectedSuperclass) {
private Class tryClass(String name, String path, Class targetClass, Class expectedSuperclass) {
final Class c;
try {
if (classLoader == null) {
c = Class.forName(name, true, classLoader);
} else {
c = classLoader.loadClass(name);
if (!classLoader.hasClass(name)) {
if (DEBUG) System.err.println("could not find class file for " + name);
seenUndefinedClasses = true;
return null;
}
c = classLoader.loadClass(name);
} catch (ClassNotFoundException e) {
if (DEBUG) LOG.debug(e);
seenUndefinedClasses = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.security.ProtectionDomain;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

public class ClassDefiningJRubyClassLoader extends URLClassLoader implements ClassDefiningClassLoader {

public final static ProtectionDomain DEFAULT_DOMAIN;

private final Set<String> definedClasses = new ConcurrentSkipListSet<>();

static {
ProtectionDomain defaultDomain = null;
try {
Expand All @@ -50,10 +55,24 @@ public ClassDefiningJRubyClassLoader(ClassLoader parent) {
}

public Class<?> defineClass(String name, byte[] bytes) {
return super.defineClass(name, bytes, 0, bytes.length, DEFAULT_DOMAIN);
Class<?> aClass = super.defineClass(name, bytes, 0, bytes.length, DEFAULT_DOMAIN);
definedClasses.add(name);
return aClass;
}

public Class<?> defineClass(String name, byte[] bytes, ProtectionDomain domain) {
return super.defineClass(name, bytes, 0, bytes.length, domain);
Class<?> aClass = super.defineClass(name, bytes, 0, bytes.length, domain);
definedClasses.add(name);
return aClass;
}

/**
* Return true if the class is loadable in this classloader, false otherwise.
*
* @param name the class name
* @return whether it's loadable
*/
public boolean hasClass(String name) {
return definedClasses.contains(name) || super.findResource(name.replace('.', '/') + ".class") != null;
}
}

0 comments on commit 4365e40

Please sign in to comment.