8155880: Fix langtools usage of the deprecated Class.newInstance method
authordarcy
Mon, 13 Jun 2016 09:12:35 -0700
changeset 38918 bf1ed1a40f5b
parent 38917 f7cfae16c87e
child 38919 88f6e6f58798
8155880: Fix langtools usage of the deprecated Class.newInstance method Reviewed-by: mcimadamore
langtools/src/java.compiler/share/classes/javax/tools/ToolProvider.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/options/Option.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletManager.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java
--- a/langtools/src/java.compiler/share/classes/javax/tools/ToolProvider.java	Mon Jun 13 12:04:09 2016 +0100
+++ b/langtools/src/java.compiler/share/classes/javax/tools/ToolProvider.java	Mon Jun 13 09:12:35 2016 -0700
@@ -125,9 +125,8 @@
     private static <T> T getSystemTool(Class<T> clazz, String moduleName, String className) {
         if (useLegacy) {
             try {
-                @SuppressWarnings("deprecation")
-                T result = Class.forName(className, true, ClassLoader.getSystemClassLoader()).asSubclass(clazz).newInstance();
-                return result;
+                return Class.forName(className, true, ClassLoader.getSystemClassLoader()).
+                    asSubclass(clazz).getConstructor().newInstance();
             } catch (ReflectiveOperationException e) {
                 throw new Error(e);
             }
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Jun 13 12:04:09 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Mon Jun 13 09:12:35 2016 -0700
@@ -31,6 +31,7 @@
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.lang.reflect.Method;
+import java.lang.reflect.Constructor;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.file.Path;
@@ -282,9 +283,7 @@
 
         if (options.isSet(XPRINT)) {
             try {
-                @SuppressWarnings("deprecation")
-                Processor processor = PrintingProcessor.class.newInstance();
-                processorIterator = List.of(processor).iterator();
+                processorIterator = List.of(new PrintingProcessor()).iterator();
             } catch (Throwable t) {
                 AssertionError assertError =
                     new AssertionError("Problem instantiating PrintingProcessor.");
@@ -540,38 +539,40 @@
             if (nextProc != null)
                 return true;
             else {
-                if (!names.hasNext())
+                if (!names.hasNext()) {
                     return false;
-                else {
-                    String processorName = names.next();
+                } else {
+                    Processor processor = getNextProcessor(names.next());
+                    if (processor == null) {
+                        return false;
+                    } else {
+                        nextProc = processor;
+                        return true;
+                    }
+                }
+            }
+        }
 
-                    Processor processor;
-                    try {
-                        try {
-                            Class<?> processorClass = processorCL.loadClass(processorName);
-                            ensureReadable(processorClass);
-                            @SuppressWarnings("deprecation")
-                            Object tmp = processorClass.newInstance();
-                            processor = (Processor) tmp;
-                        } catch (ClassNotFoundException cnfe) {
-                            log.error("proc.processor.not.found", processorName);
-                            return false;
-                        } catch (ClassCastException cce) {
-                            log.error("proc.processor.wrong.type", processorName);
-                            return false;
-                        } catch (Exception e ) {
-                            log.error("proc.processor.cant.instantiate", processorName);
-                            return false;
-                        }
-                    } catch(ClientCodeException e) {
-                        throw e;
-                    } catch(Throwable t) {
-                        throw new AnnotationProcessingError(t);
-                    }
-                    nextProc = processor;
-                    return true;
+        private Processor getNextProcessor(String processorName) {
+            try {
+                try {
+                    Class<?> processorClass = processorCL.loadClass(processorName);
+                    ensureReadable(processorClass);
+                    return (Processor) processorClass.getConstructor().newInstance();
+                } catch (ClassNotFoundException cnfe) {
+                    log.error("proc.processor.not.found", processorName);
+                    return null;
+                } catch (ClassCastException cce) {
+                    log.error("proc.processor.wrong.type", processorName);
+                    return null;
+                } catch (Exception e ) {
+                    log.error("proc.processor.cant.instantiate", processorName);
+                    return null;
                 }
-
+            } catch (ClientCodeException e) {
+                throw e;
+            } catch (Throwable t) {
+                throw new AnnotationProcessingError(t);
             }
         }
 
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/options/Option.java	Mon Jun 13 12:04:09 2016 +0100
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/options/Option.java	Mon Jun 13 09:12:35 2016 -0700
@@ -151,8 +151,8 @@
             // Construct transformer
             try {
                 Class<?> trCls = Class.forName(classname);
-                @SuppressWarnings("deprecation")
-                Transformer transformer = (Transformer) trCls.newInstance();
+                Transformer transformer =
+                    (Transformer) trCls.getConstructor().newInstance();
                 transformer.setExtra(extra);
                 helper.addTransformer(suffix, transformer);
             } catch (Exception e) {
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletManager.java	Mon Jun 13 12:04:09 2016 +0100
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletManager.java	Mon Jun 13 09:12:35 2016 -0700
@@ -252,8 +252,7 @@
             }
             tagClassLoader = fileManager.getClassLoader(TAGLET_PATH);
             Class<?> customTagClass = tagClassLoader.loadClass(classname);
-            @SuppressWarnings("deprecation")
-            Object instance = customTagClass.newInstance();
+            Object instance = customTagClass.getConstructor().newInstance();
             Taglet newLegacy = new UserTaglet((jdk.javadoc.doclet.taglet.Taglet)instance);
             String tname = newLegacy.getName();
             Taglet t = customTags.get(tname);
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java	Mon Jun 13 12:04:09 2016 +0100
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java	Mon Jun 13 09:12:35 2016 -0700
@@ -273,9 +273,9 @@
             initMessager();
             messager.setLocale(locale);
             try {
-                Object o = docletClass.newInstance();
+                Object o = docletClass.getConstructor().newInstance();
                 doclet = (Doclet) o;
-            } catch (InstantiationException | IllegalAccessException exc) {
+            } catch (ReflectiveOperationException exc) {
                 exc.printStackTrace();
                 if (!apiMode) {
                     error("main.could_not_instantiate_class", docletClass);