From 2b19ff8d26f90626b5d9d3516d3e552bc575b49c Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 23 Sep 2020 19:52:36 -0400 Subject: [PATCH] loadJSONObject/Array returns null if not found (https://github.com/processing/processing/pull/6081) --- core/src/processing/core/PApplet.java | 280 ++++++++++++++------------ core/todo.txt | 13 +- 2 files changed, 156 insertions(+), 137 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 5ad30132b..1a388aeea 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -5840,13 +5840,16 @@ public class PApplet implements PConstants { public JSONObject loadJSONObject(String filename) { // can't pass of createReader() to the constructor b/c of resource leak BufferedReader reader = createReader(filename); - JSONObject outgoing = new JSONObject(reader); - try { - reader.close(); - } catch (IOException e) { // not sure what would cause this - e.printStackTrace(); + if (reader != null) { + JSONObject outgoing = new JSONObject(reader); + try { + reader.close(); + } catch (IOException e) { // not sure what would cause this + e.printStackTrace(); + } + return outgoing; } - return outgoing; + return null; } @@ -5856,13 +5859,16 @@ public class PApplet implements PConstants { static public JSONObject loadJSONObject(File file) { // can't pass of createReader() to the constructor b/c of resource leak BufferedReader reader = createReader(file); - JSONObject outgoing = new JSONObject(reader); - try { - reader.close(); - } catch (IOException e) { // not sure what would cause this - e.printStackTrace(); + if (reader != null) { + JSONObject outgoing = new JSONObject(reader); + try { + reader.close(); + } catch (IOException e) { // not sure what would cause this + e.printStackTrace(); + } + return outgoing; } - return outgoing; + return null; } @@ -5950,26 +5956,32 @@ public class PApplet implements PConstants { public JSONArray loadJSONArray(String filename) { // can't pass of createReader() to the constructor b/c of resource leak BufferedReader reader = createReader(filename); - JSONArray outgoing = new JSONArray(reader); - try { - reader.close(); - } catch (IOException e) { // not sure what would cause this - e.printStackTrace(); + if (reader != null) { + JSONArray outgoing = new JSONArray(reader); + try { + reader.close(); + } catch (IOException e) { // not sure what would cause this + e.printStackTrace(); + } + return outgoing; } - return outgoing; + return null; } static public JSONArray loadJSONArray(File file) { // can't pass of createReader() to the constructor b/c of resource leak BufferedReader reader = createReader(file); - JSONArray outgoing = new JSONArray(reader); - try { - reader.close(); - } catch (IOException e) { // not sure what would cause this - e.printStackTrace(); + if (reader != null) { + JSONArray outgoing = new JSONArray(reader); + try { + reader.close(); + } catch (IOException e) { // not sure what would cause this + e.printStackTrace(); + } + return outgoing; } - return outgoing; + return null; } @@ -11320,13 +11332,13 @@ public class PApplet implements PConstants { /** * - * Defines if textures repeat or draw once within a texture map. - * The two parameters are CLAMP (the default behavior) and REPEAT. + * Defines if textures repeat or draw once within a texture map. + * The two parameters are CLAMP (the default behavior) and REPEAT. * This function only works with the P2D and P3D renderers. * * * @webref image:textures - * @webBrief Defines if textures repeat or draw once within a texture map. + * @webBrief Defines if textures repeat or draw once within a texture map. * @param wrap Either CLAMP (default) or REPEAT * @see PGraphics#texture(PImage) * @see PGraphics#textureMode(int) @@ -11442,18 +11454,18 @@ public class PApplet implements PConstants { /** - * Use the beginContour() and endContour() function to - * create negative shapes within shapes such as the center of the - * letter 'O'. beginContour() begins recording vertices for the - * shape and endContour() stops recording. The vertices that - * define a negative shape must "wind" in the opposite direction from - * the exterior shape. First draw vertices for the exterior shape in + * Use the beginContour() and endContour() function to + * create negative shapes within shapes such as the center of the + * letter 'O'. beginContour() begins recording vertices for the + * shape and endContour() stops recording. The vertices that + * define a negative shape must "wind" in the opposite direction from + * the exterior shape. First draw vertices for the exterior shape in * clockwise order, then for internal shapes, draw vertices counterclockwise.
*
- * These functions can only be used within a beginShape()/endShape() - * pair and transformations such as translate(), rotate(), and - * scale() do not work within a beginContour()/endContour() - * pair. It is also not possible to use other shapes, such as ellipse() + * These functions can only be used within a beginShape()/endShape() + * pair and transformations such as translate(), rotate(), and + * scale() do not work within a beginContour()/endContour() + * pair. It is also not possible to use other shapes, such as ellipse() * or rect() within. * * @webref shape:vertex @@ -11466,20 +11478,20 @@ public class PApplet implements PConstants { /** - * Use the beginContour() and endContour() function to - * create negative shapes within shapes such as the center of the - * letter 'O'. beginContour() begins recording vertices for - * the shape and endContour() stops recording. The vertices - * that define a negative shape must "wind" in the opposite direction - * from the exterior shape. First draw vertices for the exterior shape + * Use the beginContour() and endContour() function to + * create negative shapes within shapes such as the center of the + * letter 'O'. beginContour() begins recording vertices for + * the shape and endContour() stops recording. The vertices + * that define a negative shape must "wind" in the opposite direction + * from the exterior shape. First draw vertices for the exterior shape * in clockwise order, then for internal shapes, draw vertices counterclockwise.
*
- * These functions can only be used within a beginShape()/endShape() - * pair and transformations such as translate(), rotate(), and - * scale() do not work within a beginContour()/endContour() - * pair. It is also not possible to use other shapes, such as ellipse() - * or rect() within. - * + * These functions can only be used within a beginShape()/endShape() + * pair and transformations such as translate(), rotate(), and + * scale() do not work within a beginContour()/endContour() + * pair. It is also not possible to use other shapes, such as ellipse() + * or rect() within. + * * @webref shape:vertex * @webBrief Stops recording vertices for the shape. */ @@ -11517,23 +11529,23 @@ public class PApplet implements PConstants { /** - * Loads geometry into a variable of type PShape. SVG and OBJ - * files may be loaded. To load correctly, the file must be located - * in the data directory of the current sketch. In most cases, - * loadShape() should be used inside setup() because + * Loads geometry into a variable of type PShape. SVG and OBJ + * files may be loaded. To load correctly, the file must be located + * in the data directory of the current sketch. In most cases, + * loadShape() should be used inside setup() because * loading shapes inside draw() will reduce the speed of a sketch.
*
- * Alternatively, the file maybe be loaded from anywhere on the local - * computer using an absolute path (something that starts with / on - * Unix and Linux, or a drive letter on Windows), or the filename + * Alternatively, the file maybe be loaded from anywhere on the local + * computer using an absolute path (something that starts with / on + * Unix and Linux, or a drive letter on Windows), or the filename * parameter can be a URL for a file found on a network.
*
- * If the file is not available or an error occurs, null will - * be returned and an error message will be printed to the console. - * The error message does not halt the program, however the null value - * may cause a NullPointerException if your code does not check whether + * If the file is not available or an error occurs, null will + * be returned and an error message will be printed to the console. + * The error message does not halt the program, however the null value + * may cause a NullPointerException if your code does not check whether * the value returned is null.
- * + * * @webref shape * @webBrief Loads geometry into a variable of type PShape. * @param filename name of file to load, can be .svg or .obj @@ -11554,35 +11566,35 @@ public class PApplet implements PConstants { /** - * The createShape() function is used to define a new shape. - * Once created, this shape can be drawn with the shape() - * function. The basic way to use the function defines new primitive - * shapes. One of the following parameters are used as the first - * parameter: ELLIPSE, RECT, ARC, TRIANGLE, - * SPHERE, BOX, QUAD, or LINE. The - * parameters for each of these different shapes are the same as their - * corresponding functions: ellipse(), rect(), arc(), - * triangle(), sphere(), box(), quad(), and + * The createShape() function is used to define a new shape. + * Once created, this shape can be drawn with the shape() + * function. The basic way to use the function defines new primitive + * shapes. One of the following parameters are used as the first + * parameter: ELLIPSE, RECT, ARC, TRIANGLE, + * SPHERE, BOX, QUAD, or LINE. The + * parameters for each of these different shapes are the same as their + * corresponding functions: ellipse(), rect(), arc(), + * triangle(), sphere(), box(), quad(), and * line(). The first example above clarifies how this works.
*
- * Custom, unique shapes can be made by using createShape() without - * a parameter. After the shape is started, the drawing attributes and - * geometry can be set directly to the shape within the beginShape() - * and endShape() methods. See the second example above for specifics, + * Custom, unique shapes can be made by using createShape() without + * a parameter. After the shape is started, the drawing attributes and + * geometry can be set directly to the shape within the beginShape() + * and endShape() methods. See the second example above for specifics, * and the reference for beginShape() for all of its options.
*
- * The createShape() function can also be used to make a complex - * shape made of other shapes. This is called a "group" and it's created by - * using the parameter GROUP as the first parameter. See the fourth + * The createShape() function can also be used to make a complex + * shape made of other shapes. This is called a "group" and it's created by + * using the parameter GROUP as the first parameter. See the fourth * example above to see how it works.
*
- * After using createShape(), stroke and fill color can be set by - * calling methods like setFill() and setStroke(), as seen - * in the examples above. The complete list of methods and fields for the + * After using createShape(), stroke and fill color can be set by + * calling methods like setFill() and setStroke(), as seen + * in the examples above. The complete list of methods and fields for the * PShape class are in the Processing Javadoc. - * + * * @webref shape - * @webBrief The createShape() function is used to define a new shape. + * @webBrief The createShape() function is used to define a new shape. * @see PShape * @see PShape#endShape() * @see PApplet#loadShape(String) @@ -11607,25 +11619,25 @@ public class PApplet implements PConstants { /** - * Loads a shader into the PShader object. The shader file must be - * loaded in the sketch's "data" folder/directory to load correctly. - * Shaders are compatible with the P2D and P3D renderers, but not + * Loads a shader into the PShader object. The shader file must be + * loaded in the sketch's "data" folder/directory to load correctly. + * Shaders are compatible with the P2D and P3D renderers, but not * with the default renderer.
*
- * Alternatively, the file maybe be loaded from anywhere on the local - * computer using an absolute path (something that starts with / on - * Unix and Linux, or a drive letter on Windows), or the filename + * Alternatively, the file maybe be loaded from anywhere on the local + * computer using an absolute path (something that starts with / on + * Unix and Linux, or a drive letter on Windows), or the filename * parameter can be a URL for a file found on a network.
*
- * If the file is not available or an error occurs, null will - * be returned and an error message will be printed to the console. - * The error message does not halt the program, however the null - * value may cause a NullPointerException if your code does not check + * If the file is not available or an error occurs, null will + * be returned and an error message will be printed to the console. + * The error message does not halt the program, however the null + * value may cause a NullPointerException if your code does not check * whether the value returned is null.
* * * @webref rendering:shaders - * @webBrief Loads a shader into the PShader object. + * @webBrief Loads a shader into the PShader object. * @param fragFilename name of fragment shader file */ public PShader loadShader(String fragFilename) { @@ -11643,7 +11655,7 @@ public class PApplet implements PConstants { /** * - * Applies the shader specified by the parameters. It's compatible with + * Applies the shader specified by the parameters. It's compatible with * the P2D and P3D renderers, but not with the default renderer. * * @@ -11668,12 +11680,12 @@ public class PApplet implements PConstants { /** * - * Restores the default shaders. Code that runs after resetShader() + * Restores the default shaders. Code that runs after resetShader() * will not be affected by previously defined shaders. * * * @webref rendering:shaders - * @webBrief Restores the default shaders. + * @webBrief Restores the default shaders. */ public void resetShader() { if (recorder != null) recorder.resetShader(); @@ -11736,11 +11748,11 @@ public class PApplet implements PConstants { /** * - * Blends the pixels in the display window according to a defined mode. - * There is a choice of the following modes to blend the source pixels (A) - * with the ones of pixels already in the display window (B). Each pixel's - * final color is the result of applying one of the blend modes with each - * channel of (A) and (B) independently. The red channel is compared with + * Blends the pixels in the display window according to a defined mode. + * There is a choice of the following modes to blend the source pixels (A) + * with the ones of pixels already in the display window (B). Each pixel's + * final color is the result of applying one of the blend modes with each + * channel of (A) and (B) independently. The red channel is compared with * red, green with green, and blue with blue.
*
* BLEND - linear interpolation of colors: C = A*factor + B. This is the default.
@@ -11763,15 +11775,15 @@ public class PApplet implements PConstants { *
* REPLACE - the pixels entirely replace the others and don't utilize alpha (transparency) values
*
- * We recommend using blendMode() and not the previous blend() - * function. However, unlike blend(), the blendMode() function - * does not support the following: HARD_LIGHT, SOFT_LIGHT, OVERLAY, DODGE, - * BURN. On older hardware, the LIGHTEST, DARKEST, and DIFFERENCE modes might - * not be available as well. + * We recommend using blendMode() and not the previous blend() + * function. However, unlike blend(), the blendMode() function + * does not support the following: HARD_LIGHT, SOFT_LIGHT, OVERLAY, DODGE, + * BURN. On older hardware, the LIGHTEST, DARKEST, and DIFFERENCE modes might + * not be available as well. * * * @webref rendering - * @webBrief Blends the pixels in the display window according to a defined mode. + * @webBrief Blends the pixels in the display window according to a defined mode. * @param mode the blending mode to use */ public void blendMode(int mode) { @@ -11826,17 +11838,17 @@ public class PApplet implements PConstants { /** - * Specifies vertex coordinates for quadratic Bezier curves. Each call - * to quadraticVertex() defines the position of one control - * point and one anchor point of a Bezier curve, adding a new segment - * to a line or shape. The first time quadraticVertex() is used - * within a beginShape() call, it must be prefaced with a call - * to vertex() to set the first anchor point. This function must - * be used between beginShape() and endShape() and only - * when there is no MODE parameter specified to beginShape(). - * Using the 3D version requires rendering with P3D (see the Environment + * Specifies vertex coordinates for quadratic Bezier curves. Each call + * to quadraticVertex() defines the position of one control + * point and one anchor point of a Bezier curve, adding a new segment + * to a line or shape. The first time quadraticVertex() is used + * within a beginShape() call, it must be prefaced with a call + * to vertex() to set the first anchor point. This function must + * be used between beginShape() and endShape() and only + * when there is no MODE parameter specified to beginShape(). + * Using the 3D version requires rendering with P3D (see the Environment * reference for more information). - * + * * @webref shape:vertex * @webBrief Specifies vertex coordinates for quadratic Bezier curves. * @param cx the x-coordinate of the control point @@ -12021,7 +12033,7 @@ public class PApplet implements PConstants { * counter-clockwise around the defined shape. * * @webref shape:2d_primitives - * @webBrief A quad is a quadrilateral, a four sided polygon. + * @webBrief A quad is a quadrilateral, a four sided polygon. * @param x1 x-coordinate of the first corner * @param y1 y-coordinate of the first corner * @param x2 x-coordinate of the second corner @@ -12268,7 +12280,7 @@ public class PApplet implements PConstants { * function. * * @webref shape:2d_primitives - * @webBrief Draws a circle to the screen. + * @webBrief Draws a circle to the screen. * @param x x-coordinate of the ellipse * @param y y-coordinate of the ellipse * @param extent width and height of the ellipse by default @@ -12923,7 +12935,7 @@ public class PApplet implements PConstants { * the font. * * - * + * * @webref typography:attributes * @webBrief Sets the current alignment for drawing text. * @param alignX horizontal alignment, either LEFT, CENTER, or RIGHT @@ -13688,7 +13700,7 @@ public class PApplet implements PConstants { * * @webref transform * @webBrief Shears a shape around the x-axis the amount specified by the - * angle parameter. + * angle parameter. * @param angle angle of shear specified in radians * @see PGraphics#popMatrix() * @see PGraphics#pushMatrix() @@ -13967,7 +13979,7 @@ public class PApplet implements PConstants { * * * @webref lights_camera:camera - * @webBrief Sets the position of the camera. + * @webBrief Sets the position of the camera. * @see PGraphics#beginCamera() * @see PGraphics#endCamera() * @see PGraphics#frustum(float, float, float, float, float, float) @@ -14075,7 +14087,7 @@ public class PApplet implements PConstants { * * @webref lights_camera:camera * @webBrief Sets a perspective projection applying foreshortening, making distant - * objects appear smaller than closer ones. + * objects appear smaller than closer ones. */ public void perspective() { if (recorder != null) recorder.perspective(); @@ -14913,7 +14925,7 @@ public class PApplet implements PConstants { * * @webref lights_camera:lights * @webBrief Sets the default ambient light, directional light, falloff, and specular - * values. + * values. * @usage web_application * @see PGraphics#ambientLight(float, float, float, float, float, float) * @see PGraphics#directionalLight(float, float, float, float, float, float) @@ -15257,14 +15269,14 @@ public class PApplet implements PConstants { /** - * Clears the pixels within a buffer. This function only works on - * PGraphics objects created with the createGraphics() - * function. Unlike the main graphics context (the display window), - * pixels in additional graphics areas created with createGraphics() - * can be entirely or partially transparent. This function clears - * everything in a PGraphics object to make all of the pixels + * Clears the pixels within a buffer. This function only works on + * PGraphics objects created with the createGraphics() + * function. Unlike the main graphics context (the display window), + * pixels in additional graphics areas created with createGraphics() + * can be entirely or partially transparent. This function clears + * everything in a PGraphics object to make all of the pixels * 100% transparent. - * + * * @webref color:setting * @webBrief Clears the pixels within a buffer. */ @@ -15393,7 +15405,7 @@ public class PApplet implements PConstants { * example, the following two lines of code are equivalent means of getting the * red value of the color value c:
*
- * + * *
    * float r1 = red(c); // Simpler, but slower to calculate
    * float r2 = c >> 16 & 0xFF; // Very fast to calculate
@@ -15431,7 +15443,7 @@ public class PApplet implements PConstants {
    * example, the following two lines of code are equivalent means of getting the
    * green value of the color value c:
*
- * + * *
    * float g1 = green(c); // Simpler, but slower to calculate
    * float g2 = c >> 8 & 0xFF; // Very fast to calculate
@@ -15469,7 +15481,7 @@ public class PApplet implements PConstants {
    * the following two lines of code are equivalent means of getting the blue
    * value of the color value c:
*
- * + * *
    * float b1 = blue(c); // Simpler, but slower to calculate
    * float b2 = c & 0xFF; // Very fast to calculate
diff --git a/core/todo.txt b/core/todo.txt
index 07c7fa2a3..2779b127f 100644
--- a/core/todo.txt
+++ b/core/todo.txt
@@ -1,4 +1,14 @@
 1272 (4.0a3)
+X loadJSONObject/Array() now return null if the given file was not found
+X   https://github.com/processing/processing/pull/6081
+
+
+fixed in 4.x (close/lock these with final 4.0 release)
+X Export Application broken in Processing 3.5.4 when using P2D or P3D renderers
+X   may be a JOGL bug, fixed by the 2.4 RC (therefore fixed in 4.x already?)
+_   https://github.com/processing/processing/issues/5983
+X Cannot run rotateZ() within the PShape class
+_   https://github.com/processing/processing/issues/5770
 
 
 _ JDK 11.0.7+ workarounds/hacks in place, fix them
@@ -15,9 +25,6 @@ _   https://github.com/processing/processing4/pull/98
 
 
 noted by Casey
-_ Export Application broken in Processing 3.5.4 when using P2D or P3D renderers
-_   may be a JOGL bug, fixed by the 2.4 RC (therefore fixed in 4.x already?)
-_   https://github.com/processing/processing/issues/5983
 _ Sketches on Windows/Linux don't take UI sizing into account
 _   https://github.com/processing/processing/issues/4894
 _   https://github.com/processing/processing/issues/4895