Skip to content

Commit bf3d148

Browse files
committed
Change to Constructor#newInstance from Class#newInstance
Because Class#newInstance was deprecated since JDK 9+.
1 parent 2e9632d commit bf3d148

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

src/main/java/org/apache/ibatis/builder/BaseBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected Object createInstance(String alias) {
102102
return null;
103103
}
104104
try {
105-
return resolveClass(alias).newInstance();
105+
return resolveClass(alias).getDeclaredConstructor().newInstance();
106106
} catch (Exception e) {
107107
throw new BuilderException("Error creating instance. Cause: " + e, e);
108108
}

src/main/java/org/apache/ibatis/builder/annotation/ProviderSqlSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private Object[] extractProviderMethodArguments(Map<String, Object> params, Stri
204204
private String invokeProviderMethod(Object... args) throws Exception {
205205
Object targetObject = null;
206206
if (!Modifier.isStatic(providerMethod.getModifiers())) {
207-
targetObject = providerType.newInstance();
207+
targetObject = providerType.getDeclaredConstructor().newInstance();
208208
}
209209
CharSequence sql = (CharSequence) providerMethod.invoke(targetObject, args);
210210
return sql != null ? sql.toString() : null;

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private void pluginElement(XNode parent) throws Exception {
185185
for (XNode child : parent.getChildren()) {
186186
String interceptor = child.getStringAttribute("interceptor");
187187
Properties properties = child.getChildrenAsProperties();
188-
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
188+
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor().newInstance();
189189
interceptorInstance.setProperties(properties);
190190
configuration.addInterceptor(interceptorInstance);
191191
}
@@ -196,7 +196,7 @@ private void objectFactoryElement(XNode context) throws Exception {
196196
if (context != null) {
197197
String type = context.getStringAttribute("type");
198198
Properties properties = context.getChildrenAsProperties();
199-
ObjectFactory factory = (ObjectFactory) resolveClass(type).newInstance();
199+
ObjectFactory factory = (ObjectFactory) resolveClass(type).getDeclaredConstructor().newInstance();
200200
factory.setProperties(properties);
201201
configuration.setObjectFactory(factory);
202202
}
@@ -205,15 +205,15 @@ private void objectFactoryElement(XNode context) throws Exception {
205205
private void objectWrapperFactoryElement(XNode context) throws Exception {
206206
if (context != null) {
207207
String type = context.getStringAttribute("type");
208-
ObjectWrapperFactory factory = (ObjectWrapperFactory) resolveClass(type).newInstance();
208+
ObjectWrapperFactory factory = (ObjectWrapperFactory) resolveClass(type).getDeclaredConstructor().newInstance();
209209
configuration.setObjectWrapperFactory(factory);
210210
}
211211
}
212212

213213
private void reflectorFactoryElement(XNode context) throws Exception {
214214
if (context != null) {
215215
String type = context.getStringAttribute("type");
216-
ReflectorFactory factory = (ReflectorFactory) resolveClass(type).newInstance();
216+
ReflectorFactory factory = (ReflectorFactory) resolveClass(type).getDeclaredConstructor().newInstance();
217217
configuration.setReflectorFactory(factory);
218218
}
219219
}
@@ -298,7 +298,7 @@ private void databaseIdProviderElement(XNode context) throws Exception {
298298
type = "DB_VENDOR";
299299
}
300300
Properties properties = context.getChildrenAsProperties();
301-
databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance();
301+
databaseIdProvider = (DatabaseIdProvider) resolveClass(type).getDeclaredConstructor().newInstance();
302302
databaseIdProvider.setProperties(properties);
303303
}
304304
Environment environment = configuration.getEnvironment();
@@ -312,7 +312,7 @@ private TransactionFactory transactionManagerElement(XNode context) throws Excep
312312
if (context != null) {
313313
String type = context.getStringAttribute("type");
314314
Properties props = context.getChildrenAsProperties();
315-
TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance();
315+
TransactionFactory factory = (TransactionFactory) resolveClass(type).getDeclaredConstructor().newInstance();
316316
factory.setProperties(props);
317317
return factory;
318318
}
@@ -323,7 +323,7 @@ private DataSourceFactory dataSourceElement(XNode context) throws Exception {
323323
if (context != null) {
324324
String type = context.getStringAttribute("type");
325325
Properties props = context.getChildrenAsProperties();
326-
DataSourceFactory factory = (DataSourceFactory) resolveClass(type).newInstance();
326+
DataSourceFactory factory = (DataSourceFactory) resolveClass(type).getDeclaredConstructor().newInstance();
327327
factory.setProperties(props);
328328
return factory;
329329
}

src/main/java/org/apache/ibatis/datasource/unpooled/UnpooledDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public Integer getDefaultNetworkTimeout() {
193193

194194
/**
195195
* Sets the default network timeout value to wait for the database operation to complete. See {@link Connection#setNetworkTimeout(java.util.concurrent.Executor, int)}
196-
*
196+
*
197197
* @param defaultNetworkTimeout
198198
* The time in milliseconds to wait for the database operation to complete.
199199
* @since 3.5.2
@@ -234,7 +234,7 @@ private synchronized void initializeDriver() throws SQLException {
234234
}
235235
// DriverManager requires the driver to be loaded via the system ClassLoader.
236236
// http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
237-
Driver driverInstance = (Driver)driverType.newInstance();
237+
Driver driverInstance = (Driver)driverType.getDeclaredConstructor().newInstance();
238238
DriverManager.registerDriver(new DriverProxy(driverInstance));
239239
registeredDrivers.put(driver, driverInstance);
240240
} catch (Exception e) {

src/main/java/org/apache/ibatis/io/VFS.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ static VFS createVFS() {
5757
for (int i = 0; vfs == null || !vfs.isValid(); i++) {
5858
Class<? extends VFS> impl = impls.get(i);
5959
try {
60-
vfs = impl.newInstance();
60+
vfs = impl.getDeclaredConstructor().newInstance();
6161
if (vfs == null || !vfs.isValid()) {
6262
if (log.isDebugEnabled()) {
6363
log.debug("VFS implementation " + impl.getName() +
6464
" is not valid in this environment.");
6565
}
6666
}
67-
} catch (InstantiationException | IllegalAccessException e) {
67+
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
6868
log.error("Failed to instantiate " + impl, e);
6969
return null;
7070
}

src/test/java/org/apache/ibatis/builder/XmlMapperBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void createInstanceWithAbstractClass() {
128128
when(builder).createInstance("org.apache.ibatis.builder.BaseBuilder");
129129
then(caughtException())
130130
.isInstanceOf(BuilderException.class)
131-
.hasMessage("Error creating instance. Cause: java.lang.InstantiationException: org.apache.ibatis.builder.BaseBuilder");
131+
.hasMessage("Error creating instance. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.builder.BaseBuilder.<init>()");
132132
}
133133

134134
@Test

src/test/java/org/apache/ibatis/exceptions/GeneralExceptionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
7272
}
7373

7474
private void testExceptionConstructors(Class<?> exceptionType) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
75-
Exception e = (Exception) exceptionType.newInstance();
75+
Exception e = (Exception) exceptionType.getDeclaredConstructor().newInstance();
7676
testThrowException(e);
7777
e = (Exception) exceptionType.getConstructor(String.class).newInstance(EXPECTED_MESSAGE);
7878
testThrowException(e);

0 commit comments

Comments
 (0)