Skip to content

Commit

Permalink
Fixing possible NoClassDefFoundError when trying to load nonexisting …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
spinscale authored and s1monw committed Apr 26, 2013
1 parent 22e25cc commit 90353ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,22 @@ public <T> Class<? extends T> getAsClass(String setting, Class<? extends T> defa
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ public void testLoadFromDelimitedString() {
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");
}
}

0 comments on commit 90353ce

Please sign in to comment.