Skip to content

Commit f35b408

Browse files
committed
[refactor] Revise mechanism to get new class instance using reflection.
1 parent 623a5ab commit f35b408

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

extensions/indexes/lucene/src/main/java/org/exist/indexing/lucene/AnalyzerConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.Serial;
2626
import java.lang.invoke.*;
2727
import java.lang.reflect.Field;
28+
import java.lang.reflect.InvocationTargetException;
2829
import java.util.ArrayList;
2930
import java.util.HashSet;
3031
import java.util.List;
@@ -317,11 +318,11 @@ static KeyTypedValue<?> getConstructorParameter(final Element param) throws Para
317318
final Class<?> fieldClazz = Class.forName(clazzName);
318319
final Field field = fieldClazz.getField(fieldName);
319320
field.setAccessible(true);
320-
final Object fValue = field.get(fieldClazz.newInstance());
321+
final Object fValue = field.get(fieldClazz.getDeclaredConstructor().newInstance());
321322
yield new KeyTypedValue<>(name, fValue, Object.class);
322323

323324
} catch (final NoSuchFieldException | ClassNotFoundException | InstantiationException |
324-
IllegalAccessException reflectiveOperationException) {
325+
IllegalAccessException | NoSuchMethodException | InvocationTargetException reflectiveOperationException) {
325326
throw new ParameterException(reflectiveOperationException.getMessage(), reflectiveOperationException);
326327
}
327328
}

extensions/indexes/range/src/main/java/org/exist/indexing/range/RangeIndexConfigElement.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import javax.xml.datatype.XMLGregorianCalendar;
4040
import java.io.IOException;
41+
import java.lang.reflect.InvocationTargetException;
4142
import java.util.Map;
4243

4344
import static org.exist.indexing.lucene.LuceneIndexConfig.MATCH_ATTR;
@@ -108,20 +109,20 @@ public RangeIndexConfigElement(Element node, Map<String, String> namespaces) thr
108109
if (!caseStr.isEmpty()) {
109110
caseSensitive = "yes".equalsIgnoreCase(caseStr);
110111
}
111-
String custom = node.getAttribute("converter");
112+
final String custom = node.getAttribute("converter");
112113
if (!custom.isEmpty()) {
113114
try {
114-
Class customClass = Class.forName(custom);
115-
typeConverter = (org.exist.indexing.range.conversion.TypeConverter) customClass.newInstance();
115+
final Class<?> customClass = Class.forName(custom);
116+
typeConverter = (org.exist.indexing.range.conversion.TypeConverter) customClass.getDeclaredConstructor().newInstance();
116117
} catch (ClassNotFoundException e) {
117118
RangeIndex.LOG.warn("Class for custom-type not found: {}", custom);
118-
} catch (InstantiationException | IllegalAccessException e) {
119+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
119120
RangeIndex.LOG.warn("Failed to initialize custom-type: {}", custom, e);
120121
}
121122
}
122123
}
123124

124-
private void parseChildren(Node root) throws DatabaseConfigurationException {
125+
private void parseChildren(final Node root) throws DatabaseConfigurationException {
125126
Node child = root.getFirstChild();
126127
while (child != null) {
127128
if (child.getNodeType() == Node.ELEMENT_NODE) {

extensions/indexes/range/src/main/java/org/exist/indexing/range/RangeIndexConfigField.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.exist.xquery.value.Type;
2929
import org.w3c.dom.Element;
3030

31+
import java.lang.reflect.InvocationTargetException;
3132
import java.util.Map;
3233

3334
/**
@@ -77,23 +78,23 @@ public RangeIndexConfigField(NodePath parentPath, Element elem, Map<String, Stri
7778
throw new DatabaseConfigurationException("Invalid type declared for range index on " + match + ": " + typeStr);
7879
}
7980
}
80-
String custom = elem.getAttribute("converter");
81+
final String custom = elem.getAttribute("converter");
8182
if (!custom.isEmpty()) {
8283
try {
83-
Class customClass = Class.forName(custom);
84-
typeConverter = (org.exist.indexing.range.conversion.TypeConverter) customClass.newInstance();
84+
final Class<?> customClass = Class.forName(custom);
85+
typeConverter = (org.exist.indexing.range.conversion.TypeConverter) customClass.getDeclaredConstructor().newInstance();
8586
} catch (ClassNotFoundException e) {
8687
RangeIndex.LOG.warn("Class for custom-type not found: {}", custom);
87-
} catch (InstantiationException | IllegalAccessException e) {
88+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
8889
RangeIndex.LOG.warn("Failed to initialize custom-type: {}", custom, e);
8990
}
9091
}
91-
String nested = elem.getAttribute("nested");
92+
final String nested = elem.getAttribute("nested");
9293
includeNested = (nested.isEmpty() || "yes".equalsIgnoreCase(nested));
9394
path.setIncludeDescendants(includeNested);
9495

9596
// normalize whitespace if whitespace="normalize"
96-
String whitespace = elem.getAttribute("whitespace");
97+
final String whitespace = elem.getAttribute("whitespace");
9798
if (!whitespace.isEmpty()) {
9899
if ("trim".equalsIgnoreCase(whitespace)) {
99100
wsTreatment = XMLString.SUPPRESS_BOTH;

extensions/modules/xslfo/src/main/java/org/exist/xquery/modules/xslfo/XSLFOModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
package org.exist.xquery.modules.xslfo;
2323

24+
import java.lang.reflect.InvocationTargetException;
2425
import java.util.List;
2526
import java.util.Map;
2627

@@ -88,8 +89,9 @@ public synchronized ProcessorAdapter getProcessorAdapter() {
8889

8990
try {
9091
final Class<ProcessorAdapter> clazzAdapter = (Class<ProcessorAdapter>) Class.forName(processorAdapter);
91-
adapter = clazzAdapter.newInstance();
92-
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException cnfe) {
92+
adapter = clazzAdapter.getDeclaredConstructor().newInstance();
93+
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException |
94+
NoSuchMethodException | InvocationTargetException cnfe) {
9395
logger.error("Unable to instantiate FO Processor Adapter:{}", cnfe.getMessage(), cnfe);
9496
}
9597
}

0 commit comments

Comments
 (0)