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

Reviewed-by: mcimadamore
This commit is contained in:
Joe Darcy 2016-06-13 09:12:35 -07:00
parent 2c8f465c47
commit c52bd6ba1d
5 changed files with 39 additions and 40 deletions

View file

@ -125,9 +125,8 @@ public class ToolProvider {
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);
}

View file

@ -31,6 +31,7 @@ import java.io.IOException;
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 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
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 @@ public class JavacProcessingEnvironment implements ProcessingEnvironment, Closea
if (nextProc != null)
return true;
else {
if (!names.hasNext())
if (!names.hasNext()) {
return false;
else {
String processorName = names.next();
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);
} else {
Processor processor = getNextProcessor(names.next());
if (processor == null) {
return false;
} else {
nextProc = processor;
return true;
}
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);
}
}

View file

@ -151,8 +151,8 @@ public enum Option {
// 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) {

View file

@ -252,8 +252,7 @@ public class TagletManager {
}
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);

View file

@ -273,9 +273,9 @@ public class Start extends ToolOption.Helper {
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);