KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jarg > PackageCollection


1 /* ====================================================================
2  * Copyright (c) 2002, Hidetoshi Ohuchi <hchacha@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the hchacha nor the names of its contributors
17  * may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  * ====================================================================
33  */

34 package jarg;
35
36 import java.util.Map JavaDoc;
37 import java.util.TreeMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 import org.apache.bcel.classfile.*;
41
42 /**
43  * The collection of the package handler in the jar file.
44  *
45  * @version $Id: PackageCollection.java,v 1.2 2003/01/28 17:30:53 hchacha Exp $
46  * @author Hidetoshi Ohuchi &lt;hchacha@users.sourceforge.net&gt;
47  */

48 class PackageCollection {
49    private Jarg app;
50    Map JavaDoc packages = new TreeMap JavaDoc();
51
52    PackageCollection(Jarg app) {
53       this.app = app;
54    }
55
56    void addPackageHandler(PackageHandler pkgh) {
57       String JavaDoc key = pkgh.packagename;
58       this.packages.put(key, pkgh);
59    }
60
61    void markUsedClass(String JavaDoc classname) {
62       // classname : example/pushpuzzle/PushPuzzle
63
// classname : [B
64
// classname : [Lorg/apache/bcel/classfile/Constant;
65
// System.err.println(classname);
66
if (classname.charAt(0) == '[') {
67          // array
68
} else {
69          markUsedClass0(classname);
70       }
71    }
72
73    private void markUsedClass0(String JavaDoc classname) {
74       String JavaDoc pkey = "";
75       String JavaDoc ckey = classname.replace('/','.');
76       int idx = classname.lastIndexOf('/');
77       if (idx >= 0) {
78          pkey = classname.substring(0, idx+1);
79       }
80       PackageHandler pkgh = (PackageHandler)this.packages.get(pkey);
81       if (pkgh != null) {
82          ClassHandler clsh = (ClassHandler)pkgh.classes.get(ckey);
83          if (clsh != null) {
84             clsh.isUsed = true;
85          }
86       }
87    }
88
89    void doCheckUsedClass() {
90       Iterator JavaDoc pit = this.packages.keySet().iterator();
91       while (pit.hasNext()) {
92          String JavaDoc pkey = (String JavaDoc)pit.next();
93          PackageHandler pkgh = (PackageHandler)this.packages.get(pkey);
94
95          // check used classes in packages
96
pkgh.doCheckUsedClass(this);
97       }
98    }
99
100    void doOptimize() {
101       Iterator JavaDoc pit = this.packages.keySet().iterator();
102       while (pit.hasNext()) {
103          String JavaDoc pkey = (String JavaDoc)pit.next();
104          PackageHandler pkgh = (PackageHandler)this.packages.get(pkey);
105
106          // optimize package
107
pkgh.doOptimize();
108       }
109    }
110 }
111
Popular Tags