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 & HelpIntegration › Reflective Programming and finding class [SOLVED]
Page Index Toggle Pages: 1
Reflective Programming and finding class [SOLVED] (Read 901 times)
Reflective Programming and finding class [SOLVED]
Mar 17th, 2008, 7:25pm
 
Hi, I'm trying to reflectively access processing's functions through the Java.lang.reflect library.

So far I've got managed to make it so I can call code from one sketch window and have it run in the other through the reflect interface, which is great as a proof of concept, however i've run into a snag.

The .getMethod("methodName",Object[] paramTypes) function requires that primitive classes such as int and float are passed in the paramTypes object array as Float.TYPE etc.

I want to be able to make the function that runs getMethod to read the parameter types as it receives them, so I don't have to send two lots of parameters through. I had hoped that this might be a case of using the getClass function, but as you have to pass the parameters as an object[], that returns a float object, instead of a float primitive, so throws an error.

Ideally, I want to be able to pass a parameter Object array and the code work it out its self.

Does anyone know if this is possible, or if i'm living in blind hope?


Cheers

Martin
Re: Reflective Programming and getting the right c
Reply #1 - Mar 17th, 2008, 7:25pm
 
Broken, messy code wouldn't fit in the same post...

Quote:


import processing.opengl.*;
import java.lang.reflect.*;
import java.util.ArrayList;
import processing.core.*;

ExampleFrame look;
void setup(){
 size(200,200,OPENGL);
 look=new ExampleFrame();
 look.setVisible(true);
}
void draw(){
 
 line(200,0,0,200);
 Object[] par=new Object[2];
     par[0]=Integer.TYPE;
     par[1]=Integer.TYPE;
     
 //look.up();
 //println(look.embed.draw());
 println(frameRate);
}

public void drawLine(){
      line(50,100,30,400);
    }

    //can call the update method on any passed object
public void callUpdate(String com,Object[] args){
 
 //we need to build the class[] array for the getMethod
 Class[] cls = new Class[0];
 Class myC;
 for(int t=0;t<args.length;t++){
   Class tempC = args[t].getClass();
   Field[] typeC = tempC.getFields();
   if(typeC.length>0){
     Field myType=typeC[typeC.length-1];
     myC = myType.getType();
     println(myType.getName());
     cls = (Class[]) append(cls,tempC);
     println(myType);
   }
   
   
   
 }
 
 //Class[] cls = {Float.TYPE,Float.TYPE,Float.TYPE,Float.TYPE};
 
 Class go = this.getClass();
 Method me;
 try{
   me = go.getMethod(com,cls);
   me.invoke(this,args);
 }catch(Exception e){
   e.printStackTrace();
 }
}


public class ExampleFrame extends Frame {
   PApplet embed;
    public ExampleFrame() {
        super("Embedded PApplet");
        setSize(400,400);
        setLayout(new BorderLayout());
        embed = new Embedded();
        add(embed, BorderLayout.CENTER);
        // important to call this whenever embedding a PApplet.
        // It ensures that the animation thread is started and
        // that other internal variables are properly set.
        embed.init();
    }
}

public class Embedded extends PApplet{

    public void setup() {
        // original setup code here ...
        size(400, 400,OPENGL);
        frameRate(30);
        // prevent thread from starving everything else
        //noLoop();
    }

    public void draw() {
        // drawing code goes here
        float x= 10.0;
        float y=100.0;
        float x1=50.0;
        float y1=400.0;
        Object[] pars = {new Float(10.0),new Float(100.0),new Float(50.0),new Float(400.0)};
        callUpdate("line",pars);
        line(0,0,400,400);
        println("Embed:"+frameRate);
    }
   
    /*

*/
    public void mousePressed() {
        // do something based on mouse movement

        // update the screen (run draw once)
        redraw();
    }
}


Re: Reflective Programming and finding class [SOLV
Reply #2 - Mar 18th, 2008, 11:58am
 
Hi, worked it out, I found a function here;

http://www.mortbay.org/xref/org/mortbay/util/TypeUtil.html

and took out the bit i needed, so now I can call functions in one applet window from another.

I'll post an example in the Exhibition board in case other people are interested in using it.
Page Index Toggle Pages: 1