KickJava   Java API By Example, From Geeks To Geeks.

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


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.GETSTATIC;
25 import org.apache.bcel.generic.Instruction;
26 import org.apache.bcel.generic.ObjectType;
27
28 import edu.umd.cs.findbugs.ba.Location;
29 import edu.umd.cs.findbugs.ba.RepositoryLookupFailureCallback;
30
31 /**
32  * Stream factory for streams created by loading a value
33  * from a static field. This is mainly to handle
34  * System.in, System.out, and System.err.
35  */

36 public class StaticFieldLoadStreamFactory implements StreamFactory {
37     public String JavaDoc streamBaseClass;
38     public String JavaDoc className;
39     public String JavaDoc fieldName;
40     public String JavaDoc fieldSig;
41
42     /**
43      * Constructor.
44      * Created Stream objects will be marked as uninteresting.
45      *
46      * @param streamBaseClass the base class of the stream objects created
47      * by the factory
48      * @param className name of the class containing the static field
49      * @param fieldName name of the static field
50      * @param fieldSig signature of the static field
51      */

52     public StaticFieldLoadStreamFactory(String JavaDoc streamBaseClass, String JavaDoc className,
53                                         String JavaDoc fieldName, String JavaDoc fieldSig) {
54         this.streamBaseClass = streamBaseClass;
55         this.className = className;
56         this.fieldName = fieldName;
57         this.fieldSig = fieldSig;
58     }
59
60     public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg,
61                                RepositoryLookupFailureCallback lookupFailureCallback) {
62
63         Instruction ins = location.getHandle().getInstruction();
64         if (ins.getOpcode() != Constants.GETSTATIC)
65             return null;
66
67         GETSTATIC getstatic = (GETSTATIC) ins;
68         if (!className.equals(getstatic.getClassName(cpg))
69                 || !fieldName.equals(getstatic.getName(cpg))
70                 || !fieldSig.equals(getstatic.getSignature(cpg)))
71             return null;
72
73         return new Stream(location, type.getClassName(), streamBaseClass)
74                 .setIgnoreImplicitExceptions(true)
75                 .setIsOpenOnCreation(true);
76     }
77 }
78
79 // vim:ts=3
80
Popular Tags