JDK 9 RFR of JDK-8155516: Suppress warnings from uses of deprecated Class.newInstance langtools

Joseph D. Darcy joe.darcy at oracle.com
Tue May 3 00:57:32 UTC 2016


Hi Jon,

I filed the follow-up issue

     JDK-8155880: Fix langtools usage of the deprecated 
Class.newInstance method

Cheers,

-Joe

On 4/27/2016 4:59 PM, Jonathan Gibbons wrote:
> Joe,
>
> I guess this is OK for now, but as a general policy for langtools, I 
> hope we are not going to see @SuppressWarnings proliferating 
> throughout the code.  If a method has been deprecated, it is 
> presumably for a good reason (<cough> Optional.get </cough>) and there 
> should in general be a replacement, and we should endeavor to improve 
> the quality of the code base by using the recommended replacements, 
> rather than perpetuating the sins of our predecessors.
>
> As for @SuppressWarnings, the review yesterday had a credible use of 
> the annotation insofar as the replacement method was not available in 
> JDK 8, which (for the duration of JDK 9 development) is our bootstrap 
> compiler.
>
> For all of Stuart's protestations that these are not breaking changes, 
> we are sure doing a lot of high priority reacting to non-breaking 
> changes!
>
> -- Jon
>
> On 04/27/2016 04:21 PM, Jonathan Gibbons wrote:
>> What is the recommended approach for avoiding the issue, as compared 
>> to sweeping it under the carpet?
>>
>> -- Jon
>>
>>
>> On 04/27/2016 02:27 PM, Joseph D. Darcy wrote:
>>> Hello,
>>>
>>> As part of the effort to address
>>>
>>>     JDK-6850612: Deprecate Class.newInstance since it violates the 
>>> checked exception language contract
>>>
>>> the uses of Class.newInstance in the langtools repo have to be 
>>> address in some way, either by suppressing the resulting warning or 
>>> by refactoring the code to use other recommended idioms.
>>>
>>> Please review the fix for
>>>
>>>     JDK-8155516: Suppress warnings from uses of deprecated 
>>> Class.newInstance langtools
>>>     http://cr.openjdk.java.net/~darcy/8155516.0/
>>>
>>> which takes the suppression approach.
>>>
>>> Patch below.
>>>
>>> Thanks,
>>>
>>> -Joe
>>>
>>> --- 
>>> old/src/java.compiler/share/classes/javax/tools/ToolProvider.java 
>>> 2016-04-27 14:16:21.269126143 -0700
>>> +++ 
>>> new/src/java.compiler/share/classes/javax/tools/ToolProvider.java 
>>> 2016-04-27 14:16:21.177126140 -0700
>>> @@ -123,7 +123,9 @@
>>>      private static <T> T getSystemTool(Class<T> clazz, String 
>>> moduleName, String className) {
>>>          if (useLegacy) {
>>>              try {
>>> -                return Class.forName(className, true, 
>>> ClassLoader.getSystemClassLoader()).asSubclass(clazz).newInstance();
>>> +                @SuppressWarnings("deprecation")
>>> +                T result = Class.forName(className, true, 
>>> ClassLoader.getSystemClassLoader()).asSubclass(clazz).newInstance();
>>> +                return result;
>>>              } catch (ReflectiveOperationException e) {
>>>                  throw new Error(e);
>>>              }
>>> --- 
>>> old/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java 
>>> 2016-04-27 14:16:21.573126153 -0700
>>> +++ 
>>> new/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java 
>>> 2016-04-27 14:16:21.489126150 -0700
>>> @@ -282,6 +282,7 @@
>>>
>>>          if (options.isSet(XPRINT)) {
>>>              try {
>>> +                @SuppressWarnings("deprecation")
>>>                  Processor processor = 
>>> PrintingProcessor.class.newInstance();
>>>                  processorIterator = List.of(processor).iterator();
>>>              } catch (Throwable t) {
>>> @@ -549,8 +550,9 @@
>>>                          try {
>>>                              Class<?> processorClass = 
>>> processorCL.loadClass(processorName);
>>>                              ensureReadable(processorClass);
>>> -                            processor =
>>> -                                (Processor) 
>>> (processorClass.newInstance());
>>> +                            @SuppressWarnings("deprecation")
>>> +                            Object tmp = processorClass.newInstance();
>>> +                            processor = (Processor) tmp;
>>>                          } catch (ClassNotFoundException cnfe) {
>>> log.error("proc.processor.not.found", processorName);
>>>                              return false;
>>> --- 
>>> old/src/jdk.compiler/share/classes/com/sun/tools/sjavac/options/Option.java 
>>> 2016-04-27 14:16:21.881126163 -0700
>>> +++ 
>>> new/src/jdk.compiler/share/classes/com/sun/tools/sjavac/options/Option.java 
>>> 2016-04-27 14:16:21.797126161 -0700
>>> @@ -151,6 +151,7 @@
>>>              // Construct transformer
>>>              try {
>>>                  Class<?> trCls = Class.forName(classname);
>>> +                @SuppressWarnings("deprecation")
>>>                  Transformer transformer = (Transformer) 
>>> trCls.newInstance();
>>>                  transformer.setExtra(extra);
>>>                  helper.addTransformer(suffix, transformer);
>>> --- 
>>> old/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletManager.java 
>>> 2016-04-27 14:16:22.181126173 -0700
>>> +++ 
>>> new/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/taglets/TagletManager.java 
>>> 2016-04-27 14:16:22.097126171 -0700
>>> @@ -247,6 +247,7 @@
>>>              tagClassLoader = fileManager.getClassLoader(TAGLET_PATH);
>>>              Class<?> customTagClass = 
>>> tagClassLoader.loadClass(classname);
>>>              ensureReadable(customTagClass);
>>> +            @SuppressWarnings("deprecation")
>>>              Object instance = customTagClass.newInstance();
>>>              Taglet newLegacy = new 
>>> UserTaglet((jdk.javadoc.doclet.taglet.Taglet)instance);
>>>              String tname = newLegacy.getName();
>>> --- 
>>> old/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java 
>>> 2016-04-27 14:16:22.481126183 -0700
>>> +++ 
>>> new/src/jdk.javadoc/share/classes/jdk/javadoc/internal/tool/Start.java 
>>> 2016-04-27 14:16:22.393126180 -0700
>>> @@ -252,7 +252,9 @@
>>>              initMessager();
>>>              messager.setLocale(locale);
>>>              try {
>>> -                doclet = (Doclet) docletClass.newInstance();
>>> +                @SuppressWarnings("deprecation")
>>> +                Object o = docletClass.newInstance();
>>> +                doclet = (Doclet) o;
>>>              } catch (InstantiationException | 
>>> IllegalAccessException exc) {
>>>                  exc.printStackTrace();
>>>                  if (!apiMode) {
>>>
>>
>



More information about the compiler-dev mailing list