KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ExceptionFinder


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27 import org.aspectj.compiler.base.ast.*;
28
29 import org.aspectj.compiler.crosscuts.ast.ProceedExpr;
30
31 import java.util.*;
32
33
34 public class ExceptionFinder extends Walker {
35     /**
36      * Finds all exceptions that can be thrown from ast
37      *
38      * @param ast where to search for exceptions
39      * @param deFacto true == exceptions that make actually be thrown
40      * false == exceptions that JLS flow analysis wants to know about
41      */

42     
43     public static Set getPossibleExceptions(ASTObject ast, boolean deFacto) {
44         if (ast == null) {
45             return new HashSet();
46         }
47
48         ExceptionFinder finder = new ExceptionFinder(ast.getCompiler());
49         finder.deFacto = deFacto;
50         finder.process(ast);
51         return finder.exceptions;
52     }
53
54
55     boolean deFacto;
56     Set exceptions = new HashSet();
57
58     Set findingInMethods = new HashSet();
59
60     private ExceptionFinder(JavaCompiler compiler) {
61         super(compiler);
62     }
63
64     void addAll(Set set, TypeDs typeDs) {
65         if (typeDs == null) return;
66
67         final int N = typeDs.size();
68         for(int i=0; i<N; i++) {
69             set.add(typeDs.get(i).getType());
70         }
71     }
72
73     void removeSubtypes(Set types, Type baseType) {
74         for(Iterator iter = types.iterator(); iter.hasNext(); ) {
75             Type checkType = (Type)iter.next();
76             if (checkType.isSubtypeOf(baseType)) {
77                 iter.remove();
78             }
79         }
80     }
81
82     void removeCaughtExceptions(Set exceptions, CatchClauses catchClauses) {
83         if (catchClauses == null) return;
84
85         final int N = catchClauses.size();
86         for(int i=0; i<N; i++) {
87             Type exceptionType = catchClauses.get(i).getFormal().getType();
88             removeSubtypes(exceptions, exceptionType);
89         }
90     }
91
92     public ASTObject process(ASTObject node) {
93         if (node instanceof TryCatchStmt) {
94             TryCatchStmt tryStmt = (TryCatchStmt) node;
95             Set bodyExceptions = getPossibleExceptions(tryStmt.getBody(), deFacto);
96             removeCaughtExceptions(bodyExceptions, tryStmt.getCatches());
97             exceptions.addAll(bodyExceptions);
98             super.process(tryStmt.getCatches());
99             return node;
100         }
101
102         if (node instanceof ThrowStmt) {
103             ThrowStmt throwStmt = (ThrowStmt)node;
104             exceptions.add(throwStmt.getExpr().getType());
105         } else if (node instanceof CallExpr) {
106             CallExpr callExpr = (CallExpr)node;
107             if (callExpr.getId().equals("aspectOf")) {
108                 return super.process(node);
109             }
110
111             Method method = callExpr.getMethod();
112
113             if (method == null) {
114                 //System.out.println(">>>>>>>>>>>>>>>>>>>>>unbound call to: " + callExpr.getId());
115
return super.process(node);
116             }
117             addAll(exceptions, method.getThrows());
118         } else if (node instanceof NewInstanceExpr) {
119             NewInstanceExpr newExpr = (NewInstanceExpr)node;
120             Constructor init = newExpr.getConstructor();
121             addAll(exceptions, init.getThrows());
122         } else if (node instanceof ProceedExpr) {
123             exceptions.addAll(((ProceedExpr)node).getPossibleExceptions(deFacto));
124         }
125             
126         if (node instanceof TypeDec) return node;
127         return super.process(node);
128     }
129 }
130
131
Popular Tags