KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sun.org.apache.bcel.internal.verifier.statics;
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.Constants;
58 import com.sun.org.apache.bcel.internal.Repository;
59 import com.sun.org.apache.bcel.internal.classfile.*;
60 import com.sun.org.apache.bcel.internal.classfile.Deprecated; // Use _this_ one!
61
import com.sun.org.apache.bcel.internal.classfile.DescendingVisitor; // Use _this_ one!
62
import com.sun.org.apache.bcel.internal.classfile.EmptyVisitor; // Use _this_ one!
63
import com.sun.org.apache.bcel.internal.classfile.Visitor; // Use _this_ one!
64
import com.sun.org.apache.bcel.internal.generic.*;
65 import com.sun.org.apache.bcel.internal.verifier.*;
66 import com.sun.org.apache.bcel.internal.verifier.exc.*;
67 import java.util.HashMap JavaDoc;
68 import java.util.HashSet JavaDoc;
69
70 /**
71  * This PassVerifier verifies a class file according to
72  * pass 2 as described in The Java Virtual Machine
73  * Specification, 2nd edition.
74  * More detailed information is to be found at the do_verify()
75  * method's documentation.
76  *
77  * @version $Id: Pass2Verifier.java,v 1.1.1.1 2001/10/29 20:00:36 jvanzyl Exp $
78  * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
79  * @see #do_verify()
80  */

