From a764e59e1d3a8e330f0d484fdb26b35ca3f0b2e4 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Sat, 22 Mar 2008 02:15:12 +0000 Subject: bringing pdj-0.8.3 into the main branch svn path=/trunk/externals/loaders/pdj/; revision=9621 --- src/java/com/cycling74/max/Attribute.java | 463 ++++++++++++++++++++++++++++++ 1 file changed, 463 insertions(+) create mode 100644 src/java/com/cycling74/max/Attribute.java (limited to 'src/java/com/cycling74/max/Attribute.java') diff --git a/src/java/com/cycling74/max/Attribute.java b/src/java/com/cycling74/max/Attribute.java new file mode 100644 index 0000000..2e5cccf --- /dev/null +++ b/src/java/com/cycling74/max/Attribute.java @@ -0,0 +1,463 @@ +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