KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.bcel.generic.ConstantPoolGen;
23 import org.apache.bcel.generic.Instruction;
24 import org.apache.bcel.generic.InvokeInstruction;
25 import org.apache.bcel.generic.ObjectType;
26
27 import edu.umd.cs.findbugs.ba.Hierarchy;
28 import edu.umd.cs.findbugs.ba.Location;
29 import edu.umd.cs.findbugs.ba.ObjectTypeFactory;
30 import edu.umd.cs.findbugs.ba.RepositoryLookupFailureCallback;
31
32 /**
33  * Factory for stream objects of a particular
34  * base class type returned by any method.
35  * This factory helps us keep track of streams returned
36  * by methods; we don't want to report them, but we do
37  * want to keep track of whether or not they are closed,
38  * to avoid reporting unclosed streams in the same
39  * equivalence class.
40  */

41 public class AnyMethodReturnValueStreamFactory implements StreamFactory {
42     private ObjectType baseClassType;
43     private String JavaDoc bugType;
44
45     public AnyMethodReturnValueStreamFactory(String JavaDoc streamBase) {
46         this.baseClassType = ObjectTypeFactory.getInstance(streamBase);
47         this.bugType = null;
48     }
49
50     public AnyMethodReturnValueStreamFactory setBugType(String JavaDoc bugType) {
51         this.bugType = bugType;
52         return this;
53     }
54
55     public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg,
56                                RepositoryLookupFailureCallback lookupFailureCallback) {
57
58         Instruction ins = location.getHandle().getInstruction();
59
60         try {
61             if (ins instanceof InvokeInstruction) {
62                 if (!Hierarchy.isSubtype(type, baseClassType))
63                     return null;
64
65                 Stream stream = new Stream(location, type.getClassName(), baseClassType.getClassName())
66                         .setIsOpenOnCreation(true)
67                         .setIgnoreImplicitExceptions(true);
68                 if (bugType != null)
69                     stream.setInteresting(bugType);
70
71                 return stream;
72             }
73         } catch (ClassNotFoundException JavaDoc e) {
74             lookupFailureCallback.reportMissingClass(e);
75         }
76
77         return null;
78     }
79 }
80
81 // vim:ts=4
82
Popular Tags