1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 
19 import org.apache.bcel.Repository;
20 import org.apache.bcel.classfile.ClassParser;
21 import org.apache.bcel.classfile.Field;
22 import org.apache.bcel.classfile.JavaClass;
23 import org.apache.bcel.classfile.Method;
24 import org.apache.bcel.generic.ClassGen;
25 import org.apache.bcel.generic.FieldGen;
26 import org.apache.bcel.generic.MethodGen;
27 
28 /**
29  * Test BCEL if an input file is identical to the outfile generated
30  * with BCEL. Of course there may some small differences, e.g., because
31  * BCEL generates local variable tables by default.
32  *
33  * Try to:
34  * <pre>
35  * % java id <someclass>
36  * % java listclass -code <someclass> &gt; foo
37  * % java listclass -code <someclass>.clazz &gt; bar
38  * % diff foo bar | more
39  * <pre>
40  *
41  * @version $Id$
42  */
43 public class id {
44 
main(String[] argv)45     public static void main(String[] argv) throws Exception {
46         JavaClass clazz;
47 
48         if ((clazz = Repository.lookupClass(argv[0])) == null) {
49             clazz = new ClassParser(argv[0]).parse(); // May throw IOException
50         }
51 
52         ClassGen cg = new ClassGen(clazz);
53 
54         for (Method method : clazz.getMethods()) {
55             MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool());
56             cg.replaceMethod(method, mg.getMethod());
57         }
58 
59         for (Field field : clazz.getFields()) {
60             FieldGen fg = new FieldGen(field, cg.getConstantPool());
61             cg.replaceField(field, fg.getField());
62         }
63 
64         cg.getJavaClass().dump(clazz.getClassName() + ".clazz");
65     }
66 }
67