KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > visitclass > Util


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2005, 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.visitclass;
21
22 import org.apache.bcel.Repository;
23 import org.apache.bcel.classfile.Attribute;
24 import org.apache.bcel.classfile.Code;
25 import org.apache.bcel.classfile.CodeException;
26 import org.apache.bcel.classfile.Constant;
27 import org.apache.bcel.classfile.ConstantClass;
28 import org.apache.bcel.classfile.ConstantPool;
29 import org.apache.bcel.classfile.InnerClass;
30 import org.apache.bcel.classfile.InnerClasses;
31 import org.apache.bcel.classfile.JavaClass;
32 import org.apache.bcel.classfile.LineNumber;
33 import org.apache.bcel.classfile.LineNumberTable;
34
35 import edu.umd.cs.findbugs.annotations.CheckForNull;
36
37 /**
38  * @author pugh
39  */

40 public class Util {
41     /**
42      * Determine the outer class of obj.
43      * @param obj
44      * @return JavaClass for outer class, or null if obj is not an outer class
45      * @throws ClassNotFoundException
46      */

47             
48     @CheckForNull public static JavaClass getOuterClass(JavaClass obj) throws ClassNotFoundException JavaDoc {
49         for(Attribute a : obj.getAttributes())
50             if (a instanceof InnerClasses) {
51                 for(InnerClass ic : ((InnerClasses) a).getInnerClasses()) {
52                     if (obj.getClassNameIndex() == ic.getInnerClassIndex()) {
53 // System.out.println("Outer class is " + ic.getOuterClassIndex());
54
ConstantClass oc = (ConstantClass) obj.getConstantPool().getConstant(ic.getOuterClassIndex());
55                         String JavaDoc ocName = oc.getBytes(obj.getConstantPool());
56                         return Repository.lookupClass(ocName);
57                     }
58                 }
59             }
60         return null;
61     }
62     
63      public static int getSizeOfSurroundingTryBlock(ConstantPool constantPool, Code code, String JavaDoc vmNameOfExceptionClass, int pc) {
64             int size = Integer.MAX_VALUE;
65             int tightStartPC = 0;
66             int tightEndPC = Integer.MAX_VALUE;
67             if (code.getExceptionTable() == null) return size;
68             for (CodeException catchBlock : code.getExceptionTable()) {
69                 if (vmNameOfExceptionClass != null) {
70                 Constant catchType = constantPool.getConstant(catchBlock.getCatchType());
71                 if (catchType == null || catchType instanceof ConstantClass
72                         && !((ConstantClass)catchType).getBytes(constantPool).equals(vmNameOfExceptionClass)) continue;
73                 }
74                 int startPC = catchBlock.getStartPC();
75                 int endPC = catchBlock.getEndPC();
76                 if (pc >= startPC && pc <= endPC) {
77                     int thisSize = endPC - startPC;
78                     if (size > thisSize) {
79                         size = thisSize;
80                         tightStartPC = startPC;
81                         tightEndPC = endPC;
82                     }
83                 }
84             }
85             if (size == Integer.MAX_VALUE) return size;
86             
87             // try to guestimate number of lines that correspond
88
size = (size+7) / 8;
89             LineNumberTable lineNumberTable = code.getLineNumberTable();
90             if (lineNumberTable == null) return size;
91         
92             int count = 0;
93             for(LineNumber line : lineNumberTable.getLineNumberTable()) {
94                 if (line.getStartPC() > tightEndPC) break;
95                 if (line.getStartPC() >= tightStartPC) count++;
96             }
97             return count;
98
99      }
100
101
102 }
103
Popular Tags