Sagar2018

Helloworld.java

Apr 6th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.04 KB | None | 0 0
  1. /***
  2.  * ASM examples: examples showing how ASM can be used
  3.  * Copyright (c) 2000-2011 INRIA, France Telecom
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. Neither the name of the copyright holders nor the names of its
  15.  *    contributors may be used to endorse or promote products derived from
  16.  *    this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  28.  * THE POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30. import java.io.FileOutputStream;
  31. import java.io.PrintStream;
  32.  
  33. import org.objectweb.asm.ClassWriter;
  34. import org.objectweb.asm.MethodVisitor;
  35. import org.objectweb.asm.Opcodes;
  36. import org.objectweb.asm.Type;
  37. import org.objectweb.asm.commons.GeneratorAdapter;
  38. import org.objectweb.asm.commons.Method;
  39.  
  40. /**
  41.  * @author Eric Bruneton
  42.  */
  43. public class Helloworld extends ClassLoader implements Opcodes {
  44.  
  45.     public static void main(final String args[]) throws Exception {
  46.         // Generates the bytecode corresponding to the following Java class:
  47.         //
  48.         // public class Example {
  49.         // public static void main (String[] args) {
  50.         // System.out.println("Hello world!");
  51.         // }
  52.         // }
  53.  
  54.         // creates a ClassWriter for the Example public class,
  55.         // which inherits from Object
  56.         ClassWriter cw = new ClassWriter(0);
  57.         cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
  58.  
  59.         // creates a MethodWriter for the (implicit) constructor
  60.         MethodVisitor mw = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null,
  61.                 null);
  62.         // pushes the 'this' variable
  63.         mw.visitVarInsn(ALOAD, 0);
  64.         // invokes the super class constructor
  65.         mw.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V",
  66.                 false);
  67.         mw.visitInsn(RETURN);
  68.         // this code uses a maximum of one stack element and one local variable
  69.         mw.visitMaxs(1, 1);
  70.         mw.visitEnd();
  71.  
  72.         // creates a MethodWriter for the 'main' method
  73.         mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main",
  74.                 "([Ljava/lang/String;)V", null, null);
  75.         // pushes the 'out' field (of type PrintStream) of the System class
  76.         mw.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
  77.                 "Ljava/io/PrintStream;");
  78.         // pushes the "Hello World!" String constant
  79.         mw.visitLdcInsn("Hello world!");
  80.         // invokes the 'println' method (defined in the PrintStream class)
  81.         mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
  82.                 "(Ljava/lang/String;)V", false);
  83.         mw.visitInsn(RETURN);
  84.         // this code uses a maximum of two stack elements and two local
  85.         // variables
  86.         mw.visitMaxs(2, 2);
  87.         mw.visitEnd();
  88.  
  89.         // gets the bytecode of the Example class, and loads it dynamically
  90.         byte[] code = cw.toByteArray();
  91.  
  92.         FileOutputStream fos = new FileOutputStream("Example.class");
  93.         fos.write(code);
  94.         fos.close();
  95.  
  96.         Helloworld loader = new Helloworld();
  97.         Class<?> exampleClass = loader.defineClass("Example", code, 0,
  98.                 code.length);
  99.  
  100.         // uses the dynamically generated class to print 'Helloworld'
  101.         exampleClass.getMethods()[0].invoke(null, new Object[] { null });
  102.  
  103.         // ------------------------------------------------------------------------
  104.         // Same example with a GeneratorAdapter (more convenient but slower)
  105.         // ------------------------------------------------------------------------
  106.  
  107.         cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
  108.         cw.visit(V1_1, ACC_PUBLIC, "Example", null, "java/lang/Object", null);
  109.  
  110.         // creates a GeneratorAdapter for the (implicit) constructor
  111.         Method m = Method.getMethod("void <init> ()");
  112.         GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, m, null, null,
  113.                 cw);
  114.         mg.loadThis();
  115.         mg.invokeConstructor(Type.getType(Object.class), m);
  116.         mg.returnValue();
  117.         mg.endMethod();
  118.  
  119.         // creates a GeneratorAdapter for the 'main' method
  120.         m = Method.getMethod("void main (String[])");
  121.         mg = new GeneratorAdapter(ACC_PUBLIC + ACC_STATIC, m, null, null, cw);
  122.         mg.getStatic(Type.getType(System.class), "out",
  123.                 Type.getType(PrintStream.class));
  124.         mg.push("Hello world!");
  125.         mg.invokeVirtual(Type.getType(PrintStream.class),
  126.                 Method.getMethod("void println (String)"));
  127.         mg.returnValue();
  128.         mg.endMethod();
  129.  
  130.         cw.visitEnd();
  131.  
  132.         code = cw.toByteArray();
  133.         loader = new Helloworld();
  134.         exampleClass = loader.defineClass("Example", code, 0, code.length);
  135.  
  136.         // uses the dynamically generated class to print 'Helloworld'
  137.         exampleClass.getMethods()[0].invoke(null, new Object[] { null });
  138.     }
  139. }
Add Comment
Please, Sign In to add comment