From 34f9caa1371ecf479107e1d5370b30029b34ab13 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 18 Oct 2017 22:42:13 -0400 Subject: [PATCH] deal with a handful of Java 9 changes and deprecations --- app/src/processing/app/Base.java | 3 ++- app/src/processing/app/Platform.java | 6 +++--- .../app/contrib/ToolContribution.java | 4 ++-- app/src/processing/app/ui/Editor.java | 2 +- app/src/processing/app/ui/Toolkit.java | 2 +- core/src/processing/core/PApplet.java | 6 +++--- core/src/processing/data/JSONArray.java | 10 +++++----- core/src/processing/data/JSONObject.java | 18 +++++++++--------- core/src/processing/data/Table.java | 10 +++++----- core/todo.txt | 7 +++++++ 10 files changed, 38 insertions(+), 30 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index a0203d1d7..925152750 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -712,7 +712,8 @@ public class Base { protected void initInternalTool(String className) { try { Class toolClass = Class.forName(className); - final Tool tool = (Tool) toolClass.newInstance(); + final Tool tool = (Tool) + toolClass.getDeclaredConstructor().newInstance(); tool.init(this); internalTools.add(tool); diff --git a/app/src/processing/app/Platform.java b/app/src/processing/app/Platform.java index 89315b92a..16a31bf80 100644 --- a/app/src/processing/app/Platform.java +++ b/app/src/processing/app/Platform.java @@ -41,14 +41,14 @@ import processing.core.PConstants; public class Platform { static DefaultPlatform inst; - static Map platformNames = new HashMap(); + static Map platformNames = new HashMap<>(); static { platformNames.put(PConstants.WINDOWS, "windows"); //$NON-NLS-1$ platformNames.put(PConstants.MACOSX, "macosx"); //$NON-NLS-1$ platformNames.put(PConstants.LINUX, "linux"); //$NON-NLS-1$ } - static Map platformIndices = new HashMap(); + static Map platformIndices = new HashMap<>(); static { platformIndices.put("windows", PConstants.WINDOWS); //$NON-NLS-1$ platformIndices.put("macosx", PConstants.MACOSX); //$NON-NLS-1$ @@ -86,7 +86,7 @@ public class Platform { } else if (Platform.isLinux()) { platformClass = Class.forName("processing.app.platform.LinuxPlatform"); //$NON-NLS-1$ } - inst = (DefaultPlatform) platformClass.newInstance(); + inst = (DefaultPlatform) platformClass.getDeclaredConstructor().newInstance(); } catch (Exception e) { Messages.showError("Problem Setting the Platform", "An unknown error occurred while trying to load\n" + diff --git a/app/src/processing/app/contrib/ToolContribution.java b/app/src/processing/app/contrib/ToolContribution.java index b3af82bf7..a9afc7f29 100644 --- a/app/src/processing/app/contrib/ToolContribution.java +++ b/app/src/processing/app/contrib/ToolContribution.java @@ -63,7 +63,7 @@ public class ToolContribution extends LocalContribution implements Tool, Compara String className = initLoader(null); if (className != null) { Class toolClass = loader.loadClass(className); - tool = (Tool) toolClass.newInstance(); + tool = (Tool) toolClass.getDeclaredConstructor().newInstance(); } referenceFile = new File(folder, "reference/index.html"); @@ -95,7 +95,7 @@ public class ToolContribution extends LocalContribution implements Tool, Compara static public List loadAll(File toolsFolder) { File[] list = ContributionType.TOOL.listCandidates(toolsFolder); - ArrayList outgoing = new ArrayList(); + ArrayList outgoing = new ArrayList<>(); // If toolsFolder does not exist or is inaccessible (stranger things have // happened, and are reported as bugs) list will come back null. if (list != null) { diff --git a/app/src/processing/app/ui/Editor.java b/app/src/processing/app/ui/Editor.java index 4ced89bb8..2c5ed8613 100644 --- a/app/src/processing/app/ui/Editor.java +++ b/app/src/processing/app/ui/Editor.java @@ -2479,7 +2479,7 @@ public abstract class Editor extends JFrame implements RunnerListener { // on macosx, setting the destructive property places this option // away from the others at the lefthand side pane.putClientProperty("Quaqua.OptionPane.destructiveOption", - new Integer(2)); + Integer.valueOf(2)); JDialog dialog = pane.createDialog(this, null); dialog.setVisible(true); diff --git a/app/src/processing/app/ui/Toolkit.java b/app/src/processing/app/ui/Toolkit.java index 12779ab4a..34f24c03a 100644 --- a/app/src/processing/app/ui/Toolkit.java +++ b/app/src/processing/app/ui/Toolkit.java @@ -385,7 +385,7 @@ public class Toolkit { cleanChars = cleanString.toCharArray(); cleanCharas = new Character[cleanChars.length]; for (int i = 0; i < cleanChars.length; i++) { - cleanCharas[i] = new Character(cleanChars[i]); + cleanCharas[i] = cleanChars[i]; } Arrays.sort(cleanCharas, charComparator); // sorts in increasing order for (char mnem : cleanCharas) { diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 24f661377..116f9e505 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -9596,7 +9596,7 @@ public class PApplet implements PConstants { static final public float parseFloat(String what, float otherwise) { try { - return new Float(what).floatValue(); + return Float.parseFloat(what); } catch (NumberFormatException e) { } return otherwise; @@ -9646,7 +9646,7 @@ public class PApplet implements PConstants { float output[] = new float[what.length]; for (int i = 0; i < what.length; i++) { try { - output[i] = new Float(what[i]).floatValue(); + output[i] = Float.parseFloat(what[i]); } catch (NumberFormatException e) { output[i] = missing; } @@ -10669,7 +10669,7 @@ public class PApplet implements PConstants { try { Class c = Thread.currentThread().getContextClassLoader().loadClass(name); - sketch = (PApplet) c.newInstance(); + sketch = (PApplet) c.getDeclaredConstructor().newInstance(); } catch (RuntimeException re) { // Don't re-package runtime exceptions throw re; diff --git a/core/src/processing/data/JSONArray.java b/core/src/processing/data/JSONArray.java index 9755a6e74..ea8276bd8 100644 --- a/core/src/processing/data/JSONArray.java +++ b/core/src/processing/data/JSONArray.java @@ -109,7 +109,7 @@ public class JSONArray { * Construct an empty JSONArray. */ public JSONArray() { - this.myArrayList = new ArrayList(); + this.myArrayList = new ArrayList<>(); } @@ -165,7 +165,7 @@ public class JSONArray { * @nowebref */ public JSONArray(IntList list) { - myArrayList = new ArrayList(); + myArrayList = new ArrayList<>(); for (int item : list.values()) { myArrayList.add(Integer.valueOf(item)); } @@ -176,9 +176,9 @@ public class JSONArray { * @nowebref */ public JSONArray(FloatList list) { - myArrayList = new ArrayList(); + myArrayList = new ArrayList<>(); for (float item : list.values()) { - myArrayList.add(new Float(item)); + myArrayList.add(Float.valueOf(item)); } } @@ -187,7 +187,7 @@ public class JSONArray { * @nowebref */ public JSONArray(StringList list) { - myArrayList = new ArrayList(); + myArrayList = new ArrayList<>(); for (String item : list.values()) { myArrayList.add(item); } diff --git a/core/src/processing/data/JSONObject.java b/core/src/processing/data/JSONObject.java index 768fdd7f8..f76900ae0 100644 --- a/core/src/processing/data/JSONObject.java +++ b/core/src/processing/data/JSONObject.java @@ -123,7 +123,7 @@ public class JSONObject { * string objects. This is used by JSONObject.put(string, object). */ private static HashMap keyPool = - new HashMap(keyPoolSize); + new HashMap<>(keyPoolSize); // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . @@ -197,7 +197,7 @@ public class JSONObject { * @nowebref */ public JSONObject() { - this.map = new HashMap(); + this.map = new HashMap<>(); } @@ -291,7 +291,7 @@ public class JSONObject { * the JSONObject. */ protected JSONObject(HashMap map) { - this.map = new HashMap(); + this.map = new HashMap<>(); if (map != null) { Iterator i = map.entrySet().iterator(); while (i.hasNext()) { @@ -309,7 +309,7 @@ public class JSONObject { * @nowebref */ public JSONObject(IntDict dict) { - map = new HashMap(); + map = new HashMap<>(); for (int i = 0; i < dict.size(); i++) { setInt(dict.key(i), dict.value(i)); } @@ -320,7 +320,7 @@ public class JSONObject { * @nowebref */ public JSONObject(FloatDict dict) { - map = new HashMap(); + map = new HashMap<>(); for (int i = 0; i < dict.size(); i++) { setFloat(dict.key(i), dict.value(i)); } @@ -331,7 +331,7 @@ public class JSONObject { * @nowebref */ public JSONObject(StringDict dict) { - map = new HashMap(); + map = new HashMap<>(); for (int i = 0; i < dict.size(); i++) { setString(dict.key(i), dict.value(i)); } @@ -1212,7 +1212,7 @@ public class JSONObject { * @see JSONObject#setBoolean(String, boolean) */ public JSONObject setFloat(String key, float value) { - this.put(key, new Double(value)); + this.put(key, Double.valueOf(value)); return this; } @@ -1226,7 +1226,7 @@ public class JSONObject { * @throws RuntimeException If the key is null or if the number is NaN or infinite. */ public JSONObject setDouble(String key, double value) { - this.put(key, new Double(value)); + this.put(key, Double.valueOf(value)); return this; } @@ -1326,7 +1326,7 @@ public class JSONObject { pooled = (String)keyPool.get(key); if (pooled == null) { if (keyPool.size() >= keyPoolSize) { - keyPool = new HashMap(keyPoolSize); + keyPool = new HashMap<>(keyPoolSize); } keyPool.put(key, key); } else { diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 657fd6e5d..f96c15230 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -1076,7 +1076,7 @@ public class Table { } Field[] fields = target.getDeclaredFields(); - ArrayList inuse = new ArrayList(); + ArrayList inuse = new ArrayList<>(); for (Field field : fields) { String name = field.getName(); if (getColumnIndex(name, false) != -1) { @@ -2215,7 +2215,7 @@ public class Table { // only create this on first get(). subsequent calls to set the title will // also update this array, but only if it exists. if (columnIndices == null) { - columnIndices = new HashMap(); + columnIndices = new HashMap<>(); for (int col = 0; col < columns.length; col++) { columnIndices.put(columnTitles[col], col); } @@ -4195,8 +4195,8 @@ public class Table { static class HashMapBlows { - HashMap dataToIndex = new HashMap(); - ArrayList indexToData = new ArrayList(); + HashMap dataToIndex = new HashMap<>(); + ArrayList indexToData = new ArrayList<>(); HashMapBlows() { } @@ -4255,7 +4255,7 @@ public class Table { void read(DataInputStream input) throws IOException { int count = input.readInt(); //System.out.println("found " + count + " entries in category map"); - dataToIndex = new HashMap(count); + dataToIndex = new HashMap<>(count); for (int i = 0; i < count; i++) { String str = input.readUTF(); //System.out.println(i + " " + str); diff --git a/core/todo.txt b/core/todo.txt index 496be7412..a33849f7d 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -17,6 +17,13 @@ X https://github.com/processing/processing/pull/5202 _ need to make this work behind the scenes instead _ create icon.png or have an 'icons' folder with multiple sizes +_ Switch to getModifiersEx() and fix the AWT modifiers used in PSurfaceAWT +_ this is an easy fix, but need to check impact elsewhere +_ does anything else rely on these modifiers? + +_ Fix Java 9 incompatibilities inside PSurfaceFX +_ https://github.com/processing/processing/issues/5286 + _ Hitting ESC in FX2D app on macOS throws IllegalStateException _ https://github.com/processing/processing/issues/5249