Merge branch 'bytecode' into refactoring

This commit is contained in:
JanUlrich 2015-11-06 15:19:19 +01:00
commit a221adb28c
19 changed files with 374 additions and 53 deletions

View File

@ -7,6 +7,7 @@ import java.util.*;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.LoggerConfiguration;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.typeinference.TypeinferenceResultSet;
import de.dhbwstuttgart.typeinference.exceptions.TypeinferenceException;
@ -34,13 +35,13 @@ public class ConsoleInterface {
/////////////////////////
// Parsen:
/////////////////////////
compiler.parse(filenames);
Menge<SourceFile> sourceFiles = compiler.parse(filenames);
/////////////////////////
// Typrekonstruktion:
/////////////////////////
try{
resultSet = compiler.typeReconstruction();
resultSet = compiler.typeReconstruction(sourceFiles);
}catch(TypeinferenceException texc){
texc.printStackTrace();
fail("Fehler bei Typinferenzalgorithmus. Message: "+texc.getMessage());

View File

@ -236,7 +236,7 @@ public class FormalParameter extends SyntaxTreeNode implements Typeable, TypeIns
public String getDescription() {
String ret = "";
if(this.getType() != null && !(this.getType() instanceof TypePlaceholder)){
ret += this.getType().getName() + " ";
ret += this.getType().getBytecodeSignature(null);
}
return ret+this.getIdentifier();
}

View File

@ -4,7 +4,10 @@ import de.dhbwstuttgart.bytecode.ClassGenerator;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.misc.DeclId;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
import de.dhbwstuttgart.typeinference.Menge;
@ -45,11 +48,16 @@ public class ASTFactory {
return new Constructor(method, superClass);
}
public static Class createClass(String className, Class superClass) {
public static Class createClass(String className, String superClass, SourceFile parent) {
// TODO bytecode createClass
Class generatedClass = new Class(className, 0);
generatedClass.addField(ASTFactory.createEmptyConstructor(superClass));
generatedClass.parserPostProcessing(superClass);
generatedClass.addField(ASTFactory.createEmptyConstructor(generatedClass));
if(superClass != null){
generatedClass.superclassid = UsedId.createFromQualifiedName(superClass, 0);
}
generatedClass.parserPostProcessing(parent);
return generatedClass;
}

View File

@ -16,6 +16,7 @@ import de.dhbwstuttgart.parser.JavaClassName;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.misc.UsedId;
@ -852,8 +853,7 @@ public class RefType extends ObjectType implements IMatchable
String combinedType = getCombinedType(cg);
if(!combinedType.equals(getName().toString())){
// TODO bytecode statt getParentClass die eigene Class?!?!
Class classObject = ASTFactory.createClass(getName().toString(), ASTFactory.createObjectClass());
Class generatedClass = ASTFactory.createClass(getCombinedType(cg), classObject);
Class generatedClass = ASTFactory.createClass(getCombinedType(cg), getName().toString(), new SourceFile());
cg.addExtraClass(generatedClass.genByteCode(new TypeinferenceResultSet(generatedClass, new Menge<>(), new ResultSet())).getByteCode());
}
@ -869,7 +869,14 @@ public class RefType extends ObjectType implements IMatchable
sb.append(getName().toString().replace(".", "%"));
sb.append("%%");
for(Type type: parameter){
if(type instanceof RefType){
sb.append(((RefType) type).getCombinedType(cg).replace(".", "%"));
}else if(type instanceof TypePlaceholder){
sb.append(((TypePlaceholder) type).getBytecodeType(cg).toString().replace(".", "%"));
}else{
sb.append(((TypePlaceholder) type).getBytecodeType(cg).toString().replace(".", "%"));
}
sb.append("%");
}
}else{

View File

@ -39,11 +39,7 @@ public abstract class BytecodeTest extends TestCase{
protected Class getClassToTest(){
Class classToTest = null;
try {
File file = new File(rootDirectory);
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader classLoader = new URLClassLoader(urls);
ClassLoader classLoader = getClassLoader();
classToTest = classLoader.loadClass(testName);
@ -54,4 +50,12 @@ public abstract class BytecodeTest extends TestCase{
return classToTest;
}
protected ClassLoader getClassLoader() throws Exception{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/");
URL url = file.toURL();
URL[] urls = new URL[]{url};
return new URLClassLoader(urls);
}
}

View File

@ -34,7 +34,11 @@ public class ExtendsObjectTest extends BytecodeTest{
@Test
public void testConstruct(){
try{
Class cls = getClassToTest();
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Constructor method = cls.getConstructor(new Class[]{});
method.newInstance();

View File

@ -33,7 +33,11 @@ public class ExtendsVectorStringTest extends BytecodeTest{
@Test
public void testConstruct() throws Exception{
Class cls = getClassToTest();
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Constructor method = cls.getConstructor(new Class[]{});
method.newInstance();

View File

@ -34,7 +34,11 @@ public class ExtendsVectorTest extends BytecodeTest{
@Test
public void testConstruct(){
try{
Class cls = getClassToTest();
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Constructor method = cls.getConstructor(new Class[]{});
method.newInstance();

View File

@ -0,0 +1,7 @@
import java.util.Vector;
class LocalVariableStringVector{
void method() {
Vector<String> vector;
}
}

View File

@ -0,0 +1,39 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import bytecode.BytecodeTest;
public class LocalVariableStringVectorTest extends BytecodeTest{
@Override
protected void init() {
testName = "LocalVariableStringVector";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testCompiler() {
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,7 @@
import java.util.Vector;
class LocalVariableVector{
void method() {
Vector vector;
}
}

View File

@ -0,0 +1,38 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
import bytecode.BytecodeTest;
public class LocalVariableVectorTest extends BytecodeTest{
@Override
protected void init() {
testName = "LocalVariableVector";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testCompiler() {
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
assertTrue(true);
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

View File

@ -1,6 +1,6 @@
import java.util.Vector;
class TypedVector{
class MethodWithTypedVector{
public void method(Vector<String> v) {
}

View File

@ -2,7 +2,10 @@ package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Stack;
import java.util.Vector;
@ -12,30 +15,31 @@ import bytecode.BytecodeTest;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
public class TypedVectorTest extends BytecodeTest{
public class MethodWithTypedVectorTest extends BytecodeTest{
@Override
protected void init() {
testName = "TypedVector";
testName = "MethodWithTypedVector";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void test() {
try{
Class cls = getClassToTest();
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Vector<String> stringVector = new Vector<String>();
Class stringVector = classLoader.loadClass("java%util%Vector%%java%lang%String%");
Class[] params = new Class[1];
params[0] = stringVector.getClass();
params[0] = stringVector;
Method method = cls.getDeclaredMethod("method", params);
method.invoke(obj, stringVector);
assertTrue(true);
method.invoke(obj, stringVector.newInstance());
}catch(Exception e){
Logger.getLogger("SingleClassTester").error(e.toString(), Section.CODEGEN);
fail();
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,7 @@
import java.util.Vector;
class MethodWithUntypedVector{
public void method(Vector v) {
}
}

View File

@ -0,0 +1,68 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Stack;
import java.util.Vector;
import org.junit.Test;
import bytecode.BytecodeTest;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
public class MethodWithUntypedVectorTest extends BytecodeTest{
@Override
protected void init() {
testName = "MethodWithUntypedVector";
rootDirectory = System.getProperty("user.dir")+"/test/bytecode/types/";
}
@Test
public void testObjectVector() {
try{
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
File file = new File(rootDirectory);
URL url = file.toURL();
URL[] urls = new URL[]{url};
Object object = new Object();
Class[] params = new Class[1];
params[0] = object.getClass();
Method method = cls.getDeclaredMethod("method", params);
method.invoke(obj, object);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testGenericClass() {
try{
ClassLoader classLoader = getClassLoader();
Class untypedVectorTest = classLoader.loadClass(testName);
Class stringVector = classLoader.loadClass("java%util%Vector%%java%lang%Object%");
Class[] params = new Class[1];
params[0] = stringVector;
Method method = untypedVectorTest.getDeclaredMethod("method", params);
method.invoke(untypedVectorTest.newInstance(), stringVector.newInstance());
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

View File

@ -2,7 +2,10 @@ package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Vector;
import org.junit.Test;
@ -20,40 +23,48 @@ public class OverloadingTest extends BytecodeTest{
@Test
public void testString() {
try{
Class cls = getClassToTest();
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Vector<String> stringVector = new Vector<String>();
File file = new File(rootDirectory);
URL url = file.toURL();
URL[] urls = new URL[]{url};
Class stringVector = classLoader.loadClass("java%util%Vector%%java%lang%String%");
Class[] params = new Class[1];
params[0] = stringVector.getClass();
params[0] = stringVector;
Method method = cls.getDeclaredMethod("method", params);
method.invoke(obj, stringVector);
method.invoke(obj, stringVector.newInstance());
assertTrue(true);
}catch(Exception e){
e.printStackTrace();
fail();
throw new RuntimeException(e);
}
}
@Test
public void testInteger() {
try{
Class cls = getClassToTest();
ClassLoader classLoader = getClassLoader();
Class cls = classLoader.loadClass(testName);
Object obj = cls.newInstance();
Vector<Integer> stringVector = new Vector<Integer>();
Class integerVector = classLoader.loadClass("java%util%Vector%%java%lang%Integer%");
Class[] params = new Class[1];
params[0] = stringVector.getClass();
params[0] = integerVector;
Method method = cls.getDeclaredMethod("method", params);
method.invoke(obj, stringVector);
method.invoke(obj, integerVector.newInstance());
assertTrue(true);
}catch(Exception e){
e.printStackTrace();
fail();
throw new RuntimeException(e);
}
}

View File

@ -0,0 +1,120 @@
package bytecode.types;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Stack;
import java.util.Vector;
import org.junit.Test;
import bytecode.BytecodeTest;
import de.dhbwstuttgart.logger.Logger;
import de.dhbwstuttgart.logger.Section;
public class ReflectionTest{
@Test
public void testUntypedVectorDeclaredMethods() {
try{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader classLoader = new URLClassLoader(urls);
Class untypedVectorTest = classLoader.loadClass("UntypedVector");
for(Method method: untypedVectorTest.getDeclaredMethods()){
System.out.println(method.toGenericString());
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testUntypedVectorDeclaredMethodsCallMethod() {
try{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader classLoader = new URLClassLoader(urls);
Class untypedVectorTest = classLoader.loadClass("UntypedVector");
Class stringVector = classLoader.loadClass("java%util%Vector%%java%lang%Object%");
Object stringVectorObj = stringVector.newInstance();
for(Method method: untypedVectorTest.getDeclaredMethods()){
method.invoke(untypedVectorTest.newInstance(), stringVectorObj);
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testUntypedVectorMethod() {
try{
File file = new File(System.getProperty("user.dir")+"/test/bytecode/types/");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader classLoader = new URLClassLoader(urls);
Class untypedVectorTest = classLoader.loadClass("UntypedVector");
Class stringVector = classLoader.loadClass("java%util%Vector%%java%lang%Object%");
Class[] params = new Class[1];
params[0] = stringVector;
Method method = untypedVectorTest.getDeclaredMethod("method", params);
method.invoke(untypedVectorTest.newInstance(), stringVector.newInstance());
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testStdVectorAdd() {
try{
Vector<String> vector = new Vector<String>();
Class vectorClass = vector.getClass();
String helloWorld = new String("Hello World!");
Class[] params = new Class[1];
params[0] = new Object().getClass();
Method method = vectorClass.getDeclaredMethod("add", params);
method.invoke(vector, helloWorld);
}catch(Exception e){
throw new RuntimeException(e);
}
}
@Test
public void testStdVectorEnsureCapacity() {
try{
Vector<String> vector = new Vector<String>();
Class vectorClass = vector.getClass();
Integer integer = new Integer(1);
Class[] params = new Class[1];
params[0] = int.class;
Method method = vectorClass.getDeclaredMethod("ensureCapacity", params);
method.invoke(vector, integer);
}catch(Exception e){
throw new RuntimeException(e);
}
}
}

View File

@ -1,12 +0,0 @@
import java.util.Vector;
public class Test {
public static void main(String[] args) {
Overloading o = new Overloading();
Vector<String> stringVector = new Vector<String>();
o.method(stringVector);
}
}