2018-05-02 18:54:07 +00:00
|
|
|
package bytecode;
|
|
|
|
|
2018-07-18 15:01:01 +00:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
2018-05-02 18:54:07 +00:00
|
|
|
import java.io.File;
|
2018-07-18 15:01:01 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
2018-05-02 18:54:07 +00:00
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import de.dhbwstuttgart.core.JavaTXCompiler;
|
|
|
|
|
|
|
|
public class LambdaTest {
|
|
|
|
private static String path;
|
|
|
|
private static File fileToTest;
|
|
|
|
private static JavaTXCompiler compiler;
|
2018-07-18 15:01:01 +00:00
|
|
|
private static ClassLoader loader;
|
|
|
|
private static Class<?> classToTest;
|
|
|
|
private static String pathToClassFile;
|
|
|
|
private static Object instanceOfClass;
|
2018-05-02 18:54:07 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void generateBC() throws Exception {
|
|
|
|
path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Lambda.jav";
|
|
|
|
fileToTest = new File(path);
|
|
|
|
compiler = new JavaTXCompiler(fileToTest);
|
2018-06-20 16:07:17 +00:00
|
|
|
compiler.generateBytecode(System.getProperty("user.dir")+"/testBytecode/generatedBC/");
|
2018-07-18 15:01:01 +00:00
|
|
|
pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/";
|
|
|
|
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
|
|
|
|
classToTest = loader.loadClass("Lambda");
|
|
|
|
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
|
2018-07-18 20:49:21 +00:00
|
|
|
|
2018-07-18 15:01:01 +00:00
|
|
|
Method m = classToTest.getDeclaredMethod("m");
|
2018-07-18 15:54:44 +00:00
|
|
|
Class<?> lambda = m.invoke(instanceOfClass).getClass();
|
|
|
|
Method apply = lambda.getMethod("apply", Object.class);
|
2018-07-18 20:49:21 +00:00
|
|
|
apply.setAccessible(true);
|
|
|
|
|
|
|
|
Integer i = 77;
|
|
|
|
// result = 77*77 = 5929
|
2018-07-19 11:54:31 +00:00
|
|
|
Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i);
|
2018-07-18 20:49:21 +00:00
|
|
|
assertEquals(5929, result);
|
2018-05-02 18:54:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|