KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > verifier > Verifier


1 package com.sun.org.apache.bcel.internal.verifier;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.*;
58 import com.sun.org.apache.bcel.internal.classfile.*;
59 import com.sun.org.apache.bcel.internal.generic.*;
60 import com.sun.org.apache.bcel.internal.util.*;
61 import com.sun.org.apache.bcel.internal.verifier.statics.*;
62 import com.sun.org.apache.bcel.internal.verifier.structurals.*;
63 import com.sun.org.apache.bcel.internal.verifier.exc.*;
64 import com.sun.org.apache.bcel.internal.verifier.exc.Utility; // Ambigous if not declared explicitely.
65
import java.util.ArrayList JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.Iterator JavaDoc;
68
69 /**
70  * A Verifier instance is there to verify a class file according to The Java Virtual
71  * Machine Specification, 2nd Edition.
72  *
73  * Pass-3b-verification includes pass-3a-verification;
74  * pass-3a-verification includes pass-2-verification;
75  * pass-2-verification includes pass-1-verification.
76  *
77  * A Verifier creates PassVerifier instances to perform the actual verification.
78  *
79  * @version $Id: Verifier.java,v 1.1.1.1 2001/10/29 20:00:31 jvanzyl Exp $
80  * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
81  * @see com.sun.org.apache.bcel.internal.verifier.PassVerifier
82  */

