8155874: Fix java.desktop deprecation warnings about Class.newInstance
Reviewed-by: serb, alexsch
--- a/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaUtils.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaUtils.java Wed Nov 09 11:28:13 2016 -0800
@@ -179,8 +179,8 @@
T getInstance() {
try {
ReflectUtil.checkPackageAccess(clazz);
- return clazz.newInstance();
- } catch (InstantiationException | IllegalAccessException ignored) {
+ return clazz.getDeclaredConstructor().newInstance();
+ } catch (ReflectiveOperationException ignored) {
}
return null;
}
--- a/jdk/src/java.desktop/share/classes/com/sun/beans/decoder/DocumentHandler.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/beans/decoder/DocumentHandler.java Wed Nov 09 11:28:13 2016 -0800
@@ -280,7 +280,8 @@
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
ElementHandler parent = this.handler;
try {
- this.handler = getElementHandler(qName).newInstance();
+ this.handler =
+ getElementHandler(qName).getDeclaredConstructor().newInstance();
this.handler.setOwner(this);
this.handler.setParent(parent);
}
--- a/jdk/src/java.desktop/share/classes/com/sun/beans/finder/InstanceFinder.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/beans/finder/InstanceFinder.java Wed Nov 09 11:28:13 2016 -0800
@@ -94,7 +94,7 @@
}
if (this.type.isAssignableFrom(type)) {
@SuppressWarnings("unchecked")
- T tmp = (T) type.newInstance();
+ T tmp = (T) type.getDeclaredConstructor().newInstance();
return tmp;
}
}
--- a/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKStyle.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/gtk/GTKStyle.java Wed Nov 09 11:28:13 2016 -0800
@@ -1131,16 +1131,12 @@
getContextClassLoader());
if (methodName == null) {
- return c.newInstance();
+ return c.getDeclaredConstructor().newInstance();
}
Method m = c.getMethod(methodName, (Class<?>[])null);
return m.invoke(c, (Object[])null);
- } catch (ClassNotFoundException cnfe) {
- } catch (IllegalAccessException iae) {
- } catch (InvocationTargetException ite) {
- } catch (NoSuchMethodException nsme) {
- } catch (InstantiationException ie) {
+ } catch (ReflectiveOperationException e) {
}
return null;
}
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/JARSoundbankReader.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/JARSoundbankReader.java Wed Nov 09 11:28:13 2016 -0800
@@ -90,12 +90,10 @@
Class<?> c = Class.forName(line.trim(), false, ucl);
if (Soundbank.class.isAssignableFrom(c)) {
ReflectUtil.checkPackageAccess(c);
- Object o = c.newInstance();
+ Object o = c.getDeclaredConstructor().newInstance();
soundbanks.add((Soundbank) o);
}
- } catch (ClassNotFoundException ignored) {
- } catch (InstantiationException ignored) {
- } catch (IllegalAccessException ignored) {
+ } catch (ReflectiveOperationException ignored) {
}
}
line = r.readLine();
--- a/jdk/src/java.desktop/share/classes/com/sun/media/sound/ModelAbstractOscillator.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/com/sun/media/sound/ModelAbstractOscillator.java Wed Nov 09 11:28:13 2016 -0800
@@ -137,10 +137,8 @@
public ModelOscillatorStream open(float samplerate) {
ModelAbstractOscillator oscs;
try {
- oscs = this.getClass().newInstance();
- } catch (InstantiationException e) {
- throw new IllegalArgumentException(e);
- } catch (IllegalAccessException e) {
+ oscs = this.getClass().getDeclaredConstructor().newInstance();
+ } catch (ReflectiveOperationException e) {
throw new IllegalArgumentException(e);
}
oscs.setSampleRate(samplerate);
--- a/jdk/src/java.desktop/share/classes/java/awt/GraphicsEnvironment.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/java/awt/GraphicsEnvironment.java Wed Nov 09 11:28:13 2016 -0800
@@ -108,7 +108,7 @@
ClassLoader cl = ClassLoader.getSystemClassLoader();
geCls = Class.forName(nm, true, cl);
}
- ge = (GraphicsEnvironment)geCls.newInstance();
+ ge = (GraphicsEnvironment)geCls.getConstructor().newInstance();
// long t1 = System.currentTimeMillis();
// System.out.println("GE creation took " + (t1-t0)+ "ms.");
if (isHeadless()) {
@@ -116,12 +116,9 @@
}
} catch (ClassNotFoundException e) {
throw new Error("Could not find class: "+nm);
- } catch (InstantiationException e) {
+ } catch (ReflectiveOperationException | IllegalArgumentException e) {
throw new Error("Could not instantiate Graphics Environment: "
+ nm);
- } catch (IllegalAccessException e) {
- throw new Error ("Could not access Graphics Environment: "
- + nm);
}
return ge;
}
--- a/jdk/src/java.desktop/share/classes/java/awt/Toolkit.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/java/awt/Toolkit.java Wed Nov 09 11:28:13 2016 -0800
@@ -468,7 +468,7 @@
private static void fallbackToLoadClassForAT(String atName) {
try {
Class<?> c = Class.forName(atName, false, ClassLoader.getSystemClassLoader());
- c.newInstance();
+ c.getConstructor().newInstance();
} catch (ClassNotFoundException e) {
newAWTError(e, "Assistive Technology not found: " + atName);
} catch (InstantiationException e) {
@@ -583,15 +583,13 @@
}
try {
if (cls != null) {
- toolkit = (Toolkit)cls.newInstance();
+ toolkit = (Toolkit)cls.getConstructor().newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}
}
- } catch (final InstantiationException ignored) {
- throw new AWTError("Could not instantiate Toolkit: " + nm);
- } catch (final IllegalAccessException ignored) {
- throw new AWTError("Could not access Toolkit: " + nm);
+ } catch (final ReflectiveOperationException ignored) {
+ throw new AWTError("Could not create Toolkit: " + nm);
}
return null;
}
--- a/jdk/src/java.desktop/share/classes/java/awt/print/PrinterJob.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/java/awt/print/PrinterJob.java Wed Nov 09 11:28:13 2016 -0800
@@ -79,13 +79,12 @@
public PrinterJob run() {
String nm = System.getProperty("java.awt.printerjob", null);
try {
- return (PrinterJob)Class.forName(nm).newInstance();
+ return (PrinterJob)Class.forName(nm).
+ getConstructor().newInstance();
} catch (ClassNotFoundException e) {
throw new AWTError("PrinterJob not found: " + nm);
- } catch (InstantiationException e) {
+ } catch (ReflectiveOperationException e) {
throw new AWTError("Could not instantiate PrinterJob: " + nm);
- } catch (IllegalAccessException e) {
- throw new AWTError("Could not access PrinterJob: " + nm);
}
}
});
--- a/jdk/src/java.desktop/share/classes/java/beans/Introspector.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/java/beans/Introspector.java Wed Nov 09 11:28:13 2016 -0800
@@ -36,6 +36,7 @@
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
@@ -1280,11 +1281,12 @@
*/
static Object instantiate(Class<?> sibling, String className)
throws InstantiationException, IllegalAccessException,
+ NoSuchMethodException, InvocationTargetException,
ClassNotFoundException {
// First check with sibling's classloader (if any).
ClassLoader cl = sibling.getClassLoader();
Class<?> cls = ClassFinder.findClass(className, cl);
- return cls.newInstance();
+ return cls.getDeclaredConstructor().newInstance();
}
} // end class Introspector
--- a/jdk/src/java.desktop/share/classes/java/beans/MetaData.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/java/beans/MetaData.java Wed Nov 09 11:28:13 2016 -0800
@@ -1261,9 +1261,9 @@
internalPersistenceDelegates.put(typeName, defaultPersistenceDelegate);
try {
String name = type.getName();
- Class c = Class.forName("java.beans.MetaData$" + name.replace('.', '_')
+ Class<?> c = Class.forName("java.beans.MetaData$" + name.replace('.', '_')
+ "_PersistenceDelegate");
- pd = (PersistenceDelegate)c.newInstance();
+ pd = (PersistenceDelegate)c.getDeclaredConstructor().newInstance();
internalPersistenceDelegates.put(typeName, pd);
}
catch (ClassNotFoundException e) {
--- a/jdk/src/java.desktop/share/classes/java/beans/PropertyDescriptor.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/java/beans/PropertyDescriptor.java Wed Nov 09 11:28:13 2016 -0800
@@ -470,14 +470,14 @@
Constructor<?> ctor = null;
if (bean != null) {
try {
- ctor = cls.getConstructor(new Class<?>[] { Object.class });
+ ctor = cls.getDeclaredConstructor(new Class<?>[] { Object.class });
} catch (Exception ex) {
// Fall through
}
}
try {
if (ctor == null) {
- editor = cls.newInstance();
+ editor = cls.getDeclaredConstructor().newInstance();
} else {
editor = ctor.newInstance(new Object[] { bean });
}
--- a/jdk/src/java.desktop/share/classes/javax/swing/JEditorPane.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JEditorPane.java Wed Nov 09 11:28:13 2016 -0800
@@ -1196,7 +1196,7 @@
// registerEditorKitForContentType(type, class, null).
c = SwingUtilities.loadSystemClass(classname);
}
- k = (EditorKit) c.newInstance();
+ k = (EditorKit) c.getDeclaredConstructor().newInstance();
kitRegistry.put(type, k);
} catch (Throwable e) {
k = null;
--- a/jdk/src/java.desktop/share/classes/javax/swing/UIManager.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/UIManager.java Wed Nov 09 11:28:13 2016 -0800
@@ -526,14 +526,16 @@
if (info.getName().equals(name)) {
Class<?> cls = Class.forName(UIManager.class.getModule(),
info.getClassName());
- LookAndFeel laf = (LookAndFeel) cls.newInstance();
+ LookAndFeel laf =
+ (LookAndFeel) cls.getDeclaredConstructor().newInstance();
if (!laf.isSupportedLookAndFeel()) {
break;
}
return laf;
}
}
- } catch (InstantiationException | IllegalAccessException ignore) {
+ } catch (ReflectiveOperationException |
+ IllegalArgumentException ignore) {
}
throw new UnsupportedLookAndFeelException(name);
@@ -625,7 +627,16 @@
}
else {
Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
- setLookAndFeel((LookAndFeel)(lnfClass.newInstance()));
+ try {
+ LookAndFeel laf =
+ (LookAndFeel)lnfClass.getDeclaredConstructor().newInstance();
+ setLookAndFeel(laf);
+ } catch (ReflectiveOperationException | IllegalArgumentException e) {
+ InstantiationException ex =
+ new InstantiationException("Wrapped Exception");
+ ex.initCause(e);
+ throw ex;
+ }
}
}
@@ -1093,7 +1104,8 @@
String className = getLAFState().swingProps.getProperty(multiplexingLAFKey, defaultName);
try {
Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
- multiLookAndFeel = (LookAndFeel)lnfClass.newInstance();
+ multiLookAndFeel =
+ (LookAndFeel)lnfClass.getDeclaredConstructor().newInstance();
} catch (Exception exc) {
System.err.println("UIManager: failed loading " + className);
}
@@ -1427,7 +1439,8 @@
String className = p.nextToken();
try {
Class<?> lnfClass = SwingUtilities.loadSystemClass(className);
- LookAndFeel newLAF = (LookAndFeel)lnfClass.newInstance();
+ LookAndFeel newLAF =
+ (LookAndFeel)lnfClass.getDeclaredConstructor().newInstance();
newLAF.initialize();
auxLookAndFeels.addElement(newLAF);
}
--- a/jdk/src/java.desktop/share/classes/javax/swing/text/html/ObjectView.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/text/html/ObjectView.java Wed Nov 09 11:28:13 2016 -0800
@@ -46,7 +46,7 @@
* <p>
* If the class can successfully be loaded, an attempt will
* be made to create an instance of it by calling
- * <code>Class.newInstance</code>. An attempt will be made
+ * <code>Class.getDeclaredConstructor().newInstance</code>. An attempt will be made
* to narrow the instance to type <code>java.awt.Component</code>
* to display the object.
* <p>
@@ -92,7 +92,7 @@
ReflectUtil.checkPackageAccess(classname);
Class<?> c = Class.forName(classname, true,Thread.currentThread().
getContextClassLoader());
- Object o = c.newInstance();
+ Object o = c.getDeclaredConstructor().newInstance();
if (o instanceof Component) {
Component comp = (Component) o;
setParameters(comp, attr);
--- a/jdk/src/java.desktop/share/classes/sun/awt/FontConfiguration.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/awt/FontConfiguration.java Wed Nov 09 11:28:13 2016 -0800
@@ -971,7 +971,7 @@
if (fcc != null) {
try {
- fc = (Charset) fcc.newInstance();
+ fc = (Charset) fcc.getDeclaredConstructor().newInstance();
} catch (Exception e) {
}
}
--- a/jdk/src/java.desktop/share/classes/sun/awt/shell/ShellFolder.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/awt/shell/ShellFolder.java Wed Nov 09 11:28:13 2016 -0800
@@ -226,13 +226,10 @@
}
try {
shellFolderManager =
- (ShellFolderManager)managerClass.newInstance();
- } catch (InstantiationException e) {
+ (ShellFolderManager)managerClass.getDeclaredConstructor().newInstance();
+ } catch (ReflectiveOperationException e) {
throw new Error("Could not instantiate Shell Folder Manager: "
+ managerClass.getName());
- } catch (IllegalAccessException e) {
- throw new Error ("Could not access Shell Folder Manager: "
- + managerClass.getName());
}
invoker = shellFolderManager.createInvoker();
--- a/jdk/src/java.desktop/share/classes/sun/font/FontManagerFactory.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/font/FontManagerFactory.java Wed Nov 09 11:28:13 2016 -0800
@@ -80,10 +80,9 @@
DEFAULT_CLASS);
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class<?> fmClass = Class.forName(fmClassName, true, cl);
- instance = (FontManager) fmClass.newInstance();
- } catch (ClassNotFoundException |
- InstantiationException |
- IllegalAccessException ex) {
+ instance =
+ (FontManager) fmClass.getDeclaredConstructor().newInstance();
+ } catch (ReflectiveOperationException ex) {
throw new InternalError(ex);
}
--- a/jdk/src/java.desktop/share/classes/sun/java2d/cmm/CMSManager.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/cmm/CMSManager.java Wed Nov 09 11:28:13 2016 -0800
@@ -57,7 +57,7 @@
if (cmmProviderClass != null) {
try {
Class<?> cls = Class.forName(cmmProviderClass);
- provider = (CMMServiceProvider)cls.newInstance();
+ provider = (CMMServiceProvider)cls.getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
}
}
--- a/jdk/src/java.desktop/share/classes/sun/java2d/loops/GraphicsPrimitiveProxy.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/loops/GraphicsPrimitiveProxy.java Wed Nov 09 11:28:13 2016 -0800
@@ -81,18 +81,15 @@
+ relativeClassName;
try {
Class<?> clazz = Class.forName(name);
- GraphicsPrimitive p = (GraphicsPrimitive) clazz.newInstance();
+ GraphicsPrimitive p =
+ (GraphicsPrimitive) clazz.getDeclaredConstructor().newInstance();
if (!satisfiesSameAs(p)) {
throw new RuntimeException("Primitive " + p
+ " incompatible with proxy for "
+ name);
}
return p;
- } catch (ClassNotFoundException ex) {
- throw new RuntimeException(ex.toString());
- } catch (InstantiationException ex) {
- throw new RuntimeException(ex.toString());
- } catch (IllegalAccessException ex) {
+ } catch (ReflectiveOperationException ex) {
throw new RuntimeException(ex.toString());
}
// A RuntimeException should never happen in a deployed JDK, because
--- a/jdk/src/java.desktop/share/classes/sun/java2d/pipe/RenderingEngine.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/pipe/RenderingEngine.java Wed Nov 09 11:28:13 2016 -0800
@@ -127,7 +127,7 @@
if (reClass != null) {
try {
Class<?> cls = Class.forName(reClass);
- reImpl = (RenderingEngine) cls.newInstance();
+ reImpl = (RenderingEngine) cls.getConstructor().newInstance();
} catch (ReflectiveOperationException ignored0) {
}
}
@@ -135,7 +135,7 @@
final String marlinREClass = "sun.java2d.marlin.MarlinRenderingEngine";
try {
Class<?> cls = Class.forName(marlinREClass);
- reImpl = (RenderingEngine) cls.newInstance();
+ reImpl = (RenderingEngine) cls.getConstructor().newInstance();
} catch (ReflectiveOperationException ignored1) {
}
}
--- a/jdk/src/java.desktop/unix/classes/sun/font/XMap.java Wed Nov 09 10:26:29 2016 -0800
+++ b/jdk/src/java.desktop/unix/classes/sun/font/XMap.java Wed Nov 09 11:28:13 2016 -0800
@@ -172,7 +172,8 @@
if (className != null) {
try {
if (className.startsWith("sun.awt")) {
- enc = ((Charset)Class.forName(className).newInstance()).newEncoder();
+ enc = ((Charset)Class.forName(className).getDeclaredConstructor().
+ newInstance()).newEncoder();
} else {
enc = Charset.forName(className).newEncoder();
}