KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > applicator > AccessibleObjectApplicator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.applicator;
6
7 import com.tc.exception.TCNotSupportedMethodException;
8 import com.tc.object.ClientObjectManager;
9 import com.tc.object.TCObject;
10 import com.tc.object.TraversedReferences;
11 import com.tc.object.dna.api.DNA;
12 import com.tc.object.dna.api.DNACursor;
13 import com.tc.object.dna.api.DNAWriter;
14 import com.tc.object.dna.api.PhysicalAction;
15 import com.tc.object.dna.impl.DNAEncoding;
16 import com.tc.object.tx.optimistic.OptimisticTransactionManager;
17 import com.tc.util.Assert;
18
19 import java.io.IOException JavaDoc;
20 import java.lang.reflect.AccessibleObject JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * TODO Sep 11, 2006: I, asi, am too lazy to write a single sentence describing what this class is for.
26  */

27 public class AccessibleObjectApplicator extends BaseApplicator {
28   private static final String JavaDoc DECLARING_CLASS_FIELD_NAME = "java.lang.reflect.AccessibleObject.declaringClass";
29   private static final String JavaDoc ACCESSIBLE_OBJECT_NAME_FILED_NAME = "java.lang.reflect.AccessibleObject.name";
30   private static final String JavaDoc OVERRIDE_FILED_NAME = "java.lang.reflect.AccessibleObject.override";
31   private static final String JavaDoc PARAMETER_TYPES = "java.lang.reflect.AccessibleObject.parameterTypes";
32   private static final String JavaDoc ACCESSIBLE_OBJECT_TYPE = "java.lang.reflect.AccessibleObject.type";
33   private static final String JavaDoc METHOD_CLASS_NAME = Method JavaDoc.class.getName();
34
35   public AccessibleObjectApplicator(DNAEncoding encoding) {
36     super(encoding);
37   }
38
39   public TraversedReferences getPortableObjects(Object JavaDoc pojo, TraversedReferences addTo) {
40     if (pojo instanceof Method JavaDoc) {
41       addTo.addAnonymousReference(((Method JavaDoc) pojo).getDeclaringClass());
42       addTo.addAnonymousReference(((Method JavaDoc) pojo).getName());
43       addTo.addAnonymousReference(((Method JavaDoc) pojo).getParameterTypes());
44     }
45     return addTo;
46   }
47
48   public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object JavaDoc po) throws IOException JavaDoc,
49       IllegalArgumentException JavaDoc, ClassNotFoundException JavaDoc {
50     DNACursor cursor = dna.getCursor();
51     Assert.eval(cursor.getActionCount() <= 1);
52
53     if (po instanceof Method JavaDoc) {
54       if (cursor.next(encoding)) {
55         PhysicalAction a = (PhysicalAction) cursor.getAction();
56         Boolean JavaDoc value = (Boolean JavaDoc) a.getObject();
57         ((Method JavaDoc)po).setAccessible(value.booleanValue());
58       }
59     }
60   }
61
62   public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object JavaDoc pojo) {
63     Class JavaDoc declaringClass = null;
64     String JavaDoc name = null;
65     boolean override = false;
66     AccessibleObject JavaDoc ao = (AccessibleObject JavaDoc) pojo;
67     if (ao instanceof Method JavaDoc) {
68       declaringClass = ((Method JavaDoc) ao).getDeclaringClass();
69       name = ((Method JavaDoc) ao).getName();
70       override = ((Method JavaDoc) ao).isAccessible();
71
72       Class JavaDoc[] parameterTypes = ((Method JavaDoc) ao).getParameterTypes();
73
74       writer.addPhysicalAction(ACCESSIBLE_OBJECT_TYPE, pojo.getClass().getName());
75       writer.addPhysicalAction(DECLARING_CLASS_FIELD_NAME, declaringClass);
76       writer.addPhysicalAction(ACCESSIBLE_OBJECT_NAME_FILED_NAME, name);
77       writer.addPhysicalAction(OVERRIDE_FILED_NAME, new Boolean JavaDoc(override));
78       writer.addPhysicalAction(PARAMETER_TYPES, parameterTypes);
79     }
80   }
81
82   public Object JavaDoc getNewInstance(ClientObjectManager objectManager, DNA dna) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
83     DNACursor cursor = dna.getCursor();
84     Assert.assertEquals(5, cursor.getActionCount());
85
86     cursor.next(encoding);
87     PhysicalAction a = cursor.getPhysicalAction();
88     String JavaDoc objectType = (String JavaDoc) a.getObject();
89
90     cursor.next(encoding);
91     a = cursor.getPhysicalAction();
92     Class JavaDoc declaringClass = (Class JavaDoc) a.getObject();
93
94     cursor.next(encoding);
95     a = cursor.getPhysicalAction();
96     String JavaDoc name = (String JavaDoc) a.getObject();
97
98     cursor.next(encoding);
99     a = cursor.getPhysicalAction();
100     Boolean JavaDoc override = (Boolean JavaDoc) a.getObject();
101
102     cursor.next(encoding);
103     a = cursor.getPhysicalAction();
104     Object JavaDoc[] values = (Object JavaDoc[]) a.getObject();
105     Class JavaDoc[] parameterTypes = new Class JavaDoc[values.length];
106     System.arraycopy(values, 0, parameterTypes, 0, values.length);
107
108     if (METHOD_CLASS_NAME.equals(objectType)) {
109       try {
110         Method JavaDoc m = declaringClass.getDeclaredMethod(name, parameterTypes);
111         m.setAccessible(override.booleanValue());
112         return m;
113       } catch (NoSuchMethodException JavaDoc e) {
114         throw new AssertionError JavaDoc(e);
115       }
116     }
117     throw new AssertionError JavaDoc("Object type not known.");
118   }
119
120   public Map JavaDoc connectedCopy(Object JavaDoc source, Object JavaDoc dest, Map JavaDoc visited, ClientObjectManager objectManager,
121                            OptimisticTransactionManager txManager) {
122     throw new TCNotSupportedMethodException();
123   }
124 }
125
Popular Tags