KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004, 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.detect;
21
22 import java.util.BitSet JavaDoc;
23
24 import org.apache.bcel.Constants;
25 import org.apache.bcel.generic.ConstantPoolGen;
26 import org.apache.bcel.generic.Instruction;
27 import org.apache.bcel.generic.InvokeInstruction;
28 import org.apache.bcel.generic.ObjectType;
29 import org.apache.bcel.generic.ReferenceType;
30
31 import edu.umd.cs.findbugs.ba.Hierarchy;
32 import edu.umd.cs.findbugs.ba.Location;
33 import edu.umd.cs.findbugs.ba.ObjectTypeFactory;
34 import edu.umd.cs.findbugs.ba.RepositoryLookupFailureCallback;
35
36 /**
37  * StreamFactory for streams that are created as the result
38  * of calling a method on an object.
39  */

40 public class MethodReturnValueStreamFactory implements StreamFactory {
41     private static final BitSet JavaDoc invokeOpcodeSet = new BitSet JavaDoc();
42
43     static {
44         invokeOpcodeSet.set(Constants.INVOKEINTERFACE);
45         invokeOpcodeSet.set(Constants.INVOKESPECIAL);
46         invokeOpcodeSet.set(Constants.INVOKESTATIC);
47         invokeOpcodeSet.set(Constants.INVOKEVIRTUAL);
48     }
49
50     private ObjectType baseClassType;
51     private String JavaDoc methodName;
52     private String JavaDoc methodSig;
53     private boolean isUninteresting;
54     private String JavaDoc bugType;
55
56     /**
57      * Constructor.
58      * The Streams created will be marked as uninteresting.
59      *
60      * @param baseClass base class through which the method will be
61      * called (we check instances of the base class and all subtypes)
62      * @param methodName name of the method called
63      * @param methodSig signature of the method called
64      */

65     public MethodReturnValueStreamFactory(String JavaDoc baseClass, String JavaDoc methodName, String JavaDoc methodSig) {
66         this.baseClassType = ObjectTypeFactory.getInstance(baseClass);
67         this.methodName = methodName;
68         this.methodSig = methodSig;
69         this.isUninteresting = true;
70     }
71
72     /**
73      * Constructor.
74      * The Streams created will be marked as interesting.
75      *
76      * @param baseClass base class through which the method will be
77      * called (we check instances of the base class and all subtypes)
78      * @param methodName name of the method called
79      * @param methodSig signature of the method called
80      * @param bugType the bug type that should be reported if
81      * the stream is not closed on all paths out of the method
82      */

83     public MethodReturnValueStreamFactory(String JavaDoc baseClass, String JavaDoc methodName, String JavaDoc methodSig,
84                                           String JavaDoc bugType) {
85         this.baseClassType = ObjectTypeFactory.getInstance(baseClass);
86         this.methodName = methodName;
87         this.methodSig = methodSig;
88         this.isUninteresting = false;
89         this.bugType = bugType;
90     }
91
92     public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg,
93                                RepositoryLookupFailureCallback lookupFailureCallback) {
94
95         try {
96             Instruction ins = location.getHandle().getInstruction();
97
98             // For now, just support instance methods
99
short opcode = ins.getOpcode();
100             if (!invokeOpcodeSet.get(opcode))
101                 return null;
102
103             // Is invoked class a subtype of the base class we want
104
// FIXME: should test be different for INVOKESPECIAL and INVOKESTATIC?
105
InvokeInstruction inv = (InvokeInstruction) ins;
106             ReferenceType classType = inv.getReferenceType(cpg);
107             if (!Hierarchy.isSubtype(classType, baseClassType))
108                 return null;
109
110             // See if method name and signature match
111
String JavaDoc methodName = inv.getMethodName(cpg);
112             String JavaDoc methodSig = inv.getSignature(cpg);
113             if (!this.methodName.equals(methodName) || !this.methodSig.equals(methodSig))
114                 return null;
115
116             String JavaDoc streamClass = type.getClassName();
117             Stream result = new Stream(location, streamClass, streamClass)
118                     .setIgnoreImplicitExceptions(true)
119                     .setIsOpenOnCreation(true);
120             if (!isUninteresting)
121                 result.setInteresting(bugType);
122             return result;
123         } catch (ClassNotFoundException JavaDoc e) {
124             lookupFailureCallback.reportMissingClass(e);
125         }
126
127         return null;
128     }
129 }
130
131 // vim:ts=3
132
Popular Tags