]> gerrit.simantics Code Review - simantics/platform.git/blob
5f5d20ddcfb907fb3a033156aac136f54d36d614
[simantics/platform.git] /
1 package org.simantics.scl.compiler.tests.experimentation;\r
2 \r
3 import java.io.PrintWriter;\r
4 import java.lang.reflect.Method;\r
5 import java.util.concurrent.atomic.AtomicReference;\r
6 \r
7 import org.objectweb.asm.ClassVisitor;\r
8 import org.objectweb.asm.ClassWriter;\r
9 import org.objectweb.asm.MethodVisitor;\r
10 import org.objectweb.asm.Opcodes;\r
11 import org.objectweb.asm.util.TraceClassVisitor;\r
12 \r
13 public class RecursiveInitialization {\r
14 \r
15     public static void main(String[] args) throws Exception {\r
16         ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);\r
17         ClassVisitor classVisitor = new TraceClassVisitor(classWriter, new PrintWriter(System.out));\r
18         classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "test/Test", null, "java/lang/Object", new String[0]);\r
19         MethodVisitor methodWriter = classVisitor.visitMethod(\r
20                 Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,\r
21                 "main", "()Ljava/lang/Object;", null, null);\r
22         \r
23         methodWriter.visitCode();        \r
24         methodWriter.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");\r
25         methodWriter.visitVarInsn(Opcodes.ASTORE, 2);\r
26 \r
27         methodWriter.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");\r
28         methodWriter.visitVarInsn(Opcodes.ASTORE, 1);\r
29         \r
30         methodWriter.visitVarInsn(Opcodes.ALOAD, 2);\r
31         methodWriter.visitVarInsn(Opcodes.ALOAD, 1);\r
32         //methodWriter.visitInsn(Opcodes.ACONST_NULL);\r
33         methodWriter.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicReference", "<init>", "(Ljava/lang/Object;)V", false);\r
34         \r
35         methodWriter.visitVarInsn(Opcodes.ALOAD, 1);\r
36         methodWriter.visitVarInsn(Opcodes.ALOAD, 2);\r
37         methodWriter.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicReference", "<init>", "(Ljava/lang/Object;)V", false);\r
38 \r
39         methodWriter.visitVarInsn(Opcodes.ALOAD, 2);\r
40         methodWriter.visitInsn(Opcodes.ARETURN);\r
41         methodWriter.visitMaxs(0, 0);\r
42         methodWriter.visitEnd();\r
43         \r
44         byte[] bytes = classWriter.toByteArray();\r
45         ClassLoader classLoader = new ClassLoader() {\r
46             @Override\r
47             protected Class<?> findClass(String name) throws ClassNotFoundException {\r
48                 if(!name.equals("test.Test"))\r
49                     throw new ClassNotFoundException();\r
50                 return defineClass("test.Test", bytes, 0, bytes.length);\r
51             }\r
52         };\r
53         Class<?> clazz = classLoader.loadClass("test.Test");\r
54         Method method = clazz.getMethod("main");\r
55         AtomicReference<Object> result = (AtomicReference<Object>)method.invoke(null);\r
56         System.out.println(result.hashCode());\r
57         //System.out.println(result.get().hashCode());\r
58     }\r
59     \r
60 }\r