1 22 package org.jboss.aop.instrument; 23 24 import java.util.Iterator ; 25 26 import javassist.CtBehavior; 27 import javassist.CtClass; 28 import javassist.CtConstructor; 29 import javassist.CtMethod; 30 import javassist.NotFoundException; 31 import javassist.expr.MethodCall; 32 import javassist.expr.NewExpr; 33 34 import org.jboss.aop.AspectManager; 35 import org.jboss.aop.ClassAdvisor; 36 import org.jboss.aop.pointcut.DeclareDef; 37 38 43 public class DeclareChecker 44 { 45 public static void checkDeclares(AspectManager manager, CtClass clazz, ClassAdvisor advisor) 46 { 47 for (Iterator it = manager.getDeclares() ; it.hasNext() ; ) 48 { 49 DeclareDef declare = (DeclareDef)it.next(); 50 if (declare.matches(advisor, clazz)) 51 { 52 StringBuffer sb = new StringBuffer (" condition\n\t'" + declare.getExpr() + "'\nwas broken for class " + 53 clazz.getName() + "\n\t" + declare.getMsg() + "\n"); 54 55 if (declare.getWarning()) 56 { 57 sb.insert(0, "WARNING: declare-warning"); 58 System.out.println(sb.toString()); 59 } 60 else 61 { 62 sb.insert(0, "ERROR: declare-error"); 63 throw new RuntimeException (sb.toString()); 64 } 65 } 66 } 67 } 68 69 public static void checkDeclares(AspectManager manager, NewExpr call, ClassAdvisor advisor) throws NotFoundException 70 { 71 checkDeclares(manager, call, null, advisor); 72 } 73 74 public static void checkDeclares(AspectManager manager, MethodCall call, ClassAdvisor advisor) throws NotFoundException 75 { 76 checkDeclares(manager, null, call, advisor); 77 } 78 79 private static void checkDeclares(AspectManager manager, NewExpr newcall, MethodCall mcall, ClassAdvisor advisor) throws NotFoundException 80 { 81 for (Iterator it = manager.getDeclares() ; it.hasNext() ; ) 82 { 83 DeclareDef declare = (DeclareDef)it.next(); 84 85 StringBuffer sb = new StringBuffer (" condition\n\t'" + declare.getExpr() + "'\nwas broken for "); 86 87 if ((newcall != null && declare.matchesCall(advisor, newcall) || (mcall != null && declare.matchesCall(advisor, mcall)))) 88 { 89 if (mcall != null) 90 { 91 sb.append("method call:"); 92 CtBehavior caller = mcall.where(); 93 if (caller instanceof CtConstructor) 94 { 95 CtConstructor con = (CtConstructor)caller; 96 addConstructor(sb, con); 97 sb.append(" calls "); 98 addMethod(sb, mcall.getMethod()); 99 } 100 else if (caller instanceof CtMethod) 101 { 102 CtMethod met = (CtMethod)caller; 103 addMethod(sb, met); 104 sb.append(" calls "); 105 addMethod(sb, mcall.getMethod()); 106 } 107 } 108 else if (newcall != null) 109 { 110 sb.append("constructor call: "); 111 CtBehavior caller = newcall.where(); 112 if (caller instanceof CtConstructor) 113 { 114 CtConstructor con = (CtConstructor)caller; 115 addConstructor(sb, con); 116 sb.append(" calls "); 117 addConstructor(sb, newcall.getConstructor()); 118 } 119 else if (caller instanceof CtMethod) 120 { 121 CtMethod met = (CtMethod)caller; 122 addMethod(sb, met); 123 sb.append(" calls "); 124 addConstructor(sb, newcall.getConstructor()); 125 } 126 } 127 128 sb.append("\n\t" + declare.getMsg() + "\n"); 129 130 if (declare.getWarning()) 131 { 132 sb.insert(0, "WARNING: declare-warning"); 133 System.out.println(sb.toString()); 134 } 135 else 136 { 137 sb.insert(0, "ERROR: declare-error"); 138 throw new RuntimeException (sb.toString()); 139 } 140 } 141 } 142 } 143 144 private static void addMethod(StringBuffer sb, CtMethod method) 145 { 146 sb.append(method.getDeclaringClass().getName() + "." + method.getName() + method.getSignature()); 147 } 148 149 private static void addConstructor(StringBuffer sb, CtConstructor con) 150 { 151 sb.append(con.getDeclaringClass().getName() + ".new" + con.getSignature()); 152 } 153 154 } 155 | Popular Tags |