01: /*
02: * Created on Nov 18, 2005
03: */
04: package uk.org.ponder.reflect;
05:
06: import uk.org.ponder.saxalizer.AccessMethod;
07: import uk.org.ponder.saxalizer.MethodAnalyser;
08: import uk.org.ponder.saxalizer.SAXalizerMappingContext;
09:
10: public class BeanCloner {
11: // Cost of m-invoking bean factory:
12: // 1 reflect construct for factory
13: // 1 list construct, Spring filling overhead for each argument
14: // 1 object array construct, conversion
15: // 1 reflect invocation for factory method
16:
17: // alternatively, cost of clone:
18: // 1 reflective construct for object
19: // 1 reflective invoke for each "spare" property
20: // code clients need to use "set" syntax rather than function calls
21: // slight oddness of programming model - bean must be truly stateless sans deps.
22: // Probably these last two are the killer...
23: private SAXalizerMappingContext mappingcontext;
24: private ReflectiveCache reflectivecache;
25:
26: public void setMappingContext(SAXalizerMappingContext mappingcontext) {
27: this .mappingcontext = mappingcontext;
28: }
29:
30: public void setReflectiveCache(ReflectiveCache reflectivecache) {
31: this .reflectivecache = reflectivecache;
32: }
33:
34: public Object cloneBean(Object bean) {
35: return null;
36: }
37:
38: // A shallow clone, "with respect to" a given class, i.e. cloning all
39: // properties which are NOT PRESENT in the supplied class!
40: // We would really like to FastClass this stuff, but a) it is nasty to write,
41: // and b) clone-based RSAC operations seem to have a tendency of smelling
42: // a little bad after a bit of experience.
43: public Object cloneWithRespect(Object bean, Class respectful) {
44: Class beanclz = bean.getClass();
45: MethodAnalyser targetma = mappingcontext.getAnalyser(beanclz);
46: MethodAnalyser respectma = mappingcontext
47: .getAnalyser(respectful);
48:
49: Object togo = reflectivecache.construct(beanclz);
50:
51: for (int i = 0; i < targetma.allgetters.length; ++i) {
52: AccessMethod pot = targetma.allgetters[i];
53: if (!pot.canGet() || !pot.canSet())
54: continue;
55: String propname = pot.getPropertyName();
56: if (respectma.getAccessMethod(propname) != null) {
57: Object getit = pot.getChildObject(bean);
58: pot.setChildObject(togo, getit);
59: }
60: }
61: return togo;
62: }
63: }
|