1 package dynaop; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.OutputStream ; 6 import java.io.PrintStream ; 7 import java.io.Serializable ; 8 import java.lang.reflect.Method ; 9 import java.util.Arrays ; 10 import java.util.HashSet ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 import java.util.Properties ; 14 import java.util.Set ; 15 16 import javax.xml.transform.Transformer ; 17 import javax.xml.transform.TransformerFactory ; 18 import javax.xml.transform.stream.StreamResult ; 19 import javax.xml.transform.stream.StreamSource ; 20 21 import dynaop.util.ArrayObject; 22 import dynaop.util.Classes; 23 24 35 public class Debug { 36 37 PrintStream out; 38 39 public Debug(PrintStream out) { 40 this.out = out; 41 }; 42 43 public Debug(OutputStream out) { 44 this.out = new PrintStream (out); 45 }; 46 47 50 public Debug() { 51 this.out = System.out; 52 }; 53 54 58 62 public static ProxyType getProxyType(Class clazz) { 63 return getProxyType(Aspects.getInstance(), clazz); 64 } 65 66 69 public static ProxyType getProxyType(Aspects aspects, 70 Class clazz) { 71 return immutable(aspects.createProxyType(clazz, 72 new ProxyTypeBuilder(clazz))); 73 } 74 75 static class ImmutableProxyType implements ProxyType, Serializable { 76 77 static long serialVersionUID = 0; 78 79 private ProxyType proxyType; 80 81 ImmutableProxyType(ProxyType proxyType) { 82 this.proxyType = proxyType; 83 } 84 85 public Class [] getInterfaces() { 86 return (Class []) ArrayObject.clone( 87 proxyType.getInterfaces()); 88 } 89 90 public InterceptorFactory[] getInterceptorFactories(Method method) { 91 InterceptorFactory[] interceptorFactories = 92 proxyType.getInterceptorFactories(method); 93 return (interceptorFactories == null) ? null : 94 (InterceptorFactory[]) ArrayObject.clone(interceptorFactories); 95 } 96 97 public String toString() { 98 return proxyType.toString(); 99 } 100 } 101 102 static ProxyType immutable(ProxyType proxyType) { 103 return new ImmutableProxyType(proxyType); 104 } 105 106 110 void printInterceptorFactory(InterceptorFactory factory) { 111 this.out.println("\t\t\t<interceptorFactory>"); 112 113 Properties p = factory.getProperties(); 114 for (Iterator i = p.entrySet().iterator(); i.hasNext();) { 115 Map.Entry entry = (Map.Entry ) i.next(); 116 this.out.println("\t\t\t\t<property>"); 117 this.out.println("\t\t\t\t\t<name>" + entry.getKey() + "</name>"); 118 this.out.println("\t\t\t\t\t<value>" + entry.getValue() + 119 "</value>"); 120 this.out.println("\t\t\t\t</property>"); 121 } 122 123 this.out.println("\t\t\t</interceptorFactory>"); 124 } 125 126 void printMethod(ProxyType type, Method method) { 127 this.out.println("\t\t<method>"); 128 129 this.out.println("\t\t\t<signature>" + 130 Pointcuts.methodToString(method) + "</signature>"); 131 132 InterceptorFactory[] interceptorFactories = 133 type.getInterceptorFactories(method); 134 if (interceptorFactories != null) 135 for (int i = 0; i < interceptorFactories.length; i++) 136 printInterceptorFactory(interceptorFactories[i]); 137 138 this.out.println("\t\t</method>"); 139 } 140 141 void printMethods(ProxyType type, Class clazz, Set visited) { 142 Method [] methods = clazz.getMethods(); 143 144 this.out.println("\t<interface class=\"" + clazz.getName() + "\">"); 145 146 for (int i = 0; i < methods.length; i++) { 147 if (visited.contains(methods[i])) 148 continue; 149 visited.add(methods[i]); 150 printMethod(type, methods[i]); 151 } 152 153 this.out.println("\t</interface>"); 154 } 155 156 159 public void printXml(Class clazz, ProxyType type) { 160 this.out.println("<proxyType>"); 161 162 this.out.println("\t<class>" + clazz.getName() + "</class>"); 163 164 if (type != null) { 165 Class [] interfaces = type.getInterfaces(); 166 167 this.out.println("\t<interface class=\"java.lang.Object\">"); 168 for (int i = 0; i < Classes.OBJECT_METHODS.length; i++) 169 printMethod(type, Classes.OBJECT_METHODS[i]); 170 this.out.println("\t</interface>"); 171 172 Set visited = new HashSet (); 173 visited.addAll(Arrays.asList(Object .class.getMethods())); 174 175 printMethods(type, clazz, visited); 176 177 for (int i = 0; i < interfaces.length; i++) 178 printMethods(type, interfaces[i], visited); 179 } 180 181 this.out.println("</proxyType>"); 182 } 183 184 public static void main(String [] args) throws Exception { 185 if (args.length != 1) { 186 System.err.println("Usage: Debug [class name]"); 187 System.exit(0); 188 } 189 190 Class clazz = Class.forName(args[0]); 191 192 ByteArrayOutputStream out = new ByteArrayOutputStream (); 194 new Debug(out).printXml(clazz, getProxyType(clazz)); 195 196 Transformer t = TransformerFactory.newInstance().newTransformer( 198 new StreamSource (Debug.class.getResourceAsStream("debug.xsl"))); 199 t.transform( 200 new StreamSource (new ByteArrayInputStream (out.toByteArray())), 201 new StreamResult (System.out) 202 ); 203 } 204 } | Popular Tags |