01: /*
02: * Copyright 2006 JBoss Inc
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.drools.base;
18:
19: import java.lang.reflect.Field;
20:
21: import org.drools.RuntimeDroolsException;
22:
23: /**
24: * A helper class with utility methods
25: *
26: * @author etirelli
27: */
28: public class ShadowProxyHelper {
29:
30: public static void copyState(final ShadowProxy from,
31: final ShadowProxy to) {
32: final Field[] fields = from.getClass().getDeclaredFields();
33: for (int i = 0; i < fields.length; i++) {
34: if (fields[i].getName().endsWith(
35: ShadowProxyFactory.FIELD_SET_FLAG)) {
36: fields[i].setAccessible(true);
37: try {
38: if (fields[i].getBoolean(from)) {
39: final String fieldName = fields[i]
40: .getName()
41: .substring(
42: 0,
43: fields[i].getName().length()
44: - ShadowProxyFactory.FIELD_SET_FLAG
45: .length());
46: final Field flag = to.getClass()
47: .getDeclaredField(fields[i].getName());
48: final Field fieldFrom = from.getClass()
49: .getDeclaredField(fieldName);
50: final Field fieldTo = to.getClass()
51: .getDeclaredField(fieldName);
52: flag.setAccessible(true);
53: fieldFrom.setAccessible(true);
54: fieldTo.setAccessible(true);
55:
56: // we know it is set
57: flag.setBoolean(to, true);
58: // copy the value from "from" shadow proxy
59: fieldTo.set(to, fieldFrom.get(from));
60: }
61: } catch (final Exception e) {
62: throw new RuntimeDroolsException(
63: "Unable to copy state from one shadow proxy to another");
64: }
65:
66: }
67: }
68: }
69:
70: }
|