View Javadoc

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  package org.apache.commons.jci.classes;
19  
20  import org.objectweb.asm.ClassWriter;
21  import org.objectweb.asm.Label;
22  import org.objectweb.asm.MethodVisitor;
23  import org.objectweb.asm.Opcodes;
24  
25  
26  public class ExtendedDump implements Opcodes {
27  
28      public static byte[] dump() throws Exception {
29  
30          ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
31          MethodVisitor mv;
32  
33          cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, "jci/Extended", null, "jci/Simple", null);
34  
35          cw.visitSource("Extended.java", null);
36  
37          {
38              mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
39              mv.visitCode();
40              Label l0 = new Label();
41              mv.visitLabel(l0);
42              mv.visitLineNumber(3, l0);
43              mv.visitVarInsn(ALOAD, 0);
44              mv.visitMethodInsn(INVOKESPECIAL, "jci/Simple", "<init>", "()V");
45              mv.visitInsn(RETURN);
46              Label l1 = new Label();
47              mv.visitLabel(l1);
48              mv.visitLocalVariable("this", "Ljci/Extended;", null, l0, l1, 0);
49              mv.visitMaxs(1, 1);
50              mv.visitEnd();
51          }
52          {
53              mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
54              mv.visitCode();
55              Label l0 = new Label();
56              mv.visitLabel(l0);
57              mv.visitLineNumber(6, l0);
58              mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
59              mv.visitInsn(DUP);
60              mv.visitLdcInsn("Extended:");
61              mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
62              mv.visitVarInsn(ALOAD, 0);
63              mv.visitMethodInsn(INVOKESPECIAL, "jci/Simple", "toString", "()Ljava/lang/String;");
64              mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
65              mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
66              mv.visitInsn(ARETURN);
67              Label l1 = new Label();
68              mv.visitLabel(l1);
69              mv.visitLocalVariable("this", "Ljci/Extended;", null, l0, l1, 0);
70              mv.visitMaxs(3, 1);
71              mv.visitEnd();
72          }
73          cw.visitEnd();
74  
75          return cw.toByteArray();
76      }
77  }