Unverified 提交 db69a996 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1235 from katzyn/newInstance

Do not use deprecated Class.newInstance()
...@@ -489,7 +489,7 @@ public class Set extends Prepared { ...@@ -489,7 +489,7 @@ public class Set extends Prepared {
Class<RowFactory> rowFactoryClass = JdbcUtils.loadUserClass(rowFactoryName); Class<RowFactory> rowFactoryClass = JdbcUtils.loadUserClass(rowFactoryName);
RowFactory rowFactory; RowFactory rowFactory;
try { try {
rowFactory = rowFactoryClass.newInstance(); rowFactory = rowFactoryClass.getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
......
...@@ -2179,7 +2179,7 @@ public class Database implements DataHandler { ...@@ -2179,7 +2179,7 @@ public class Database implements DataHandler {
} else { } else {
try { try {
eventListener = (DatabaseEventListener) eventListener = (DatabaseEventListener)
JdbcUtils.loadUserClass(className).newInstance(); JdbcUtils.loadUserClass(className).getDeclaredConstructor().newInstance();
String url = databaseURL; String url = databaseURL;
if (cipher != null) { if (cipher != null) {
url += ";CIPHER=" + cipher; url += ";CIPHER=" + cipher;
...@@ -2939,7 +2939,7 @@ public class Database implements DataHandler { ...@@ -2939,7 +2939,7 @@ public class Database implements DataHandler {
!serializerName.equals("null")) { !serializerName.equals("null")) {
try { try {
javaObjectSerializer = (JavaObjectSerializer) javaObjectSerializer = (JavaObjectSerializer)
JdbcUtils.loadUserClass(serializerName).newInstance(); JdbcUtils.loadUserClass(serializerName).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
...@@ -2968,7 +2968,7 @@ public class Database implements DataHandler { ...@@ -2968,7 +2968,7 @@ public class Database implements DataHandler {
TableEngine engine = tableEngines.get(tableEngine); TableEngine engine = tableEngines.get(tableEngine);
if (engine == null) { if (engine == null) {
try { try {
engine = (TableEngine) JdbcUtils.loadUserClass(tableEngine).newInstance(); engine = (TableEngine) JdbcUtils.loadUserClass(tableEngine).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
......
...@@ -409,7 +409,7 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -409,7 +409,7 @@ public class SessionRemote extends SessionWithState implements DataHandler {
className = StringUtils.trim(className, true, true, "'"); className = StringUtils.trim(className, true, true, "'");
try { try {
eventListener = (DatabaseEventListener) JdbcUtils eventListener = (DatabaseEventListener) JdbcUtils
.loadUserClass(className).newInstance(); .loadUserClass(className).getDeclaredConstructor().newInstance();
} catch (Throwable e) { } catch (Throwable e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
...@@ -794,7 +794,7 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -794,7 +794,7 @@ public class SessionRemote extends SessionWithState implements DataHandler {
if (!serializerFQN.isEmpty() && !serializerFQN.equals("null")) { if (!serializerFQN.isEmpty() && !serializerFQN.equals("null")) {
try { try {
javaObjectSerializer = (JavaObjectSerializer) JdbcUtils javaObjectSerializer = (JavaObjectSerializer) JdbcUtils
.loadUserClass(serializerFQN).newInstance(); .loadUserClass(serializerFQN).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
......
...@@ -39,7 +39,7 @@ public class UserAggregate extends DbObjectBase { ...@@ -39,7 +39,7 @@ public class UserAggregate extends DbObjectBase {
} }
Object obj; Object obj;
try { try {
obj = javaClass.newInstance(); obj = javaClass.getDeclaredConstructor().newInstance();
Aggregate agg; Aggregate agg;
if (obj instanceof Aggregate) { if (obj instanceof Aggregate) {
agg = (Aggregate) obj; agg = (Aggregate) obj;
......
...@@ -189,7 +189,7 @@ public class TraceSystem implements TraceWriter { ...@@ -189,7 +189,7 @@ public class TraceSystem implements TraceWriter {
if (level == ADAPTER) { if (level == ADAPTER) {
String adapterClass = "org.h2.message.TraceWriterAdapter"; String adapterClass = "org.h2.message.TraceWriterAdapter";
try { try {
writer = (TraceWriter) Class.forName(adapterClass).newInstance(); writer = (TraceWriter) Class.forName(adapterClass).getDeclaredConstructor().newInstance();
} catch (Throwable e) { } catch (Throwable e) {
e = DbException.get(ErrorCode.CLASS_NOT_FOUND_1, e, adapterClass); e = DbException.get(ErrorCode.CLASS_NOT_FOUND_1, e, adapterClass);
write(ERROR, Trace.DATABASE, adapterClass, e); write(ERROR, Trace.DATABASE, adapterClass, e);
......
...@@ -74,7 +74,7 @@ public class TriggerObject extends SchemaObjectBase { ...@@ -74,7 +74,7 @@ public class TriggerObject extends SchemaObjectBase {
Connection c2 = sysSession.createConnection(false); Connection c2 = sysSession.createConnection(false);
Object obj; Object obj;
if (triggerClassName != null) { if (triggerClassName != null) {
obj = JdbcUtils.loadUserClass(triggerClassName).newInstance(); obj = JdbcUtils.loadUserClass(triggerClassName).getDeclaredConstructor().newInstance();
} else { } else {
obj = loadFromSource(); obj = loadFromSource();
} }
......
...@@ -245,7 +245,7 @@ public class DefaultAuthenticator implements Authenticator { ...@@ -245,7 +245,7 @@ public class DefaultAuthenticator implements Authenticator {
CredentialsValidator currentValidator = null; CredentialsValidator currentValidator = null;
try { try {
currentValidator = (CredentialsValidator) Class.forName(currentRealmConfig.getValidatorClass()) currentValidator = (CredentialsValidator) Class.forName(currentRealmConfig.getValidatorClass())
.newInstance(); .getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new AuthenticationException("invalid validator class fo realm " + currentRealmName, e); throw new AuthenticationException("invalid validator class fo realm " + currentRealmName, e);
} }
...@@ -260,7 +260,7 @@ public class DefaultAuthenticator implements Authenticator { ...@@ -260,7 +260,7 @@ public class DefaultAuthenticator implements Authenticator {
UserToRolesMapper currentUserToRolesMapper = null; UserToRolesMapper currentUserToRolesMapper = null;
try { try {
currentUserToRolesMapper = (UserToRolesMapper) Class currentUserToRolesMapper = (UserToRolesMapper) Class
.forName(currentUserToRolesMapperConfig.getClassName()).newInstance(); .forName(currentUserToRolesMapperConfig.getClassName()).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new AuthenticationException("Invalid class in UserToRolesMapperConfig", e); throw new AuthenticationException("Invalid class in UserToRolesMapperConfig", e);
} }
......
...@@ -78,7 +78,7 @@ public abstract class FilePath { ...@@ -78,7 +78,7 @@ public abstract class FilePath {
"org.h2.store.fs.FilePathRetryOnInterrupt" "org.h2.store.fs.FilePathRetryOnInterrupt"
}) { }) {
try { try {
FilePath p = (FilePath) Class.forName(c).newInstance(); FilePath p = (FilePath) Class.forName(c).getDeclaredConstructor().newInstance();
map.put(p.getScheme(), p); map.put(p.getScheme(), p);
if (defaultProvider == null) { if (defaultProvider == null) {
defaultProvider = p; defaultProvider = p;
......
...@@ -41,7 +41,7 @@ public abstract class FilePathWrapper extends FilePath { ...@@ -41,7 +41,7 @@ public abstract class FilePathWrapper extends FilePath {
private FilePathWrapper create(String path, FilePath base) { private FilePathWrapper create(String path, FilePath base) {
try { try {
FilePathWrapper p = getClass().newInstance(); FilePathWrapper p = getClass().getDeclaredConstructor().newInstance();
p.name = path; p.name = path;
p.base = base; p.base = base;
return p; return p;
......
...@@ -114,7 +114,7 @@ public class JdbcUtils { ...@@ -114,7 +114,7 @@ public class JdbcUtils {
String clazz = SysProperties.JAVA_OBJECT_SERIALIZER; String clazz = SysProperties.JAVA_OBJECT_SERIALIZER;
if (clazz != null) { if (clazz != null) {
try { try {
serializer = (JavaObjectSerializer) loadUserClass(clazz).newInstance(); serializer = (JavaObjectSerializer) loadUserClass(clazz).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
...@@ -124,7 +124,7 @@ public class JdbcUtils { ...@@ -124,7 +124,7 @@ public class JdbcUtils {
if (customTypeHandlerClass != null) { if (customTypeHandlerClass != null) {
try { try {
customDataTypesHandler = (CustomDataTypesHandler) customDataTypesHandler = (CustomDataTypesHandler)
loadUserClass(customTypeHandlerClass).newInstance(); loadUserClass(customTypeHandlerClass).getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw DbException.convert(e); throw DbException.convert(e);
} }
...@@ -290,7 +290,7 @@ public class JdbcUtils { ...@@ -290,7 +290,7 @@ public class JdbcUtils {
Class<?> d = loadUserClass(driver); Class<?> d = loadUserClass(driver);
if (java.sql.Driver.class.isAssignableFrom(d)) { if (java.sql.Driver.class.isAssignableFrom(d)) {
try { try {
Driver driverInstance = (Driver) d.newInstance(); Driver driverInstance = (Driver) d.getDeclaredConstructor().newInstance();
return driverInstance.connect(url, prop); /*fix issue #695 with drivers with the same return driverInstance.connect(url, prop); /*fix issue #695 with drivers with the same
jdbc subprotocol in classpath of jdbc drivers (as example redshift and postgresql drivers)*/ jdbc subprotocol in classpath of jdbc drivers (as example redshift and postgresql drivers)*/
} catch (Exception e) { } catch (Exception e) {
...@@ -299,7 +299,7 @@ public class JdbcUtils { ...@@ -299,7 +299,7 @@ public class JdbcUtils {
} else if (javax.naming.Context.class.isAssignableFrom(d)) { } else if (javax.naming.Context.class.isAssignableFrom(d)) {
// JNDI context // JNDI context
try { try {
Context context = (Context) d.newInstance(); Context context = (Context) d.getDeclaredConstructor().newInstance();
DataSource ds = (DataSource) context.lookup(url); DataSource ds = (DataSource) context.lookup(url);
String user = prop.getProperty("user"); String user = prop.getProperty("user");
String password = prop.getProperty("password"); String password = prop.getProperty("password");
......
...@@ -399,7 +399,7 @@ public class SourceCompiler { ...@@ -399,7 +399,7 @@ public class SourceCompiler {
System.setErr(temp); System.setErr(temp);
Method compile; Method compile;
compile = JAVAC_SUN.getMethod("compile", String[].class); compile = JAVAC_SUN.getMethod("compile", String[].class);
Object javac = JAVAC_SUN.newInstance(); Object javac = JAVAC_SUN.getDeclaredConstructor().newInstance();
compile.invoke(javac, (Object) new String[] { compile.invoke(javac, (Object) new String[] {
"-sourcepath", COMPILE_DIR, "-sourcepath", COMPILE_DIR,
// "-Xlint:unchecked", // "-Xlint:unchecked",
......
...@@ -1054,11 +1054,8 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -1054,11 +1054,8 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
private static TestBase createTest(String className) { private static TestBase createTest(String className) {
try { try {
Class<?> clazz = Class.forName(className); Class<?> clazz = Class.forName(className);
return (TestBase) clazz.newInstance(); return (TestBase) clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception | NoClassDefFoundError e) {
// ignore
TestBase.printlnWithTime(0, className + " class not found");
} catch (NoClassDefFoundError e) {
// ignore // ignore
TestBase.printlnWithTime(0, className + " class not found"); TestBase.printlnWithTime(0, className + " class not found");
} }
......
...@@ -1303,7 +1303,7 @@ public abstract class TestBase { ...@@ -1303,7 +1303,7 @@ public abstract class TestBase {
try { try {
return (TestBase) new SecurityManager() { return (TestBase) new SecurityManager() {
Class<?> clazz = getClassContext()[2]; Class<?> clazz = getClassContext()[2];
}.clazz.newInstance(); }.clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
......
...@@ -107,7 +107,7 @@ class Database { ...@@ -107,7 +107,7 @@ class Database {
Thread.sleep(100); Thread.sleep(100);
} else if (url.startsWith("jdbc:derby://")) { } else if (url.startsWith("jdbc:derby://")) {
serverDerby = Class.forName( serverDerby = Class.forName(
"org.apache.derby.drda.NetworkServerControl").newInstance(); "org.apache.derby.drda.NetworkServerControl").getDeclaredConstructor().newInstance();
Method m = serverDerby.getClass().getMethod("start", PrintWriter.class); Method m = serverDerby.getClass().getMethod("start", PrintWriter.class);
m.invoke(serverDerby, new Object[] { null }); m.invoke(serverDerby, new Object[] { null });
// serverDerby = new NetworkServerControl(); // serverDerby = new NetworkServerControl();
......
...@@ -111,7 +111,7 @@ public class TestPerformance implements Database.DatabaseTest { ...@@ -111,7 +111,7 @@ public class TestPerformance implements Database.DatabaseTest {
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
String testString = prop.getProperty("test" + i); String testString = prop.getProperty("test" + i);
if (testString != null) { if (testString != null) {
Bench bench = (Bench) Class.forName(testString).newInstance(); Bench bench = (Bench) Class.forName(testString).getDeclaredConstructor().newInstance();
tests.add(bench); tests.add(bench);
} }
} }
......
...@@ -28,7 +28,7 @@ public abstract class TaskDef { ...@@ -28,7 +28,7 @@ public abstract class TaskDef {
TaskDef task; TaskDef task;
try { try {
String className = args[0]; String className = args[0];
task = (TaskDef) Class.forName(className).newInstance(); task = (TaskDef) Class.forName(className).getDeclaredConstructor().newInstance();
System.out.println("running"); System.out.println("running");
} catch (Throwable t) { } catch (Throwable t) {
System.out.println("init error: " + t); System.out.println("init error: " + t);
......
...@@ -205,7 +205,7 @@ public class TestRecover { ...@@ -205,7 +205,7 @@ public class TestRecover {
// ignore // ignore
} }
try { try {
Driver driver = (Driver) Class.forName(DRIVER).newInstance(); Driver driver = (Driver) Class.forName(DRIVER).getDeclaredConstructor().newInstance();
DriverManager.registerDriver(driver); DriverManager.registerDriver(driver);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -63,9 +63,9 @@ public class TestClassLoaderLeak extends TestBase { ...@@ -63,9 +63,9 @@ public class TestClassLoaderLeak extends TestBase {
} }
} }
DriverManager.registerDriver((Driver) DriverManager.registerDriver((Driver)
Class.forName("org.h2.Driver").newInstance()); Class.forName("org.h2.Driver").getDeclaredConstructor().newInstance());
DriverManager.registerDriver((Driver) DriverManager.registerDriver((Driver)
Class.forName("org.h2.upgrade.v1_1.Driver").newInstance()); Class.forName("org.h2.upgrade.v1_1.Driver").getDeclaredConstructor().newInstance());
} }
private static WeakReference<ClassLoader> createClassLoader() throws Exception { private static WeakReference<ClassLoader> createClassLoader() throws Exception {
......
...@@ -974,7 +974,7 @@ public class BuildBase { ...@@ -974,7 +974,7 @@ public class BuildBase {
})); }));
} }
Method compile = clazz.getMethod("compile", new Class<?>[] { String[].class }); Method compile = clazz.getMethod("compile", new Class<?>[] { String[].class });
Object instance = clazz.newInstance(); Object instance = clazz.getDeclaredConstructor().newInstance();
result = (Integer) invoke(compile, instance, new Object[] { array }); result = (Integer) invoke(compile, instance, new Object[] { array });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -66,7 +66,7 @@ public class Db { ...@@ -66,7 +66,7 @@ public class Db {
private static <T> T instance(Class<T> clazz) { private static <T> T instance(Class<T> clazz) {
try { try {
return clazz.newInstance(); return clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
......
...@@ -65,7 +65,7 @@ public class ClassUtils { ...@@ -65,7 +65,7 @@ public class ClassUtils {
return (T) new ArrayList<>(); return (T) new ArrayList<>();
} }
try { try {
return clazz.newInstance(); return clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) { } catch (Exception e) {
if (MAKE_ACCESSIBLE) { if (MAKE_ACCESSIBLE) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors(); Constructor<?>[] constructors = clazz.getDeclaredConstructors();
...@@ -74,7 +74,7 @@ public class ClassUtils { ...@@ -74,7 +74,7 @@ public class ClassUtils {
if (c.getParameterTypes().length == 0) { if (c.getParameterTypes().length == 0) {
c.setAccessible(true); c.setAccessible(true);
try { try {
return clazz.newInstance(); return clazz.getDeclaredConstructor().newInstance();
} catch (Exception e2) { } catch (Exception e2) {
// ignore // ignore
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论