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:
22: class ClassPersistenceDelegate extends PersistenceDelegate {
23: @Override
24: protected Expression instantiate(Object oldInstance, Encoder out) {
25: Class<?> value = (Class) oldInstance;
26: Field fld = null;
27: final String TYPE = "TYPE"; //$NON-NLS-1$
28: Expression result;
29: /*
30: * Special treatment to String.class to prevent endless loop of
31: * instantiation.
32: */
33: if (value == String.class) {
34: return new Expression(value, "", "getClass", null); //$NON-NLS-1$//$NON-NLS-2$
35: }
36: try {
37: if (value.equals(Integer.TYPE)) {
38: fld = Integer.class.getField(TYPE);
39: } else if (value.equals(Short.TYPE)) {
40: fld = Short.class.getField(TYPE);
41: } else if (value.equals(Long.TYPE)) {
42: fld = Long.class.getField(TYPE);
43: } else if (value.equals(Float.TYPE)) {
44: fld = Float.class.getField(TYPE);
45: } else if (value.equals(Double.TYPE)) {
46: fld = Double.class.getField(TYPE);
47: } else if (value.equals(Byte.TYPE)) {
48: fld = Byte.class.getField(TYPE);
49: } else if (value.equals(Character.TYPE)) {
50: fld = Character.class.getField(TYPE);
51: } else if (value.equals(Boolean.TYPE)) {
52: fld = Boolean.class.getField(TYPE);
53: }
54: } catch (NoSuchFieldException e) {
55: // impossible situation for valid java.lang classes
56: // implementation with version >= 1.1
57: throw new AssertionError(e);
58: }
59: if (fld != null) {
60: // we have primitive type
61: result = new Expression(oldInstance, fld,
62: "get", new Object[] { null }); //$NON-NLS-1$
63: } else {
64: result = new Expression(oldInstance, String.class,
65: "forName", //$NON-NLS-1$
66: new Object[] { value.getName() });
67: }
68: return result;
69: }
70:
71: @Override
72: /*
73: * It's unnecessary to do anything for initialization, because two mutatable
74: * class objects are actually the same.
75: */
76: protected void initialize(Class<?> type, Object oldInstance,
77: Object newInstance, Encoder out) {
78: // do nothing
79: }
80:
81: @Override
82: protected boolean mutatesTo(Object oldInstance, Object newInstance) {
83: if (oldInstance instanceof Class
84: && newInstance instanceof Class) {
85: Class<?> c1 = (Class) oldInstance;
86: Class<?> c2 = (Class) newInstance;
87: if (c1.getName().equals(c2.getName())) {
88: return true;
89: }
90: return false;
91: }
92: return super.mutatesTo(oldInstance, newInstance);
93: }
94: }
|