81 public final class Pass2Verifier extends PassVerifier implements Constants{
82
83     /**
84      * The LocalVariableInfo instances used by Pass3bVerifier.
85      * localVariablesInfos[i] denotes the information for the
86      * local variables of method number i in the
87      * JavaClass this verifier operates on.
88      */

89     private LocalVariablesInfo[] localVariablesInfos;
90     
91     /** The Verifier that created this. */
92     private Verifier myOwner;
93
94     /**
95      * Should only be instantiated by a Verifier.
96      *
97      * @see Verifier
98      */

99     public Pass2Verifier(Verifier owner){
100         myOwner = owner;
101     }
102
103     /**
104      * Returns a LocalVariablesInfo object containing information
105      * about the usage of the local variables in the Code attribute
106      * of the said method or <B>null</B> if the class file this
107      * Pass2Verifier operates on could not be pass-2-verified correctly.
108      * The method number method_nr is the method you get using
109      * <B>Repository.lookupClass(myOwner.getClassname()).getMethods()[method_nr];</B>.
110      * You should not add own information. Leave that to JustIce.
111      */

112     public LocalVariablesInfo getLocalVariablesInfo(int method_nr){
113         if (this.verify() != VerificationResult.VR_OK) return null; // It's cached, don't worry.
114
if (method_nr < 0 || method_nr >= localVariablesInfos.length){
115             throw new AssertionViolatedException("Method number out of range.");
116         }
117         return localVariablesInfos[method_nr];
118     }
119     
120     /**
121      * Pass 2 is the pass where static properties of the
122      * class file are checked without looking into "Code"
123      * arrays of methods.
124      * This verification pass is usually invoked when
125      * a class is resolved; and it may be possible that
126      * this verification pass has to load in other classes
127      * such as superclasses or implemented interfaces.
128      * Therefore, Pass 1 is run on them.<BR>
129      * Note that most referenced classes are <B>not</B> loaded
130      * in for verification or for an existance check by this
131      * pass; only the syntactical correctness of their names
132      * and descriptors (a.k.a. signatures) is checked.<BR>
133      * Very few checks that conceptually belong here
134      * are delayed until pass 3a in JustIce. JustIce does
135      * not only check for syntactical correctness but also
136      * for semantical sanity - therefore it needs access to
137      * the "Code" array of methods in a few cases. Please
138      * see the pass 3a documentation, too.
139      *
140      * @see com.sun.org.apache.bcel.internal.verifier.statics.Pass3aVerifier
141      */

142     public VerificationResult do_verify(){
143         VerificationResult vr1 = myOwner.doPass1();
144         if (vr1.equals(VerificationResult.VR_OK)){
145             
146             // For every method, we could have information about the local variables out of LocalVariableTable attributes of
147
// the Code attributes.
148
localVariablesInfos = new LocalVariablesInfo[Repository.lookupClass(myOwner.getClassName()).getMethods().length];
149
150             VerificationResult vr = VerificationResult.VR_OK; // default.
151
try{
152                 constant_pool_entries_satisfy_static_constraints();
153                 field_and_method_refs_are_valid();
154                 every_class_has_an_accessible_superclass();
155                 final_methods_are_not_overridden();
156             }
157             catch (ClassConstraintException cce){
158                 vr = new VerificationResult(VerificationResult.VERIFIED_REJECTED, cce.getMessage());
159             }
160             return vr;
161         }
162         else
163             return VerificationResult.VR_NOTYET;
164     }
165
166     /**
167      * Ensures that every class has a super class and that
168      * <B>final</B> classes are not subclassed.
169      * This means, the class this Pass2Verifier operates
170      * on has proper super classes (transitively) up to
171      * java.lang.Object.
172      * The reason for really loading (and Pass1-verifying)
173      * all of those classes here is that we need them in
174      * Pass2 anyway to verify no final methods are overridden
175      * (that could be declared anywhere in the ancestor hierarchy).
176      *
177      * @throws ClassConstraintException otherwise.
178      */

179     private void every_class_has_an_accessible_superclass(){
180         HashSet JavaDoc hs = new HashSet JavaDoc(); // save class names to detect circular inheritance
181
JavaClass jc = Repository.lookupClass(myOwner.getClassName());
182         int supidx = -1;
183
184         while (supidx != 0){
185             supidx = jc.getSuperclassNameIndex();
186         
187             if (supidx == 0){
188                 if (jc != Repository.lookupClass(Type.OBJECT.getClassName())){
189                     throw new ClassConstraintException("Superclass of '"+jc.getClassName()+"' missing but not "+Type.OBJECT.getClassName()+" itself!");
190                 }
191             }
192             else{
193                 String JavaDoc supername = jc.getSuperclassName();
194                 if (! hs.add(supername)){ // If supername already is in the list
195
throw new ClassConstraintException("Circular superclass hierarchy detected.");
196                 }
197                 Verifier v = VerifierFactory.getVerifier(supername);
198                 VerificationResult vr = v.doPass1();
199
200                 if (vr != VerificationResult.VR_OK){
201                     throw new ClassConstraintException("Could not load in ancestor class '"+supername+"'.");
202                 }
203                 jc = Repository.lookupClass(supername);
204
205                 if (jc.isFinal()){
206                     throw new ClassConstraintException("Ancestor class '"+supername+"' has the FINAL access modifier and must therefore not be subclassed.");
207                 }
208             }
209         }
210     }
211
212     /**
213      * Ensures that <B>final</B> methods are not overridden.
214      * <B>Precondition to run this method:
215      * constant_pool_entries_satisfy_static_constraints() and
216      * every_class_has_an_accessible_superclass() have to be invoked before
217      * (in that order).</B>
218      *
219      * @throws ClassConstraintException otherwise.
220      * @see #constant_pool_entries_satisfy_static_constraints()
221      * @see #every_class_has_an_accessible_superclass()
222      */

223     private void final_methods_are_not_overridden(){
224         HashMap JavaDoc hashmap = new HashMap JavaDoc();
225         JavaClass jc = Repository.lookupClass(myOwner.getClassName());
226         
227         int supidx = -1;
228         while (supidx != 0){
229             supidx = jc.getSuperclassNameIndex();
230
231             ConstantPoolGen cpg = new ConstantPoolGen(jc.getConstantPool());
232             Method[] methods = jc.getMethods();
233             for (int i=0; i<methods.length; i++){
234                 String JavaDoc name_and_sig = (methods[i].getName()+methods[i].getSignature());
235
236                 if (hashmap.containsKey(name_and_sig)){
237                     if (methods[i].isFinal()){
238                         throw new ClassConstraintException("Method '"+name_and_sig+"' in class '"+hashmap.get(name_and_sig)+"' overrides the final (not-overridable) definition in class '"+jc.getClassName()+"'.");
239                     }
240                     else{
241                         if (!methods[i].isStatic()){ // static methods don't inherit
242
hashmap.put(name_and_sig, jc.getClassName());
243                         }
244                     }
245                 }
246                 else{
247                     if (!methods[i].isStatic()){ // static methods don't inherit
248
hashmap.put(name_and_sig, jc.getClassName());
249                     }
250                 }
251             }
252         
253             jc = Repository.lookupClass(jc.getSuperclassName()); // Well, for OBJECT this returns OBJECT so it works (could return anything but must not throw an Exception).
254
}
255
256     }
257
258     /**
259      * Ensures that the constant pool entries satisfy the static constraints
260      * as described in The Java Virtual Machine Specification, 2nd Edition.
261      *
262      * @throws ClassConstraintException otherwise.
263      */

264     private void constant_pool_entries_satisfy_static_constraints(){
265         // Most of the consistency is handled internally by BCEL; here
266
// we only have to verify if the indices of the constants point
267
// to constants of the appropriate type and such.
268
JavaClass jc = Repository.lookupClass(myOwner.getClassName());
269         new CPESSC_Visitor(jc); // constructor implicitely traverses jc
270
}
271
272     /**
273      * A Visitor class that ensures the constant pool satisfies the static
274      * constraints.
275    * The visitXXX() methods throw ClassConstraintException instances otherwise.
276    *
277    * @see #constant_pool_entries_satisfy_static_constraints()
278      */

279     private class CPESSC_Visitor extends com.sun.org.apache.bcel.internal.classfile.EmptyVisitor implements Visitor{
280         private Class JavaDoc CONST_Class;
281         private Class JavaDoc CONST_Fieldref;
282         private Class JavaDoc CONST_Methodref;
283         private Class JavaDoc CONST_InterfaceMethodref;
284         private Class JavaDoc CONST_String;
285         private Class JavaDoc CONST_Integer;
286         private Class JavaDoc CONST_Float;
287         private Class JavaDoc CONST_Long;
288         private Class JavaDoc CONST_Double;
289         private Class JavaDoc CONST_NameAndType;
290         private Class JavaDoc CONST_Utf8;
291
292         private final JavaClass jc;
293         private final ConstantPool cp; // ==jc.getConstantPool() -- only here to save typing work and computing power.
294
private final int cplen; // == cp.getLength() -- to save computing power.
295
private DescendingVisitor carrier;
296
297         private HashSet JavaDoc field_names = new HashSet JavaDoc();
298         private HashSet JavaDoc field_names_and_desc = new HashSet JavaDoc();
299         private HashSet JavaDoc method_names_and_desc = new HashSet JavaDoc();
300         
301         private CPESSC_Visitor(JavaClass _jc){
302             jc = _jc;
303             cp = _jc.getConstantPool();
304             cplen = cp.getLength();
305             
306             CONST_Class = com.sun.org.apache.bcel.internal.classfile.ConstantClass.class;
307             CONST_Fieldref = com.sun.org.apache.bcel.internal.classfile.ConstantFieldref.class;
308             CONST_Methodref = com.sun.org.apache.bcel.internal.classfile.ConstantMethodref.class;
309             CONST_InterfaceMethodref = com.sun.org.apache.bcel.internal.classfile.ConstantInterfaceMethodref.class;
310             CONST_String = com.sun.org.apache.bcel.internal.classfile.ConstantString.class;
311             CONST_Integer = com.sun.org.apache.bcel.internal.classfile.ConstantInteger.class;
312             CONST_Float = com.sun.org.apache.bcel.internal.classfile.ConstantFloat.class;
313             CONST_Long = com.sun.org.apache.bcel.internal.classfile.ConstantLong.class;
314             CONST_Double = com.sun.org.apache.bcel.internal.classfile.ConstantDouble.class;
315             CONST_NameAndType = com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType.class;
316             CONST_Utf8 = com.sun.org.apache.bcel.internal.classfile.ConstantUtf8.class;
317         
318             carrier = new DescendingVisitor(_jc, this);
319             carrier.visit();
320         }
321         
322         private void checkIndex(Node referrer, int index, Class JavaDoc shouldbe){
323             if ((index < 0) || (index >= cplen)){
324                 throw new ClassConstraintException("Invalid index '"+index+"' used by '"+tostring(referrer)+"'.");
325             }
326             Constant c = cp.getConstant(index);
327             if (! shouldbe.isInstance(c)){
328                 String JavaDoc isnot = shouldbe.toString().substring(shouldbe.toString().lastIndexOf(".")+1); //Cut all before last "."
329
throw new ClassCastException JavaDoc("Illegal constant '"+tostring(c)+"' at index '"+index+"'. '"+tostring(referrer)+"' expects a '"+shouldbe+"'.");
330             }
331         }
332         ///////////////////////////////////////
333
// ClassFile structure (vmspec2 4.1) //
334
///////////////////////////////////////
335
public void visitJavaClass(JavaClass obj){
336             Attribute[] atts = obj.getAttributes();
337             boolean foundSourceFile = false;
338             boolean foundInnerClasses = false;
339             
340             // Is there an InnerClass referenced?
341
// This is a costly check; existing verifiers don't do it!
342
boolean hasInnerClass = new InnerClassDetector(jc).innerClassReferenced();
343             
344             for (int i=0; i<atts.length; i++){
345                 if ((! (atts[i] instanceof SourceFile)) &&
346                     (! (atts[i] instanceof Deprecated JavaDoc)) &&
347                     (! (atts[i] instanceof InnerClasses)) &&
348                     (! (atts[i] instanceof Synthetic))){
349                     addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of the ClassFile structure '"+tostring(obj)+"' is unknown and will therefore be ignored.");
350                 }
351                 
352                 if (atts[i] instanceof SourceFile){
353                     if (foundSourceFile == false) foundSourceFile = true;
354                     else throw new ClassConstraintException("A ClassFile structure (like '"+tostring(obj)+"') may have no more than one SourceFile attribute."); //vmspec2 4.7.7
355
}
356             
357                 if (atts[i] instanceof InnerClasses){
358                     if (foundInnerClasses == false) foundInnerClasses = true;
359                     else{
360                         if (hasInnerClass){
361                             throw new ClassConstraintException("A Classfile structure (like '"+tostring(obj)+"') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case). More than one InnerClasses attribute was found.");
362                         }
363                     }
364                     if (!hasInnerClass){
365                         addMessage("No referenced Inner Class found, but InnerClasses attribute '"+tostring(atts[i])+"' found. Strongly suggest removal of that attribute.");
366                     }
367                 }
368
369             }
370             if (hasInnerClass && !foundInnerClasses){
371                 //throw new ClassConstraintException("A Classfile structure (like '"+tostring(obj)+"') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case). No InnerClasses attribute was found.");
372
//vmspec2, page 125 says it would be a constraint: but existing verifiers
373
//don't check it and javac doesn't satisfy it when it comes to anonymous
374
//inner classes
375
addMessage("A Classfile structure (like '"+tostring(obj)+"') must have exactly one InnerClasses attribute if at least one Inner Class is referenced (which is the case). No InnerClasses attribute was found.");
376             }
377         }
378         /////////////////////////////
379
// CONSTANTS (vmspec2 4.4) //
380
/////////////////////////////
381
public void visitConstantClass(ConstantClass obj){
382             if (obj.getTag() != Constants.CONSTANT_Class){
383                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
384             }
385             checkIndex(obj, obj.getNameIndex(), CONST_Utf8);
386         
387         }
388         public void visitConstantFieldref(ConstantFieldref obj){
389             if (obj.getTag() != Constants.CONSTANT_Fieldref){
390                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
391             }
392             checkIndex(obj, obj.getClassIndex(), CONST_Class);
393             checkIndex(obj, obj.getNameAndTypeIndex(), CONST_NameAndType);
394         }
395         public void visitConstantMethodref(ConstantMethodref obj){
396             if (obj.getTag() != Constants.CONSTANT_Methodref){
397                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
398             }
399             checkIndex(obj, obj.getClassIndex(), CONST_Class);
400             checkIndex(obj, obj.getNameAndTypeIndex(), CONST_NameAndType);
401         }
402         public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj){
403             if (obj.getTag() != Constants.CONSTANT_InterfaceMethodref){
404                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
405             }
406             checkIndex(obj, obj.getClassIndex(), CONST_Class);
407             checkIndex(obj, obj.getNameAndTypeIndex(), CONST_NameAndType);
408         }
409         public void visitConstantString(ConstantString obj){
410             if (obj.getTag() != Constants.CONSTANT_String){
411                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
412             }
413             checkIndex(obj, obj.getStringIndex(), CONST_Utf8);
414         }
415         public void visitConstantInteger(ConstantInteger obj){
416             if (obj.getTag() != Constants.CONSTANT_Integer){
417                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
418             }
419             // no indices to check
420
}
421         public void visitConstantFloat(ConstantFloat obj){
422             if (obj.getTag() != Constants.CONSTANT_Float){
423                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
424             }
425             //no indices to check
426
}
427         public void visitConstantLong(ConstantLong obj){
428             if (obj.getTag() != Constants.CONSTANT_Long){
429                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
430             }
431             //no indices to check
432
}
433         public void visitConstantDouble(ConstantDouble obj){
434             if (obj.getTag() != Constants.CONSTANT_Double){
435                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
436             }
437             //no indices to check
438
}
439         public void visitConstantNameAndType(ConstantNameAndType obj){
440             if (obj.getTag() != Constants.CONSTANT_NameAndType){
441                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
442             }
443             checkIndex(obj, obj.getNameIndex(), CONST_Utf8);
444             //checkIndex(obj, obj.getDescriptorIndex(), CONST_Utf8); //inconsistently named in BCEL, see below.
445
checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8);
446         }
447         public void visitConstantUtf8(ConstantUtf8 obj){
448             if (obj.getTag() != Constants.CONSTANT_Utf8){
449                 throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
450             }
451             //no indices to check
452
}
453         //////////////////////////
454
// FIELDS (vmspec2 4.5) //
455
//////////////////////////
456
public void visitField(Field obj){
457
458             if (jc.isClass()){
459                 int maxone=0;
460                 if (obj.isPrivate()) maxone++;
461                 if (obj.isProtected()) maxone++;
462                 if (obj.isPublic()) maxone++;
463                 if (maxone > 1){
464                     throw new ClassConstraintException("Field '"+tostring(obj)+"' must only have at most one of its ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC modifiers set.");
465                 }
466             
467                 if (obj.isFinal() && obj.isVolatile()){
468                     throw new ClassConstraintException("Field '"+tostring(obj)+"' must only have at most one of its ACC_FINAL, ACC_VOLATILE modifiers set.");
469                 }
470             }
471             else{ // isInterface!
472
if (!obj.isPublic()){
473                     throw new ClassConstraintException("Interface field '"+tostring(obj)+"' must have the ACC_PUBLIC modifier set but hasn't!");
474                 }
475                 if (!obj.isStatic()){
476                     throw new ClassConstraintException("Interface field '"+tostring(obj)+"' must have the ACC_STATIC modifier set but hasn't!");
477                 }
478                 if (!obj.isFinal()){
479                     throw new ClassConstraintException("Interface field '"+tostring(obj)+"' must have the ACC_FINAL modifier set but hasn't!");
480                 }
481             }
482
483             if ((obj.getAccessFlags() & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_STATIC|ACC_FINAL|ACC_VOLATILE|ACC_TRANSIENT)) > 0){
484                 addMessage("Field '"+tostring(obj)+"' has access flag(s) other than ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT set (ignored).");
485             }
486
487             checkIndex(obj, obj.getNameIndex(), CONST_Utf8);
488             
489             String JavaDoc name = obj.getName();
490             if (! validFieldName(name)){
491                 throw new ClassConstraintException("Field '"+tostring(obj)+"' has illegal name '"+obj.getName()+"'.");
492             }
493
494             // A descriptor is often named signature in BCEL
495
checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8);
496             
497             String JavaDoc sig = ((ConstantUtf8) (cp.getConstant(obj.getSignatureIndex()))).getBytes(); // Field or Method signature(=descriptor)
498

