01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.beans;
19:
20: import java.lang.reflect.Field;
21: import java.util.HashMap;
22:
23: class StaticFieldPersistenceDelegate extends PersistenceDelegate {
24:
25: public StaticFieldPersistenceDelegate() {
26: super ();
27: }
28:
29: static HashMap<Object, String> pairs = new HashMap<Object, String>();
30:
31: static void init(Class<?> clz) {
32: Field[] field = clz.getFields();
33: for (int i = 0; i < field.length; i++) {
34: Object value = null;
35: try {
36: value = field[i].get(clz);
37: } catch (Exception e) {
38: return;
39: }
40: if (value.getClass() == clz) {
41: pairs.put(value, field[i].getName());
42: }
43: }
44: }
45:
46: @Override
47: protected Expression instantiate(Object oldInstance, Encoder enc) {
48: Field field = null;
49: try {
50: field = oldInstance.getClass().getDeclaredField(
51: pairs.get(oldInstance));
52: } catch (Exception e) {
53: enc.getExceptionListener().exceptionThrown(e);
54: }
55: return new Expression(oldInstance, field, "get", //$NON-NLS-1$
56: new Object[] { oldInstance.getClass() });
57: }
58:
59: }
|