kylin-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (KYLIN-3290) Avoid calling Class#newInstance
Date Tue, 20 Nov 2018 08:16:00 GMT

    [ https://issues.apache.org/jira/browse/KYLIN-3290?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16692816#comment-16692816
] 

ASF GitHub Bot commented on KYLIN-3290:
---------------------------------------

shaofengshi closed pull request #351: KYLIN-3290 Leverage getDecalredConstructor().newInstance()
instead of newInstance()
URL: https://github.com/apache/kylin/pull/351
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core-common/src/main/java/org/apache/kylin/common/util/ClassUtil.java b/core-common/src/main/java/org/apache/kylin/common/util/ClassUtil.java
index cd78fe0e01..5019b21053 100644
--- a/core-common/src/main/java/org/apache/kylin/common/util/ClassUtil.java
+++ b/core-common/src/main/java/org/apache/kylin/common/util/ClassUtil.java
@@ -85,7 +85,7 @@ private static String forRenamedClass(String name) {
 
     public static Object newInstance(String clz) {
         try {
-            return forName(clz, Object.class).newInstance();
+            return forName(clz, Object.class).getDeclaredConstructor().newInstance();
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/DictionaryInfoSerializer.java
b/core-dictionary/src/main/java/org/apache/kylin/dict/DictionaryInfoSerializer.java
index ee88ea7cb3..96d34a1439 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/DictionaryInfoSerializer.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/DictionaryInfoSerializer.java
@@ -21,6 +21,7 @@
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
 
 import org.apache.kylin.common.persistence.Serializer;
 import org.apache.kylin.common.util.ClassUtil;
@@ -63,14 +64,19 @@ public DictionaryInfo deserialize(DataInputStream in) throws IOException
{
         if (infoOnly == false) {
             Dictionary<String> dict;
             try {
-                dict = (Dictionary<String>) ClassUtil.forName(obj.getDictionaryClass(),
Dictionary.class).newInstance();
+                dict = (Dictionary<String>) ClassUtil.forName(obj.getDictionaryClass(),
Dictionary.class).getDeclaredConstructor().newInstance();
             } catch (InstantiationException e) {
                 throw new RuntimeException(e);
             } catch (IllegalAccessException e) {
                 throw new RuntimeException(e);
             } catch (ClassNotFoundException e) {
                 throw new RuntimeException(e);
+            } catch (NoSuchMethodException e) {
+                throw new RuntimeException(e);
+            } catch (InvocationTargetException e) {
+                throw new RuntimeException(e);
             }
+
             dict.readFields(in);
             obj.setDictionaryObject(dict);
         }
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/DictionarySerializer.java
b/core-dictionary/src/main/java/org/apache/kylin/dict/DictionarySerializer.java
index 72dc9953c3..23f2b88608 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/DictionarySerializer.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/DictionarySerializer.java
@@ -41,7 +41,7 @@ private DictionarySerializer() {
         try {
             final DataInputStream dataInputStream = new DataInputStream(inputStream);
             final String type = dataInputStream.readUTF();
-            final Dictionary<?> dictionary = ClassUtil.forName(type, Dictionary.class).newInstance();
+            final Dictionary<?> dictionary = ClassUtil.forName(type, Dictionary.class).getDeclaredConstructor().newInstance();
             dictionary.readFields(dataInputStream);
             return dictionary;
         } catch (Exception e) {
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java b/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java
index 1377e8ef13..8e5979289d 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary.java
@@ -49,7 +49,7 @@ protected boolean isNullObjectForm(T value) {
     @Override
     protected void setConverterByName(String converterName) throws Exception {
         converterName = "org.apache.kylin.dict.Number2BytesConverter";
-        this.bytesConvert = ClassUtil.forName(converterName, BytesConverter.class).newInstance();
+        this.bytesConvert = ClassUtil.forName(converterName, BytesConverter.class).getDeclaredConstructor().newInstance();
         ((Number2BytesConverter)this.bytesConvert).setMaxDigitsBeforeDecimalPoint(Number2BytesConverter.MAX_DIGITS_BEFORE_DECIMAL_POINT_LEGACY);
     }
 
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary2.java b/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary2.java
index 0efe9362ca..24700ca48d 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary2.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/NumberDictionary2.java
@@ -40,7 +40,7 @@ public NumberDictionary2(byte[] trieBytes) {
     @Override
     protected void setConverterByName(String converterName) throws Exception {
         converterName = "org.apache.kylin.dict.Number2BytesConverter";
-        this.bytesConvert = ClassUtil.forName(converterName, BytesConverter.class).newInstance();
+        this.bytesConvert = ClassUtil.forName(converterName, BytesConverter.class).getDeclaredConstructor().newInstance();
     }
 
 }
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionary.java b/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionary.java
index 8303bb06a6..b0eb5bb98a 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionary.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionary.java
@@ -126,7 +126,7 @@ private void init(byte[] trieBytes) {
     }
 
     protected void setConverterByName(String converterName) throws Exception {
-        this.bytesConvert = ClassUtil.forName(converterName, BytesConverter.class).newInstance();
+        this.bytesConvert = ClassUtil.forName(converterName, BytesConverter.class).getDeclaredConstructor().newInstance();
     }
 
     @Override
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionaryForest.java
b/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionaryForest.java
index 4642cf4dd4..3d6d781ac9 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionaryForest.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/TrieDictionaryForest.java
@@ -211,7 +211,7 @@ public void readFields(DataInput in) throws IOException {
             String converterName = in.readUTF();
             BytesConverter<T> bytesConverter = null;
             if (converterName.isEmpty() == false)
-                bytesConverter = ClassUtil.forName(converterName, BytesConverter.class).newInstance();
+                bytesConverter = ClassUtil.forName(converterName, BytesConverter.class).getDeclaredConstructor().newInstance();
             //init accuOffset
             int accuSize = in.readInt();
             ArrayList<Integer> accuOffset = new ArrayList<>();
diff --git a/core-dictionary/src/main/java/org/apache/kylin/dict/global/GlobalDictHDFSStore.java
b/core-dictionary/src/main/java/org/apache/kylin/dict/global/GlobalDictHDFSStore.java
index f286085d1c..17ced1cb35 100644
--- a/core-dictionary/src/main/java/org/apache/kylin/dict/global/GlobalDictHDFSStore.java
+++ b/core-dictionary/src/main/java/org/apache/kylin/dict/global/GlobalDictHDFSStore.java
@@ -316,7 +316,7 @@ public GlobalDictMetadata readIndexFile(Path dir) throws IOException {
                 String converterName = in.readUTF();
                 BytesConverter converter;
                 try {
-                    converter = ClassUtil.forName(converterName, BytesConverter.class).newInstance();
+                    converter = ClassUtil.forName(converterName, BytesConverter.class).getDeclaredConstructor().newInstance();
                 } catch (Exception e) {
                     throw new RuntimeException("Fail to instantiate BytesConverter: " + converterName,
e);
                 }
@@ -386,7 +386,7 @@ public GlobalDictMetadata readIndexFile(Path dir) throws IOException {
                 String converterName = in.readUTF();
                 BytesConverter converter;
                 try {
-                    converter = ClassUtil.forName(converterName, BytesConverter.class).newInstance();
+                    converter = ClassUtil.forName(converterName, BytesConverter.class).getDeclaredConstructor().newInstance();
                 } catch (Exception e) {
                     throw new RuntimeException("Fail to instantiate BytesConverter: " + converterName,
e);
                 }
diff --git a/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java b/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
index eb3c773c2a..5a2051f593 100644
--- a/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
+++ b/kylin-it/src/test/java/org/apache/kylin/jdbc/ITJDBCDriverTest.java
@@ -96,7 +96,7 @@ protected static void startJetty() throws Exception {
 
     protected Connection getConnection() throws Exception {
 
-        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
+        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").getDeclaredConstructor().newInstance();
         Properties info = new Properties();
         info.put("user", "ADMIN");
         info.put("password", "KYLIN");
diff --git a/query/src/main/java/org/apache/kylin/query/QueryConnection.java b/query/src/main/java/org/apache/kylin/query/QueryConnection.java
index 1ea715fefb..3ef4b77073 100644
--- a/query/src/main/java/org/apache/kylin/query/QueryConnection.java
+++ b/query/src/main/java/org/apache/kylin/query/QueryConnection.java
@@ -19,6 +19,7 @@
 package org.apache.kylin.query;
 
 import java.io.File;
+import java.lang.reflect.InvocationTargetException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
@@ -38,9 +39,9 @@ public static Connection getConnection(String project) throws SQLException
{
             try {
                 Class<?> aClass = Thread.currentThread().getContextClassLoader()
                         .loadClass("org.apache.calcite.jdbc.Driver");
-                Driver o = (Driver) aClass.newInstance();
+                Driver o = (Driver) aClass.getDeclaredConstructor().newInstance();
                 DriverManager.registerDriver(o);
-            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
e) {
+            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException e) {
                 e.printStackTrace();
             }
             isRegister = true;
diff --git a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
index fb64c565d7..5ea043746a 100644
--- a/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
+++ b/query/src/main/java/org/apache/kylin/query/relnode/OLAPAggregateRel.java
@@ -110,7 +110,7 @@
         for (String func : udafs.keySet()) {
             try {
                 AGGR_FUNC_PARAM_AS_MEASURE_MAP.put(func,
-                        ((ParamAsMeasureCount) (udafs.get(func).newInstance())).getParamAsMeasureCount());
+                        ((ParamAsMeasureCount) (udafs.get(func).getDeclaredConstructor().newInstance())).getParamAsMeasureCount());
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
diff --git a/server-base/src/test/java/org/apache/kylin/rest/bean/BeanValidator.java b/server-base/src/test/java/org/apache/kylin/rest/bean/BeanValidator.java
index a7aea99915..256a20e27e 100644
--- a/server-base/src/test/java/org/apache/kylin/rest/bean/BeanValidator.java
+++ b/server-base/src/test/java/org/apache/kylin/rest/bean/BeanValidator.java
@@ -67,7 +67,7 @@
                     try {
                         Object value = buildValue(returnType);
 
-                        T bean = clazz.newInstance();
+                        T bean = clazz.getDeclaredConstructor().newInstance();
 
                         setter.invoke(bean, value);
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Avoid calling Class#newInstance
> -------------------------------
>
>                 Key: KYLIN-3290
>                 URL: https://issues.apache.org/jira/browse/KYLIN-3290
>             Project: Kylin
>          Issue Type: Task
>            Reporter: Ted Yu
>            Assignee: Wu Bin
>            Priority: Minor
>              Labels: jdk
>             Fix For: v2.6.0
>
>
> Class#newInstance is deprecated starting in Java 9 - https://bugs.openjdk.java.net/browse/JDK-6850612
- because it may throw undeclared checked exceptions.
> The suggested replacement is getDeclaredConstructor().newInstance(), which wraps the
checked exceptions in InvocationException.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message