View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel;
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  
28  import org.apache.bcel.generic.ArrayType;
29  import org.apache.bcel.generic.ClassGen;
30  import org.apache.bcel.generic.ConstantPoolGen;
31  import org.apache.bcel.generic.InstructionFactory;
32  import org.apache.bcel.generic.InstructionList;
33  import org.apache.bcel.generic.MethodGen;
34  import org.apache.bcel.generic.Type;
35  
36  public class HelloWorldCreator {
37      private static final String ORG_APACHE_BCEL_HELLO_WORLD = "org.apache.bcel.HelloWorld";
38      public static void main(final String[] args) throws Exception {
39          final org.apache.bcel.HelloWorldCreator creator = new org.apache.bcel.HelloWorldCreator();
40          final Path path = Paths.get("target/test-classes/org/apache/bcel/HelloWorld.class");
41          Files.deleteIfExists(path);
42          try (OutputStream out = Files.newOutputStream(path)) {
43              creator.create(out);
44          }
45      }
46      private final InstructionFactory factory;
47      private final ConstantPoolGen cp;
48  
49      private final ClassGen cg;
50  
51      public HelloWorldCreator() {
52          cg = new ClassGen(ORG_APACHE_BCEL_HELLO_WORLD, "java.lang.Object", "HelloWorld.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {});
53          cg.setMajor(Const.MAJOR_1_8);
54          cg.setMinor(Const.MINOR_1_8);
55  
56          cp = cg.getConstantPool();
57          factory = new InstructionFactory(cg, cp);
58      }
59  
60      public void create(final OutputStream out) throws IOException {
61          createConstructor();
62          createMainMethod();
63          cg.getJavaClass().dump(out);
64      }
65  
66      private void createConstructor() {
67          final InstructionList il = new InstructionList();
68          final MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", ORG_APACHE_BCEL_HELLO_WORLD, il, cp);
69  
70          il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
71          il.append(factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
72          il.append(InstructionFactory.createReturn(Type.VOID));
73          method.setMaxStack();
74          method.setMaxLocals();
75          cg.addMethod(method.getMethod());
76          il.dispose();
77      }
78  
79      private void createMainMethod() {
80          final InstructionList il = factory.createPrintln("Hello World!");
81          final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { new ArrayType(Type.STRING, 1) }, new String[] { "arg0" }, "main",
82                  ORG_APACHE_BCEL_HELLO_WORLD, il, cp);
83  
84          il.append(InstructionFactory.createReturn(Type.VOID));
85          method.setMaxStack();
86          method.setMaxLocals();
87          cg.addMethod(method.getMethod());
88          il.dispose();
89      }
90  }