Skip to content

Commit 610114d

Browse files
committed
Replace getDeclaredConstructor().getInstance() with LoaderUtil
1 parent 2c0675e commit 610114d

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

log4j-api/src/main/java/org/apache/logging/log4j/LogManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class LogManager {
9292
final Class<? extends LoggerContextFactory> factoryClass = provider.loadLoggerContextFactory();
9393
if (factoryClass != null) {
9494
try {
95-
factories.put(provider.getPriority(), factoryClass.getDeclaredConstructor().newInstance());
95+
factories.put(provider.getPriority(), LoaderUtil.newInstanceOf(factoryClass));
9696
} catch (final Exception e) {
9797
LOGGER.error("Unable to create class {} specified in provider URL {}",
9898
factoryClass.getName(), provider.getUrl(), e);

log4j-api/src/main/java/org/apache/logging/log4j/spi/ThreadContextMapFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.logging.log4j.ThreadContext;
2222
import org.apache.logging.log4j.status.StatusLogger;
2323
import org.apache.logging.log4j.util.Constants;
24+
import org.apache.logging.log4j.util.LoaderUtil;
2425
import org.apache.logging.log4j.util.PropertiesUtil;
2526
import org.apache.logging.log4j.util.ProviderUtil;
2627

@@ -103,7 +104,7 @@ public static ThreadContextMap createThreadContextMap() {
103104
final Class<? extends ThreadContextMap> clazz = provider.loadThreadContextMap();
104105
if (clazz != null) {
105106
try {
106-
result = clazz.getDeclaredConstructor().newInstance();
107+
result = LoaderUtil.newInstanceOf(clazz);
107108
break;
108109
} catch (final Exception e) {
109110
LOGGER.error("Unable to locate or load configured ThreadContextMap {}",

log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public class ContextDataFactory {
5555
* In graalvm doc (https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md),
5656
* graalvm is not support MethodHandle now, so the Constructor need not to return MethodHandle.
5757
*/
58-
private static final Constructor<?> DEFAULT_CONSTRUCTOR = createDefaultConstructor(CACHED_CLASS);
59-
private static final Constructor<?> INITIAL_CAPACITY_CONSTRUCTOR = createInitialCapacityConstructor(CACHED_CLASS);
58+
private static final Constructor<? extends StringMap> DEFAULT_CONSTRUCTOR = createDefaultConstructor(CACHED_CLASS);
59+
private static final Constructor<? extends StringMap> INITIAL_CAPACITY_CONSTRUCTOR =
60+
createInitialCapacityConstructor(CACHED_CLASS);
6061

6162
private static final StringMap EMPTY_STRING_MAP = createContextData(0);
6263

@@ -75,7 +76,8 @@ private static Class<? extends StringMap> createCachedClass(final String classNa
7576
}
7677
}
7778

78-
private static Constructor<?> createDefaultConstructor(final Class<? extends StringMap> cachedClass){
79+
private static Constructor<? extends StringMap> createDefaultConstructor(
80+
final Class<? extends StringMap> cachedClass) {
7981
if (cachedClass == null) {
8082
return null;
8183
}
@@ -86,7 +88,8 @@ private static Constructor<?> createDefaultConstructor(final Class<? extends Str
8688
}
8789
}
8890

89-
private static Constructor<?> createInitialCapacityConstructor(final Class<? extends StringMap> cachedClass){
91+
private static Constructor<? extends StringMap> createInitialCapacityConstructor(
92+
final Class<? extends StringMap> cachedClass) {
9093
if (cachedClass == null) {
9194
return null;
9295
}
@@ -102,7 +105,7 @@ public static StringMap createContextData() {
102105
return new SortedArrayStringMap();
103106
}
104107
try {
105-
return (IndexedStringMap) DEFAULT_CONSTRUCTOR.newInstance();
108+
return DEFAULT_CONSTRUCTOR.newInstance();
106109
} catch (final Throwable ignored) {
107110
return new SortedArrayStringMap();
108111
}
@@ -113,7 +116,7 @@ public static StringMap createContextData(final int initialCapacity) {
113116
return new SortedArrayStringMap(initialCapacity);
114117
}
115118
try {
116-
return (IndexedStringMap) INITIAL_CAPACITY_CONSTRUCTOR.newInstance(initialCapacity);
119+
return INITIAL_CAPACITY_CONSTRUCTOR.newInstance(initialCapacity);
117120
} catch (final Throwable ignored) {
118121
return new SortedArrayStringMap(initialCapacity);
119122
}

log4j-core/src/main/java/org/apache/logging/log4j/core/net/MulticastDnsAdvertiser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void unadvertise(final Object serviceInfo) {
135135

136136
private static Object createJmDnsVersion1() {
137137
try {
138-
return jmDNSClass.getConstructor().newInstance();
138+
return LoaderUtil.newInstanceOf(jmDNSClass);
139139
} catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
140140
LOGGER.warn("Unable to instantiate JMDNS", e);
141141
}

0 commit comments

Comments
 (0)