Skip to content

Commit

Permalink
Merge pull request #4635 from evolvedbinary/hotfix/int_in_analyzer_conf
Browse files Browse the repository at this point in the history
[bugfix] analyzer conf doesn't accept primitives
  • Loading branch information
dizzzz authored Dec 3, 2022
2 parents 5fc5553 + f8841e1 commit b47e668
Show file tree
Hide file tree
Showing 2 changed files with 543 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,23 @@ protected static Analyzer configureAnalyzer(Element config) throws DatabaseConfi
// Classname is defined.

// Probe class
Class<?> clazz = null;
final Class<?> untypedClazz;
try {
clazz = Class.forName(className);
untypedClazz = Class.forName(className);

} catch (ClassNotFoundException e) {
LOG.error(String.format("Lucene index: analyzer class %s not found. (%s)", className, e.getMessage()));
return null;
}

// CHeck if class is an Analyzer
if (!Analyzer.class.isAssignableFrom(clazz)) {
if (!Analyzer.class.isAssignableFrom(untypedClazz)) {
LOG.error(String.format("Lucene index: analyzer class has to be a subclass of %s", Analyzer.class.getName()));
return null;
}

final Class<? extends Analyzer> clazz = (Class<? extends Analyzer>) untypedClazz;

// Get list of parameters
List<KeyTypedValue<?>> cParams;
try {
Expand Down Expand Up @@ -216,7 +218,7 @@ protected static Analyzer configureAnalyzer(Element config) throws DatabaseConfi
newAnalyzer = createInstance(clazz, cParamClasses, cParamValues, false);

} else {
// Either no parameters have been provided or more than one parameter
// Either no parameters have been provided, or more than one parameter

// Extend arrays with (default) Version object info, add to front.
Class<?>[] vcParamClasses = addVersionToClasses(cParamClasses);
Expand Down Expand Up @@ -252,7 +254,7 @@ protected static Analyzer configureAnalyzer(Element config) throws DatabaseConfi
* @param warnOnError true if an error should be treated as a warning
* @return The lucene analyzer
*/
private static Analyzer createInstance(final Class<?> clazz, final Class<?>[] vcParamClasses,
static <T extends Analyzer> T createInstance(final Class<T> clazz, final Class<?>[] vcParamClasses,
final Object[] vcParamValues, final boolean warnOnError) {

final String className = clazz.getName();
Expand All @@ -267,7 +269,7 @@ private static Analyzer createInstance(final Class<?> clazz, final Class<?>[] vc
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Using analyzer %s", className));
}
return (Analyzer)methodHandle.invokeWithArguments(vcParamValues);
return (T) methodHandle.invokeWithArguments(vcParamValues);
} catch (final NoSuchMethodException e) {
final String message = String.format("Could not find matching analyzer class constructor %s: %s", className, e.getMessage());
if (warnOnError) {
Expand Down Expand Up @@ -318,7 +320,7 @@ private static Class<?>[] addVersionToClasses(final Class<?>[] cParamClasses) {
* @return List of triples key-value-valueType
* @throws org.exist.indexing.lucene.AnalyzerConfig.ParameterException
*/
private static List<KeyTypedValue<?>> getAllConstructorParameters(Element config) throws ParameterException {
static List<KeyTypedValue<?>> getAllConstructorParameters(Element config) throws ParameterException {
final List<KeyTypedValue<?>> parameters = new ArrayList<>();
final NodeList params = config.getElementsByTagNameNS(CollectionConfiguration.NAMESPACE, PARAM_ELEMENT_NAME);

Expand Down Expand Up @@ -455,29 +457,43 @@ static KeyTypedValue<?> getConstructorParameter(final Element param) throws Para
}

case "java.lang.Integer":
case "int":

if (value == null) {
throw new ParameterException("The 'value' attribute must exist and must contain an integer value.");
}

try {
final Integer n = Integer.parseInt(value);
parameter = new KeyTypedValue<>(name, n, Integer.class);
} catch (NumberFormatException ex) {
} catch (final NumberFormatException ex) {
LOG.error(String.format("Value %s could not be converted to an integer. %s", value, ex.getMessage()));
}
break;

case "int":
if (value == null) {
throw new ParameterException("The 'value' attribute must exist and must contain an int value.");
}
try {
final Integer n = Integer.parseInt(value);
parameter = new KeyTypedValue<>(name, n.intValue(), int.class);
} catch (final NumberFormatException ex) {
LOG.error(String.format("Value %s could not be converted to an int. %s", value, ex.getMessage()));
}
break;

case "java.lang.Boolean":
case "boolean":
if (value == null) {
throw new ParameterException("The 'value' attribute must exist and must contain a Boolean value.");
}
final Boolean b1 = Boolean.parseBoolean(value);
parameter = new KeyTypedValue<>(name, b1, Boolean.class);
break;

case "boolean":
if (value == null) {
throw new ParameterException("The 'value' attribute must exist and must contain a boolean value.");
}

final boolean b = Boolean.parseBoolean(value);
parameter = new KeyTypedValue<>(name, b, boolean.class);
final Boolean b2 = Boolean.parseBoolean(value);
parameter = new KeyTypedValue<>(name, b2.booleanValue(), boolean.class);
break;

default:
Expand Down
Loading

0 comments on commit b47e668

Please sign in to comment.