From d4b6073760225f735cbbc585b94985d3f7b26810 Mon Sep 17 00:00:00 2001
From: Fayez Abu Alia <fayez.abu-alia@student.uni-tuebingen.de>
Date: Wed, 14 Nov 2018 16:19:25 +0100
Subject: [PATCH] Faculty funktioniert rihtig

---
 .../bytecode/BytecodeGenMethod.java           | 23 +++++++++++++++----
 test/bytecode/FacultyTest.java                |  8 ++++---
 test/bytecode/javFiles/Faculty.jav            | 16 ++++++-------
 3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java
index 3da49bca..5422fed2 100644
--- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java
+++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java
@@ -110,7 +110,7 @@ public class BytecodeGenMethod implements StatementVisitor {
 	}
 
 	public BytecodeGenMethod(LambdaExpression lambdaExpression, ResultSet resultSet, MethodVisitor mv,
-			int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles, String path, int lamCounter) {
+			int indexOfFirstParamLam, boolean isInterface, HashMap<String, byte[]> classFiles, String path, int lamCounter, SourceFile sf) {
 
 		this.resultSet = resultSet;
 		this.mv = mv;
@@ -118,6 +118,7 @@ public class BytecodeGenMethod implements StatementVisitor {
 		this.classFiles = classFiles;
 		this.path = path;
 		this.lamCounter = lamCounter;
+		this.sf = sf;
 		Iterator<FormalParameter> itr = lambdaExpression.params.iterator();
 		int i = indexOfFirstParamLam;
 		while (itr.hasNext()) {
@@ -378,6 +379,12 @@ public class BytecodeGenMethod implements StatementVisitor {
 			case BIGGEREQUAL:
 				mv.visitJumpInsn(Opcodes.IF_ICMPLT, branchLabel);
 				break;
+			case EQUAL:
+				mv.visitJumpInsn(Opcodes.IF_ICMPNE, branchLabel);
+				break;
+			case NOTEQUAL:
+				mv.visitJumpInsn(Opcodes.IFEQ, branchLabel);
+				break;
 			default:
 				break;
 			}
@@ -403,6 +410,12 @@ public class BytecodeGenMethod implements StatementVisitor {
 		case BIGGEREQUAL:
 			mv.visitJumpInsn(Opcodes.IFLT, branchLabel);
 			break;
+		case EQUAL:
+			mv.visitJumpInsn(Opcodes.IFNE, branchLabel);
+			break;
+		case NOTEQUAL:
+			mv.visitJumpInsn(Opcodes.IFEQ, branchLabel);
+			break;
 		default:
 			break;
 		}
@@ -567,7 +580,7 @@ public class BytecodeGenMethod implements StatementVisitor {
 				methodName, arg3.toString(), null, null);
 
 		new BytecodeGenMethod(lambdaExpression, this.resultSet, mvLambdaBody, indexOfFirstParamLam, isInterface,
-				classFiles,this.path, lamCounter);
+				classFiles,this.path, lamCounter, sf);
 
 		mvLambdaBody.visitMaxs(0, 0);
 		mvLambdaBody.visitEnd();
@@ -850,11 +863,13 @@ public class BytecodeGenMethod implements StatementVisitor {
 //			methCallType = "L"+methCallType+";";
 //		}
 		for(java.lang.reflect.Method m : methods) {
-			if(name.equals(m.getName()) && i == m.getParameterCount() && methCallType.equals(m.getReturnType().getName().replace(".", "/"))) {
+			if(name.equals(m.getName()) && i == m.getParameterCount() && 
+					(methCallType.equals(m.getReturnType().getName().replace(".", "/")) || 
+							m.getReturnType().getName().replace(".", "/").equals(Type.getInternalName(Object.class)))) {
 				boolean typesEqual = true;
 				Class<?>[] pTypes = m.getParameterTypes();
 				for(int j = 0; j<typesOfParams.length; ++j) {
-					if(!typesOfParams[j].equals(pTypes[j])) {
+					if(!typesOfParams[j].equals(pTypes[j].getName().replaceAll(".", "/")) && !pTypes[j].getName().replace(".", "/").equals(Type.getInternalName(Object.class))) {
 						typesEqual = false;
 						break;
 					}
diff --git a/test/bytecode/FacultyTest.java b/test/bytecode/FacultyTest.java
index fb8f4fad..1fea3e02 100644
--- a/test/bytecode/FacultyTest.java
+++ b/test/bytecode/FacultyTest.java
@@ -41,10 +41,12 @@ public class FacultyTest {
 //
 //		// Damit man auf die Methode zugreifen kann
 		apply.setAccessible(true);
-
+		// Field value
+		Object fieldVal = fact.get(instanceOfClass);
 		Integer i = 3;
-		
-		Integer result = (Integer) apply.invoke(instanceOfClass, i);
+		Method applyFromField = fieldVal.getClass().getDeclaredMethod("apply", Object.class);
+		applyFromField.setAccessible(true);
+		Integer result = (Integer) applyFromField.invoke(instanceOfClass, i);
 //		Integer result = (Integer) m.invoke(instanceOfClass,i);
 
 		assertEquals(6, result);
diff --git a/test/bytecode/javFiles/Faculty.jav b/test/bytecode/javFiles/Faculty.jav
index a6171e5f..ff84a2e2 100644
--- a/test/bytecode/javFiles/Faculty.jav
+++ b/test/bytecode/javFiles/Faculty.jav
@@ -4,15 +4,13 @@ public class Faculty {
 	public fact;
 	Faculty() {
 		fact = (x) -> {
-			if(x<0) {
-				return 0;
-			}else if (x < 1) {
-				  return x;
-				}
-				else {
-				  return x * (fact.apply(x-1));
-				}
-			};
+			if (x == 1) {
+				return 1;
+			}
+			else {
+			  return x * (fact.apply(x-1));
+			}
+		};
 	}
 	
 //  m (x) {