<commit_before>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);
	}<commit_msg>Improve ASM example with default constructor<commit_after>package magic.compiler;

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

import org.junit.Test;
import org.objectweb.asm.ClassWriter;
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
		);

		
		{	// 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);
	}
{'fraction_non_alphanumeric': 0.12229938271604938, 'fraction_numerical': 0.0023148148148148147, 'mean_word_length': 3.1890145395799676, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '240010', 'n_tokens_mistral': 933, 'n_tokens_neox': 815, 'n_words': 184}