new file: ../../../java/bytecode/InheritTest2.java

modified:   AA.jav
	modified:   CC.jav
	new file:   Inherit2.jav
This commit is contained in:
pl@gohorb.ba-horb.de 2020-05-12 10:02:41 +02:00
parent c27e1fa4e1
commit c8ece0a010
4 changed files with 235 additions and 1 deletions

View File

@ -0,0 +1,211 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
import java.util.Vector;
import java.util.stream.Collectors;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.common.collect.Lists;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class InheritTest2 {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static Class<?> classToTestAA, classToTestBB, classToTestCC, classToTestDD;
private static String pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/";;
//private static String pathToClassFile1 = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
private static Object instanceOfClass;
private static Object instanceOfClassAA, instanceOfClassBB, instanceOfClassCC, instanceOfClassDD;
private static HashMap<ArrayList<String>, Method> hm = new HashMap<>();
private static List<ResultSet> typeinferenceResult;
private static List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/AA.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
List<ResultSet> typeinferenceResult = compiler.typeInference();
List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTestAA = loader.loadClass("AA");
instanceOfClassAA = classToTestAA.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/BB.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
classToTestBB = loader.loadClass("BB");
instanceOfClassBB = classToTestBB.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/CC.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
classToTestCC = loader.loadClass("CC");
instanceOfClassCC = classToTestCC.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/DD.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
classToTestDD = loader.loadClass("DD");
instanceOfClassDD = classToTestDD.getDeclaredConstructor().newInstance();
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/Inherit2.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
typeinferenceResult = compiler.typeInference();
simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
compiler.generateBytecode(new File(pathToClassFile),typeinferenceResult,simplifyResultsForAllSourceFiles);
classToTest = loader.loadClass("Inherit2");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
@Test
public void testInheritClassName() {
assertEquals("Inherit2", classToTest.getName());
}
@Test
public void testAAName() {
assertEquals("AA", classToTestAA.getName());
}
@Test
public void testBBName() {
assertEquals("BB", classToTestBB.getName());
}
@Test
public void testCCName() {
assertEquals("CC", classToTestCC.getName());
}
@Test
public void testDDName() {
assertEquals("DD", classToTestDD.getName());
}
@Test
public void testmainAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA);
assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA");
Method main = classToTest.getDeclaredMethod("main", classToTestAA);
assertEquals(main.invoke(instanceOfClass, instanceOfClassAA), "AA");
}
@Test
public void testmainBB() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA);
assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA");
Method main = classToTest.getDeclaredMethod("main", classToTestAA);
assertEquals(main.invoke(instanceOfClass, instanceOfClassBB), "AA");
}
@Test
public void testmainCC() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC);
assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC");
Method main = classToTest.getDeclaredMethod("main", classToTestCC);
assertEquals(main.invoke(instanceOfClass, instanceOfClassCC), "CC");
}
@Test
public void testmainDD() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC);
assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC");
Method main = classToTest.getDeclaredMethod("main", classToTestCC);
assertEquals(main.invoke(instanceOfClass, instanceOfClassDD), "CC");
}
//PL 2020-05-12: Die folgenden Test funktionieren erst, wenn Generics im Bytecode implementiert sind
@Test
public void testmainVectorAA() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA);
assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA");
Vector v = new Vector<>();
v.add(instanceOfClassAA);
Method main = classToTest.getDeclaredMethod("main", Vector.class);
try {
assertEquals(main.invoke(instanceOfClass, v), "AA");
}
catch (java.lang.reflect.InvocationTargetException e) {
testmainVectorCC();
}
}
@Test
public void testmainVectorBB() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestAA.getDeclaredMethod("m2", classToTestAA);
assertEquals(m2.invoke(instanceOfClassAA, instanceOfClassAA), "AA");
Vector v = new Vector<>();
v.add(instanceOfClassBB);
Method main = classToTest.getDeclaredMethod("main", Vector.class);
try {
assertEquals(main.invoke(instanceOfClass, v), "AA");
}
catch (java.lang.reflect.InvocationTargetException e) {
testmainVectorCC();
}
}
@Test
public void testmainVectorCC() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC);
assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC");
Vector v = new Vector<>();
v.add(instanceOfClassCC);
Method main = classToTest.getDeclaredMethod("main", Vector.class);
String erg;
assertEquals(erg= (String) main.invoke(instanceOfClass, v),
erg.equals("CC")? "CC": "AA");
}
@Test
public void testmainVectorDD() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, MalformedURLException, ClassNotFoundException, NoSuchFieldException {
Method m2 = classToTestCC.getDeclaredMethod("m2", classToTestCC);
assertEquals(m2.invoke(instanceOfClassCC, instanceOfClassCC), "CC");
Vector v = new Vector<>();
v.add(instanceOfClassDD);
Method main = classToTest.getDeclaredMethod("main", Vector.class);
String erg;
assertEquals(erg= (String) main.invoke(instanceOfClass, v),
erg.equals("CC")? "CC": "AA");
}
}

View File

@ -3,4 +3,6 @@ import java.lang.String;
public class AA {
m(Integer i) { return "AA"; }
m2(AA x) { return "AA"; }
}

View File

@ -3,5 +3,9 @@ import java.lang.String;
public class CC extends BB {
m(Integer i) { return "CC"; }
m(Integer i) {
return "CC";
}
m2(CC x) { return "CC"; }
}

View File

@ -0,0 +1,17 @@
import java.util.Vector;
import java.lang.Integer;
import java.lang.String;
public class Inherit2 {
main(d) {
return d.m2(d);
}
main(v) {
var aa = v.elementAt(0);
return aa.m2(aa);
}
}