1 package org.simantics.scl.compiler.tests.experimentation;
\r
3 import java.io.PrintWriter;
\r
4 import java.lang.reflect.Method;
\r
5 import java.util.concurrent.atomic.AtomicReference;
\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
13 public class RecursiveInitialization {
\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
23 methodWriter.visitCode();
\r
24 methodWriter.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");
\r
25 methodWriter.visitVarInsn(Opcodes.ASTORE, 2);
\r
27 methodWriter.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference");
\r
28 methodWriter.visitVarInsn(Opcodes.ASTORE, 1);
\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
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
39 methodWriter.visitVarInsn(Opcodes.ALOAD, 2);
\r
40 methodWriter.visitInsn(Opcodes.ARETURN);
\r
41 methodWriter.visitMaxs(0, 0);
\r
42 methodWriter.visitEnd();
\r
44 byte[] bytes = classWriter.toByteArray();
\r
45 ClassLoader classLoader = new ClassLoader() {
\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
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