Skip to content

Commit 0f0cac9

Browse files
authored
Added ChangeCode Methods with Input
Tested with existing code before committed
1 parent fde8537 commit 0f0cac9

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

CodeTestHelper.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,43 @@ public String getMethodOutputChangedCode(String program, String className, Strin
17261726

17271727
}
17281728

1729+
public String getMethodOutputWithInput(Method m, Object[] arguments, String input) {
1730+
inContent = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
1731+
System.setIn(inContent);
1732+
1733+
String output = getStaticMethodOutput(m, arguments);
1734+
1735+
System.setIn(System.in);
1736+
1737+
return output;
1738+
}
1739+
1740+
public String getMethodOutputChangedCodeWithInput(String program, String className, String methodName, String input) {
1741+
// System.out.println(program);
1742+
1743+
try {
1744+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
1745+
1746+
Iterable<? extends JavaFileObject> fileObjects;
1747+
fileObjects = getJavaSourceFromString(program);
1748+
1749+
compiler.getTask(null, null, null, null, null, fileObjects).call();
1750+
1751+
Class<?> clazz = Class.forName(className);
1752+
1753+
Method m = clazz.getMethod(methodName, new Class[] { String[].class });
1754+
1755+
Object[] _args = new Object[] { new String[0] };
1756+
1757+
String output = getMethodOutputWithInput(m, _args, input);
1758+
1759+
return output;
1760+
} catch (Exception e) {
1761+
return "Error: " + e;
1762+
}
1763+
1764+
}
1765+
17291766
private Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
17301767
final JavaSourceFromString jsfs;
17311768
jsfs = new JavaSourceFromString("code", code);

0 commit comments

Comments
 (0)