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.bcel;
19  
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  
26  import org.apache.bcel.generic.ArrayType;
27  import org.apache.bcel.generic.ClassGen;
28  import org.apache.bcel.generic.ConstantPoolGen;
29  import org.apache.bcel.generic.InstructionFactory;
30  import org.apache.bcel.generic.InstructionList;
31  import org.apache.bcel.generic.MethodGen;
32  import org.apache.bcel.generic.Type;
33  
34  public class HelloWorldCreator {
35      private static final String ORG_APACHE_BCEL_HELLO_WORLD = "org.apache.bcel.HelloWorld";
36      public static void main(final String[] args) throws Exception {
37          final org.apache.bcel.HelloWorldCreator creator = new org.apache.bcel.HelloWorldCreator();
38          final Path path = Paths.get("target/test-classes/org/apache/bcel/HelloWorld.class");
39          Files.deleteIfExists(path);
40          try (OutputStream out = Files.newOutputStream(path)) {
41              creator.create(out);
42          }
43      }
44      private final InstructionFactory factory;
45      private final ConstantPoolGen cp;
46  
47      private final ClassGen cg;
48  
49      public HelloWorldCreator() {
50          cg = new ClassGen(ORG_APACHE_BCEL_HELLO_WORLD, "java.lang.Object", "HelloWorld.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {});
51          cg.setMajor(Const.MAJOR_1_8);
52          cg.setMinor(Const.MINOR_1_8);
53  
54          cp = cg.getConstantPool();
55          factory = new InstructionFactory(cg, cp);
56      }
57  
58      public void create(final OutputStream out) throws IOException {
59          createConstructor();
60          createMainMethod();
61          cg.getJavaClass().dump(out);
62      }
63  
64      private void createConstructor() {
65          final InstructionList il = new InstructionList();
66          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", ORG_APACHE_BCEL_HELLO_WORLD, il, cp);
67  
68          il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
69          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
70          il.append(InstructionFactory.createReturn(Type.VOID));
71          method.setMaxStack();
72          method.setMaxLocals();
73          cg.addMethod(method.getMethod());
74          il.dispose();
75      }
76  
77      private void createMainMethod() {
78          final InstructionList il = factory.createPrintln("Hello World!");
79          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { new ArrayType(Type.STRING, 1) }, new String[] { "arg0" }, "main",
80                  ORG_APACHE_BCEL_HELLO_WORLD, il, cp);
81  
82          il.append(InstructionFactory.createReturn(Type.VOID));
83          method.setMaxStack();
84          method.setMaxLocals();
85          cg.addMethod(method.getMethod());
86          il.dispose();
87      }
88  }