KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > type > ExceptionObjectType


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba.type;
21
22 import org.apache.bcel.Constants;
23 import org.apache.bcel.generic.ObjectType;
24 import org.apache.bcel.generic.Type;
25
26
27 /**
28  * Special ReferenceType representing the type of a caught exception.
29  * Keeps track of the entire set of exceptions that can be caught,
30  * and whether they are explicit or implicit.
31  */

32 public class ExceptionObjectType extends ObjectType implements Constants, ExtendedTypes {
33     /**
34      *
35      */

36     private static final long serialVersionUID = 1L;
37     private ExceptionSet exceptionSet;
38
39     /**
40      * Constructor.
41      *
42      * @param className the class name
43      * @param exceptionSet the set of exceptions
44      */

45     private ExceptionObjectType(String JavaDoc className, ExceptionSet exceptionSet) {
46         super(className);
47         this.exceptionSet = exceptionSet;
48     }
49
50     /**
51      * Initialize object from an exception set.
52      *
53      * @param exceptionSet the exception set
54      * @return a Type that is a supertype of all of the exceptions in
55      * the exception set
56      */

57     public static Type fromExceptionSet(ExceptionSet exceptionSet) throws ClassNotFoundException JavaDoc {
58         Type commonSupertype = exceptionSet.getCommonSupertype();
59         if (commonSupertype.getType() != T_OBJECT)
60             return commonSupertype;
61
62         ObjectType exceptionSupertype = (ObjectType) commonSupertype;
63         return new ExceptionObjectType(exceptionSupertype.getClassName(), exceptionSet);
64     }
65
66     @Override JavaDoc
67          public byte getType() {
68         return T_EXCEPTION;
69     }
70
71     @Override JavaDoc
72          public int hashCode() {
73         return getSignature().hashCode();
74     }
75
76     @Override JavaDoc
77          public boolean equals(Object JavaDoc o) {
78         if (o == null) return false;
79         if (o.getClass() != this.getClass()) return false;
80
81         ExceptionObjectType other = (ExceptionObjectType) o;
82         return getSignature().equals(other.getSignature())
83                 && exceptionSet.equals(other.exceptionSet);
84     }
85
86     /**
87      * Return the exception set.
88      *
89      * @return the ExceptionSet
90      */

91     public ExceptionSet getExceptionSet() {
92         return exceptionSet;
93     }
94
95     @Override JavaDoc
96          public String JavaDoc toString() {
97         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
98         buf.append("<exception:");
99         boolean first = true;
100         for (ExceptionSet.ThrownExceptionIterator i = exceptionSet.iterator(); i.hasNext();) {
101             if (first)
102                 first = false;
103             else
104                 buf.append(',');
105             buf.append(i.next().toString());
106         }
107         buf.append(">");
108         return buf.toString();
109     }
110 }
111
112 // vim:ts=4
113
Popular Tags