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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* Copyright © 2003-2009 Israfil Consulting Services Corporation
* Copyright © 2003-2009 Christian Edward Gruber
* All Rights Reserved
*
* This software is licensed under the Berkeley Standard Distribution license,
* (BSD license), as defined below:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of Israfil Consulting Services nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* $Id: DynamicallyMutableObject.java 127 2006-11-09 18:04:43Z cgruber $
*/
package net.israfil.foundation.dynamic;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.israfil.foundation.core.Strings;
import net.israfil.foundation.core.Types;
/**
* An abstract class which implements the DynamicallyMutableObject
* infrastructure. It sets a field's value, by mutator method if such
* exists, or directly if none such exists.
*
* @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
*
*/
public abstract class DynamicallyMutableObject extends DynamicallyAccessibleObject implements DynamicallyMutable {
private static Logger logger = Logger.getLogger(DynamicallyMutableObject.class.getName());
public void setNull(String attributeName, Class<?> valueType) {
setNull(this,attributeName,valueType);
}
public void setNull(Object target, String attributeName, Class<?> valueType) {
if (valueType == null)
throw new IllegalArgumentException("Cannot give a valueType of null for setNull(String,Class) method.");
set(target,attributeName, null, valueType);
}
public void set(String attributeName,Object value) {
set(this,attributeName,value);
}
public static void set(Object target, String attributeName,Object value) {
if (value == null) set(target,attributeName, value, null);
else set(target,attributeName, value, value.getClass());
}
public void set(String attributeName, Object value, Class<?> valueType) {
set(this,attributeName,value,valueType);
}
public static void set(Object target, String attributeName, Object value,
Class<?> valueType) {
if (value == null) {
if (valueType != null && valueType.isPrimitive()) throw new IllegalArgumentException("Attempted to set null on a variable or accessor for attribute '"+attributeName+"' of primitive type: " + valueType);
String selector = getMutatorSelector(target,attributeName,valueType);
if (selector != null) {
Method m = DynamicUtil.getMethodForSelector(target,selector);
Class<?>[] parmTypes = m.getParameterTypes();
if (parmTypes[0].isPrimitive())
throw new IllegalArgumentException("Attempted to set null using an accessor for attribute '"+attributeName+"' of primitive type: " + parmTypes[0]);
DynamicUtil.performOn(target,selector,new Object[]{value});
} else {
Field f = DynamicUtil.getField(target,attributeName);
if (f == null || f.getType().isPrimitive())
throw new IllegalArgumentException("Attempted to set null but could not find a set"+Strings.camel(attributeName)+" method with a single non-primitive type.");
else
_setField(target,attributeName,valueType);
}
} else /* value != null */ {
if (valueType == null) valueType = value.getClass();
if (hasMutator(target,attributeName,valueType)) {
DynamicUtil.performOn(target,getMutatorSelector(target,attributeName,valueType),new Object[]{value});
return;
} else if(hasMutator(target,attributeName,DynamicUtil.getPrimitiveTypeEquivalent(valueType))) {
DynamicUtil.performOn(target,getMutatorSelector(target,attributeName,DynamicUtil.getPrimitiveTypeEquivalent(valueType)),new Object[]{value});
return;
} else {
_setField(target,attributeName,value);
}
}
}
protected void _setField(String attributeName,Object value) {
_setField(this,attributeName,value);
}
protected static void _setField(Object target, String attributeName,Object value) {
Field f = DynamicUtil.getField(target,attributeName);
try {
if (f == null) {
RuntimeException rte = new RuntimeException("Could not find field named " + attributeName,
new NoSuchFieldException(target + " has no property " + attributeName));
logger.log(Level.FINEST,"Failed to set field.",rte);
throw rte;
}
f.set(target,Types.convert(value,f.getType()));
} catch (IllegalAccessException e) {
logger.log(Level.FINEST,"Object attempted to dynamically access a inaccessible field.",e);
throw new RuntimeException(e);
}
}
protected static final String mutatePrefix = "set";
protected String getMutatorSelector(String attributeName, Class<?> type) {
return getMutatorSelector(this,attributeName,type);
}
protected static String getMutatorSelector(Object target,
String attributeName, Class<?> type) {
if (attributeName == null || attributeName.equals("")) return null;
String camelAttr = Strings.camel(attributeName);
String selector = null;
if (type == null) {
Method method = _tryToFindMutator(target,attributeName);
if (method == null) {
return null;
} else /* method != null */ {
if (method.getParameterTypes().length != 1) return null;
else return "set"+Strings.camel(attributeName)+":"+method.getParameterTypes()[0].getName();
}
} else /* type != null */ {
String tmp = mutatePrefix+camelAttr+":"+type.getName();
if (DynamicUtil.respondsTo(target,tmp)) selector = tmp;
for (Class<?> c : DynamicUtil.getAllParentTypes(type)) {
tmp = mutatePrefix + camelAttr + ":" + c.getName();
if (DynamicUtil.respondsTo(target, tmp))
selector = tmp;
}
}
return selector;
}
protected static Method _tryToFindMutator(Object target, String attributeName) {
Method method = null;
Method[] methods = target.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals("set"+Strings.camel(attributeName))) {
if (method == null) method = methods[i];
else throw new IllegalArgumentException("Attempted to find "+mutatePrefix+Strings.camel(attributeName) + " without a given type, but more than one such method exists. Should use set(String,Object,Class)");
}
}
return method;
}
protected static Method _tryToFindMutator(Object target,
String attributeName, Class<?> type) {
if (type == null) throw new IllegalArgumentException("Must set a type, or use _tryToFindMutator(Object,String).");
String methodName = mutatePrefix+Strings.camel(attributeName);
Method m = DynamicUtil.getMethodForSelector(target,methodName+":"+type.getName());
for (Class<?> c : DynamicUtil.getAllParentTypes(type)) {
m = DynamicUtil.getMethodForSelector(target,methodName+":"+c.getName());
}
return m;
}
public static Method getMutator(Object target, String attributeName,
Class<?> type) {
if (type == null) {
return _tryToFindMutator(target,attributeName);
} else {
return _tryToFindMutator(target,attributeName,type);
}
}
public boolean hasMutator(String attributeName, Class<?> type) {
return hasMutator(this,attributeName,type);
}
public static boolean hasMutator(Object target, String attributeName,
Class<?> type) {
return getMutatorSelector(target,attributeName,type) != null;
}
}
|