11package rprocessing ;
22
33import java .awt .Component ;
4+ import java .awt .Frame ;
45import java .awt .Window ;
56import java .awt .event .ComponentAdapter ;
67import java .awt .event .ComponentEvent ;
78import java .lang .Thread .UncaughtExceptionHandler ;
9+ import java .lang .reflect .Field ;
810import java .lang .reflect .Method ;
911import java .util .ArrayList ;
1012import java .util .Arrays ;
3941
4042/**
4143 * RlangPApplet PApplet for R language, powered by Renjin.
42- *
44+ *
4345 * @author github.com/gaocegege
4446 */
4547public class RLangPApplet extends BuiltinApplet {
@@ -62,14 +64,16 @@ public class RLangPApplet extends BuiltinApplet {
6264
6365 private final CountDownLatch finishedLatch = new CountDownLatch (1 );
6466
67+ private Field frameField ;
68+
6569 private RSketchError terminalException = null ;
6670
6771 private boolean hasSize = false ;
6872 private SEXP sizeFunction = null ;
6973
7074 /**
7175 * Mode for Processing.
72- *
76+ *
7377 * @author github.com/gaocegege
7478 */
7579 private enum Mode {
@@ -107,7 +111,7 @@ public void prePassCode() {
107111 if (isSameClass (source , ExpressionVector .class )) {
108112 ExpressionVector ev = (ExpressionVector ) source ;
109113 // Stores the expressions except size().
110- List <SEXP > sexps = new ArrayList <SEXP >();
114+ List <SEXP > sexps = new ArrayList <>();
111115 for (int i = ev .length () - 1 ; i >= 0 ; --i ) {
112116 if (isSameClass (ev .get (i ), FunctionCall .class )
113117 && isSameClass (((FunctionCall ) ev .get (i )).getFunction (), Symbol .class )) {
@@ -179,7 +183,16 @@ public void runBlock(final String[] arguments) throws RSketchError {
179183 // exits or we explicitly tell it to minimize.
180184 // (If it's disposed, it'll leave a gray blank window behind it.)
181185 log ("Disabling fullscreen." );
182- macosxFullScreenToggle (frame );
186+ if (frameField != null ) {
187+ try {
188+ Frame frame = (Frame ) frameField .get (this );
189+ // This is probably a holdover from Processing 2.x
190+ // and likely shouldn't be used anymore. [fry 210703]
191+ macosxFullScreenToggle (frame );
192+ } catch (Exception e ) {
193+ // safe enough to ignore; this was a workaround
194+ }
195+ }
183196 }
184197 if (surface instanceof PSurfaceFX ) {
185198 // Sadly, JavaFX is an abomination, and there's no way to run an FX sketch more than once,
@@ -215,14 +228,32 @@ private static void macosxFullScreenToggle(final Window window) {
215228 }
216229 }
217230
231+ // method to find the frame field, rather than relying on an Exception
232+ private Field getFrameField () {
233+ for (Field field : getClass ().getFields ()) {
234+ if (field .getName ().equals ("frame" )) {
235+ return field ;
236+ }
237+ }
238+ return null ;
239+ }
240+
218241 /**
219- *
242+ *
220243 * @see processing.core.PApplet#initSurface()
221244 */
222245 @ Override
223246 protected PSurface initSurface () {
224247 final PSurface s = super .initSurface ();
225- this .frame = null ; // eliminate a memory leak from 2.x compat hack
248+ frameField = getFrameField ();
249+ if (frameField != null ) {
250+ try {
251+ // eliminate a memory leak from 2.x compat hack
252+ frameField .set (this , null );
253+ } catch (Exception e ) {
254+ // safe enough to ignore; this was a workaround
255+ }
256+ }
226257 // s.setTitle(pySketchPath.getFileName().toString().replaceAll("\\..*$", ""));
227258 if (s instanceof PSurfaceAWT ) {
228259 final PSurfaceAWT surf = (PSurfaceAWT ) s ;
@@ -293,7 +324,7 @@ public void settings() {
293324
294325 /**
295326 * Evaluate the program code.
296- *
327+ *
297328 * @see processing.core.PApplet#setup()
298329 */
299330 @ Override
@@ -334,7 +365,7 @@ public void handleDraw() {
334365
335366 /**
336367 * Call the draw function in R script.
337- *
368+ *
338369 * @see processing.core.PApplet#draw()
339370 */
340371 @ Override
@@ -348,7 +379,7 @@ public void draw() {
348379
349380 /**
350381 * Detect whether the program is in active mode.
351- *
382+ *
352383 * @return
353384 */
354385 @ SuppressWarnings ("rawtypes" )
@@ -361,7 +392,7 @@ private boolean isActiveMode() {
361392
362393 /**
363394 * Detect whether the program is in mix mode. After: isActiveMode()
364- *
395+ *
365396 * @return
366397 */
367398 private boolean isMixMode () {
@@ -495,7 +526,7 @@ protected void wrapKeyVariables() {
495526
496527 /**
497528 * Return whether the object has same class with clazz.
498- *
529+ *
499530 * @param obj
500531 * @param clazz
501532 * @return
0 commit comments