KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectTypeFactory;
30 import edu.umd.cs.findbugs.ba.RepositoryLookupFailureCallback;
31
32 /**
33  * A StreamFactory for normal java.io streams that
34  * are created using NEW instructions.
35  */

36 public class IOStreamFactory implements StreamFactory {
37     private ObjectType baseClassType;
38     private ObjectType[] uninterestingSubclassTypeList;
39     private String JavaDoc bugType;
40
41     public IOStreamFactory(String JavaDoc baseClass, String JavaDoc[] uninterestingSubclassList, String JavaDoc bugType) {
42         this.baseClassType = ObjectTypeFactory.getInstance(baseClass);
43         this.uninterestingSubclassTypeList = new ObjectType[uninterestingSubclassList.length];
44         for (int i = 0; i < uninterestingSubclassList.length; ++i) {
45             this.uninterestingSubclassTypeList[i] = ObjectTypeFactory.getInstance(uninterestingSubclassList[i]);
46         }
47         this.bugType = bugType;
48     }
49
50     public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg,
51                                RepositoryLookupFailureCallback lookupFailureCallback) {
52
53         try {
54             Instruction ins = location.getHandle().getInstruction();
55
56             if (ins.getOpcode() != Constants.NEW)
57                 return null;
58
59             if (Hierarchy.isSubtype(type, baseClassType)) {
60                 boolean isUninteresting = false;
61                 for (ObjectType aUninterestingSubclassTypeList : uninterestingSubclassTypeList) {
62                     if (Hierarchy.isSubtype(type, aUninterestingSubclassTypeList)) {
63                         isUninteresting = true;
64                         break;
65                     }
66                 }
67                 Stream result = new Stream(location, type.getClassName(), baseClassType.getClassName())
68                         .setIgnoreImplicitExceptions(true);
69                 if (!isUninteresting)
70                     result.setInteresting(bugType);
71                 return result;
72             }
73         } catch (ClassNotFoundException JavaDoc e) {
74             lookupFailureCallback.reportMissingClass(e);
75         }
76
77         return null;
78     }
79 }
80
81 // vim:ts=3
82
Popular Tags