8155874: Fix java.desktop deprecation warnings about Class.newInstance
Reviewed-by: serb, alexsch
This commit is contained in:
parent
088852ae20
commit
f385a9f5dd
@ -179,8 +179,8 @@ final class AquaUtils {
|
||||
T getInstance() {
|
||||
try {
|
||||
ReflectUtil.checkPackageAccess(clazz);
|
||||
return clazz.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException ignored) {
|
||||
return clazz.getDeclaredConstructor().newInstance();
|
||||
} catch (ReflectiveOperationException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -280,7 +280,8 @@ public final class DocumentHandler extends DefaultHandler {
|
||||
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);
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ class InstanceFinder<T> {
|
||||
}
|
||||
if (this.type.isAssignableFrom(type)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
T tmp = (T) type.newInstance();
|
||||
T tmp = (T) type.getDeclaredConstructor().newInstance();
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
|
@ -1131,16 +1131,12 @@ class GTKStyle extends SynthStyle implements GTKConstants {
|
||||
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;
|
||||
}
|
||||
|
@ -90,12 +90,10 @@ public final class JARSoundbankReader extends SoundbankReader {
|
||||
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();
|
||||
|
@ -137,10 +137,8 @@ public abstract class ModelAbstractOscillator
|
||||
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);
|
||||
|
@ -108,7 +108,7 @@ public abstract class GraphicsEnvironment {
|
||||
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 @@ public abstract class GraphicsEnvironment {
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
@ -468,7 +468,7 @@ public abstract class Toolkit {
|
||||
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 @@ public abstract class Toolkit {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -79,13 +79,12 @@ public abstract class PrinterJob {
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -36,6 +36,7 @@ import java.awt.Component;
|
||||
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 @@ public class Introspector {
|
||||
*/
|
||||
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
|
||||
|
@ -1261,9 +1261,9 @@ static final class sun_swing_PrintColorUIResource_PersistenceDelegate extends Pe
|
||||
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) {
|
||||
|
@ -470,14 +470,14 @@ public class PropertyDescriptor extends FeatureDescriptor {
|
||||
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 });
|
||||
}
|
||||
|
@ -1196,7 +1196,7 @@ public class JEditorPane extends JTextComponent {
|
||||
// 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;
|
||||
|
@ -526,14 +526,16 @@ public class UIManager implements Serializable
|
||||
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 @@ public class UIManager implements Serializable
|
||||
}
|
||||
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 @@ public class UIManager implements Serializable
|
||||
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 @@ public class UIManager implements Serializable
|
||||
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);
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ import sun.reflect.misc.ReflectUtil;
|
||||
* <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 @@ public class ObjectView extends ComponentView {
|
||||
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);
|
||||
|
@ -971,7 +971,7 @@ public abstract class FontConfiguration {
|
||||
|
||||
if (fcc != null) {
|
||||
try {
|
||||
fc = (Charset) fcc.newInstance();
|
||||
fc = (Charset) fcc.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
@ -226,13 +226,10 @@ public abstract class ShellFolder extends File {
|
||||
}
|
||||
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();
|
||||
|
@ -80,10 +80,9 @@ public final class FontManagerFactory {
|
||||
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);
|
||||
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public class CMSManager {
|
||||
if (cmmProviderClass != null) {
|
||||
try {
|
||||
Class<?> cls = Class.forName(cmmProviderClass);
|
||||
provider = (CMMServiceProvider)cls.newInstance();
|
||||
provider = (CMMServiceProvider)cls.getConstructor().newInstance();
|
||||
} catch (ReflectiveOperationException e) {
|
||||
}
|
||||
}
|
||||
|
@ -81,18 +81,15 @@ public class GraphicsPrimitiveProxy extends GraphicsPrimitive {
|
||||
+ 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
|
||||
|
@ -127,7 +127,7 @@ public abstract class RenderingEngine {
|
||||
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 @@ public abstract class RenderingEngine {
|
||||
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) {
|
||||
}
|
||||
}
|
||||
|
@ -172,7 +172,8 @@ class XMap {
|
||||
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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user