package com.cycling74.max; import java.lang.reflect.*; import com.e1.pdj.PDJError; class Attribute { boolean readOnly = false; // the method setter and getter if specified Method setter; Method getter; // the field to update if no setter or getter is specified Field field; // attribute type char type; // the owner of the field or method Object obj; // tells if the arguments are a array boolean isArray; Attribute(Object obj, String name, String setter_name, String getter_name) { char typeCheck = 0; try { this.obj = obj; if ( getter == null ) { field = obj.getClass().getDeclaredField(name); field.setAccessible(true); type = mapType(field.getType()); if ( type == '-' ) { throw new PDJError("Field: " + name + " is a unsupported type"); } isArray = Character.isLowerCase(type); } else { getter = obj.getClass().getDeclaredMethod(getter_name, null); type = mapType(getter.getReturnType()); if ( type == '-' ) { throw new PDJError("Method: " + getter_name + " returns a unsupported type"); } isArray = Character.isLowerCase(type); } if ( setter == null ) { field = obj.getClass().getDeclaredField(name); field.setAccessible(true); typeCheck = mapType(field.getType()); } else { Method methods[] = obj.getClass().getMethods(); for(int i=0;i 1 ) throw new PDJError("Method: " + setter_name + " has too much parameters to be a setter"); typeCheck = mapType(c[0]); setter = methods[i]; break; } } if ( typeCheck == 0 ) { throw new NoSuchMethodException(setter_name); } } } catch ( Exception e ) { throw new PDJError(e); } if ( typeCheck != type ) { throw new PDJError("Object type for setter and getter is not the same"); } } private char mapType(Class clz) { if ( clz == Integer.TYPE ) return 'i'; if ( clz == int[].class ) return 'I'; if ( clz == Float.TYPE ) return 'f'; if ( clz == float[].class ) return 'F'; if ( clz == Double.TYPE ) return 'd'; if ( clz == double[].class ) return 'D'; if ( clz == Boolean.TYPE ) return 'z'; if ( clz == Boolean[].class ) return 'Z'; if ( clz == Byte.TYPE ) return 'b'; if ( clz == byte[].class ) return 'B'; if ( clz == Character.TYPE ) return 'c'; if ( clz == char[].class ) return 'C'; if ( clz == Short.TYPE ) return 's'; if ( clz == short[].class ) return 'S'; if ( clz == String.class ) return 'g'; if ( clz == String[].class ) return 'G'; if ( clz == Atom.class ) return 'a'; if ( clz == Atom[].class ) return 'A'; return '-'; } private void fieldSetter(Atom[] value) throws Exception { Object work; int i; switch ( type ) { case 'z' : field.setBoolean(obj, value[0].toBoolean()); break; case 'Z' : work = field.get(obj); for (i=0;i