package magic.compiler;

import org.junit.Test;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

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

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
		);
		
		cw.visitEnd();
		
		byte[] bcode=cw.toByteArray();
		
		Class<?> klass=cl.define(bcode);
		
		assertNotNull(klass);
	}
}

@@ -18,16 +18,123 @@
 piler;%0A%0A
+import static org.junit.Assert.assertNotNull;%0Aimport static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;%0A%0A
 import o
@@ -224,41 +224,88 @@
 es;%0A
-%0A
 import 
-static org.junit.Assert.*
+org.objectweb.asm.Type;%0Aimport org.objectweb.asm.commons.GeneratorAdapter
 ;%0Aim
@@ -301,39 +301,32 @@
 Adapter;%0Aimport 
-static 
 org.objectweb.as
@@ -331,21 +331,22 @@
 asm.
-ClassWriter.*
+commons.Method
 ;%0A%0Ap
@@ -385,17 +385,19 @@
 oader cl
-=
+ = 
 new MyCl
@@ -411,12 +411,10 @@
 er()
- 
 ;%0A
-%09
 %0A%09pr
@@ -474,18 +474,16 @@
 oader %7B%0A
-%09%09
 %0A%09%09publi
@@ -543,16 +543,17 @@
 ss(null,
+ 
 bcode, 0
@@ -581,17 +581,17 @@
 %09%7D;%0A
-%09
 %0A%09@Test
- 
+%0A%09
 publ
@@ -686,21 +686,17 @@
 isit(52,
-%0A%09%09%09%09
+ 
 Opcodes.
@@ -726,17 +726,16 @@
 C_SUPER,
- 
  // acce
@@ -759,92 +759,30 @@
 t%22, 
-                           // classname%0A%09%09%09%09null,                                   
+// classname%0A%09%09%09%09null,
  // 
@@ -822,20 +822,17 @@
 ric?%0A%09%09%09
-    
+%09
 %22java/la
@@ -842,37 +842,16 @@
 Object%22,
-                     
  // supe
@@ -860,20 +860,17 @@
 lass%0A%09%09%09
-    
+%09
 new Stri
@@ -880,33 +880,8 @@
 %5D %7B%7D
-                         
  // 
@@ -896,21 +896,309 @@
 es%0A%09%09);%0A
+%0A
 %09%09%0A%09%09
+%7B%09// no-arg constructor%0A%09%09%09%09Method m = Method.getMethod(%22void %3Cinit%3E ()%22);%0A%09%09%09%09GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);%0A%09%09%09%09mg.loadThis();%0A%09%09%09%09mg.invokeConstructor(Type.getType(Object.class), m);%0A%09%09%09%09mg.returnValue();%0A%09%09%09%09mg.endMethod();%0A%09%09%7D%0A%0A%09%09
 cw.visit
@@ -1204,18 +1204,16 @@
 tEnd();%0A
-%09%09
 %0A%09%09byte%5B
@@ -1219,17 +1219,19 @@
 %5B%5D bcode
-=
+ = 
 cw.toByt
@@ -1240,18 +1240,16 @@
 rray();%0A
-%09%09
 %0A%09%09Class
@@ -1257,17 +1257,19 @@
 ?%3E klass
-=
+ = 
 cl.defin
@@ -1278,18 +1278,16 @@
 bcode);%0A
-%09%09
 %0A%09%09asser
@@ -1302,14 +1302,211 @@
 (klass);
+%0A%0A%09%09Object o;%0A%09%09try %7B%0A%09%09%09o = klass.newInstance();%0A%09%09%7D catch (InstantiationException e) %7B%0A%09%09%09throw new Error(e);%0A%09%09%7D catch (IllegalAccessException e) %7B%0A%09%09%09throw new Error(e);%0A%09%09%7D%0A%09%09assertNotNull(o);
 %0A%09%7D%0A%7D%0A

Improve ASM example with default constructor