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.
IndexProcessing DevelopmentLibraries,  Tool Development › reflection, or code as a data
Page Index Toggle Pages: 1
reflection, or code as a data (Read 1269 times)
reflection, or code as a data
Oct 16th, 2009, 1:39pm
 
Hello, I'm writing my ph.d and I'm using processingjs.
I have such task:
get xml, parse it and convert to valid processingjs script.
Please, see this xml:
url: w3.org/TR/SVG/images/animate/anim01.svg
Yes, it's "plain old SVG". But take into consideration this string:
Code:
 <animate attributeName="x" attributeType="XML"
            begin="0s" dur="9s" fill="freeze" from="300" to="0" />

As you can see the change of attribute "x" during the time is declared.
Is there any possibility to use some king of Java reflection and write common mehtod for changing attribute value?
For example:
Code:

class AnimatableObject{
 String id, name;
 float x, y;
 color fillColor = color(255,255,255);
 int stokeWidth = 0;
 color strokeColor = color(255,255,255);
 ArrayList transformations;

 AnimatableObject(String idStr, String idName, float xPos, float yPos){
   id = idStr;
   name = idName;
   x = xPos;
   y = yPos;
 }
}

class Rectangle extends AnimatableObject{
 float height, width;

 Rectangle(String idStr, String idName, float xPos, float yPos, float w, float h){
   super(idStr, idName, xPos, yPos);
   width = w;
   height = h;
 }

 Rectangle(String idStr, String idName, float xPos, float yPos, color fColor, int sWidth, color sColor, float w, float h){
   super(idStr, idName, xPos, yPos, fColor, sWidth, sColor);
   width = w;
   height = h;
 }
}

class AbstractTransformation{
 String fromValue;
 String toValue;
 int startFrame;
 int endFrame;
 
 void transform(){
   //abstract, does nothing, use implementators
 }
}

class AttributeTransformation extends AbstractTransformation{
 String attrName;
 String attrType;
 
 void transform(){
   //transform logic goes here
 }
}


In draw method I would like to take each transformation from each AnimatableObject and perform it.
I don't want to write transformation for x and y, I would like to have common method which just takes filed from AnimatableObject and changes its value.

Is it possible?
Re: reflection, or code as a data
Reply #1 - Oct 17th, 2009, 9:15am
 
First, why is this question in Libraries section? If it is about Processing.js, it should be in Processing Implementations section.

Second, I am confused. If you use Processing.js, you cannot use "Java reflection". PJS parses and transform Processing code to JavaScript code, but that's still JS that run the code. So you need JS "reflection" instead.

And it needs to understand how PJS converts Java classes to JS objects, so you can use JS introspection on them. Which should be simple, IIRC you can do obj['fieldName'] to access that field by its name.
Page Index Toggle Pages: 1