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.util.List;
21:
22: class UtilListPersistenceDelegate extends DefaultPersistenceDelegate {
23: @Override
24: @SuppressWarnings({"nls","boxing"})
25: protected void initialize(Class<?> type, Object oldInstance,
26: Object newInstance, Encoder enc) {
27:
28: List<?> list = (List) oldInstance;
29: int size = list.size();
30: for (int i = 0; i < size; i++) {
31: Expression getterExp = new Expression(oldInstance, "get",
32: new Object[] { i });
33: try {
34: // Calculate the old value of the property
35: Object oldVal = getterExp.getValue();
36: // Write the getter expression to the encoder
37: enc.writeExpression(getterExp);
38: // Get the target value that exists in the new environment
39: Object targetVal = enc.get(oldVal);
40: // Get the current property value in the new environment
41: Object newVal = null;
42: try {
43: newVal = new Expression(newInstance, "get",
44: new Object[] { i }).getValue();
45: } catch (IndexOutOfBoundsException ex) {
46: // The newInstance has no elements, so current property
47: // value remains null
48: }
49: /*
50: * Make the target value and current property value equivalent
51: * in the new environment
52: */
53: if (null == targetVal) {
54: if (null != newVal) {
55: // Set to null
56: Statement setterStm = new Statement(
57: oldInstance, "add",
58: new Object[] { null });
59: enc.writeStatement(setterStm);
60: }
61: } else {
62: PersistenceDelegate pd = enc
63: .getPersistenceDelegate(targetVal
64: .getClass());
65: if (!pd.mutatesTo(targetVal, newVal)) {
66: Statement setterStm = new Statement(
67: oldInstance, "add",
68: new Object[] { oldVal });
69: enc.writeStatement(setterStm);
70: }
71: }
72: } catch (Exception ex) {
73: enc.getExceptionListener().exceptionThrown(ex);
74: }
75: }
76: }
77: }
|