Skip to content

Commit

Permalink
Added catch for NoClassDefFoundError wherever there was a ClassNotFou…
Browse files Browse the repository at this point in the history
…ndException (apache#5870)

Fixes apache#5726 

### Motivation

When running pulsar-io connectors and functions from the Intellij IDE some actions fail
due to uncaught class-not-found throwables.
The expectation being that the class is being dynamically loaded and only the ClassNotFoundException will occur if the class is not found.
When the function is created or run with https://pulsar.apache.org/docs/en/functions-deploying/#local-run-mode this is indeed the case.
When running under the control https://pulsar.apache.org/docs/en/functions-debug/#debug-with-localrun-mode as a gradle plugin the class may already be known and throw a NoClassDefFoundError.
It seems to me that any time ClassNotFoundException is handled then NoClassDefFoundError should also be caught.

### Modifications

Wherever there was a `catch (ClassNotFoundException ` I replaced it with 
`catch (ClassNotFoundException | NoClassDefFoundError ` .
There were multiple cases where the ClassNotFoundException were handled e.g. the jar loader failed so the nar loader was used to handle the jar loader's failure.
  • Loading branch information
phreed authored and sijie committed Dec 20, 2019
1 parent 5ffca81 commit 17e22d1
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ private void updateConfigurationAndRegisterListeners() {
addDynamicConfigValidator("loadManagerClassName", (className) -> {
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
log.warn("Configured load-manager class {} not found {}", className, e.getMessage());
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static <T> Class<T> newClassInstance(String className) {
// that loaded the API
return (Class<T>) Thread.currentThread().getContextClassLoader().loadClass(className);
}
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public <T> T newInstance(String key, Class<T> t, VerifiableProperties properties
Class<?> c = null;
try {
c = Class.forName(key);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException("class not found for :" + key);
}
if (c == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static NarClassLoader getFromArchive(File narPath, Set<String> additional
File unpacked = NarUnpacker.unpackNar(narPath, NAR_CACHE_DIR);
try {
return new NarClassLoader(unpacked, additionalJars, NarClassLoader.class.getClassLoader());
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IOException(e);
}
}
Expand All @@ -151,7 +151,7 @@ public static NarClassLoader getFromArchive(File narPath, Set<String> additional
File unpacked = NarUnpacker.unpackNar(narPath, NAR_CACHE_DIR);
try {
return new NarClassLoader(unpacked, additionalJars, parent);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IOException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static boolean isProtobufClass(Class<?> pojoClazz) {
try {
Class<?> protobufBaseClass = Class.forName("com.google.protobuf.GeneratedMessageV3");
return protobufBaseClass.isAssignableFrom(pojoClazz);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// If function does not have protobuf in classpath then it cannot be protobuf
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private TimestampExtractor<I> getTimeStampExtractor(WindowConfig windowConfig) {
try {
theCls = Class.forName(windowConfig.getTimestampExtractorClassName(),
true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
throw new RuntimeException(
String.format("Timestamp extractor class %s must be in class path",
windowConfig.getTimestampExtractorClassName()), cnfe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Object createInstance(String userClassName,
Class<?> theCls;
try {
theCls = Class.forName(userClassName, true, classLoader);
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
throw new RuntimeException("Class " + userClassName + " must be in class path", cnfe);
}
Object result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public static Object createInstance(String userClassName, ClassLoader classLoade
Class<?> theCls;
try {
theCls = Class.forName(userClassName);
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
try {
theCls = Class.forName(userClassName, true, classLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new RuntimeException("User class must be in class path", cnfe);
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public static void implementsClass(String className, Class<?> klass, ClassLoader
Class<?> objectClass;
try {
objectClass = loadClass(className, classLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException("Cannot find/load class " + className);
}

Expand All @@ -288,7 +288,7 @@ public static Class<?> loadClass(String className, ClassLoader classLoader) thro
Class<?> objectClass;
try {
objectClass = Class.forName(className);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
if (classLoader != null) {
objectClass = classLoader.loadClass(className);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static FunctionDetails convert(FunctionConfig functionConfig, ClassLoader
if (classLoader != null) {
try {
typeArgs = FunctionCommon.getFunctionTypes(functionConfig, classLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("Function class %s must be in class path", functionConfig.getClassName()), e);
}
Expand Down Expand Up @@ -386,15 +386,15 @@ private static void doJavaChecks(FunctionConfig functionConfig, ClassLoader clsL
String.format("Function class %s does not implement the correct interface",
functionClass.getName()));
}
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("Function class %s must be in class path", functionConfig.getClassName()), e);
}

Class<?>[] typeArgs;
try {
typeArgs = FunctionCommon.getFunctionTypes(functionConfig, clsLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("Function class %s must be in class path", functionConfig.getClassName()), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static <T> T createInstance(String userClassName,
Class<?> theCls;
try {
theCls = Class.forName(userClassName, true, classLoader);
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
throw new RuntimeException("User class must be in class path", cnfe);
}
if (!xface.isAssignableFrom(theCls)) {
Expand Down Expand Up @@ -104,7 +104,7 @@ public static Object createInstance(String userClassName,
Class<?> theCls;
try {
theCls = Class.forName(userClassName, true, classLoader);
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
throw new RuntimeException("User class must be in class path", cnfe);
}
Object result;
Expand Down Expand Up @@ -138,7 +138,7 @@ public static Object createInstance(String userClassName,
Class<?> theCls;
try {
theCls = Class.forName(userClassName, true, classLoader);
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
throw new RuntimeException("User class must be in class path", cnfe);
}
Object result;
Expand Down Expand Up @@ -184,7 +184,7 @@ public static boolean classExistsInJar(java.io.File jar, String fqcn) {
Class.forName(fqcn, false, loader);
loader.close();
return true;
} catch (ClassNotFoundException | IOException e) {
} catch (ClassNotFoundException | NoClassDefFoundError | IOException e) {
return false;
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public static boolean classInJarImplementsIface(java.io.File jar, String fqcn, C
ret = true;
}
loader.close();
} catch (ClassNotFoundException | IOException e) {
} catch (ClassNotFoundException | NoClassDefFoundError | IOException e) {
throw new RuntimeException(e);
}
return ret;
Expand All @@ -238,7 +238,7 @@ public static boolean classImplementsIface(String fqcn, Class xface) {
if (xface.isAssignableFrom(Class.forName(fqcn))){
ret = true;
}
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new RuntimeException(e);
}
return ret;
Expand Down Expand Up @@ -287,7 +287,7 @@ public static Class loadClass(String className, ClassLoader classLoader) throws
} else {
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException var4) {
} catch (ClassNotFoundException | NoClassDefFoundError var4) {
if (className.charAt(0) != '[') {
throw var4;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public static ExtractedSinkDetails validate(SinkConfig sinkConfig, Path archiveP
try {
typeArg = getSinkType(sinkClassName, narClassLoader);
classLoader = narClassLoader;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("Sink class %s must be in class path", sinkClassName), e);
}
Expand All @@ -388,13 +388,13 @@ public static ExtractedSinkDetails validate(SinkConfig sinkConfig, Path archiveP
try {
typeArg = getSinkType(sinkClassName, jarClassLoader);
classLoader = jarClassLoader;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// class not found in JAR try loading as a NAR and searching for the class
if (narClassLoader != null) {
try {
typeArg = getSinkType(sinkClassName, narClassLoader);
classLoader = narClassLoader;
} catch (ClassNotFoundException e1) {
} catch (ClassNotFoundException | NoClassDefFoundError e1) {
throw new IllegalArgumentException(
String.format("Sink class %s must be in class path", sinkClassName), e1);
}
Expand All @@ -407,7 +407,7 @@ public static ExtractedSinkDetails validate(SinkConfig sinkConfig, Path archiveP
try {
typeArg = getSinkType(sinkClassName, narClassLoader);
classLoader = narClassLoader;
} catch (ClassNotFoundException e1) {
} catch (ClassNotFoundException | NoClassDefFoundError e1) {
throw new IllegalArgumentException(
String.format("Sink class %s must be in class path", sinkClassName), e1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public static ExtractedSourceDetails validate(SourceConfig sourceConfig, Path ar
try {
typeArg = getSourceType(sourceClassName, narClassLoader);
classLoader = narClassLoader;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("Source class %s must be in class path", sourceClassName), e);
}
Expand All @@ -282,13 +282,13 @@ public static ExtractedSourceDetails validate(SourceConfig sourceConfig, Path ar
try {
typeArg = getSourceType(sourceClassName, jarClassLoader);
classLoader = jarClassLoader;
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
// class not found in JAR try loading as a NAR and searching for the class
if (narClassLoader != null) {
try {
typeArg = getSourceType(sourceClassName, narClassLoader);
classLoader = narClassLoader;
} catch (ClassNotFoundException e1) {
} catch (ClassNotFoundException | NoClassDefFoundError e1) {
throw new IllegalArgumentException(
String.format("Source class %s must be in class path", sourceClassName), e1);
}
Expand All @@ -301,7 +301,7 @@ public static ExtractedSourceDetails validate(SourceConfig sourceConfig, Path ar
try {
typeArg = getSourceType(sourceClassName, narClassLoader);
classLoader = narClassLoader;
} catch (ClassNotFoundException e1) {
} catch (ClassNotFoundException | NoClassDefFoundError e1) {
throw new IllegalArgumentException(
String.format("Source class %s must be in class path", sourceClassName), e1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void validateSerde(String inputSerializer, Class<?> typeArg, Class
if (inputSerializer.equals(DEFAULT_SERDE)) return;
try {
Class<?> serdeClass = FunctionCommon.loadClass(inputSerializer, clsLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("The input serialization/deserialization class %s does not exist",
inputSerializer));
Expand All @@ -84,7 +84,7 @@ public static void validateSerde(String inputSerializer, Class<?> typeArg, Class
try {
fnInputClass = Class.forName(typeArg.getName(), true, clsLoader);
serdeInputClass = Class.forName(serDeTypes[0].getName(), true, clsLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException("Failed to load type class", e);
}

Expand Down Expand Up @@ -115,7 +115,7 @@ private static void validateSchemaType(String schemaClassName, Class<?> typeArg,
try {
fnInputClass = Class.forName(typeArg.getName(), true, clsLoader);
schemaInputClass = Class.forName(schemaTypes[0].getName(), true, clsLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException("Failed to load type class", e);
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public static void validateFunctionClassTypes(ClassLoader classLoader, Function.
Class functionClass;
try {
functionClass = classLoader.loadClass(functionDetailsBuilder.getClassName());
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new IllegalArgumentException(
String.format("Function class %s must be in class path", functionDetailsBuilder.getClassName()), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public FlumeConfiguration getFlumeConfiguration() {
} catch (IOException ex) {
LOGGER.error("Unable to load file:" + file
+ " (I/O failure) - Exception follows.", ex);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
LOGGER.error("Configuration resolver class not found", e);
} catch (InstantiationException e) {
LOGGER.error("Instantiation exception", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public Class<?> getClassByNameOrNull(String name) {
if (clazz == null) {
try {
clazz = Class.forName(name, true, classLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
return null;
}
// two putters can race here, but they'll put the same class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public Class<?> getClassByNameOrNull(String name) {
if (clazz == null) {
try {
clazz = Class.forName(name, true, classLoader);
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
return null;
}
// two putters can race here, but they'll put the same class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class SqliteUtils {
static {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
} catch (ClassNotFoundException | NoClassDefFoundError e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static <T> T createInstance(String userClassName,
Class<?> theCls;
try {
theCls = Class.forName(userClassName, true, classLoader);
} catch (ClassNotFoundException cnfe) {
} catch (ClassNotFoundException | NoClassDefFoundError cnfe) {
throw new RuntimeException("User class must be in class path", cnfe);
}
if (!xface.isAssignableFrom(theCls)) {
Expand Down

0 comments on commit 17e22d1

Please sign in to comment.