KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uni_hamburg > eggink > autojar > Avisitor


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

19
20 import java.io.*;
21 import java.util.*;
22 import org.apache.bcel.*;
23 import org.apache.bcel.classfile.*;
24 import org.apache.bcel.util.*;
25
26 /** Visitor handles ConstantClass entries
27  *
28  * @author Bernd Eggink, RRZ Uni Hamburg (Bernd.Eggink@rrz.uni-hamburg.de)
29  *
30  */

31
32 class Avisitor
33     extends EmptyVisitor
34 {
35     private JavaClass klass;
36     private int indexForName;
37
38     //----------------------------------------------------------------------
39

40     /** @param klass JavaClass object containing the code */
41     
42     Avisitor(JavaClass klass)
43     {
44         this.klass = klass;
45         indexForName = -1;
46     }
47
48     //----------------------------------------------------------------------
49

50     int getIndexForName()
51     {
52         return indexForName;
53     }
54     
55     //----------------------------------------------------------------------
56

57     public void visitConstantClass(ConstantClass cc)
58     {
59         String JavaDoc cstr = klass.getConstantPool().getConstant(cc.getNameIndex()).toString();
60
61         int ia = cstr.indexOf('"'),
62                 ie = cstr.lastIndexOf('"');
63         String JavaDoc name = cstr.substring(ia + 1, ie);
64         
65         // skip arrays
66

67         if (name.startsWith("["))
68             return;
69
70         try
71         {
72             Autojar.lookupClass(name);
73         }
74         catch (IOException ex)
75         {
76             ex.printStackTrace();
77         }
78     }
79
80     //----------------------------------------------------------------------
81

82     public void visitConstantMethodref(ConstantMethodref ref)
83     {
84         ConstantPool pool = klass.getConstantPool();
85         String JavaDoc cstr = ref.getClass(pool);
86
87         if (cstr.equals("java.lang.Class"))
88         {
89             int iname = ref.getNameAndTypeIndex();
90             String JavaDoc name = ((ConstantNameAndType)pool.getConstant(iname)).getName(pool);
91
92             if (name.equals("forName"))
93                 indexForName = iname;
94         }
95     }
96     
97 }
98
99
100
Popular Tags