Fixing possible NoClassDefFoundError when trying to load nonexisting classes
In order to handle exceptions correctly, when classes are not found, one needs to handle ClassNotFoundException as well as NoClassDefFoundError in order to be sure to have caught every possible case. We did not cater for the latter in ImmutableSettings yet. This fix is just executing the same logic for both exceptions instead of simply bubbling up NoClassDefFoundError.
This commit is contained in:
parent
22e25cc165
commit
90353ceb79
|
@ -332,16 +332,22 @@ public class ImmutableSettings implements Settings {
|
|||
try {
|
||||
return (Class<? extends T>) getClassLoader().loadClass(fullClassName);
|
||||
} catch (ClassNotFoundException e1) {
|
||||
fullClassName = prefixValue + toCamelCase(sValue).toLowerCase() + "." + Strings.capitalize(toCamelCase(sValue)) + suffixClassName;
|
||||
try {
|
||||
return (Class<? extends T>) getClassLoader().loadClass(fullClassName);
|
||||
} catch (ClassNotFoundException e2) {
|
||||
throw new NoClassSettingsException("Failed to load class setting [" + setting + "] with value [" + get(setting) + "]", e2);
|
||||
}
|
||||
return loadClass(prefixValue, sValue, suffixClassName, setting);
|
||||
} catch (NoClassDefFoundError e1) {
|
||||
return loadClass(prefixValue, sValue, suffixClassName, setting);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private <T> Class<? extends T> loadClass(String prefixValue, String sValue, String suffixClassName, String setting) {
|
||||
String fullClassName = prefixValue + toCamelCase(sValue).toLowerCase() + "." + Strings.capitalize(toCamelCase(sValue)) + suffixClassName;
|
||||
try {
|
||||
return (Class<? extends T>) getClassLoader().loadClass(fullClassName);
|
||||
} catch (ClassNotFoundException e2) {
|
||||
throw new NoClassSettingsException("Failed to load class setting [" + setting + "] with value [" + get(setting) + "]", e2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getAsArray(String settingPrefix) throws SettingsException {
|
||||
return getAsArray(settingPrefix, Strings.EMPTY_ARRAY);
|
||||
|
|
|
@ -81,4 +81,10 @@ public class ImmutableSettingsTests {
|
|||
assertThat(settings.toDelimitedString(';'), equalTo("key1=value1;key2=value2;"));
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = NoClassSettingsException.class)
|
||||
public void testThatAllClassNotFoundExceptionsAreCaught() {
|
||||
// this should be nGram in order to really work, but for sure not not throw a NoClassDefFoundError
|
||||
Settings settings = settingsBuilder().put("type", "ngram").build();
|
||||
settings.getAsClass("type", null, "org.elasticsearch.index.analysis.", "TokenFilterFactory");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue