KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > model > RegenerateClassFeatures


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
20 package edu.umd.cs.findbugs.model;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26 import java.util.zip.ZipFile JavaDoc;
27
28 import org.apache.bcel.Repository;
29 import org.apache.bcel.classfile.ClassParser;
30 import org.apache.bcel.classfile.JavaClass;
31
32 import edu.umd.cs.findbugs.BugCollection;
33 import edu.umd.cs.findbugs.Project;
34 import edu.umd.cs.findbugs.SortedBugCollection;
35
36 /**
37  * Repopulate a BugCollection with class features from
38  * the classes in a specified jar file.
39  *
40  * @author David Hovemeyer
41  */

42 public class RegenerateClassFeatures {
43     private BugCollection bugCollection;
44     private String JavaDoc jarFile;
45     
46     public RegenerateClassFeatures(BugCollection bugCollection, String JavaDoc jarFile) {
47         this.bugCollection = bugCollection;
48         this.jarFile = jarFile;
49     }
50     
51     public RegenerateClassFeatures execute() throws IOException JavaDoc {
52         bugCollection.clearClassFeatures();
53         
54         ZipFile JavaDoc zipFile = new ZipFile JavaDoc(jarFile);
55         
56         ArrayList JavaDoc<JavaClass> classList = new ArrayList JavaDoc<JavaClass>();
57
58         // Add all classes to repository (for hierarchy queries)
59
Enumeration JavaDoc<? extends ZipEntry JavaDoc> entries = zipFile.entries();
60         while (entries.hasMoreElements()) {
61             ZipEntry JavaDoc entry = entries.nextElement();
62             
63             if (!entry.getName().endsWith(".class"))
64                 continue;
65             
66             ClassParser parser = new ClassParser(zipFile.getInputStream(entry), entry.getName());
67             JavaClass javaClass = parser.parse();
68             
69             Repository.addClass(javaClass);
70             classList.add(javaClass);
71         }
72         
73         for (JavaClass javaClass : classList) {
74             ClassFeatureSet classFeatureSet = new ClassFeatureSet().initialize(javaClass);
75             bugCollection.setClassFeatureSet(classFeatureSet);
76         }
77         
78         return this;
79     }
80     
81     /**
82      * @return Returns the bugCollection.
83      */

84     public BugCollection getBugCollection() {
85         return bugCollection;
86     }
87     
88     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
89         if (args.length != 2) {
90             System.err.println("Usage: " + RegenerateClassFeatures.class.getName() + " <bug collection> <jar file>");
91             System.exit(1);
92         }
93         
94         SortedBugCollection bugCollection = new SortedBugCollection();
95         Project project = new Project();
96         
97         bugCollection.readXML(args[0], project);
98         
99         new RegenerateClassFeatures(bugCollection, args[1]).execute();
100         
101         bugCollection.writeXML(System.out, project);
102     }
103 }
104
Popular Tags