KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > FeatureDependencyCollector


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.util.*;
36
37 import org.apache.oro.text.perl.*;
38
39 public class FeatureDependencyCollector extends CollectorBase {
40     private static final Perl5Util perl = new Perl5Util();
41
42     private Class_info thisClass;
43
44     public void visitClassfile(Classfile classfile) {
45         thisClass = classfile.getRawClass();
46
47         classfile.getConstantPool().accept(this);
48     }
49
50     public void visitFieldRef_info(FieldRef_info entry) {
51         if (entry.getRawClass() != thisClass) {
52             add(entry.getClassName() + "." + entry.getRawNameAndType().getName());
53         }
54     }
55
56     public void visitMethodRef_info(MethodRef_info entry) {
57         if ((entry.getRawClass() != thisClass) && !perl.match("/<.*init>/", entry.getRawNameAndType().getName())) {
58             add(entry.getClassName() + "." + entry.getRawNameAndType().getName());
59         }
60     }
61
62     public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) {
63         if (entry.getRawClass() != thisClass) {
64             add(entry.getClassName() + "." + entry.getRawNameAndType().getName());
65         }
66     }
67
68     public void visitMethod_info(Method_info entry) {
69         processSignature(entry.getDescriptor());
70     
71         super.visitMethod_info(entry);
72     }
73
74     public void visitCode_attribute(Code_attribute attribute) {
75
76         byte[] code = attribute.getCode();
77
78         Iterator ci = attribute.iterator();
79         while (ci.hasNext()) {
80             Instruction instr = (Instruction) ci.next();
81             switch (instr.getOpcode()) {
82                 case 0xb2: // getstatic
83
case 0xb3: // putstatic
84
case 0xb4: // getfield
85
case 0xb5: // putfield
86
case 0xb6: // invokevirtual
87
case 0xb7: // invokespecial
88
case 0xb8: // invokestatic
89
case 0xb9: // invokeinterface
90
int start = instr.getStart();
91                     int index = (code[start+1] << 8) | code[start+2];
92                     ((Visitable) attribute.getClassfile().getConstantPool().get(index)).accept(this);
93                     break;
94                 default:
95                     // Do nothing
96
break;
97             }
98         }
99
100         Iterator i = attribute.getAttributes().iterator();
101         while (i.hasNext()) {
102             ((Visitable) i.next()).accept(this);
103         }
104     }
105
106     private void processSignature(String JavaDoc str) {
107         int currentPos = 0;
108         int startPos;
109         int endPos;
110
111         while ((startPos = str.indexOf('L', currentPos)) != -1) {
112             if ((endPos = str.indexOf(';', startPos)) != -1) {
113                 String JavaDoc candidate = str.substring(startPos + 1, endPos);
114                 if (!thisClass.getName().equals(candidate)) {
115                     add(SignatureHelper.path2ClassName(candidate));
116                 }
117                 currentPos = endPos + 1;
118             } else {
119                 currentPos = startPos + 1;
120             }
121         }
122     }
123 }
124
Popular Tags