KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > NonPortableWalkVisitor


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

5 package com.tc.object;
6
7 import com.tc.aspectwerkz.reflect.impl.java.JavaClassInfo;
8 import com.tc.logging.TCLogger;
9 import com.tc.object.config.DSOClientConfigHelper;
10 import com.tc.object.config.TransparencyClassSpec;
11 import com.tc.object.walker.MemberValue;
12 import com.tc.object.walker.PrintVisitor;
13 import com.tc.object.walker.Visitor;
14 import com.tc.object.walker.WalkTest;
15 import com.tc.object.walker.PrintVisitor.OutputSink;
16 import com.tc.object.walker.PrintVisitor.ValueFormatter;
17
18 import java.lang.reflect.Field JavaDoc;
19
20 public class NonPortableWalkVisitor implements Visitor, ValueFormatter, WalkTest, OutputSink {
21
22   public static final String JavaDoc MARKER = "!!";
23   private static final String JavaDoc NON_PORTABLE = MARKER + " ";
24   private static final String JavaDoc PORTABLE = spaces(NON_PORTABLE.length());
25   private static final LiteralValues literals = new LiteralValues();
26
27   private final PrintVisitor delegate;
28   private final ClientObjectManager objMgr;
29   private final DSOClientConfigHelper config;
30   private final TCLogger logger;
31   private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
32
33   public NonPortableWalkVisitor(TCLogger logger, ClientObjectManager objMgr, DSOClientConfigHelper config, Object JavaDoc root) {
34     this.logger = logger;
35     this.objMgr = objMgr;
36     this.config = config;
37     delegate = new PrintVisitor(this, this, this);
38
39     logger.warn("Dumping object graph of non-portable instance of type " + root.getClass().getName()
40                 + ". Lines that start with " + NonPortableWalkVisitor.MARKER + " are non-portable types.");
41   }
42
43   public void output(String JavaDoc line) {
44     logger.warn(buffer.toString() + line);
45     buffer = new StringBuffer JavaDoc();
46   }
47
48   private static String JavaDoc spaces(int n) {
49     String JavaDoc s = "";
50     for (int i = 0; i < n; i++) {
51       s += " ";
52     }
53     return s;
54   }
55
56   public void visitMapEntry(int index, int depth) {
57     buffer.append(PORTABLE);
58     delegate.visitMapEntry(index, depth);
59   }
60
61   public void visitRootObject(MemberValue value) {
62     indicatePortability(value);
63     delegate.visitRootObject(value);
64   }
65
66   public void visitValue(MemberValue value, int depth) {
67     if (skipVisit(value.getSourceField())) { return; }
68     indicatePortability(value);
69     delegate.visitValue(value, depth);
70   }
71
72   public String JavaDoc format(Object JavaDoc value) {
73     if (value == null) { return "null"; }
74
75     int type = literals.valueFor(value);
76     switch (type) {
77       case LiteralValues.OBJECT: {
78         return "(" + value.getClass().getName() + ")";
79       }
80       case LiteralValues.JAVA_LANG_CLASSLOADER: {
81         return "Classloader (" + value.getClass().getName() + ")";
82       }
83       case LiteralValues.STRING: {
84         return "\"" + value + "\"";
85       }
86       default: {
87         return String.valueOf(value);
88       }
89     }
90   }
91
92   public String JavaDoc valueAdornment(MemberValue value) {
93     if (isTransient(value)) { return " (transient)"; }
94
95     Object JavaDoc o = value.getValueObject();
96     if (o != null && config.isNeverAdaptable(JavaClassInfo.getClassInfo(o.getClass()))) { return " (never portable)"; }
97
98     return null;
99   }
100
101   public boolean shouldTraverse(MemberValue val) {
102     if (literals.isLiteralInstance(val.getValueObject())) { return false; }
103     if (isTransient(val)) { return false; }
104
105     Object JavaDoc o = val.getValueObject();
106     if (o != null && config.isNeverAdaptable(JavaClassInfo.getClassInfo(o.getClass()))) { return false; }
107
108     return true;
109   }
110
111   private boolean isTransient(MemberValue val) {
112     Field JavaDoc f = val.getSourceField();
113     if (f == null) { return false; }
114
115     TransparencyClassSpec spec = config.getSpec(f.getDeclaringClass().getName());
116     if (spec != null) { return spec.isTransient(f.getModifiers(), f.getName()); }
117
118     return false;
119   }
120
121   private static boolean skipVisit(Field JavaDoc field) {
122     return (field != null) && field.getType().getName().startsWith("com.tc.");
123   }
124
125   private void indicatePortability(MemberValue value) {
126     if (objMgr.isPortableInstance(value.getValueObject())) {
127       buffer.append(PORTABLE);
128     } else {
129       buffer.append(NON_PORTABLE);
130     }
131   }
132
133 }
134
Popular Tags