LUCENE-6961: Improve Exception handling in AnalysisFactories / AnalysisSPILoader: Don't wrap exceptions occuring in factory's ctor inside InvocationTargetException

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1722993 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2016-01-05 01:33:52 +00:00
parent 26a1d1ea34
commit a31bd9f2eb
3 changed files with 23 additions and 18 deletions

View File

@ -218,6 +218,10 @@ Other
(https://scan.coverity.com/projects/5620 CID 120656) (https://scan.coverity.com/projects/5620 CID 120656)
(Christine Poerschke, Coverity Scan (via Rishabh Patel)) (Christine Poerschke, Coverity Scan (via Rishabh Patel))
* LUCENE-6961: Improve Exception handling in AnalysisFactories /
AnalysisSPILoader: Don't wrap exceptions occuring in factory's
ctor inside InvocationTargetException. (Uwe Schindler)
======================= Lucene 5.4.0 ======================= ======================= Lucene 5.4.0 =======================
New Features New Features

View File

@ -17,6 +17,7 @@ package org.apache.lucene.analysis.util;
* limitations under the License. * limitations under the License.
*/ */
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Locale; import java.util.Locale;
@ -111,12 +112,7 @@ final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
public S newInstance(String name, Map<String,String> args) { public S newInstance(String name, Map<String,String> args) {
final Class<? extends S> service = lookupClass(name); final Class<? extends S> service = lookupClass(name);
try { return newFactoryInstance(service, args);
return service.getConstructor(Map.class).newInstance(args);
} catch (Exception e) {
throw new IllegalArgumentException("SPI class of type "+clazz.getName()+" with name '"+name+"' cannot be instantiated. " +
"This is likely due to a misconfiguration of the java class '" + service.getName() + "': ", e);
}
} }
public Class<? extends S> lookupClass(String name) { public Class<? extends S> lookupClass(String name) {
@ -133,4 +129,21 @@ final class AnalysisSPILoader<S extends AbstractAnalysisFactory> {
public Set<String> availableServices() { public Set<String> availableServices() {
return services.keySet(); return services.keySet();
} }
public static <T extends AbstractAnalysisFactory> T newFactoryInstance(Class<T> clazz, Map<String,String> args) {
try {
return clazz.getConstructor(Map.class).newInstance(args);
} catch (InvocationTargetException ite) {
final Throwable cause = ite.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(cause);
} catch (ReflectiveOperationException e) {
throw new UnsupportedOperationException("Factory "+clazz.getName()+" cannot be instantiated. This is likely due to missing Map<String,String> constructor.", e);
}
}
} }

View File

@ -150,10 +150,6 @@ public class TestAllAnalyzersHaveFactories extends LuceneTestCase {
} }
assertSame(c, instance.create().getClass()); assertSame(c, instance.create().getClass());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
if (e.getCause() instanceof NoSuchMethodException) {
// there is no corresponding ctor available
throw e;
}
// TODO: For now pass because some factories have not yet a default config that always works // TODO: For now pass because some factories have not yet a default config that always works
} }
} else if (TokenFilter.class.isAssignableFrom(c)) { } else if (TokenFilter.class.isAssignableFrom(c)) {
@ -174,10 +170,6 @@ public class TestAllAnalyzersHaveFactories extends LuceneTestCase {
assertSame(c, createdClazz); assertSame(c, createdClazz);
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
if (e.getCause() instanceof NoSuchMethodException) {
// there is no corresponding ctor available
throw e;
}
// TODO: For now pass because some factories have not yet a default config that always works // TODO: For now pass because some factories have not yet a default config that always works
} }
} else if (CharFilter.class.isAssignableFrom(c)) { } else if (CharFilter.class.isAssignableFrom(c)) {
@ -198,10 +190,6 @@ public class TestAllAnalyzersHaveFactories extends LuceneTestCase {
assertSame(c, createdClazz); assertSame(c, createdClazz);
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
if (e.getCause() instanceof NoSuchMethodException) {
// there is no corresponding ctor available
throw e;
}
// TODO: For now pass because some factories have not yet a default config that always works // TODO: For now pass because some factories have not yet a default config that always works
} }
} }