KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > surround > ExceptionAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.surround;
12
13 import java.util.Collections JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jdt.core.dom.AST;
21 import org.eclipse.jdt.core.dom.ASTNode;
22 import org.eclipse.jdt.core.dom.BodyDeclaration;
23 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
24 import org.eclipse.jdt.core.dom.ConstructorInvocation;
25 import org.eclipse.jdt.core.dom.IMethodBinding;
26 import org.eclipse.jdt.core.dom.ITypeBinding;
27 import org.eclipse.jdt.core.dom.MethodDeclaration;
28 import org.eclipse.jdt.core.dom.MethodInvocation;
29 import org.eclipse.jdt.core.dom.Name;
30 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
31 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
32 import org.eclipse.jdt.core.dom.ThrowStatement;
33
34 import org.eclipse.jdt.internal.corext.dom.Bindings;
35 import org.eclipse.jdt.internal.corext.dom.Selection;
36 import org.eclipse.jdt.internal.corext.refactoring.util.AbstractExceptionAnalyzer;
37
38 public class ExceptionAnalyzer extends AbstractExceptionAnalyzer {
39
40     private Selection fSelection;
41     
42     private static class ExceptionComparator implements Comparator JavaDoc {
43         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
44             int d1= getDepth((ITypeBinding)o1);
45             int d2= getDepth((ITypeBinding)o2);
46             if (d1 < d2)
47                 return 1;
48             if (d1 > d2)
49                 return -1;
50             return 0;
51         }
52         private int getDepth(ITypeBinding binding) {
53             int result= 0;
54             while (binding != null) {
55                 binding= binding.getSuperclass();
56                 result++;
57             }
58             return result;
59         }
60     }
61     
62     private ExceptionAnalyzer(Selection selection) {
63         Assert.isNotNull(selection);
64         fSelection= selection;
65     }
66     
67     public static ITypeBinding[] perform(BodyDeclaration enclosingNode, Selection selection) {
68         ExceptionAnalyzer analyzer= new ExceptionAnalyzer(selection);
69         enclosingNode.accept(analyzer);
70         List JavaDoc exceptions= analyzer.getCurrentExceptions();
71         if (enclosingNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
72             List JavaDoc thrownExceptions= ((MethodDeclaration)enclosingNode).thrownExceptions();
73             for (Iterator JavaDoc thrown= thrownExceptions.iterator(); thrown.hasNext();) {
74                 ITypeBinding thrownException= ((Name)thrown.next()).resolveTypeBinding();
75                 if (thrownException != null) {
76                     for (Iterator JavaDoc excep= exceptions.iterator(); excep.hasNext();) {
77                         ITypeBinding exception= (ITypeBinding) excep.next();
78                         if (exception.isAssignmentCompatible(thrownException))
79                             excep.remove();
80                     }
81                 }
82             }
83         }
84         Collections.sort(exceptions, new ExceptionComparator());
85         return (ITypeBinding[]) exceptions.toArray(new ITypeBinding[exceptions.size()]);
86     }
87
88     public boolean visit(ThrowStatement node) {
89         ITypeBinding exception= node.getExpression().resolveTypeBinding();
90         if (!isSelected(node) || exception == null || Bindings.isRuntimeException(exception)) // Safety net for null bindings when compiling fails.
91
return true;
92         
93         addException(exception);
94         return true;
95     }
96     
97     public boolean visit(MethodInvocation node) {
98         if (!isSelected(node))
99             return false;
100         return handleExceptions(node.resolveMethodBinding(), node.getAST());
101     }
102     
103     public boolean visit(SuperMethodInvocation node) {
104         if (!isSelected(node))
105             return false;
106         return handleExceptions(node.resolveMethodBinding(), node.getAST());
107     }
108     
109     public boolean visit(ClassInstanceCreation node) {
110         if (!isSelected(node))
111             return false;
112         return handleExceptions(node.resolveConstructorBinding(), node.getAST());
113     }
114     
115     public boolean visit(ConstructorInvocation node) {
116         if (!isSelected(node))
117             return false;
118         return handleExceptions(node.resolveConstructorBinding(), node.getAST());
119     }
120     
121     public boolean visit(SuperConstructorInvocation node) {
122         if (!isSelected(node))
123             return false;
124         return handleExceptions(node.resolveConstructorBinding(), node.getAST());
125     }
126
127     private boolean handleExceptions(IMethodBinding binding, AST ast) {
128         if (binding == null)
129             return true;
130         ITypeBinding[] exceptions= binding.getExceptionTypes();
131         for (int i= 0; i < exceptions.length; i++) {
132             addException(exceptions[i]);
133         }
134         return true;
135     }
136         
137     private boolean isSelected(ASTNode node) {
138         return fSelection.getVisitSelectionMode(node) == Selection.SELECTED;
139     }
140 }
141
Popular Tags