499             try{
500                 Type t = Type.getType(sig);
501             }
502             catch (ClassFormatError JavaDoc cfe){ // sometimes BCEL is a little harsh describing exceptional situations.
503
throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.");
504             }
505             
506             String JavaDoc nameanddesc = (name+sig);
507             if (field_names_and_desc.contains(nameanddesc)){
508                 throw new ClassConstraintException("No two fields (like '"+tostring(obj)+"') are allowed have same names and descriptors!");
509             }
510             if (field_names.contains(name)){
511                 addMessage("More than one field of name '"+name+"' detected (but with different type descriptors). This is very unusual.");
512             }
513             field_names_and_desc.add(nameanddesc);
514             field_names.add(name);
515             
516             Attribute[] atts = obj.getAttributes();
517             for (int i=0; i<atts.length; i++){
518                 if ((! (atts[i] instanceof ConstantValue)) &&
519                     (! (atts[i] instanceof Synthetic)) &&
520                     (! (atts[i] instanceof Deprecated JavaDoc))){
521                     addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of Field '"+tostring(obj)+"' is unknown and will therefore be ignored.");
522                 }
523                 if (! (atts[i] instanceof ConstantValue)){
524                     addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of Field '"+tostring(obj)+"' is not a ConstantValue and is therefore only of use for debuggers and such.");
525                 }
526             }
527         }
528         ///////////////////////////
529
// METHODS (vmspec2 4.6) //
530
///////////////////////////
531
public void visitMethod(Method obj){
532
533             checkIndex(obj, obj.getNameIndex(), CONST_Utf8);
534             
535             String JavaDoc name = obj.getName();
536             if (! validMethodName(name, true)){
537                 throw new ClassConstraintException("Method '"+tostring(obj)+"' has illegal name '"+name+"'.");
538             }
539
540             // A descriptor is often named signature in BCEL
541
checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8);
542
543             String JavaDoc sig = ((ConstantUtf8) (cp.getConstant(obj.getSignatureIndex()))).getBytes(); // Method's signature(=descriptor)
544