83 public class Verifier{
84     /**
85      * The name of the class this verifier operates on.
86      */

87     private final String JavaDoc classname;
88
89     /** A Pass1Verifier for this Verifier instance. */
90     private Pass1Verifier p1v;
91     /** A Pass2Verifier for this Verifier instance. */
92     private Pass2Verifier p2v;
93     /** The Pass3aVerifiers for this Verifier instance. Key: Interned string specifying the method number. */
94     private HashMap JavaDoc p3avs = new HashMap JavaDoc();
95     /** The Pass3bVerifiers for this Verifier instance. Key: Interned string specifying the method number. */
96     private HashMap JavaDoc p3bvs = new HashMap JavaDoc();
97
98     /** Returns the VerificationResult for the given pass. */
99     public VerificationResult doPass1(){
100         if (p1v == null){
101             p1v = new Pass1Verifier(this);
102         }
103         return p1v.verify();
104     }
105
106     /** Returns the VerificationResult for the given pass. */
107     public VerificationResult doPass2(){
108         if (p2v == null){
109             p2v = new Pass2Verifier(this);
110         }
111         return p2v.verify();
112     }
113         
114     /** Returns the VerificationResult for the given pass. */
115     public VerificationResult doPass3a(int method_no){
116         String JavaDoc key = Integer.toString(method_no);
117         Pass3aVerifier p3av;
118         p3av = (Pass3aVerifier) (p3avs.get(key));
119         if (p3avs.get(key) == null){
120             p3av = new Pass3aVerifier(this, method_no);
121             p3avs.put(key, p3av);
122         }
123         return p3av.verify();
124     }
125
126     /** Returns the VerificationResult for the given pass. */
127     public VerificationResult doPass3b(int method_no){
128         String JavaDoc key = Integer.toString(method_no);
129         Pass3bVerifier p3bv;
130         p3bv = (Pass3bVerifier) (p3bvs.get(key));
131         if (p3bvs.get(key) == null){
132             p3bv = new Pass3bVerifier(this, method_no);
133             p3bvs.put(key, p3bv);
134         }
135         return p3bv.verify();
136     }
137     
138     /**
139      * This class may not be no-args instantiated.
140      */

141     private Verifier(){
142         classname = ""; // never executed anyway, make compiler happy.
143
}// not noargs-instantiable
144

145     /**
146      * Instantiation is done by the VerifierFactory.
147      *
148      * @see VerifierFactory
149      */

150     Verifier(String JavaDoc fully_qualified_classname){
151         classname = fully_qualified_classname;
152         flush();
153     }
154
155     /**
156      * Returns the name of the class this verifier operates on.
157      * This is particularly interesting when this verifier was created
158      * recursively by another Verifier and you got a reference to this
159      * Verifier by the getVerifiers() method of the VerifierFactory.
160      * @see VerifierFactory
161      */

162     public final String JavaDoc getClassName(){
163         return classname;
164     }
165
166     /**
167      * Forget everything known about the class file; that means, really
168      * start a new verification of a possibly different class file from
169      * BCEL's repository.
170      *
171      */

172     public void flush(){
173         p1v = null;
174         p2v = null;
175         p3avs.clear();
176         p3bvs.clear();
177     }
178
179     /**
180      * This returns all the (warning) messages collected during verification.
181      * A prefix shows from which verifying pass a message originates.
182      */

183     public String JavaDoc[] getMessages(){
184         ArrayList JavaDoc messages = new ArrayList JavaDoc();
185
186         if (p1v != null){
187             String JavaDoc[] p1m = p1v.getMessages();
188             for (int i=0; i<p1m.length; i++){
189                 messages.add("Pass 1: "+p1m[i]);
190             }
191         }
192         if (p2v != null){
193             String JavaDoc[] p2m = p2v.getMessages();
194             for (int i=0; i<p2m.length; i++){
195                 messages.add("Pass 2: "+p2m[i]);
196             }
197         }
198         Iterator JavaDoc p3as = p3avs.values().iterator();
199         while (p3as.hasNext()){
200             Pass3aVerifier pv = (Pass3aVerifier) p3as.next();
201             String JavaDoc[] p3am = pv.getMessages();
202             int meth = pv.getMethodNo();
203             for (int i=0; i<p3am.length; i++){
204                 messages.add("Pass 3a, method "+meth+" ('"+Repository.lookupClass(classname).getMethods()[meth]+"'): "+p3am[i]);
205             }
206         }
207         Iterator JavaDoc p3bs = p3bvs.values().iterator();
208         while (p3bs.hasNext()){
209             Pass3bVerifier pv = (Pass3bVerifier) p3bs.next();
210             String JavaDoc[] p3bm = pv.getMessages();
211             int meth = pv.getMethodNo();
212             for (int i=0; i<p3bm.length; i++){
213                 messages.add("Pass 3b, method "+meth+" ('"+Repository.lookupClass(classname).getMethods()[meth]+"'): "+p3bm[i]);
214             }
215         }
216
217         String JavaDoc[] ret = new String JavaDoc[messages.size()];
218         for (int i=0; i< messages.size(); i++){
219             ret[i] = (String JavaDoc) messages.get(i);
220         }
221         
222         return ret;
223     }
224
225     /**
226      * Verifies class files.
227      * This is a simple demonstration of how the API of BCEL's
228      * class file verifier "JustIce" may be used.
229      * You should supply command-line arguments which are
230      * fully qualified namea of the classes to verify. These class files
231      * must be somewhere in your CLASSPATH (refer to Sun's
232      * documentation for questions about this) or you must have put the classes
233      * into the BCEL Repository yourself (via 'addClass(JavaClass)').
234      */

235     public static void _main(String JavaDoc [] args){
236         System.out.println("JustIce by Enver Haase, (C) 2001. http://bcel.sourceforge.net\n");
237       for(int k=0; k < args.length; k++) {
238
239             if (args[k].endsWith(".class")){
240                 int dotclasspos = args[k].lastIndexOf(".class");
241                 if (dotclasspos != -1) args[k] = args[k].substring(0,dotclasspos);
242             }
243         
244             args[k] = args[k].replace('/', '.');
245         
246             System.out.println("Now verifiying: "+args[k]+"\n");
247
248             Verifier v = VerifierFactory.getVerifier(args[k]);
249             VerificationResult vr;
250         
251             vr = v.doPass1();
252             System.out.println("Pass 1:\n"+vr);
253
254             vr = v.doPass2();
255             System.out.println("Pass 2:\n"+vr);
256
257             if (vr == VerificationResult.VR_OK){
258                 JavaClass jc = Repository.lookupClass(args[k]);
259                 for (int i=0; i<jc.getMethods().length; i++){
260                     vr = v.doPass3a(i);
261                     System.out.println("Pass 3a, method "+i+" ['"+jc.getMethods()[i]+"']:\n"+vr);
262
263                     vr = v.doPass3b(i);
264                     System.out.println("Pass 3b, method number "+i+" ['"+jc.getMethods()[i]+"']:\n"+vr);
265                 }
266             }
267         
268             System.out.println("Warnings:");
269             String JavaDoc[] warnings = v.getMessages();
270             if (warnings.length == 0) System.out.println("<none>");
271             for (int j=0; j<warnings.length; j++){
272                 System.out.println(warnings[j]);
273             }
274
275             System.out.println("\n");
276       
277             // avoid swapping.
278
v.flush();
279         Repository.clearCache();
280             System.gc();
281       }
282     }
283 }
284
Popular Tags