KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2005, 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 package edu.umd.cs.findbugs.detect;
20
21 import edu.umd.cs.findbugs.*;
22 import edu.umd.cs.findbugs.ba.*;
23 import java.util.BitSet JavaDoc;
24 import org.apache.bcel.Constants;
25 import org.apache.bcel.classfile.*;
26
27 /**
28  * equals and hashCode are blocking methods on URL's. Warn about invoking equals or hashCode on them,
29  * or defining Set or Maps with them as keys.
30  */

31 public class URLProblems extends BytecodeScanningDetector {
32
33     final static String JavaDoc[] BAD_SIGNATURES = { "Map<Ljava/net/URL",
34             "Set<Ljava/net/URL" };
35
36     final private BugReporter bugReporter;
37
38     public URLProblems(BugReporter bugReporter) {
39         this.bugReporter = bugReporter;
40     }
41
42     @Override JavaDoc
43     public void visit(Signature obj) {
44         String JavaDoc sig = obj.getSignature();
45         for (String JavaDoc s : BAD_SIGNATURES)
46             if (sig.indexOf(s) >= 0) {
47                 if (visitingField())
48                     bugReporter.reportBug(new BugInstance(this, "DMI_COLLECTION_OF_URLS",
49                             HIGH_PRIORITY).addClass(this).addVisitedField(
50                             this));
51                 else if (visitingMethod())
52                     bugReporter.reportBug(new BugInstance(this, "DMI_COLLECTION_OF_URLS",
53                             HIGH_PRIORITY).addClassAndMethod(this));
54                 else
55                     bugReporter.reportBug(new BugInstance(this, "DMI_COLLECTION_OF_URLS",
56                             HIGH_PRIORITY).addClass(this).addClass(this));
57             }
58     }
59
60     @Override JavaDoc
61     public void sawOpcode(int seen) {
62         if (seen == INVOKEVIRTUAL
63                 && getClassConstantOperand().equals("java/net/URL")) {
64             if (getNameConstantOperand().equals("equals")
65                     && getSigConstantOperand().equals("(Ljava/lang/Object;)Z")
66                     || getNameConstantOperand().equals("hashCode")
67                     && getSigConstantOperand().equals("()I")) {
68                 bugReporter.reportBug(new BugInstance(this, "DMI_BLOCKING_METHODS_ON_URL",
69                         HIGH_PRIORITY).addClassAndMethod(this)
70                         .addCalledMethod(this).addSourceLine(this));
71             }
72         }
73     }
74 }
Popular Tags