1 // ASM: a very small and fast Java bytecode manipulation framework 2 // Copyright (c) 2000-2011 INRIA, France Telecom 3 // All rights reserved. 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions 7 // are met: 8 // 1. Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // 2. Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // 3. Neither the name of the copyright holders nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 // THE POSSIBILITY OF SUCH DAMAGE. 28 package jdk8; 29 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import org.objectweb.asm.ClassWriter; 33 import org.objectweb.asm.MethodVisitor; 34 import org.objectweb.asm.Label; 35 import org.objectweb.asm.Opcodes; 36 import org.objectweb.asm.Type; 37 38 /** 39 * Generates a class with structures, instructions and patterns that cannot be produced by compiling 40 * a Java source file with the javac compiler. This includes LDC instructions with MethodType 41 * constants. 42 * 43 * <p>Ideally we should not use ASM to generate this class (which is later used to test ASM), but 44 * this would be hard to do. 45 * 46 * @author Eric Bruneton 47 */ 48 public class DumpArtificialStructures implements Opcodes { 49 main(String[] args)50 public static void main(String[] args) throws IOException { 51 FileOutputStream fileOutputStream = new FileOutputStream("Artificial$()$Structures.class"); 52 fileOutputStream.write(dump()); 53 fileOutputStream.close(); 54 } 55 dump()56 private static byte[] dump() { 57 ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 58 MethodVisitor methodVisitor; 59 60 classWriter.visit( 61 V1_8, 62 ACC_PUBLIC + ACC_SUPER, 63 "jdk8/Artificial$()$Structures", 64 null, 65 "java/lang/Object", 66 null); 67 68 classWriter.visitField(ACC_PUBLIC, "value", "I", null, null).visitEnd(); 69 70 methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "(I)V", null, null); 71 methodVisitor.visitCode(); 72 methodVisitor.visitVarInsn(ALOAD, 0); 73 methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 74 methodVisitor.visitVarInsn(ALOAD, 0); 75 methodVisitor.visitVarInsn(ILOAD, 1); 76 methodVisitor.visitFieldInsn(PUTFIELD, "jdk8/Artificial$()$Structures", "value", "I"); 77 methodVisitor.visitInsn(RETURN); 78 methodVisitor.visitMaxs(0, 0); 79 methodVisitor.visitEnd(); 80 81 methodVisitor = 82 classWriter.visitMethod( 83 ACC_PRIVATE, "<init>", "(Ljdk8/Artificial$()$Structures;)V", null, null); 84 methodVisitor.visitCode(); 85 methodVisitor.visitVarInsn(ALOAD, 0); 86 methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 87 methodVisitor.visitVarInsn(ALOAD, 0); 88 methodVisitor.visitVarInsn(ALOAD, 1); 89 Label elseLabel = new Label(); 90 methodVisitor.visitJumpInsn(IFNULL, elseLabel); 91 methodVisitor.visitVarInsn(ALOAD, 1); 92 methodVisitor.visitFieldInsn(GETFIELD, "jdk8/Artificial$()$Structures", "value", "I"); 93 Label endIfLabel = new Label(); 94 methodVisitor.visitJumpInsn(GOTO, endIfLabel); 95 methodVisitor.visitLabel(elseLabel); 96 methodVisitor.visitInsn(ICONST_0); 97 methodVisitor.visitLabel(endIfLabel); 98 methodVisitor.visitFieldInsn(PUTFIELD, "jdk8/Artificial$()$Structures", "value", "I"); 99 methodVisitor.visitInsn(RETURN); 100 methodVisitor.visitMaxs(0, 0); 101 methodVisitor.visitEnd(); 102 103 methodVisitor = 104 classWriter.visitMethod( 105 ACC_PUBLIC, "clone", "()Ljdk8/Artificial$()$Structures;", null, null); 106 methodVisitor.visitCode(); 107 methodVisitor.visitTypeInsn(NEW, "jdk8/Artificial$()$Structures"); 108 methodVisitor.visitInsn(DUP); 109 methodVisitor.visitVarInsn(ALOAD, 0); 110 methodVisitor.visitMethodInsn( 111 INVOKESPECIAL, 112 "jdk8/Artificial$()$Structures", 113 "<init>", 114 "(Ljdk8/Artificial$()$Structures;)V", 115 false); 116 methodVisitor.visitInsn(ARETURN); 117 methodVisitor.visitMaxs(0, 0); 118 methodVisitor.visitEnd(); 119 120 methodVisitor = 121 classWriter.visitMethod( 122 ACC_PUBLIC + ACC_STATIC, "methodType", "()Ljava/lang/invoke/MethodType;", null, null); 123 methodVisitor.visitCode(); 124 methodVisitor.visitLdcInsn(Type.getMethodType("(I)I")); 125 methodVisitor.visitInsn(ARETURN); 126 methodVisitor.visitMaxs(0, 0); 127 methodVisitor.visitEnd(); 128 129 classWriter.visitEnd(); 130 return classWriter.toByteArray(); 131 } 132 } 133