KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > detect > InefficientMemberAccess


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005 Dave Brosius <dbrosius@users.sourceforge.net>
4  * Copyright (C) 2005 University of Maryland
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package edu.umd.cs.findbugs.detect;
21
22
23 import edu.umd.cs.findbugs.*;
24 import edu.umd.cs.findbugs.ba.ClassContext;
25 import org.apache.bcel.classfile.JavaClass;
26 import org.apache.bcel.generic.Type;
27
28 public class InefficientMemberAccess extends BytecodeScanningDetector implements StatelessDetector {
29     
30     public static final String JavaDoc ACCESS_PREFIX = "access$";
31     private BugReporter bugReporter;
32     private String JavaDoc clsName;
33     
34     public InefficientMemberAccess(BugReporter bugReporter) {
35         this.bugReporter = bugReporter;
36     }
37
38
39     
40     @Override JavaDoc
41          public void visitClassContext(ClassContext classContext) {
42         JavaClass cls = classContext.getJavaClass();
43         clsName = cls.getClassName();
44         if (clsName.indexOf("$") >= 0)
45             super.visitClassContext(classContext);
46     }
47         
48     @Override JavaDoc
49          public void sawOpcode(int seen) {
50         int varSlot;
51         if (seen == INVOKESTATIC) {
52             String JavaDoc methodName = getNameConstantOperand();
53             if (!methodName.startsWith(ACCESS_PREFIX))
54                 return;
55             try {
56                 varSlot = Integer.parseInt(methodName.substring(ACCESS_PREFIX.length()));
57             }
58             catch (NumberFormatException JavaDoc nfe) {
59                 return;
60             }
61             String JavaDoc methodSig = getSigConstantOperand();
62             Type[] argTypes = Type.getArgumentTypes(methodSig);
63             if ((argTypes.length < 1) || (argTypes.length > 2))
64                 return;
65             String JavaDoc parCls = argTypes[0].getSignature();
66             if (parCls.length() < 3) return;
67             parCls = parCls.substring(1, parCls.length() - 1);
68             if (!parCls.equals(getClassConstantOperand()))
69                 return;
70             if ((argTypes.length == 2) && !argTypes[1].getSignature().equals(Type.getReturnType(methodSig).getSignature()))
71                 return;
72             
73             bugReporter.reportBug(new BugInstance(this, "IMA_INEFFICIENT_MEMBER_ACCESS", LOW_PRIORITY)
74                 .addClassAndMethod(this)
75                 .addSourceLine(this));
76         }
77     }
78     
79     
80     
81
82 }
83
Popular Tags