1 17 18 package org.objectweb.jac.wrappers; 19 20 21 import java.util.Arrays ; 22 import java.util.Date ; 23 import org.aopalliance.intercept.ConstructorInvocation; 24 import org.aopalliance.intercept.MethodInvocation; 25 import org.objectweb.jac.aspects.gui.GuiAC; 26 import org.objectweb.jac.core.AspectComponent; 27 import org.objectweb.jac.core.Collaboration; 28 import org.objectweb.jac.core.Interaction; 29 import org.objectweb.jac.core.Jac; 30 import org.objectweb.jac.core.NameRepository; 31 import org.objectweb.jac.core.Wrapper; 32 import org.objectweb.jac.core.Wrapping; 33 import org.objectweb.jac.core.WrappingChain; 34 import org.objectweb.jac.core.rtti.MethodItem; 35 import org.objectweb.jac.util.Log; 36 37 40 41 public class VerboseWrapper extends Wrapper { 42 43 public static final int TIMED = 0; 44 public static final int NAMED = 1; 45 public static final int STACK = 2; 46 public static final int WRAPPERS= 3; 47 48 protected int type = TIMED; 49 50 static final String DISABLE_TRACE = "DISABLE_TRACE"; 51 52 public VerboseWrapper(AspectComponent ac, int type) { 53 super(ac); 54 this.type = type; 55 } 56 57 61 62 public Object printCallingInfos(Interaction interaction) { 63 64 65 Date d1 = new Date (); 66 String on = 67 interaction.method.isStatic() 68 ? "" 69 : (" on '" 70 + NameRepository.get().getName(interaction.wrappee) 71 + "'"); 72 73 System.out.println( 74 "<<< " 75 + (d1.getTime() - Jac.getStartTime().getTime()) 76 + " ms: calling '" + interaction.method + "'" 77 + on 78 + ", args = " 79 + Arrays.asList(interaction.args) 80 + " >>>"); 81 82 83 Object ret = interaction.proceed(); 84 85 86 Date d2 = new Date (); 87 System.out.println( 88 "<<< " 89 + (d2.getTime() - Jac.getStartTime().getTime()) 90 + " ms: returning '" 91 + interaction.method 92 + "'" 93 + on 94 + ", value = " 95 + ret 96 + ", duration = " 97 + (d2.getTime() - d1.getTime()) 98 + " ms >>>"); 99 100 101 return ret; 102 } 103 104 public Object printNamedArgs(Interaction interaction) { 105 Collaboration collab = Collaboration.get(); 106 if (collab.getAttribute(DISABLE_TRACE)!=null) 107 return proceed(interaction); 108 collab.addAttribute(DISABLE_TRACE, Boolean.TRUE); 110 try { 111 String [] paramNames = GuiAC.getParameterNames(interaction.method); 112 String params = 113 interaction.method.isStatic() 114 ? "" 115 : "this=" + GuiAC.toString(interaction.wrappee); 116 Object [] args = interaction.args; 117 for (int i = 0; i < args.length; i++) { 118 if (paramNames != null) { 119 params += "," + paramNames[i] + "=" + GuiAC.toString(interaction.args[i]); 120 } else { 121 params += ",arg[" + i + "]=" + GuiAC.toString(interaction.args[i]); 122 } 123 } 124 System.out.println("Calling " + interaction.method + " with " + params); 125 } finally { 126 collab.removeAttribute(DISABLE_TRACE); 127 } 128 129 Object result = proceed(interaction); 130 131 collab.addAttribute(DISABLE_TRACE, Boolean.TRUE); 132 try { 133 if (interaction.method instanceof MethodItem) { 134 Class returnType = ((MethodItem) interaction.method).getType(); 135 if (returnType != void.class) { 136 if (returnType.isArray() 137 && Object .class.isAssignableFrom( 138 returnType.getComponentType())) 139 System.out.println( 140 "Called " 141 + interaction.method 142 + " -> " 143 + Arrays.asList((Object []) result)); 144 else 145 System.out.println( 146 "Called " + interaction.method + " -> " + GuiAC.toString(result)); 147 } 148 } 149 } finally { 150 collab.removeAttribute(DISABLE_TRACE); 151 } 152 return result; 153 } 154 155 public Object printStackTrace(Interaction interaction) { 156 System.out.println( 157 "Calling '" 158 + interaction.method 159 + "' on '" 160 + NameRepository.get().getName(interaction.wrappee) 161 + "', args = " 162 + Arrays.asList(interaction.args)); 163 new Exception ().printStackTrace(); 164 return proceed(interaction); 165 } 166 167 public Object printWrappersTrace(Interaction interaction) { 168 String on = 169 interaction.method.isStatic() 170 ? "" 171 : (" on '" 172 + NameRepository.get().getName(interaction.wrappee) 173 + "'"); 174 175 System.out.println( 176 "Calling '" 177 + interaction.method 178 + "'" 179 + on 180 + ", args = " 181 + Arrays.asList(interaction.args)); 182 WrappingChain chain = 183 Wrapping.getWrappingChain(interaction.wrappee, interaction.method); 184 System.out.println("WrappingChain = " + chain); 185 return proceed(interaction); 186 } 187 188 190 public Object invoke(MethodInvocation invocation) throws Throwable { 191 switch (type) { 192 case TIMED: 193 return printCallingInfos((Interaction) invocation); 194 case NAMED: 195 return printNamedArgs((Interaction) invocation); 196 case STACK: 197 return printStackTrace((Interaction) invocation); 198 case WRAPPERS: 199 return printWrappersTrace((Interaction) invocation); 200 default: 201 Log.error("VerboseWrapper: unknown type "+type); 202 } 203 return invocation.proceed(); 204 } 205 206 public Object construct(ConstructorInvocation invocation) 207 throws Throwable { 208 return printCallingInfos((Interaction) invocation); 209 } 210 } 211 | Popular Tags |