Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.impl / src / main / java / org / gvsig / tools / dynform / main / DynObjectProxy.java @ 1405

History | View | Annotate | Download (7.93 KB)

1
package org.gvsig.tools.dynform.main;
2

    
3
import java.lang.reflect.Field;
4
import java.lang.reflect.InvocationTargetException;
5
import java.lang.reflect.Method;
6
import java.util.HashMap;
7
import java.util.Map;
8
import java.util.logging.Level;
9
import java.util.logging.Logger;
10
import org.gvsig.tools.ToolsLocator;
11
import org.gvsig.tools.dataTypes.CoercionException;
12
import org.gvsig.tools.dataTypes.DataType;
13
import org.gvsig.tools.dataTypes.DataTypesManager;
14
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
15
import org.gvsig.tools.dynobject.DynClass;
16
import org.gvsig.tools.dynobject.DynObject;
17
import org.gvsig.tools.dynobject.DynObjectManager;
18
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
19
import org.gvsig.tools.dynobject.exception.DynMethodException;
20
import org.gvsig.tools.dynobject.impl.DefaultDynObject;
21

    
22
public class DynObjectProxy implements DynObject {
23

    
24
    private DynClass dynClass;
25
    private Object obj;
26
    private Class<? extends Object> theClass;
27
    private Map fields = null;
28
    private Map getters = null;
29
    private Map setters = null;
30
    private Map coercions = null;
31

    
32
    public DynObjectProxy(DynClass dynClass, Object obj) {
33
        this.dynClass = dynClass;
34
        this.theClass = obj.getClass();
35
        this.obj = obj;
36
        this.fields = new HashMap();
37
        this.getters = new HashMap();
38
        this.setters = new HashMap();
39
        this.coercions = new HashMap();
40
    }
41

    
42
    public DynClass getDynClass() {
43
        return this.dynClass;
44
    }
45

    
46
    private DynObjectManager getManager() {
47
        return ToolsLocator.getDynObjectManager();
48
    }
49
    
50
    private DataTypesManager getDataTypesManager() {
51
        return ToolsLocator.getDataTypesManager();
52
    }
53
    
54
    private Object coerce(Object value, Class type) {
55
        
56
        Coercion coercion = (Coercion) this.coercions.get(type);
57
        if( coercion == null ) {
58
            DataType dataType = getDataTypesManager().getDataType(type);
59
            if( dataType != null ) {
60
                coercion = dataType.getCoercion();
61
                this.coercions.put(type, coercion);
62
            }
63
        }
64
        if( coercion != null ) {
65
            try {
66
                value = coercion.coerce(value);
67
            } catch (CoercionException ex) {
68
                // Do nothing
69
            }
70
        }
71
        return value;
72
    }
73
    
74
    private Field getField(String name) {
75
        Field field = (Field) this.fields.get(name);
76
        if ( field == null ) {
77
            Field[] fields = this.theClass.getFields();
78
            for ( int i = 0; i < fields.length; i++ ) {
79
                if ( fields[i].getName().equals(name) ) {
80
                    field = fields[i];
81
                    this.fields.put(name, field);
82
                    break;
83
                }
84
            }
85
        }
86
        return field;
87
    }
88

    
89
    private Method getGetter(String name) {
90
        Method method = (Method) this.getters.get(name);
91
        if ( method == null ) {
92
            String gettername = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
93
            Method[] methods = this.theClass.getMethods();
94
            for ( int i = 0; i < methods.length; i++ ) {
95
                if ( methods[i].getName().equals(gettername) ) {
96
                    method = methods[i];
97
                    this.getters.put(name, method);
98
                    break;
99
                }
100
            }
101
        }
102
        return method;
103
    }
104

    
105
    private Method getSetter(String name) {
106
        Method method = (Method) this.setters.get(name);
107
        if ( method == null ) {
108
            String settername = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
109
            Method[] methods = this.theClass.getMethods();
110
            for ( int i = 0; i < methods.length; i++ ) {
111
                if ( methods[i].getName().equals(settername) ) {
112
                    method = methods[i];
113
                    this.setters.put(name, method);
114
                    break;
115
                }
116
            }
117
        }
118
        return method;
119
    }
120

    
121
    public void implement(DynClass dynClass) {
122
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
123
    }
124

    
125
    public void delegate(DynObject dynObject) {
126
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
127
    }
128

    
129
    public Object getDynValue(String name) throws DynFieldNotFoundException {
130
        Object r = null;
131
        Field field = this.getField(name);
132
        if ( field != null ) {
133
            try {
134
                r = field.get(this.obj);
135
            } catch (IllegalArgumentException ex) {
136
                throw new RuntimeException("Can't get value to field '"+name+"'.",ex);
137
            } catch (IllegalAccessException ex) {
138
                throw new RuntimeException("Can't get value to field '"+name+"'.",ex);
139
            }
140
        }
141
        if ( r == null ) {
142
            Method getter = this.getGetter(name);
143
            if ( getter != null ) {
144
                try {
145
                    r = getter.invoke(this.obj);
146
                } catch (IllegalAccessException ex) {
147
                throw new RuntimeException("Can't get value to field '"+name+"'.",ex);
148
                } catch (IllegalArgumentException ex) {
149
                throw new RuntimeException("Can't get value to field '"+name+"'.",ex);
150
                } catch (InvocationTargetException ex) {
151
                    throw new RuntimeException();
152
                }
153
            }
154
        }
155
        if ( r != null ) {
156
            DynClass dynClass = getManager().get(r.getClass().getName());
157
            if( dynClass!=null ) {
158
                r = new DynObjectProxy(dynClass, r);
159
            }
160
        }
161
        return r;
162
    }
163

    
164
    public void setDynValue(String name, Object value) throws DynFieldNotFoundException {
165
        Field field = this.getField(name);
166
        if ( field != null ) {
167
            try {
168
                Class<?> type = field.getType();
169
                field.set(this.obj, coerce(value,type));
170
            } catch (IllegalArgumentException ex) {
171
                throw new RuntimeException("Can't set value to field '"+name+"'.",ex);
172
            } catch (IllegalAccessException ex) {
173
                throw new RuntimeException("Can't set value to field '"+name+"'.",ex);
174
            }
175
        }
176
        Method setter = this.getSetter(name);
177
        if ( setter != null ) {
178
            try {
179
                Class<?> type = setter.getParameterTypes()[0];
180
                setter.invoke(this.obj, coerce(value,type));
181
            } catch (IllegalAccessException ex) {
182
                throw new RuntimeException("Can't set value to field '"+name+"'.",ex);
183
            } catch (IllegalArgumentException ex) {
184
                throw new RuntimeException("Can't set value to field '"+name+"'.",ex);
185
            } catch (InvocationTargetException ex) {
186
                throw new RuntimeException("Can't set value to field '"+name+"'.",ex);
187
            }
188
        }
189
    }
190

    
191
    public boolean hasDynValue(String name) {
192
        Field field = this.getField(name);
193
        if ( field != null ) {
194
            return true;
195
        }
196
        Method setter = this.getSetter(name);
197
        if ( setter != null ) {
198
            return true;
199
        }
200
        return false;
201
    }
202

    
203
    public Object invokeDynMethod(String name, Object[] args) throws DynMethodException {
204
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
205
    }
206

    
207
    public Object invokeDynMethod(int code, Object[] args) throws DynMethodException {
208
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
209
    }
210

    
211
    public void clear() {
212
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
213
    }
214

    
215
    @Override
216
    public String toString() {
217
        return DefaultDynObject.toString(this);
218
    }
219
    
220
    
221
}