This code might do the trick. It also works with processing 3 and this code is able to breach encapsulation. (As long as there is no SecurityManager in place)
PAppletFinder.java
import processing.awt.*;
import processing.opengl.*;
import processing.core.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.lang.reflect.*;
import sun.misc.*;
/**This class finds the active PApplet (Processing program)*/
public class PAppletFinder {
/**Unsafe instance used*/
public static Unsafe unsafe=null;
/**Unsafe Field offset*/
public static long fieldIndex=0;
/**Target field of the thread*/
public static Field threadTargField=null;
/**Here the three Fields unsafe,fieldIndex, threadTargField are initalized*/
static {
try {
Field f2=Unsafe.class.getDeclaredField("theUnsafe");
f2.setAccessible(true);
unsafe=(Unsafe) f2.get(null);
System.out.println("Unsafe instance: "+unsafe.toString());
threadTargField=Thread.class.getDeclaredField("target");
fieldIndex=unsafe.objectFieldOffset(threadTargField);
}
catch(Exception e) {
e.printStackTrace();
}
}
/**All results will be saved here*/
public static java.util.List<PApplet> allFoundPapplets=new LinkedList<PApplet>();
/**This class takes the contents of the window and pieces together the PApplet
* @return PApplet of the current sketch.
* @param c contents of the Processing window (must be a SmoothCanvas)*/
public static void get(Component c) {
System.out.println("Processing Window content");
PSurfaceAWT.SmoothCanvas sc=(PSurfaceAWT.SmoothCanvas) c;
try {
PSurfaceAWT psa=(PSurfaceAWT) get(sc, "this$0", sc.getClass());
PApplet prg=(PApplet) get((PSurfaceNone)psa, "sketch", PSurfaceNone.class);
allFoundPapplets.add( prg);
}
catch(Exception e) {
System.err.println("Something went wrong");
e.printStackTrace();
}
}
public static PApplet foundPapplet=null;
/**The main method to be used when using the PAppletFinder*/
public static void findAll() {
findFromWindow();
fromThreads();
System.out.println("Detection done");
}
/**This looks out for windows and gives the contents of the right one to the get method
* @return PApplet of the current sketch.*/
public static void findFromWindow() {
System.out.println("Searching Windows for instances");
JFrame mainWindow=null;
java.awt.Window win[]=java.awt.Window.getWindows();
for (int i=0; i<win.length; i++) if (win[i] instanceof JFrame) {
mainWindow=(JFrame) win[i];
Component c=mainWindow.getContentPane().getComponents()[0];
if (c instanceof PSurfaceAWT.SmoothCanvas) get(c);
}
}
/**This is used to get the value of fields
* @return Object value of a field
* @param j the Object from which the value is taken
* @param target name of the field*/
public static Object get(Object j, String target, Class<?> ref) {
try {
Field f=ref.getDeclaredField(target);
f.setAccessible(true);
return f.get(j);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**When using P2D or P3D everything is built differently however there is a weakness to be found in PSurfaceJOGL as it dispatches a Thread using an annonymous inner class as Runnable*/
public static void fromThreads() {
System.out.println("Searching Threads for instances");
//This fetches all threads
Set<Thread> threadSet=Thread.getAllStackTraces().keySet();
//It iterates upon
for (Thread th : threadSet) {
iteration:
{
try {
//Field f=th.getClass().getDeclaredField("target");
//f.setAccessible(true);
Object currinstance=null;
currinstance=unsafe.getObject(th,fieldIndex);
if (!currinstance.getClass().toString().contains("PSurfaceJOGL$")) break iteration;
System.out.println("Weak spot found! "+th.getName());
currinstance=getEnclosingInstance(currinstance);
Field f=PSurfaceJOGL.class.getDeclaredField("sketch");
f.setAccessible(true);
allFoundPapplets.add((PApplet) (currinstance=f.get(currinstance)));
System.out.println("Detection successful");
}
catch(Exception e)
{
catchBlock:
{
if (e instanceof NoSuchFieldException||e instanceof NullPointerException) {
System.err.println("target not found "+th.getName());
break catchBlock;
}
System.err.println("Something went wrong!");
e.printStackTrace();
}
}
}
}
}
//This will try to get the enclosing instance of an object (this can also be a lambda)
public static Object getEnclosingInstance(Object o) {
//This code will work for Processing 3 (uses annonymous inner classes)
try {
Class<?> classused=o.getClass();
Field this0=classused.getDeclaredField("this$0");
this0.setAccessible(true);
return this0.get(o);
}
catch(Exception e) {
System.err.println("Can't find enclosing instance of Object trying to use Lambdas... (To be expected with Processing 4)");
}
//This code should work for Processing 4 (lambdas are used instead of inner classes, Lambdas use arg$1 instead this$0)
try {
Class<?> classused=o.getClass();
Field this0=classused.getDeclaredField("arg$1");
this0.setAccessible(true);
return this0.get(o);
}
catch(Exception e) {
System.err.println("Detection Failed!");
}
return null;
}
}