KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004-2006 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
23 import edu.umd.cs.findbugs.*;
24 import org.apache.bcel.classfile.*;
25
26 public class EmptyZipFileEntry extends BytecodeScanningDetector implements StatelessDetector {
27
28     private BugReporter bugReporter;
29     private int sawPutEntry;
30     private String JavaDoc streamType;
31
32     public EmptyZipFileEntry(BugReporter bugReporter) {
33         this.bugReporter = bugReporter;
34     }
35     
36
37
38     @Override JavaDoc
39          public void visit(JavaClass obj) {
40     }
41
42     @Override JavaDoc
43          public void visit(Method obj) {
44         sawPutEntry = -10000;
45         streamType = "";
46     }
47
48     @Override JavaDoc
49          public void sawOpcode(int seen) {
50         if (seen == INVOKEVIRTUAL
51             && getNameConstantOperand().equals("putNextEntry")) {
52             streamType = getClassConstantOperand();
53             if (streamType.equals("java/util/zip/ZipOutputStream")
54             || streamType.equals("java/util/jar/JarOutputStream"))
55                 sawPutEntry = getPC();
56             else
57                 streamType = "";
58             }
59         else {
60             if (getPC() - sawPutEntry <= 7 && seen == INVOKEVIRTUAL
61                 && getNameConstantOperand().equals("closeEntry")
62             && getClassConstantOperand()
63                 .equals(streamType) )
64                         bugReporter.reportBug(new BugInstance(
65                             this,
66                             streamType.equals("java/util/zip/ZipOutputStream") ?
67                                 "AM_CREATES_EMPTY_ZIP_FILE_ENTRY" :
68                                 "AM_CREATES_EMPTY_JAR_FILE_ENTRY",
69                             NORMAL_PRIORITY)
70                             .addClassAndMethod(this)
71                             .addSourceLine(this));
72
73             }
74     
75         
76
77
78     }
79
80 }
81
Popular Tags