From 2cc4bf3aa1e42fcf0c95cabe062337835a381e84 Mon Sep 17 00:00:00 2001 From: Matthew Hague <matthew.hague@rhul.ac.uk> Date: Sat, 4 Dec 2021 14:23:17 +0000 Subject: [PATCH] Add return type warning for calls If the call did not specify the return type, the return type is inferred from the method. This can give students the impression their code is returning the right datatype (because the tester is using it) even when it is not. A warning has been added to let students know that this may not be the case. --- .../uk/ac/rhul/cs/javatester/CodeTester.java | 62 ++++++++++++------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java b/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java index 101216a..a5e478c 100644 --- a/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java +++ b/src/main/java/uk/ac/rhul/cs/javatester/CodeTester.java @@ -190,7 +190,7 @@ public class CodeTester { public String getIndentedLinesString() { StringBuilder sb = new StringBuilder(); for (String line : getLines()) { - sb.append(" " + line + ";\n"); + sb.append(" " + line + "\n"); } return idealize(sb.toString()); } @@ -379,7 +379,7 @@ public class CodeTester { ); } - recordCall(r, o, m, params); + recordCall(returnType, r, o, m, params); return r; } catch (InvocationTargetException e) { @@ -550,7 +550,7 @@ public class CodeTester { ); } - recordStaticCall(r, klass, m, params); + recordStaticCall(returnType, r, klass, m, params); return r; } @@ -810,7 +810,7 @@ public class CodeTester { if (getIsRecording()) { lines.add(className + " " + name + " = new " + className + - "(" + paramsString + ")"); + "(" + paramsString + ");"); } setLastCall("new " + className + "(" + paramsString + ")"); @@ -831,7 +831,7 @@ public class CodeTester { if (getIsRecording()) { lines.add(className + " " + name + " = new " + className + - " { " + valuesString + " }"); + " { " + valuesString + " };"); } setLastCall("new " + className + " { " + valuesString + " }"); @@ -841,18 +841,19 @@ public class CodeTester { /** * Record a call method object with given params. */ - private void recordCall(Object r, Object i, Method m, Object... params) { - recordCallGen(r, getParamsString(i), m, params); + private void recordCall( + Class<?> returnType, Object r, Object i, Method m, Object... params + ) { + recordCallGen(returnType, r, getParamsString(i), m, params); } /** * Record a static call */ - private void recordStaticCall(Object r, - Class<?> k, - Method m, - Object... params) { - recordCallGen(r, getClassString(k), m, params); + private void recordStaticCall( + Class<?> returnType, Object r, Class<?> k, Method m, Object... params + ) { + recordCallGen(returnType, r, getClassString(k), m, params); } /** @@ -870,12 +871,19 @@ public class CodeTester { * If null is not returned, a dummy name will be used in the log. * null is not returned * + * @param returnType the declared type of the variable returned to + * (or null if the return type is inferred from m -- in which case a + * warning is added to avoid students thinking their return type has + * been verified by the tester) * @param r the object returned by the call - * @param k the class the method was called on + * @param lhs the "thing" the method was called on (either class + * name or object variable name) * @param m the method called * @param params the arguments to the call */ - private void recordCallGen(Object r, String lhs, Method m, Object... params) { + private void recordCallGen( + Class<?> returnType, Object r, String lhs, Method m, Object... params + ) { String paramsString = getParamsString(params); if (getIsRecording()) { @@ -885,15 +893,27 @@ public class CodeTester { String line = ""; if (!rKlass.equals(Void.TYPE)) { - String rClassName = getClassString(rKlass); - line += rClassName + " " + rName + " = "; - } + if (returnType == null) { + lines.add(String.format( + "// The type of %s was inferred from your " + + "definition of", + rName + )); + lines.add(String.format( + "// %s. The tester has not verified it.", + m.getName() + )); + + returnType = m.getReturnType(); + } - lines.add(line + - lhs + - "." + m.getName() + - "(" + paramsString + ")"); + line += getClassString(returnType) + " " + rName + " = "; + } + + lines.add( + line + lhs + "." + m.getName() + "(" + paramsString + ");" + ); } setLastCall(lhs + "." + m.getName() + "(" + paramsString + ")"); -- GitLab