From 2dd098eb4b6ff3fe75811438bf6d7e1b93f0fc2a Mon Sep 17 00:00:00 2001 From: Sam Pottinger Date: Mon, 25 Nov 2019 14:47:12 -0800 Subject: [PATCH] Fixed Table's use of deprecated isAccessible, finishing parseInto. Fixed the Table's use of the deprected isAccessible, finishing the parseInto method in the process in order to test taht the new implementation is "correct". Note that there is not a published API for this method on processing.org so it is unclear if the interface's intended behavior was maintained. See https://processing.github.io/processing-javadocs/core/processing/data/Table.html#parseInto-java.lang.Object-java.lang.String-. --- core/src/processing/data/Table.java | 38 +++++++++++++---------- core/test/processing/data/TableTest.java | 39 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 core/test/processing/data/TableTest.java diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index 4d18651cc..538672d9a 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -1038,7 +1038,7 @@ public class Table { // Class sketchClass = sketch.getClass(); Class sketchClass = enclosingObject.getClass(); targetField = sketchClass.getDeclaredField(fieldName); -// PApplet.println("found " + targetField); + // PApplet.println("found " + targetField); Class targetArray = targetField.getType(); if (!targetArray.isArray()) { // fieldName is not an array @@ -1053,20 +1053,20 @@ public class Table { } // Object enclosingObject = sketch; -// PApplet.println("enclosing obj is " + enclosingObject); + // PApplet.println("enclosing obj is " + enclosingObject); Class enclosingClass = target.getEnclosingClass(); Constructor con = null; try { if (enclosingClass == null) { con = target.getDeclaredConstructor(); //new Class[] { }); -// PApplet.println("no enclosing class"); + // PApplet.println("no enclosing class"); } else { con = target.getDeclaredConstructor(new Class[] { enclosingClass }); -// PApplet.println("enclosed by " + enclosingClass.getName()); + // PApplet.println("enclosed by " + enclosingClass.getName()); } - if (!con.isAccessible()) { -// System.out.println("setting constructor to public"); + if (!con.canAccess(null)) { + // System.out.println("setting constructor to public"); con.setAccessible(true); } } catch (SecurityException e) { @@ -1080,14 +1080,9 @@ public class Table { for (Field field : fields) { String name = field.getName(); if (getColumnIndex(name, false) != -1) { -// System.out.println("found field " + name); - if (!field.isAccessible()) { -// PApplet.println(" changing field access"); - field.setAccessible(true); - } inuse.add(field); } else { -// System.out.println("skipping field " + name); + // System.out.println("skipping field " + name); } } @@ -1099,12 +1094,23 @@ public class Table { //item = target.newInstance(); item = con.newInstance(); } else { - item = con.newInstance(new Object[] { enclosingObject }); + item = con.newInstance(enclosingObject); } + + // Only needed once + if (index == 0) { + for (Field field : inuse) { + if (!field.canAccess(item)) { + // PApplet.println(" changing field access"); + field.setAccessible(true); + } + } + } + //Object item = defaultCons.newInstance(new Object[] { }); for (Field field : inuse) { String name = field.getName(); - //PApplet.println("gonna set field " + name); + // PApplet.println("gonna set field " + name); if (field.getType() == String.class) { field.set(item, row.getString(name)); @@ -1151,8 +1157,8 @@ public class Table { // list.add(item); Array.set(outgoing, index++, item); } - if (!targetField.isAccessible()) { -// PApplet.println("setting target field to public"); + if (!targetField.canAccess(enclosingObject)) { + // PApplet.println("setting target field to public"); targetField.setAccessible(true); } // Set the array in the sketch diff --git a/core/test/processing/data/TableTest.java b/core/test/processing/data/TableTest.java new file mode 100644 index 000000000..c9226842c --- /dev/null +++ b/core/test/processing/data/TableTest.java @@ -0,0 +1,39 @@ +package processing.data; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TableTest { + + class Person { + public String name; + public int age; + + public Person() { + name = ""; + age = -1; + } + } + + + Person[] people; + + @Test + public void parseInto() { + Table table = new Table(); + table.addColumn("name"); + table.addColumn("age"); + + TableRow row = table.addRow(); + row.setString("name", "Person1"); + row.setInt("age", 30); + + table.parseInto(this, "people"); + + Assert.assertEquals(people[0].name, "Person1"); + Assert.assertEquals(people[0].age, 30); + } +} \ No newline at end of file