package magic.compiler;

import static org.junit.Assert.*;
import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;

import org.junit.Test;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.commons.Method;

public class TestASM {

	MyClassLoader cl = new MyClassLoader();

	private static final class MyClassLoader extends ClassLoader {

		public Class<?> define(byte[] bcode) {
			return defineClass(null, bcode, 0, bcode.length);
		}
	};

	@Test
	public void testClassCreation() {
		ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
		cw.visit(52, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, // access
				"magic/Test", // classname
				null, // signature, not needed unless generic?
				"java/lang/Object", // superclass
				new String[] {} // interfaces
		);

		{	// static field
			FieldVisitor fv=cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "FOO", "Ljava/lang/String;", null, new String("foo"));
			fv.visitEnd();
		}
		
		{	// no-arg constructor
				Method m = Method.getMethod("void <init> ()");
				GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
				mg.loadThis();
				mg.invokeConstructor(Type.getType(Object.class), m);
				mg.returnValue();
				mg.endMethod();
		}

		cw.visitEnd();

		byte[] bcode = cw.toByteArray();

		Class<?> klass = cl.define(bcode);

		assertNotNull(klass);

		Object o;
		try {
			o = klass.newInstance();
		} catch (InstantiationException e) {
			throw new Error(e);
		} catch (IllegalAccessException e) {
			throw new Error(e);
		}
		assertNotNull(o);
		
		String s;
		try {
			s=(String) klass.getField("FOO").get(null);
		} catch (Throwable t) {
			throw new Error(t);
		}
		assertEquals("foo",s);
	}
}

@@ -1114,16 +1114,243 @@
 d();%0A%09%09%7D
+%0A%0A%09%09%7B%09// public final field%0A%09%09%09// note that initial value is ignored for non-static fields%0A%09%09%09FieldVisitor fv=cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, %22bar%22, %22Ljava/lang/String;%22, null, null);%0A%09%09%09fv.visitEnd();%0A%09%09%7D
 %0A%09%09%0A%09%09%7B%09
@@ -1585,16 +1585,161 @@
 s), m);%0A
+%09%0A%09%09%09%09// set a field%0A%09%09%09%09mg.loadThis();%0A%09%09%09%09mg.push(%22bar%22);%0A%09%09%09%09mg.putField(Type.getType(%22magic/Test%22), %22bar%22, Type.getType(String.class));%0A%09%09%09%09%0A
 %09%09%09%09mg.r
@@ -1776,17 +1776,16 @@
 ();%0A%09%09%7D%0A
-%0A
 %09%09cw.vis
@@ -1794,16 +1794,38 @@
 End();%0A%0A
+%09%09// Create the class%0A
 %09%09byte%5B%5D
@@ -1851,17 +1851,16 @@
 rray();%0A
-%0A
 %09%09Class%3C
@@ -2229,24 +2229,24 @@
 ror(t);%0A%09%09%7D%0A
-
 %09%09assertEqua
@@ -2258,13 +2258,146 @@
 oo%22,s);%0A
+%09%09%0A%09%09try %7B%0A%09%09%09s=(String) klass.getField(%22bar%22).get(o);%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09throw new Error(t);%0A%09%09%7D%0A%09%09assertEquals(%22bar%22,s);%0A
 %09%7D%0A%7D%0A

Test edits