| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.util; |
| 27 |
|
| 28 |
import java.lang.reflect.Method; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class Property implements Attribute { |
| 39 |
|
| 40 |
private Object object = null; |
| 41 |
private Method getter = null; |
| 42 |
private Object argument = null; |
| 43 |
private String name = null; |
| 44 |
|
| 45 |
public Property(Object object, String name) { |
| 46 |
this(object, name, null); |
| 47 |
} |
| 48 |
|
| 49 |
public Property(Object object, String name, Object argument) { |
| 50 |
|
| 51 |
this.name = name; |
| 52 |
this.argument = argument; |
| 53 |
|
| 54 |
this.object = object; |
| 55 |
|
| 56 |
StringBuffer getterName = new StringBuffer("get"); |
| 57 |
getterName.append(name.substring(0, 1).toUpperCase()); |
| 58 |
getterName.append(name.substring(1)); |
| 59 |
Class c = object.getClass(); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
try { |
| 64 |
if (argument != null) |
| 65 |
getter = c.getMethod(getterName.toString(), new Class[]{argument.getClass()}); |
| 66 |
else |
| 67 |
getter = c.getMethod(getterName.toString(), (Class[]) null); |
| 68 |
} catch (NoSuchMethodException e) { |
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public Method getGetter() { |
| 74 |
return getter; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public String getAttributeName() { |
| 80 |
if (argument == null) return name; |
| 81 |
return name + "." + argument; |
| 82 |
} |
| 83 |
|
| 84 |
public Object getAttributeValue() { |
| 85 |
if (object == null || getter == null) |
| 86 |
return null; |
| 87 |
|
| 88 |
Object result = null; |
| 89 |
Object[] args = null; |
| 90 |
if (argument != null) |
| 91 |
args = new Object[]{argument}; |
| 92 |
|
| 93 |
try { |
| 94 |
result = getter.invoke(object, args); |
| 95 |
} catch (Exception e) { |
| 96 |
|
| 97 |
e.printStackTrace(System.out); |
| 98 |
|
| 99 |
throw new RuntimeException(e.getMessage()); |
| 100 |
} |
| 101 |
|
| 102 |
return result; |
| 103 |
} |
| 104 |
|
| 105 |
public String getPropertyName() { |
| 106 |
return name; |
| 107 |
} |
| 108 |
|
| 109 |
public String toString() { |
| 110 |
return getAttributeValue().toString(); |
| 111 |
} |
| 112 |
} |