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  public class SimpleDump implements Opcodes {
26  
27      public static byte[] dump( final String to ) throws Exception {
28  
29          ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
30          MethodVisitor mv;
31  
32          cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, "jci/Simple", null, "java/lang/Object", null);
33  
34          cw.visitSource("Simple.java", null);
35  
36          {
37              mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
38              mv.visitCode();
39              Label l0 = new Label();
40              mv.visitLabel(l0);
41              mv.visitLineNumber(3, l0);
42              mv.visitVarInsn(ALOAD, 0);
43              mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
44              mv.visitInsn(RETURN);
45              Label l1 = new Label();
46              mv.visitLabel(l1);
47              mv.visitLocalVariable("this", "Ljci/Simple;", null, l0, l1, 0);
48              mv.visitMaxs(1, 1);
49              mv.visitEnd();
50          }
51          {
52              mv = cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
53              mv.visitCode();
54              Label l0 = new Label();
55              mv.visitLabel(l0);
56              mv.visitLineNumber(6, l0);
57              mv.visitLdcInsn(to);
58              mv.visitInsn(ARETURN);
59              Label l1 = new Label();
60              mv.visitLabel(l1);
61              mv.visitLocalVariable("this", "Ljci/Simple;", null, l0, l1, 0);
62              mv.visitMaxs(1, 1);
63              mv.visitEnd();
64          }
65          cw.visitEnd();
66  
67          return cw.toByteArray();
68      }
69  }