KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
23 import org.apache.bcel.generic.ConstantPoolGen;
24 import org.apache.bcel.generic.Instruction;
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.RepositoryLookupFailureCallback;
30
31 /**
32  * StreamFactory for stream objects loaded from instance fields.
33  *
34  * @author David Hovemeyer
35  */

36 public class InstanceFieldLoadStreamFactory implements StreamFactory {
37     private String JavaDoc streamBaseClass;
38     private String JavaDoc bugPatternType;
39
40     /**
41      * Constructor.
42      * By default, Streams created by this factory
43      * will not be marked as interesting.
44      * The setBugPatternType() method should be called to
45      * make the factory produce interesting streams.
46      *
47      * @param streamBaseClass the base class of the streams
48      * produced by the factory
49      */

50     public InstanceFieldLoadStreamFactory(String JavaDoc streamBaseClass) {
51         this.streamBaseClass = streamBaseClass;
52     }
53
54     /**
55      * Set the bug pattern type reported for unclosed streams
56      * loaded from this field. This makes the created
57      * streams "interesting".
58      *
59      * @param bugPatternType the bug pattern type
60      */

61     public InstanceFieldLoadStreamFactory setBugPatternType(String JavaDoc bugPatternType) {
62         this.bugPatternType = bugPatternType;
63         return this;
64     }
65
66     public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg,
67                                RepositoryLookupFailureCallback lookupFailureCallback) {
68
69         Instruction ins = location.getHandle().getInstruction();
70         if (ins.getOpcode() != Constants.GETFIELD)
71             return null;
72
73         String JavaDoc fieldClass = type.getClassName();
74         try {
75             if (fieldClass.startsWith("["))
76                 return null;
77             if (!Hierarchy.isSubtype(fieldClass, streamBaseClass))
78                 return null;
79
80             Stream stream = new Stream(location, fieldClass, streamBaseClass);
81             stream.setIsOpenOnCreation(true);
82             stream.setOpenLocation(location);
83             if (bugPatternType != null)
84                 stream.setInteresting(bugPatternType);
85
86             //System.out.println("Instance field stream at " + location);
87
return stream;
88         } catch (ClassNotFoundException JavaDoc e) {
89             lookupFailureCallback.reportMissingClass(e);
90             return null;
91         }
92     }
93 }
94
95 // vim:ts=4
96
Popular Tags