Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/techtrader/modules/tools/bytecode/ExceptionsAttribute.java


1   package com.techtrader.modules.tools.bytecode;
2   
3   
4   import java.io.DataInput;
5   import java.io.DataOutput;
6   import java.io.IOException;
7   import java.util.List;
8   import java.util.LinkedList;
9   import java.util.Iterator;
10  
11  import com.techtrader.modules.tools.bytecode.visitor.BCVisitor;
12  
13  
14  /**
15   *  Attribute indicating  what checked exceptions a method can throw; 
16   *  referenced from a BCMethod.
17   *  
18   *  @author    Abe White
19   */
20  public class ExceptionsAttribute
21    extends Attribute
22  {
23    private List _exceptionIndexes = new LinkedList ();
24  
25  
26    /**
27     *  Protected constructor.
28     */
29    public ExceptionsAttribute (int nameIndex, BCEntity owner)
30    {
31      super (nameIndex, owner);
32    }
33  
34  
35    /**  
36      *  Get the indexes into the constant pool referencing the ClassEntrys 
37     *  that describe the exception types thrown by this method.
38     */
39    public int[] getExceptionIndexes ()
40    {
41      int[] indexes = new int[_exceptionIndexes.size ()];
42  
43      Iterator indexItr =  _exceptionIndexes.iterator ();
44      for (int i = 0; i < indexes.length; i++)
45        indexes[i] = ((Integer) indexItr.next ()).intValue ();
46  
47      return indexes;
48    }
49  
50  
51    /**  
52      *  Set the indexes into the constant pool referencing the ClassEntrys 
53     *  that describe the exception types thrown by this method.
54     */
55    public void setExceptionIndexes (int[] exceptionIndexes)
56    {
57      _exceptionIndexes.clear ();
58  
59      if (exceptionIndexes != null)
60        for (int i = 0; i < exceptionIndexes.length; i++)
61          _exceptionIndexes.add (new Integer (exceptionIndexes[i]));
62    }
63  
64  
65    /**
66     *  Get the names of the exception types for this method.
67     */
68    public String[] getExceptionTypeNames ()
69    {
70      String[] names = new String[_exceptionIndexes.size ()];
71  
72      Iterator exceptions = _exceptionIndexes.iterator ();
73      for (int i = 0; i < names.length; i++)
74        names[i] = BCHelper.getExternalForm (getPool ().getClassName
75          (((Integer) exceptions.next ()).intValue ()), true);
76  
77      return names;
78    }
79  
80  
81    /**
82     *  Get the Class objects for the exceptions of this method.
83     */
84    public Class[] getExceptionTypes ()
85      throws ClassNotFoundException
86    {
87      Class[] types = new Class[_exceptionIndexes.size ()];
88  
89      Iterator exceptions = _exceptionIndexes.iterator ();
90      for (int i = 0; i < types.length; i++)
91        types[i] = BCHelper.classForName (getPool ().getClassName
92          (((Integer) exceptions.next ()).intValue ()));
93  
94      return types;
95    }
96  
97  
98    /**
99     *  Set the checked exceptions thrown by this method.
100    */
101   public void setExceptionTypeNames (String[] exceptions)
102   {
103     _exceptionIndexes.clear ();
104     if (exceptions != null)
105       for (int i = 0; i < exceptions.length; i++)
106         addExceptionTypeName (exceptions[i]);
107   }
108 
109 
110   /**
111    *  Set the checked exceptions thrown by this method.
112    */
113   public void setExceptionTypes (Class[] exceptions)
114   {
115     String[] names = null;
116     if (exceptions != null)
117     {
118       names = new String[exceptions.length];
119       for (int i = 0; i < exceptions.length; i++)
120         names[i] = exceptions[i].getName ();
121     }
122 
123     setExceptionTypeNames (names);
124   }
125 
126 
127   /**
128    *  Clear this method of all exception declarations.
129    */
130   public void clearExceptions ()
131   {
132     _exceptionIndexes.clear ();
133   }
134 
135 
136   /**
137    *  Remove an exception thrown by this method.
138    */
139   public boolean removeExceptionTypeName (String name)
140   {
141     String internalForm = BCHelper.getInternalForm (name, false);
142     for (Iterator i = _exceptionIndexes.iterator (); i.hasNext ();)
143     {
144       if (getPool ().getClassName (((Integer) i.next ()).intValue ()).
145         equals (internalForm))
146       {
147         i.remove ();
148         return true;
149       }
150     }
151     return false;
152   }
153 
154 
155   /**
156    *  Remove an exception thrown by this method.
157    */
158   public boolean removeExceptionType (Class type)
159   {
160     return removeExceptionTypeName (type.getName ());
161   }
162 
163 
164   /**  
165     *  Add an exception to those thrown by this method.
166    */
167   public void addExceptionTypeName (String name)
168   {
169     int index = getPool ().setClassName (0,
170       BCHelper.getInternalForm (name, false));
171 
172     _exceptionIndexes.add (new Integer (index));
173   }
174 
175 
176   /**  
177     *  Add an exception to those thrown by this method.
178    */
179   public void addExceptionType (Class type)
180   {
181     addExceptionTypeName (type.getName ());
182   }
183 
184 
185   /**
186    *  Return true if the method declares that it throws the given
187    *  exception.
188    */
189   public boolean throwsException (String name)
190   {
191     String[] exceptions = getExceptionTypeNames ();
192     for (int i = 0; i < exceptions.length; i++)
193       if (exceptions[i].equals (name))
194         return true;
195 
196     return false;
197   }
198 
199 
200   /**
201    *  Return true if the method declares that it throws the given
202    *  exception.
203    */
204   public boolean throwsException (Class type)
205   {
206     return throwsException (type.getName ());
207   }
208   
209 
210   public int getLength ()
211   {
212     return 2 + 2 * _exceptionIndexes.size ();
213   }
214 
215 
216   protected void copy (Attribute other)
217   {
218     setExceptionTypeNames (((ExceptionsAttribute) other).
219       getExceptionTypeNames ());
220   }
221 
222 
223   protected void readData (DataInput in, int length)
224     throws IOException
225   {
226     _exceptionIndexes.clear ();
227     int exceptionCount = in.readUnsignedShort ();
228     for (int i = 0; i < exceptionCount; i++)
229       _exceptionIndexes.add (new Integer (in.readUnsignedShort ()));
230   }
231 
232 
233   protected void writeData (DataOutput out, int length)
234     throws IOException
235   {
236     out.writeShort (_exceptionIndexes.size ());
237     for (Iterator i = _exceptionIndexes.iterator (); i.hasNext ();)
238       out.writeShort (((Number) i.next ()).shortValue ());
239   }
240 
241 
242   public void acceptVisit (BCVisitor visit)
243   {
244     visit.enterExceptionsAttribute (this);  
245     visit.exitExceptionsAttribute (this);  
246   }
247 }