modified: ../../../java/bytecode/InheritTest.java

modified:   Inherit.jav
Funktioniert soweit
This commit is contained in:
pl@gohorb.ba-horb.de 2020-05-11 17:54:37 +02:00
parent b3b25b7869
commit c27e1fa4e1
2 changed files with 60 additions and 58 deletions

View File

@ -5,6 +5,7 @@ import static org.junit.Assert.*;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
@ -42,27 +43,24 @@ public class InheritTest {
@BeforeClass @BeforeClass
public static void setUpBeforeClass() throws Exception { public static void setUpBeforeClass() throws Exception {
/*
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/AA.jav"; path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/AA.jav";
fileToTest = new File(path); fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest); compiler = new JavaTXCompiler(fileToTest);
List<ResultSet> typeinferenceResult = compiler.typeInference(); List<ResultSet> typeinferenceResult = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
*/
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTestAA = loader.loadClass("AA"); classToTestAA = loader.loadClass("AA");
instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance(); instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance();
/*
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/BB.jav"; path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/BB.jav";
fileToTest = new File(path); fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest); compiler = new JavaTXCompiler(fileToTest);
typeinferenceResult = compiler.typeInference(); typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTestBB = loader.loadClass("BB"); classToTestBB = loader.loadClass("BB");
instanceOfClassBB = classToTestBB.getDeclaredConstructor().newInstance(); instanceOfClassBB = classToTestBB.getDeclaredConstructor().newInstance();
@ -72,7 +70,6 @@ public class InheritTest {
typeinferenceResult = compiler.typeInference(); typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTestCC = loader.loadClass("CC"); classToTestCC = loader.loadClass("CC");
instanceOfClassCC = classToTestCC.getDeclaredConstructor().newInstance(); instanceOfClassCC = classToTestCC.getDeclaredConstructor().newInstance();
@ -82,35 +79,18 @@ public class InheritTest {
typeinferenceResult = compiler.typeInference(); typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTestDD = loader.loadClass("DD"); classToTestDD = loader.loadClass("DD");
instanceOfClassDD = classToTestDD.getDeclaredConstructor().newInstance(); instanceOfClassDD = classToTestDD.getDeclaredConstructor().newInstance();
*/
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit.jav"; path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit.jav";
fileToTest = new File(path); fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest); compiler = new JavaTXCompiler(fileToTest);
/*
compiler = new JavaTXCompiler(
Lists.newArrayList(fileToTest),
Lists.newArrayList(new File(pathToClassFile1)));
*/
typeinferenceResult = compiler.typeInference(); typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult); simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles); compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("Inherit"); classToTest = loader.loadClass("Inherit");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
/*
for (Method m: classToTest.getMethods()) {
ArrayList<String> param = Arrays.stream(m.getParameterTypes()).map(x -> x.getName()).collect(Collectors.toCollection(ArrayList::new));
ArrayList<String> nameParam = new ArrayList<>();
nameParam.add(m.getName());
nameParam.addAll(param);
hm.put(nameParam, m);
}
*/
} }
@Test @Test
@ -124,7 +104,6 @@ public class InheritTest {
assertEquals("AA", classToTestAA.getName()); assertEquals("AA", classToTestAA.getName());
} }
/*
@Test @Test
public void testBBName() { public void testBBName() {
assertEquals("BB", classToTestBB.getName()); assertEquals("BB", classToTestBB.getName());
@ -139,49 +118,76 @@ public class InheritTest {
public void testDDName() { public void testDDName() {
assertEquals("DD", classToTestDD.getName()); assertEquals("DD", classToTestDD.getName());
} }
*/
@Test @Test
public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException { public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("Inherit");
Method m = classToTestAA.getDeclaredMethod("m", Integer.class); Method m = classToTestAA.getDeclaredMethod("m", Integer.class);
assertEquals(m.invoke(instanceOfClassAA, 5), "AA"); assertEquals(m.invoke(instanceOfClassAA, 5), "AA");
classToTestAA = classToTest.getDeclaredField("aafield").getType();
Method main = classToTest.getDeclaredMethod("main", classToTestAA, Integer.class); Method main = classToTest.getDeclaredMethod("main", classToTestAA, Integer.class);
assertEquals(main.invoke(instanceOfClass, instanceOfClassAA, 5), "AA"); assertEquals(main.invoke(instanceOfClass, instanceOfClassAA, 5), "AA");
} }
/*
Vector<Integer> v_invoke = new Vector<>(); @Test
Vector<Integer> v = new Vector<>(); public void testmainBB() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
v.add(5); Method m = classToTestAA.getDeclaredMethod("m", Integer.class);
assertEquals(v, v_invoke); assertEquals(m.invoke(instanceOfClassBB, 5), "AA");
Method main = classToTest.getDeclaredMethod("main", classToTestAA, Integer.class);
assertEquals(main.invoke(instanceOfClass, instanceOfClassBB, 5), "AA");
} }
@Test @Test
public void testPutElementStack() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmainCC() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m = classToTest.getDeclaredMethod("putElement", Object.class, Stack.class); Method m = classToTestCC.getDeclaredMethod("m", Integer.class);
Stack<Integer> s_invoke = new Stack<>(); assertEquals(m.invoke(instanceOfClassCC, 5), "CC");
m.invoke(instanceOfClass, 5, s_invoke); Method main = classToTest.getDeclaredMethod("main", classToTestCC, Integer.class);
assertEquals(new Integer(5), s_invoke.pop()); assertEquals(main.invoke(instanceOfClass, instanceOfClassCC, 5), "CC");
} }
@Test @Test
public void testMainVector() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmainDD() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m = classToTest.getDeclaredMethod("main", Object.class, Vector.class); Method m = classToTestCC.getDeclaredMethod("m", Integer.class);
Vector<Integer> v_invoke = new Vector<>(); assertEquals(m.invoke(instanceOfClassDD, 5), "CC");
m.invoke(instanceOfClass, 6, v_invoke); Method main = classToTest.getDeclaredMethod("main", classToTestCC, Integer.class);
Vector<Integer> v = new Vector<>(); assertEquals(main.invoke(instanceOfClass, instanceOfClassDD, 5), "CC");
v.add(6);
assertEquals(v, v_invoke);
} }
@Test @Test
public void testMainStack() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { public void testmainVectorAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m = classToTest.getDeclaredMethod("main", Object.class, Stack.class); Method m = classToTestAA.getDeclaredMethod("m", Integer.class);
Stack<Integer> s_invoke = new Stack<>(); assertEquals(m.invoke(instanceOfClassAA, 5), "AA");
m.invoke(instanceOfClass, 6, s_invoke); Vector v = new Vector<>();
assertEquals(new Integer(6), s_invoke.pop()); v.add(instanceOfClassAA);
Method main = classToTest.getDeclaredMethod("main", Vector.class, Integer.class);
assertEquals(main.invoke(instanceOfClass, v, 5), "AA");
}
@Test
public void testmainVectorBB() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m = classToTestAA.getDeclaredMethod("m", Integer.class);
assertEquals(m.invoke(instanceOfClassBB, 5), "AA");
Vector v = new Vector<>();
v.add(instanceOfClassBB);
Method main = classToTest.getDeclaredMethod("main", Vector.class, Integer.class);
assertEquals(main.invoke(instanceOfClass, v, 5), "AA");
}
@Test
public void testmainVectorCC() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m = classToTestCC.getDeclaredMethod("m", Integer.class);
assertEquals(m.invoke(instanceOfClassCC, 5), "CC");
Vector v = new Vector<>();
v.add(instanceOfClassCC);
Method main = classToTest.getDeclaredMethod("main", Vector.class, Integer.class);
assertEquals(main.invoke(instanceOfClass, v, 5), "CC");
}
@Test
public void testmainVectorDD() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m = classToTestCC.getDeclaredMethod("m", Integer.class);
assertEquals(m.invoke(instanceOfClassDD, 5), "CC");
Vector v = new Vector<>();
v.add(instanceOfClassDD);
Method main = classToTest.getDeclaredMethod("main", Vector.class, Integer.class);
assertEquals(main.invoke(instanceOfClass, v, 5), "CC");
} }
*/
} }

View File

@ -6,15 +6,11 @@ import java.lang.String;
public class Inherit { public class Inherit {
AA aafield; main(d, i) {
//m(Integer i) { return "AA"; }
main2(d, i) {
return d.m(i); return d.m(i);
} }
main2(v, i) { main(v, i) {
var aa = v.elementAt(0); var aa = v.elementAt(0);
return aa.m(i); return aa.m(i);
} }