Add unit test for reflective class loading
This commit is contained in:
parent
373156eedd
commit
7aee354eb2
@ -23,5 +23,11 @@
|
|||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.ow2.asm</groupId>
|
||||||
|
<artifactId>asm-all</artifactId>
|
||||||
|
<version>5.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -3,10 +3,17 @@ package eu.mikroskeem.utils.test.reflect;
|
|||||||
import eu.mikroskeem.utils.reflect.Reflect;
|
import eu.mikroskeem.utils.reflect.Reflect;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.FieldVisitor;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
import org.objectweb.asm.Type;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import static org.objectweb.asm.Opcodes.*;
|
||||||
|
import static org.objectweb.asm.Opcodes.RETURN;
|
||||||
|
|
||||||
public class TestReflect {
|
public class TestReflect {
|
||||||
@Test
|
@Test
|
||||||
public void testGetClass(){
|
public void testGetClass(){
|
||||||
@ -75,4 +82,34 @@ public class TestReflect {
|
|||||||
Reflect.simpleWriteField(testClass, "FOOBAR", "yeah");
|
Reflect.simpleWriteField(testClass, "FOOBAR", "yeah");
|
||||||
Assert.assertEquals("yeah", Reflect.simpleReadField(testClass, "FOOBAR"));
|
Assert.assertEquals("yeah", Reflect.simpleReadField(testClass, "FOOBAR"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClassLoading(){
|
||||||
|
String className = "eu.mikroskeem.utils.test.reflect.GeneratedTestClass";
|
||||||
|
String classNameInternal = className.replaceAll("\\.", "/");
|
||||||
|
|
||||||
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
|
||||||
|
cw.visit(
|
||||||
|
V1_8,
|
||||||
|
ACC_PUBLIC + ACC_SUPER,
|
||||||
|
classNameInternal,
|
||||||
|
null,
|
||||||
|
Type.getInternalName(Object.class),
|
||||||
|
null);
|
||||||
|
FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey");
|
||||||
|
fv.visitEnd();
|
||||||
|
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
|
||||||
|
mv.visitVarInsn(ALOAD, 0);
|
||||||
|
mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false);
|
||||||
|
mv.visitInsn(RETURN);
|
||||||
|
mv.visitMaxs(0, 0);
|
||||||
|
mv.visitEnd();
|
||||||
|
cw.visitEnd();
|
||||||
|
|
||||||
|
Class<?> generated = Reflect.defineClass(ClassLoader.getSystemClassLoader(), className, cw.toByteArray());
|
||||||
|
Field testField = Reflect.getField(generated, "test");
|
||||||
|
String testFieldContent = (String) Reflect.readField(testField, null);
|
||||||
|
Assert.assertTrue(Reflect.classExists(className));
|
||||||
|
Assert.assertEquals("hey", testFieldContent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user