545             Type t;
546             Type[] ts; // needed below the try block.
547
try{
548                 t = Type.getReturnType(sig);
549                 ts = Type.getArgumentTypes(sig);
550             }
551             catch (ClassFormatError JavaDoc cfe){
552                 // Well, BCEL sometimes is a little harsh describing exceptional situations.
553
throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by Method '"+tostring(obj)+"'.");
554             }
555
556             // Check if referenced objects exist.
557
Type act = t;
558             if (act instanceof ArrayType) act = ((ArrayType) act).getBasicType();
559             if (act instanceof ObjectType){
560                 Verifier v = VerifierFactory.getVerifier( ((ObjectType) act).getClassName() );
561                 VerificationResult vr = v.doPass1();
562                 if (vr != VerificationResult.VR_OK) {
563                     throw new ClassConstraintException("Method '"+tostring(obj)+"' has a return type that does not pass verification pass 1: '"+vr+"'.");
564                 }
565             }
566             
567             for (int i=0; i<ts.length; i++){
568                 act = ts[i];
569                 if (act instanceof ArrayType) act = ((ArrayType) act).getBasicType();
570                 if (act instanceof ObjectType){
571                     Verifier v = VerifierFactory.getVerifier( ((ObjectType) act).getClassName() );
572                     VerificationResult vr = v.doPass1();
573                     if (vr != VerificationResult.VR_OK) {
574                         throw new ClassConstraintException("Method '"+tostring(obj)+"' has an argument type that does not pass verification pass 1: '"+vr+"'.");
575                     }
576                 }
577             }
578
579             // Nearly forgot this! Funny return values are allowed, but a non-empty arguments list makes a different method out of it!
580
if (name.equals(STATIC_INITIALIZER_NAME) && (ts.length != 0)){
581                 throw new ClassConstraintException("Method '"+tostring(obj)+"' has illegal name '"+name+"'. It's name resembles the class or interface initialization method which it isn't because of its arguments (==descriptor).");
582             }
583
584             if (jc.isClass()){
585                 int maxone=0;
586                 if (obj.isPrivate()) maxone++;
587                 if (obj.isProtected()) maxone++;
588                 if (obj.isPublic()) maxone++;
589                 if (maxone > 1){
590                     throw new ClassConstraintException("Method '"+tostring(obj)+"' must only have at most one of its ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC modifiers set.");
591                 }
592             
593                 if (obj.isAbstract()){
594                     if (obj.isFinal()) throw new ClassConstraintException("Abstract method '"+tostring(obj)+"' must not have the ACC_FINAL modifier set.");
595                     if (obj.isNative()) throw new ClassConstraintException("Abstract method '"+tostring(obj)+"' must not have the ACC_NATIVE modifier set.");
596                     if (obj.isPrivate()) throw new ClassConstraintException("Abstract method '"+tostring(obj)+"' must not have the ACC_PRIVATE modifier set.");
597                     if (obj.isStatic()) throw new ClassConstraintException("Abstract method '"+tostring(obj)+"' must not have the ACC_STATIC modifier set.");
598                     if (obj.isStrictfp()) throw new ClassConstraintException("Abstract method '"+tostring(obj)+"' must not have the ACC_STRICT modifier set.");
599                     if (obj.isSynchronized()) throw new ClassConstraintException("Abstract method '"+tostring(obj)+"' must not have the ACC_SYNCHRONIZED modifier set.");
600                 }
601             }
602             else{ // isInterface!
603
if (!name.equals(STATIC_INITIALIZER_NAME)){//vmspec2, p.116, 2nd paragraph
604
if (!obj.isPublic()){
605                         throw new ClassConstraintException("Interface method '"+tostring(obj)+"' must have the ACC_PUBLIC modifier set but hasn't!");
606                     }
607                     if (!obj.isAbstract()){
608                         throw new ClassConstraintException("Interface method '"+tostring(obj)+"' must have the ACC_STATIC modifier set but hasn't!");
609                     }
610                     if ( obj.isPrivate() ||
611                                 obj.isProtected() ||
612                                 obj.isStatic() ||
613                                 obj.isFinal() ||
614                                 obj.isSynchronized() ||
615                                 obj.isNative() ||
616                                 obj.isStrictfp() ){
617                         throw new ClassConstraintException("Interface method '"+tostring(obj)+"' must not have any of the ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT, ACC_STRICT modifiers set.");
618                     }
619                 }
620             }
621
622             // A specific instance initialization method... (vmspec2,Page 116).
623
if (name.equals(CONSTRUCTOR_NAME)){
624                 //..may have at most one of ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC set: is checked above.
625
//..may also have ACC_STRICT set, but none of the other flags in table 4.5 (vmspec2, page 115)
626
if ( obj.isStatic() ||
627                             obj.isFinal() ||
628                             obj.isSynchronized() ||
629                             obj.isNative() ||
630                             obj.isAbstract() ){
631                     throw new ClassConstraintException("Instance initialization method '"+tostring(obj)+"' must not have any of the ACC_STATIC, ACC_FINAL, ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT modifiers set.");
632                 }
633             }
634
635             // Class and interface initialization methods...
636
if (name.equals(STATIC_INITIALIZER_NAME)){
637                 if ((obj.getAccessFlags() & (~ACC_STRICT)) > 0){
638                     addMessage("Class or interface initialization method '"+tostring(obj)+"' has superfluous access modifier(s) set: everything but ACC_STRICT is ignored.");
639                 }
640                 if (obj.isAbstract()){
641                     throw new ClassConstraintException("Class or interface initialization method '"+tostring(obj)+"' must not be abstract. This contradicts the Java Language Specification, Second Edition (which omits this constraint) but is common practice of existing verifiers.");
642                 }
643             }
644
645             if ((obj.getAccessFlags() & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_STATIC|ACC_FINAL|ACC_SYNCHRONIZED|ACC_NATIVE|ACC_ABSTRACT|ACC_STRICT)) > 0){
646                 addMessage("Method '"+tostring(obj)+"' has access flag(s) other than ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL, ACC_SYNCHRONIZED, ACC_NATIVE, ACC_ABSTRACT, ACC_STRICT set (ignored).");
647             }
648
649             String JavaDoc nameanddesc = (name+sig);
650             if (method_names_and_desc.contains(nameanddesc)){
651                 throw new ClassConstraintException("No two methods (like '"+tostring(obj)+"') are allowed have same names and desciptors!");
652             }
653             method_names_and_desc.add(nameanddesc);
654
655             Attribute[] atts = obj.getAttributes();
656             int num_code_atts = 0;
657             for (int i=0; i<atts.length; i++){
658                 if ((! (atts[i] instanceof Code)) &&
659                     (! (atts[i] instanceof ExceptionTable)) &&
660                     (! (atts[i] instanceof Synthetic)) &&
661                     (! (atts[i] instanceof Deprecated JavaDoc))){
662                     addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of Method '"+tostring(obj)+"' is unknown and will therefore be ignored.");
663                 }
664                 if ((! (atts[i] instanceof Code)) &&
665                         (! (atts[i] instanceof ExceptionTable))){
666                     addMessage("Attribute '"+tostring(atts[i])+"' as an attribute of Method '"+tostring(obj)+"' is neither Code nor Exceptions and is therefore only of use for debuggers and such.");
667                 }
668                 if ((atts[i] instanceof Code) && (obj.isNative() || obj.isAbstract())){
669                     throw new ClassConstraintException("Native or abstract methods like '"+tostring(obj)+"' must not have a Code attribute like '"+tostring(atts[i])+"'."); //vmspec2 page120, 4.7.3
670
}
671                 if (atts[i] instanceof Code) num_code_atts++;
672             }
673             if ( !obj.isNative() && !obj.isAbstract() && num_code_atts != 1){
674                 throw new ClassConstraintException("Non-native, non-abstract methods like '"+tostring(obj)+"' must have exactly one Code attribute (found: "+num_code_atts+").");
675             }
676         }
677         ///////////////////////////////////////////////////////
678
// ClassFile-structure-ATTRIBUTES (vmspec2 4.1, 4.7) //
679
///////////////////////////////////////////////////////
680
public void visitSourceFile(SourceFile obj){//vmspec2 4.7.7
681

682             // zero or one SourceFile attr per ClassFile: see visitJavaClass()
683

684             checkIndex(obj, obj.getNameIndex(), CONST_Utf8);
685             
686             String JavaDoc name = ((ConstantUtf8) cp.getConstant(obj.getNameIndex())).getBytes();
687             if (! name.equals("SourceFile")){
688   &nb