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.Array;
21:
22: class ArrayPersistenceDelegate extends PersistenceDelegate {
23:
24: private static PersistenceDelegate pd = null;
25:
26: public static PersistenceDelegate getInstance() {
27: if (pd == null) {
28: pd = new ArrayPersistenceDelegate();
29: }
30: return pd;
31: }
32:
33: @Override
34: protected Expression instantiate(Object oldInstance, Encoder out) {
35: assert oldInstance != null && oldInstance.getClass().isArray() : oldInstance;
36:
37: int length = Array.getLength(oldInstance);
38: Class<?> componentType = oldInstance.getClass()
39: .getComponentType();
40:
41: return new Expression(oldInstance, Array.class, "newInstance", //$NON-NLS-1$
42: new Object[] { componentType, new Integer(length) });
43: }
44:
45: @Override
46: protected void initialize(Class<?> type, Object oldInstance,
47: Object newInstance, Encoder out) {
48:
49: assert oldInstance != null && oldInstance.getClass().isArray() : oldInstance;
50: assert newInstance != null && newInstance.getClass().isArray() : newInstance;
51:
52: int length = Array.getLength(oldInstance);
53: Class<?> componentType = type.getComponentType();
54: Object nullValue = Array.get(Array
55: .newInstance(componentType, 1), 0);
56:
57: for (int i = 0; i < length; ++i) {
58:
59: Object oldValue = Array.get(oldInstance, i);
60: Object newValue = Array.get(newInstance, i);
61:
62: if (oldValue != null && !oldValue.equals(newValue)
63: || oldValue == null && newValue != null) {
64: if (nullValue == null || !nullValue.equals(oldValue)) {
65: Statement s = new Statement(oldInstance, "set", //$NON-NLS-1$
66: new Object[] { new Integer(i), oldValue });
67:
68: out.writeStatement(s);
69: }
70: }
71: }
72: }
73:
74: @Override
75: protected boolean mutatesTo(Object oldInstance, Object newInstance) {
76: assert oldInstance != null && oldInstance.getClass().isArray() : oldInstance;
77:
78: if (newInstance != null) {
79: Class<? extends Object> newCl = newInstance.getClass();
80:
81: if (!newCl.isArray()) {
82: return false;
83: }
84: // both are arrays
85: int l1 = Array.getLength(oldInstance);
86: int l2 = Array.getLength(newInstance);
87: Class<?> cType1 = oldInstance.getClass().getComponentType();
88: Class<?> cType2 = newCl.getComponentType();
89:
90: if (l1 == l2 && cType1.equals(cType2)) {
91: return true;
92: }
93: return false;
94: }
95: return false;
96: }
97: }
|