We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Reflection on inner classes in the PDE
Page Index Toggle Pages: 1
Reflection on inner classes in the PDE? (Read 566 times)
Reflection on inner classes in the PDE?
Apr 21st, 2010, 1:02pm
 
I've got a question about using createGraphics() on a subclass of PGraphics that's declared within a sketch in the PDE (which if I understand things correctly means that it's actually an inner class of the PApplet.)

Here's a tiny sketch that illustrates the issue:

Code:

void setup() {
size(200, 200);

Foo myFoo = new Foo();

String fooName = myFoo.getClass().getName();

println(fooName); // Outputs "sketch_apr21b$Foo"

Foo mySecondFoo = (Foo) createGraphics(200, 200, fooName); //Exception here
}

void draw() {
}

public class Foo extends PGraphics2D {
}

The last line in setup() throws an exception and gives the error message "sketch21b$Foo needs to be updated for the current release of Processing."

I can make this work fine in Eclipse simply by moving Foo into its own class and calling createGraphics() with "packagename.Foo" as the renderer argument.  But obviously that won't work in the PDE.  Is there a solution or a workaround for this?

Re: Reflection on inner classes in the PDE?
Reply #1 - Apr 21st, 2010, 6:08pm
 
Well, I've figured out the answer after doing some digging around.  Apparently it's a Java thing:  Inner classes have a hidden extra argument to their constructors which is the parent object of the new inner object.  This is all invisible normally, but apparently when you're using reflection it's something you have to watch out for.  The upshot is that to do what I want to do I need to write a mini-re-implementation of Processing's createGraphics() method to handle inner classes properly.  (Yikes!)

Anyway, if someone else is interested in learning a little about this stuff, here's the blog post that made it clear to me:

http://www.jroller.com/tomdz/entry/reflection_inner_classes

Page Index Toggle Pages: 1