Skip to content

Commit

Permalink
Try to find class resource to avoid exception
Browse files Browse the repository at this point in the history
The exception thrown here when we cannot find a class can be very
expensive at boot time. For most third-party extensions, this
exception will be raised twice for every bound method, due to
double-checking the classloader under lock a second time. By first
looking for a .class resource, we can avoid the expensive
exception if it is unlikely to succeed.
  • Loading branch information
headius committed Dec 18, 2023
1 parent 627d8d1 commit f8c3015
Showing 1 changed file with 10 additions and 7 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,16 @@ 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);
URL resource = classLoader.findResource(path + ".class");
if (resource == null) {
if (DEBUG) LOG.debug("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

0 comments on commit f8c3015

Please sign in to comment.