KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > instrument > DeclareChecker


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.instrument;
23
24 import java.util.Iterator JavaDoc;
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 /**
39  *
40  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
41  * @version $Revision: 37406 $
42  */

43 public class DeclareChecker
44 {
45    public static void checkDeclares(AspectManager manager, CtClass clazz, ClassAdvisor advisor)
46    {
47       for (Iterator JavaDoc it = manager.getDeclares() ; it.hasNext() ; )
48       {
49          DeclareDef declare = (DeclareDef)it.next();
50          if (declare.matches(advisor, clazz))
51          {
52             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" 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 JavaDoc(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 JavaDoc it = manager.getDeclares() ; it.hasNext() ; )
82       {
83          DeclareDef declare = (DeclareDef)it.next();
84          
85          StringBuffer JavaDoc sb = new StringBuffer JavaDoc(" 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 JavaDoc(sb.toString());
139             }
140          }
141       }
142    }
143
144    private static void addMethod(StringBuffer JavaDoc sb, CtMethod method)
145    {
146       sb.append(method.getDeclaringClass().getName() + "." + method.getName() + method.getSignature());
147    }
148    
149    private static void addConstructor(StringBuffer JavaDoc sb, CtConstructor con)
150    {
151       sb.append(con.getDeclaringClass().getName() + ".new" + con.getSignature());
152    }
153    
154 }
155
Popular Tags