KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > ExceptionList


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import java.lang.reflect.*;
22 import java.util.*;
23
24 public class ExceptionList extends ArrayList
25 {
26     public ExceptionList()
27     {
28     }
29
30     public ExceptionList(Constructor template)
31     {
32         Class[] types = template.getExceptionTypes();
33         add(types);
34     }
35
36     public ExceptionList(Method template)
37     {
38         Class[] types = template.getExceptionTypes();
39         add(types);
40     }
41
42     public ExceptionList(Class[] types)
43     {
44         add(types);
45     }
46
47     public void add(Class[] types)
48     {
49         int n = types.length;
50         for (int i = 0; i < n; i++)
51         {
52             Class type = types[i];
53             if (ExceptionUtil.isUserException(type))
54             {
55                 add(type);
56             }
57         }
58     }
59
60     public ExceptionList add(String type)
61     {
62         super.add(type);
63         return this;
64     }
65
66     public ExceptionList add(Class type)
67     {
68         return add(JavaType.getName(type));
69     }
70
71     public String toString()
72     {
73         if (size() == 0)
74         {
75             return "";
76         }
77         StringBuffer sb = new StringBuffer(" throws ");
78         int comma = 0;
79         for (Iterator i = iterator(); i.hasNext(); comma++)
80         {
81             String type = (String)i.next();
82             if (comma > 0)
83             {
84                 sb.append(", ");
85             }
86             sb.append(type);
87         }
88         return sb.toString();
89     }
90 }
91
Popular Tags