/** GPVariableNode.java * J Scott Cameron * GPNode that references a variable * in a client class using the Reflection API */ import GPNode; import java.util.*; import java.lang.reflect.*; import GPPair; public class GPVariableNode implements GPNode { private String symbol;/*string representation of varible*/ private Object target;/*Instance of the class containing variable*/ private String variable;/*String name of the variable*/ private Double value;/*value of the variable*/ private Field variableObject;/*Reflection object used to reference *the field (variable)*/ /* Default Constructor */ public GPVariableNode() { value = new Double( 0); } /* parameterized Constructor that takes an instance of the * class holding the variabel and GPPair representing the * variable.*/ public GPVariableNode(Object target,GPPair gpp) { this.target = target; this.variable = gpp.name; this.symbol = gpp.sign; /* try to get the field */ try { Class c = target.getClass(); variableObject =c.getField(variable); } catch(NoSuchFieldException e) { System.out.println(e.toString() + " "+ variable +" was not found" ); } value = new Double( 0); } /*copy constructor */ public GPVariableNode(GPVariableNode gpv) { target = gpv.target; variable = gpv.variable; symbol = gpv.symbol; value = gpv.value; /* try to get the field */ try { Class c = target.getClass(); variableObject =c.getField(variable); } catch(NoSuchFieldException e) { System.out.println(e.toString() + " "+ variable ); } } /* returns a new copy of the node*/ public GPNode copy() { GPVariableNode gpv = new GPVariableNode(); gpv.target= target; gpv.variable = variable; gpv.symbol = symbol; gpv.value = value; /* try to get the field */ try { Class c = target.getClass(); gpv.variableObject =c.getField(variable); } catch(NoSuchFieldException e) { System.out.println(e.toString() + " "+ variable ); } return gpv; } /* sets the function (variable) to the given Pair */ public void setFunction(GPPair gpp) { variable = gpp.name; symbol = gpp.sign; } /* returns the current value of the associated variable */ public Double value() throws NoSuchMethodException { /*try to get the value, else throw and exception */ try { value = (Double)variableObject.get(target) ; } catch(IllegalAccessException e) { System.out.println(e); } return value; } /* this is a leaf, return 0 */ public int childCount() { return 0; } /* has no children, this shouldn't be called */ public GPNode getChild(int i) { return null; } /* has no children, does nothing */ public void setChild(int i,GPNode gpn) { ; } /* returns this since it has no children */ public GPNode find(int i) { return this; } /* returns 1, no children */ public int count() { return 1; } /* returns 1, no children */ public int nodeNumber() { return 1; } /* has no descedants, does nothing*/ public void setDescendant(int number, GPNode gpn) {} /* returns the symbol that represents the variable */ public String toString() { return symbol; } /* returns VARIABLE */ public int getType() { return VARIABLE; } }