diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 07bb7308..52feb6e5 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -187,7 +187,11 @@ public class BytecodeGen implements ASTVisitor { // Method getModifiers() ? int acc = isInterface?Opcodes.ACC_ABSTRACT:method.modifier; System.out.println(acc); + + /*Prüfe, ob die Rückgabe-Type der Methode eine Type-Variable ist*/ boolean hasGenInParameterList = genericsAndBounds.containsKey(resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())); + /*Wenn die Rückgabe-Type eine Typ-variable ist, erzeuge direkt die Signature, wenn nicht, + * prüfe, ob einer der Parameter Typ-Variable als Typ hat*/ if(!hasGenInParameterList) { for(String paramName : methodParamsAndTypes.keySet()) { String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); @@ -202,6 +206,8 @@ public class BytecodeGen implements ASTVisitor { //then create the descriptor with the new syntax. String sig = null; + /* method.getGenerics: <....> RT method(..) + * */ boolean hasGen = method.getGenerics().iterator().hasNext() || hasGenInParameterList; /* if method has generics or return type is TPH, create signature */ diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index aa7b586e..ce5d3def 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -546,7 +546,7 @@ public class BytecodeGenMethod implements StatementVisitor { cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - // generateBCForFunN(lambdaExpression,typeErasure); + generateBCForFunN(lambdaExpression,typeErasure); } private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) { @@ -580,7 +580,7 @@ public class BytecodeGenMethod implements StatementVisitor { try { System.out.println("generating " + name + ".class file..."); output = new FileOutputStream( - new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" + name + ".class")); + new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" + name + ".class")); output.write(bytecode); output.close(); System.out.println(name + ".class file generated"); @@ -772,7 +772,19 @@ public class BytecodeGenMethod implements StatementVisitor { @Override public void visit(Return aReturn) { + if(aReturn.retexpr instanceof BinaryExpr) + isBinaryExp = true; + aReturn.retexpr.accept(this); + + if (isBinaryExp) { + BinaryExpr binary = (BinaryExpr) aReturn.retexpr; + String lexpType = getResolvedType(binary.lexpr.getType()); + String rexpType = getResolvedType(binary.rexpr.getType()); + getValueOfIns(getLargerType(lexpType, rexpType)); + isBinaryExp = false; + } + mv.visitInsn(Opcodes.ARETURN); } diff --git a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index c9725a82..65f48f71 100644 --- a/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -49,9 +49,10 @@ public class DescriptorToString implements DescriptorVisitor{ desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } } -// else if(((RefType) fp.getType()).getParaList().size() > 0){ -// desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; -// } + //TODO: generate a class java%% ... %% + else if(resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()).contains("<")){ + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; + } else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 108122ac..2d6867ce 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -57,8 +57,8 @@ public class TYPEStmt implements StatementVisitor{ public void visit(LambdaExpression lambdaExpression) { TypePlaceholder tphRetType = TypePlaceholder.fresh(new NullToken()); List lambdaParams = lambdaExpression.params.getFormalparalist().stream().map((formalParameter -> formalParameter.getType())).collect(Collectors.toList()); - //lambdaParams.add(tphRetType); - lambdaParams.add(0,tphRetType); + lambdaParams.add(tphRetType); + //lambdaParams.add(0,tphRetType); constraintsSet.addUndConstraint( new Pair(lambdaExpression.getType(), new FunN(lambdaParams),PairOperator.EQUALSDOT)); @@ -184,7 +184,13 @@ public class TYPEStmt implements StatementVisitor{ receiver.expr.accept(this); } - private final RefType number = new RefType(ASTFactory.createClass(Integer.class).getClassName(), new NullToken()); + private final RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), new NullToken()); + private final RefType longg = new RefType(ASTFactory.createClass(Long.class).getClassName(), new NullToken()); + private final RefType integer = new RefType(ASTFactory.createClass(Integer.class).getClassName(), new NullToken()); + private final RefType shortt = new RefType(ASTFactory.createClass(Short.class).getClassName(), new NullToken()); + private final RefType bytee = new RefType(ASTFactory.createClass(Byte.class).getClassName(), new NullToken()); + private final RefType floatt = new RefType(ASTFactory.createClass(Float.class).getClassName(), new NullToken()); + private final RefType doublee = new RefType(ASTFactory.createClass(Double.class).getClassName(), new NullToken()); private final RefType string = new RefType(ASTFactory.createClass(String.class).getClassName(), new NullToken()); private final RefType bool = new RefType(ASTFactory.createClass(Boolean.class).getClassName(), new NullToken()); @Override @@ -205,18 +211,47 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(BinaryExpr binary) { - + binary.lexpr.accept(this); + binary.rexpr.accept(this); if(binary.operation.equals(BinaryExpr.Operator.DIV) || binary.operation.equals(BinaryExpr.Operator.MUL)|| binary.operation.equals(BinaryExpr.Operator.MOD)|| binary.operation.equals(BinaryExpr.Operator.ADD)){ Set numericAdditionOrStringConcatenation = new HashSet<>(); - Constraint numeric = new Constraint<>(); + //Zuerst der Fall für Numerische AusdrücPairOpnumericeratorke, das sind Mul, Mod und Div immer: //see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17 //Expression muss zu Numeric Convertierbar sein. also von Numeric erben - numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); - numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); + Constraint numeric = new Constraint<>(); + numeric.add(new Pair(binary.lexpr.getType(), bytee, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.rexpr.getType(), bytee, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(numeric); + numeric = new Constraint<>(); + numeric.add(new Pair(binary.lexpr.getType(), shortt, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.rexpr.getType(), shortt, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(numeric); + numeric = new Constraint<>(); + numeric.add(new Pair(binary.lexpr.getType(), integer, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.rexpr.getType(), integer, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(numeric); + numeric = new Constraint<>(); + numeric.add(new Pair(binary.lexpr.getType(), longg, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.rexpr.getType(), longg, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.getType(), longg, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(numeric); + numeric = new Constraint<>(); + numeric.add(new Pair(binary.lexpr.getType(), floatt, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.rexpr.getType(), floatt, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.getType(), floatt, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(numeric); + numeric = new Constraint<>(); + numeric.add(new Pair(binary.lexpr.getType(), doublee, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.rexpr.getType(), doublee, PairOperator.EQUALSDOT)); + numeric.add(new Pair(binary.getType(), doublee, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(numeric); /* In Java passiert bei den binären Operatoren eine sogenannte Type Promotion: https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2 diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 0d77d48f..cd6fb8e7 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -767,8 +767,8 @@ public class RuleSet implements IRuleSet{ Set result = new HashSet(); - result.add(new UnifyPair(funNLhsType.getTypeParams().get(0), funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); - for(int i = 1; i < funNLhsType.getTypeParams().size(); i++) + result.add(new UnifyPair(funNLhsType.getTypeParams().get(funNLhsType.getTypeParams().size()-1), funNRhsType.getTypeParams().get(funNRhsType.getTypeParams().size()-1), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); + for(int i = 0; i < funNLhsType.getTypeParams().size()-1; i++) result.add(new UnifyPair(funNRhsType.getTypeParams().get(i), funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); return Optional.of(result); @@ -793,8 +793,8 @@ public class RuleSet implements IRuleSet{ for(int i = 0; i < freshPlaceholders.length; i++) freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); - result.add(new UnifyPair(funNLhsType.getTypeParams().get(0), freshPlaceholders[0], PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); - for(int i = 1; i < funNLhsType.getTypeParams().size(); i++) + result.add(new UnifyPair(funNLhsType.getTypeParams().get(funNLhsType.getTypeParams().size()-1), freshPlaceholders[funNLhsType.getTypeParams().size()-1], PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); + for(int i = 0; i < funNLhsType.getTypeParams().size()-1; i++) result.add(new UnifyPair(freshPlaceholders[i], funNLhsType.getTypeParams().get(i), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); result.add(new UnifyPair(rhsType, funNLhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT, pair.getSubstitution(), pair.getBasePair())); @@ -820,8 +820,8 @@ public class RuleSet implements IRuleSet{ for(int i = 0; i < freshPlaceholders.length; i++) freshPlaceholders[i] = PlaceholderType.freshPlaceholder(); - result.add(new UnifyPair(freshPlaceholders[0], funNRhsType.getTypeParams().get(0), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); - for(int i = 1; i < funNRhsType.getTypeParams().size(); i++) + result.add(new UnifyPair(freshPlaceholders[funNRhsType.getTypeParams().size()-1], funNRhsType.getTypeParams().get(funNRhsType.getTypeParams().size()-1), PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); + for(int i = 0; i < funNRhsType.getTypeParams().size()-1; i++) result.add(new UnifyPair(funNRhsType.getTypeParams().get(i), freshPlaceholders[i], PairOperator.SMALLERDOT, pair.getSubstitution(), pair.getBasePair())); result.add(new UnifyPair(lhsType, funNRhsType.setTypeParams(new TypeParams(freshPlaceholders)), PairOperator.EQUALSDOT, pair.getSubstitution(), pair.getBasePair())); diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index ff929fc0..aec5653c 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -161,7 +161,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * ? extends ? extends Theta rausfiltern */ - Set doubleExt = eq.stream().filter(x -> (x.doubleExtended())).map(x -> { x.setUndefinedPair(); return x;}) + Set doubleExt = eq.stream().filter(x -> (x.wrongWildcard())).map(x -> { x.setUndefinedPair(); return x;}) .collect(Collectors.toCollection(HashSet::new)); if (doubleExt.size() > 0) { Set> ret = new HashSet<>(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java index 37ec79d5..5d807765 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FunNType.java @@ -1,5 +1,7 @@ package de.dhbwstuttgart.typeinference.unify.model; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Set; @@ -73,11 +75,18 @@ public class FunNType extends UnifyType { return new FunNType(newParams); } + @Override + public Boolean wrongWildcard() { + return !(new ArrayList(Arrays.asList(getTypeParams() + .get())).stream().filter(x -> (x instanceof WildcardType)).findFirst().isPresent()); + } + @Override public int hashCode() { return 181 + typeParams.hashCode(); } + @Override public boolean equals(Object obj) { if(!(obj instanceof FunNType)) diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java index cabfbcc1..b631e5ec 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyPair.java @@ -134,8 +134,8 @@ public class UnifyPair { return ret; } - public Boolean doubleExtended() { - return lhs.doubleExtended() || rhs.doubleExtended(); + public Boolean wrongWildcard() { + return lhs.wrongWildcard() || rhs.wrongWildcard(); } @Override diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java index 5beef2bf..715d9880 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java @@ -103,7 +103,7 @@ public abstract class UnifyType { return ret; } - public Boolean doubleExtended() {//default + public Boolean wrongWildcard() {//default return false; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java b/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java index e320bb9f..81999492 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/WildcardType.java @@ -41,7 +41,7 @@ public abstract class WildcardType extends UnifyType { } @Override - public Boolean doubleExtended () {//This is an error + public Boolean wrongWildcard () {//This is an error return (wildcardedType instanceof WildcardType); } diff --git a/test/bytecode/GenTest.java b/test/bytecode/GenTest.java new file mode 100644 index 00000000..d0067d08 --- /dev/null +++ b/test/bytecode/GenTest.java @@ -0,0 +1,41 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; + +import org.junit.BeforeClass; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class GenTest { + + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static String pathToClassFile; + private static Object instanceOfClass; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Gen.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(); + pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/"; + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("Gen"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } + + @Test + public void test() { + fail("Not yet implemented"); + } + +} diff --git a/test/bytecode/LambdaTest.java b/test/bytecode/LambdaTest.java new file mode 100644 index 00000000..f29690cb --- /dev/null +++ b/test/bytecode/LambdaTest.java @@ -0,0 +1,23 @@ +package bytecode; + +import java.io.File; + +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class LambdaTest { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + + @Test + public void generateBC() throws Exception { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Lambda.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(); + } + + +} diff --git a/test/bytecode/OpTest.java b/test/bytecode/OpTest.java index 33708206..1d213a78 100644 --- a/test/bytecode/OpTest.java +++ b/test/bytecode/OpTest.java @@ -36,158 +36,26 @@ public class OpTest { } @Test - public void testClassname() { - assertEquals("Op", classToTest.getName()); + public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, InstantiationException { + + Method m = classToTest.getDeclaredMethod("m", String.class,String.class); + + String result = (String) m.invoke(instanceOfClass, "Byte","Code"); + + assertEquals("ByteCode", result); } - @Test - public void testClassModifiers() { - assertEquals(Opcodes.ACC_PUBLIC, classToTest.getModifiers()); - } - -// @Test -// public void testNumberOfMethods() { -// int numOfMeth = classToTest.getDeclaredMethods().length; -// assertEquals(5, numOfMeth); -// } -// -// @Test -// public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addString = classToTest.getDeclaredMethod("addString", String.class,String.class); -// String result = (String) addString.invoke(instanceOfClass, "Byte","Code"); -// assertEquals("ByteCode", result); -// } -// @Test public void testAddInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { - Method addInt = classToTest.getDeclaredMethod("addInt", Integer.class,Integer.class); - Number result = (Number) addInt.invoke(instanceOfClass, 7,3); + + Method m = classToTest.getDeclaredMethod("m", Integer.class,Integer.class); + + Integer result = (Integer) m.invoke(instanceOfClass, 7,3); + assertEquals(10, result); } -// @Test -// public void testAddLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addLong = classToTest.getDeclaredMethod("addLong", Long.class,Long.class); -// Long result = (Long) addLong.invoke(instanceOfClass, 7L,3L); -// assertEquals(10L, result); -// } -// -// @Test -// public void testAddFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addFloat = classToTest.getDeclaredMethod("addFloat", Float.class,Float.class); -// Float result = (Float) addFloat.invoke(instanceOfClass, 7f,3f); -// assertEquals(10f, result); -// } -// -// @Test -// public void testAddDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method addDouble = classToTest.getDeclaredMethod("addDouble", Double.class,Double.class); -// Double result = (Double) addDouble.invoke(instanceOfClass, 7.0,3.0); -// assertEquals(10.0, result); -// } - -// @Test -// public void testAddIntLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Long.class); -// Long result = (Long) add.invoke(instanceOfClass, 7,3L); -// assertEquals(10L, result); -// } - -// @Test -// public void testAddDLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Double.class,Long.class); -// Double result = (Double) add.invoke(instanceOfClass, 7d,3L); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddIntShort() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Short.class); -// Short s = 3; -// Integer result = (Integer) add.invoke(instanceOfClass, 7,s); -// assertEquals(10, result); -// } - -// @Test -// public void testAddIntByte() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Byte.class); -// Byte b = 3; -// Integer result = (Integer) add.invoke(instanceOfClass, 7,b); -// assertEquals(10, result); -// } - -// @Test -// public void testAddDFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Float.class,Double.class); -// Double result = (Double) add.invoke(instanceOfClass, 7f,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddIntD() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Integer.class,Double.class); -// Double result = (Double) add.invoke(instanceOfClass, 7,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddShortD() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Short.class,Double.class); -// Short s = 7; -// Double result = (Double) add.invoke(instanceOfClass, s,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testAddByteD() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method add = classToTest.getDeclaredMethod("add", Byte.class,Double.class); -// Byte b = 7; -// Double result = (Double) add.invoke(instanceOfClass, b,3d); -// assertEquals(10d, result); -// } - -// @Test -// public void testMulInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulInt = classToTest.getDeclaredMethod("mulInt", Integer.class,Integer.class); -// Integer result = (Integer) mulInt.invoke(instanceOfClass, 7,3); -// assertEquals(21, result); -// } -// -// @Test -// public void testMulLong() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulLong = classToTest.getDeclaredMethod("mulLong", Long.class,Long.class); -// Long result = (Long) mulLong.invoke(instanceOfClass, 7L,3L); -// assertEquals(21L, result); -// } -// -// @Test -// public void testMulFloat() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulFloat = classToTest.getDeclaredMethod("mulFloat", Float.class,Float.class); -// Float result = (Float) mulFloat.invoke(instanceOfClass, 7f,3f); -// assertEquals(21f, result); -// } -// -// @Test -// public void testMulDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, -// IllegalArgumentException, InvocationTargetException, InstantiationException { -// Method mulDouble = classToTest.getDeclaredMethod("mulDouble", Double.class,Double.class); -// Double result = (Double) mulDouble.invoke(instanceOfClass, 7.0,3.0); -// assertEquals(21.0, result); -// } + } diff --git a/test/bytecode/PlusTest.java b/test/bytecode/PlusTest.java new file mode 100644 index 00000000..016cb706 --- /dev/null +++ b/test/bytecode/PlusTest.java @@ -0,0 +1,54 @@ +package bytecode; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +import org.junit.BeforeClass; +import org.junit.Test; + +import de.dhbwstuttgart.core.JavaTXCompiler; + +public class PlusTest { + private static String path; + private static File fileToTest; + private static JavaTXCompiler compiler; + private static ClassLoader loader; + private static Class classToTest; + private static String pathToClassFile; + private static Object instanceOfClass; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + path = System.getProperty("user.dir")+"/test/bytecode/javFiles/Plus.jav"; + fileToTest = new File(path); + compiler = new JavaTXCompiler(fileToTest); + compiler.generateBytecode(); + + pathToClassFile = System.getProperty("user.dir")+"/testBytecode/generatedBC/examples/"; + loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)}); + classToTest = loader.loadClass("Plus"); + instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + } + + @Test + public void testAddInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, InstantiationException { + Method addInt = classToTest.getDeclaredMethod("m", Integer.class,Integer.class); + Number result = (Number) addInt.invoke(instanceOfClass, 7,3); + assertEquals(10, result); + } + + @Test + public void testAddString() throws NoSuchMethodException, SecurityException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, InstantiationException { + Method addString = classToTest.getDeclaredMethod("m", String.class,String.class); + String result = (String) addString.invoke(instanceOfClass, "Byte","Code"); + assertEquals("ByteCode", result); + } + +} diff --git a/test/bytecode/javFiles/Gen.jav b/test/bytecode/javFiles/Gen.jav index 1f873919..5d579eb8 100644 --- a/test/bytecode/javFiles/Gen.jav +++ b/test/bytecode/javFiles/Gen.jav @@ -1,5 +1,8 @@ +import java.lang.Integer; +import java.util.Vector; + public class Gen{ - Vector m(Vector v){ + Vector m(Vector v){ return v; } -} \ No newline at end of file +} diff --git a/test/bytecode/javFiles/LamAssign.jav b/test/bytecode/javFiles/LamAssign.jav deleted file mode 100644 index 82bb31b1..00000000 --- a/test/bytecode/javFiles/LamAssign.jav +++ /dev/null @@ -1,15 +0,0 @@ -import java.lang.Integer; - -class LamAssign { - - m () { - var lam1 = (Integer x) -> { - return x; - }; - return lam1; - } -} - -interface Fun1{ - public A apply(B b); -} \ No newline at end of file diff --git a/test/bytecode/javFiles/Lambda.jav b/test/bytecode/javFiles/Lambda.jav index 378eb4d3..3aeded25 100644 --- a/test/bytecode/javFiles/Lambda.jav +++ b/test/bytecode/javFiles/Lambda.jav @@ -1,18 +1,11 @@ -class Lambda{ +import java.lang.Integer; -methode(){ - return ((f) -> f); -} -} -/* -interface Fun0{ - A apply(); -} +public class Lambda { -interface Fun1{ - A apply(B b); + m () { + var lam1 = (Integer x) -> { + return x; + }; + return lam1; + } } -*/ -interface Fun2{ - A apply(B b, C c); -} \ No newline at end of file diff --git a/test/bytecode/javFiles/Lambda4.jav b/test/bytecode/javFiles/Lambda4.jav new file mode 100644 index 00000000..378eb4d3 --- /dev/null +++ b/test/bytecode/javFiles/Lambda4.jav @@ -0,0 +1,18 @@ +class Lambda{ + +methode(){ + return ((f) -> f); +} +} +/* +interface Fun0{ + A apply(); +} + +interface Fun1{ + A apply(B b); +} +*/ +interface Fun2{ + A apply(B b, C c); +} \ No newline at end of file diff --git a/test/bytecode/javFiles/Op.jav b/test/bytecode/javFiles/Op.jav index 4aa039d5..c01e7e6e 100644 --- a/test/bytecode/javFiles/Op.jav +++ b/test/bytecode/javFiles/Op.jav @@ -9,103 +9,8 @@ import java.lang.Byte; public class Op { - addInt( a, b) { - var c = a+b; - return c; + Integer m(Integer a, Integer b) { + //var c = a+b; + return a+b; } - - -// addString(String a, String b) { -// String c = a+b; -// return c; -// } -// addLong(Long a, Long b) { -// Long c = a+b; -// return c; -// } -// addFloat(Float a, Float b) { -// Float c = a+b; -// return c; -// } -// addDouble(Double a, Double b) { -// Double c = a+b; -// return c; -// } - -// subInt(Integer a, Integer b) { -// Integer c = a-b; -// return c; -// } -// subLong(Long a, Long b) { -// Long c = a-b; -// return c; -// } -// subFloat(Float a, Float b) { -// Float c = a-b; -// return c; -// } -// subDouble(Double a, Double b) { -// Double c = a-b; -// return c; -// } - -// Long add(Integer a, Long b) { -// Long c = a+b; -// return c; -// } - -// mulInt(Integer a, Integer b) { -// Integer c = a*b; -// return c; -// } -// -// mulLong(Long a, Long b) { -// Long c = a*b; -// return c; -// } -// -// mulFloat(Float a, Float b) { -// Float c = a*b; -// return c; -// } -// -// mulDouble(Double a, Double b) { -// Double c = a*b; -// return c; -// } - -// add(Double a, Long b) { -// Double c = a+b; -// return c; -// } - -// add(Integer a, Short b) { -// Integer c = a+b; -// return c; -// } -// -// add(Integer a, Byte b) { -// Integer c = a+b; -// return c; -// } - -// add(Float a, Double b) { -// Double c = a+b; -// return c; -//} -// -// add(Integer a, Double b) { -// Double c = a+b; -// return c; -// } - -// add(Short a, Double b) { -// Double c = a+b; -// return c; -// } - -// add(Byte a, Double b) { -// Double c = a+b; -// return c; -// } } \ No newline at end of file diff --git a/test/bytecode/javFiles/Plus.jav b/test/bytecode/javFiles/Plus.jav new file mode 100644 index 00000000..4d425bf0 --- /dev/null +++ b/test/bytecode/javFiles/Plus.jav @@ -0,0 +1,8 @@ +import java.lang.Integer; + +public class Plus { + + m(a,b) { + return a+b; + } +} \ No newline at end of file diff --git a/test/typeinference/UnifyTest.java b/test/typeinference/UnifyTest.java index ac7ef221..41361751 100644 --- a/test/typeinference/UnifyTest.java +++ b/test/typeinference/UnifyTest.java @@ -65,14 +65,14 @@ public class UnifyTest { } */ - /* + @Test public void matrix() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Matrix.jav")); //JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"Matrix.jav")); //compiler.generateBytecode(); } - */ + /* @Test public void vector() throws IOException, ClassNotFoundException {