Skip to content

avoid use of deprecated methods (mostly Class.newInstance()) #1774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jme3-android/src/main/java/com/jme3/app/AndroidHarness.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public Object onRetainNonConfigurationInstance() {
}

@Override
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
initializeLogHandler();

Expand Down Expand Up @@ -240,7 +241,7 @@ public void onCreate(Bundle savedInstanceState) {
try {
if (app == null) {
Class clazz = Class.forName(appClass);
app = (LegacyApplication)clazz.newInstance();
app = (LegacyApplication) clazz.getDeclaredConstructor().newInstance();
}

app.setSettings(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void onAttach(Activity activity) {
* @param savedInstanceState the saved instance state
*/
@Override
@SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
initializeLogHandler();
logger.fine("onCreate");
Expand All @@ -258,7 +259,7 @@ public void onCreate(Bundle savedInstanceState) {
try {
if (app == null) {
Class clazz = Class.forName(appClass);
app = (LegacyApplication)clazz.newInstance();
app = (LegacyApplication) clazz.getDeclaredConstructor().newInstance();
}

app.setSettings(settings);
Expand Down
8 changes: 5 additions & 3 deletions jme3-core/src/main/java/com/jme3/anim/Armature.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import com.jme3.math.Matrix4f;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
Expand Down Expand Up @@ -130,8 +130,10 @@ public void setModelTransformClass(Class<? extends JointModelTransform> modelTra

private void instantiateJointModelTransform(Joint joint) {
try {
joint.setJointModelTransform(modelTransformClass.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
joint.setJointModelTransform(modelTransformClass.getDeclaredConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw new IllegalArgumentException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public final T[] toObjectArray() {
try {
T[] compactArr = (T[]) Array.newInstance(getElementClass(), getSerializedSize() / getTupleSize());
for (int i = 0; i < compactArr.length; i++) {
compactArr[i] = getElementClass().newInstance();
compactArr[i] = getElementClass().getDeclaredConstructor().newInstance();
deserialize(i, compactArr[i]);
}

Expand Down
21 changes: 12 additions & 9 deletions jme3-core/src/main/java/com/jme3/asset/ImplHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
package com.jme3.asset;

import com.jme3.asset.cache.AssetCache;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -115,12 +115,15 @@ public Class<?> getTypeClass() {
@Override
protected T initialValue() {
try {
T obj = type.newInstance();
T obj = type.getDeclaredConstructor().newInstance();

if (path != null) {
((AssetLocator) obj).setRootPath(path);
}
return obj;
} catch (InstantiationException | IllegalAccessException ex) {
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException ex) {
logger.log(Level.SEVERE, "Cannot create locator of type {0}, does"
+ " the class have an empty and publicly accessible"
+ " constructor?", type.getName());
Expand Down Expand Up @@ -220,12 +223,12 @@ public <T extends AssetCache> T getCache(Class<T> cacheClass) {
cache = (T) classToCacheMap.get(cacheClass);
if (cache == null) {
try {
cache = cacheClass.newInstance();
cache = cacheClass.getDeclaredConstructor().newInstance();
classToCacheMap.put(cacheClass, cache);
} catch (InstantiationException ex) {
} catch (InstantiationException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex) {
throw new IllegalArgumentException("The cache class cannot"
+ " be created, ensure it has empty constructor", ex);
} catch (IllegalAccessException ex) {
} catch (IllegalAccessException | SecurityException ex) {
throw new IllegalArgumentException("The cache class cannot "
+ "be accessed", ex);
}
Expand All @@ -246,12 +249,12 @@ public <T extends AssetProcessor> T getProcessor(Class<T> procClass) {
proc = (T) classToProcMap.get(procClass);
if (proc == null) {
try {
proc = procClass.newInstance();
proc = procClass.getDeclaredConstructor().newInstance();
classToProcMap.put(procClass, proc);
} catch (InstantiationException ex) {
} catch (InstantiationException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException ex) {
throw new IllegalArgumentException("The processor class cannot"
+ " be created, ensure it has empty constructor", ex);
} catch (IllegalAccessException ex) {
} catch (IllegalAccessException | SecurityException ex) {
throw new IllegalArgumentException("The processor class cannot "
+ "be accessed", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.jme3.animation.LoopMode;
import com.jme3.app.Application;
import com.jme3.audio.AudioData;
import com.jme3.audio.AudioNode;
import com.jme3.audio.AudioSource;
import com.jme3.cinematic.Cinematic;
Expand Down Expand Up @@ -153,7 +154,7 @@ public SoundEvent() {
@Override
public void initEvent(Application app, Cinematic cinematic) {
super.initEvent(app, cinematic);
audioNode = new AudioNode(app.getAssetManager(), path, stream);
audioNode = new AudioNode(app.getAssetManager(), path, stream ? AudioData.DataType.Stream : AudioData.DataType.Buffer);
audioNode.setPositional(false);
setLoopMode(loopMode);
}
Expand Down
4 changes: 2 additions & 2 deletions jme3-core/src/main/java/com/jme3/math/Spline.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ public void read(JmeImporter im) throws IOException {
/* Empty List as default, prevents null pointers */
float list[] = in.readFloatArray("segmentsLength", null);
if (list != null) {
segmentsLength = new ArrayList<Float>();
segmentsLength = new ArrayList<>(list.length);
for (int i = 0; i < list.length; i++) {
segmentsLength.add(new Float(list[i]));
segmentsLength.add(list[i]);
}
}
type = in.readEnum("pathSplineType", SplineType.class, SplineType.CatmullRom);
Expand Down
10 changes: 6 additions & 4 deletions jme3-core/src/main/java/com/jme3/shader/Uniform.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.jme3.math.*;
import com.jme3.util.BufferUtils;
import com.jme3.util.TempVars;

import java.lang.reflect.InvocationTargetException;
import java.nio.*;

public class Uniform extends ShaderVariable {
Expand Down Expand Up @@ -355,9 +355,11 @@ public void setValue(VarType type, Object value){
//handle the null case
if (this.value == null) {
try {
this.value = value.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("Cannot instantiate param of class " + value.getClass().getCanonicalName());
this.value = value.getClass().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw new IllegalArgumentException("Cannot instantiate param of class " + value.getClass().getCanonicalName(), e);
}
}
//feed the pivot vec 4 with the correct value
Expand Down
4 changes: 2 additions & 2 deletions jme3-core/src/main/java/com/jme3/system/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ public int getHeight() {
* Get the width of the window
*
* @return the width of the window (in pixels)
* @see #setWindowWidth(int)
* @see #setWindowSize(int, int)
*/
public int getWindowWidth() {
int w = getInteger("WindowWidth");
Expand All @@ -1036,7 +1036,7 @@ public int getWindowWidth() {
* Get the height of the window
*
* @return the height of the window (in pixels)
* @see #setWindowHeight(int)
* @see #setWindowSize(int, int)
*/
public int getWindowHeight() {
int h = getInteger("WindowHeight");
Expand Down
7 changes: 4 additions & 3 deletions jme3-core/src/main/java/com/jme3/system/JmeSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.logging.Level;
Expand Down Expand Up @@ -202,9 +203,9 @@ public static void initialize(AppSettings settings) {
systemDelegate.initialize(settings);
}

private static JmeSystemDelegate tryLoadDelegate(String className) throws InstantiationException, IllegalAccessException {
private static JmeSystemDelegate tryLoadDelegate(String className) throws InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
try {
return (JmeSystemDelegate) Class.forName(className).newInstance();
return (JmeSystemDelegate) Class.forName(className).getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException ex) {
return null;
}
Expand All @@ -227,7 +228,7 @@ private static void checkDelegate() {
}
}
}
} catch (InstantiationException | IllegalAccessException ex) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(JmeSystem.class.getName()).log(Level.SEVERE, "Failed to create JmeSystem delegate:\n{0}", ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.jme3.util;

import com.jme3.system.Annotations.Internal;

import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -28,7 +27,7 @@ protected static BufferAllocator create() {

final String className = System.getProperty(PROPERTY_BUFFER_ALLOCATOR_IMPLEMENTATION, ReflectionAllocator.class.getName());
try {
return (BufferAllocator) Class.forName(className).newInstance();
return (BufferAllocator) Class.forName(className).getDeclaredConstructor().newInstance();
} catch (final Throwable e) {
LOGGER.log(Level.WARNING, "Unable to access {0}", className);
return new PrimitiveAllocator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private Image convertImageToAwt(Image source) {
return null;
}
Image newImage = new Image(format, source.getWidth(), source.getHeight(), BufferUtils.createByteBuffer(source.getWidth() * source.getHeight() * 4), null, ColorSpace.Linear);
clazz.getMethod("convert", Image.class, Image.class).invoke(clazz.newInstance(), source, newImage);
clazz.getMethod("convert", Image.class, Image.class).invoke(clazz.getDeclaredConstructor().newInstance(), source, newImage);
return newImage;
} catch (InstantiationException
| IllegalAccessException
Expand Down
11 changes: 8 additions & 3 deletions jme3-desktop/src/main/java/com/jme3/app/AppletHarness.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.awt.Graphics;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
Expand All @@ -65,6 +66,7 @@ public static Applet getApplet(Application app){
return appToApplet.get(app);
}

@SuppressWarnings("unchecked")
private void createCanvas(){
AppSettings settings = new AppSettings(true);

Expand Down Expand Up @@ -104,10 +106,13 @@ private void createCanvas(){

try{
Class clazz = Class.forName(appClass);
app = (LegacyApplication) clazz.newInstance();
app = (LegacyApplication) clazz.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException
| InstantiationException
| IllegalAccessException ex) {
| InstantiationException
| IllegalAccessException
| NoSuchMethodException
| IllegalArgumentException
| InvocationTargetException ex) {
ex.printStackTrace();
}

Expand Down
28 changes: 20 additions & 8 deletions jme3-desktop/src/main/java/com/jme3/system/JmeDesktopSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -191,6 +192,7 @@ public void run() {
return result.get() == SettingsDialog.APPROVE_SELECTION;
}

@SuppressWarnings("unchecked")
private JmeContext newContextLwjgl(AppSettings settings, JmeContext.Type type) {
try {
Class ctxClazz = null;
Expand All @@ -208,8 +210,10 @@ private JmeContext newContextLwjgl(AppSettings settings, JmeContext.Type type) {
throw new IllegalArgumentException("Unsupported context type " + type);
}

return (JmeContext) ctxClazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
return (JmeContext) ctxClazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException ex) {
logger.log(Level.SEVERE, "Failed to create context", ex);
} catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, "CRITICAL ERROR: Context class is missing!\n"
Expand All @@ -219,6 +223,7 @@ private JmeContext newContextLwjgl(AppSettings settings, JmeContext.Type type) {
return null;
}

@SuppressWarnings("unchecked")
private JmeContext newContextJogl(AppSettings settings, JmeContext.Type type) {
try {
Class ctxClazz = null;
Expand All @@ -236,8 +241,10 @@ private JmeContext newContextJogl(AppSettings settings, JmeContext.Type type) {
throw new IllegalArgumentException("Unsupported context type " + type);
}

return (JmeContext) ctxClazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
return (JmeContext) ctxClazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException ex) {
logger.log(Level.SEVERE, "Failed to create context", ex);
} catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, "CRITICAL ERROR: Context class is missing!\n"
Expand All @@ -247,13 +254,16 @@ private JmeContext newContextJogl(AppSettings settings, JmeContext.Type type) {
return null;
}

@SuppressWarnings("unchecked")
private JmeContext newContextCustom(AppSettings settings, JmeContext.Type type) {
try {
String className = settings.getRenderer().substring("CUSTOM".length());

Class ctxClazz = Class.forName(className);
return (JmeContext) ctxClazz.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
return (JmeContext) ctxClazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException ex) {
logger.log(Level.SEVERE, "Failed to create context", ex);
} catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, "CRITICAL ERROR: Context class is missing!", ex);
Expand Down Expand Up @@ -292,11 +302,13 @@ public JmeContext newContext(AppSettings settings, Type contextType) {
private <T> T newObject(String className) {
try {
Class<T> clazz = (Class<T>) Class.forName(className);
return clazz.newInstance();
return clazz.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException ex) {
logger.log(Level.SEVERE, "CRITICAL ERROR: Audio implementation class "
+ className + " is missing!\n", ex);
} catch (IllegalAccessException | InstantiationException ex) {
} catch (InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException ex) {
logger.log(Level.SEVERE, "Failed to create context", ex);
}

Expand Down
2 changes: 1 addition & 1 deletion jme3-examples/src/main/java/jme3test/TestChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void run() {
for (Class<?> clazz : appClass) {
try {
if (LegacyApplication.class.isAssignableFrom(clazz)) {
Object app = clazz.newInstance();
Object app = clazz.getDeclaredConstructor().newInstance();
if (app instanceof SimpleApplication) {
final Method settingMethod = clazz.getMethod("setShowSettings", boolean.class);
settingMethod.invoke(app, showSetting);
Expand Down
Loading