Skip to content

Commit

Permalink
Reproduce issue #185 "InternalCompilerException while compiling varia…
Browse files Browse the repository at this point in the history
…bles initialized in while condition".
  • Loading branch information
aunkrig committed Nov 2, 2022
1 parent 2e0cc5f commit 7f574ea
Showing 1 changed file with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -1520,6 +1521,79 @@ interface Nukable {
Object[] nuke();
}

// -------------------------------

/**
* Supposed to reproduce https://github.com/janino-compiler/janino/issues/174#issuecomment-1243022653, but doesn't.
*/
@Test public void
testIssue174__JaninoCompilerTest() throws Exception {

// // public void compile_long_method(@TempDir Path tempDir) throws Exception {
String code = ReportedBugsTest.getClassCode(ReportedBugsTest.getClassBody2(1000));
// JaninoCompiler compiler = new JaninoCompiler();
// compiler.getArgs().setDestdir(tempDir.toFile().getAbsolutePath());
// compiler.getArgs().setSource(code, "Nuke.java");
// compiler.getArgs().setFullClassName("Nuke");
// compiler.compile();

// URL[] urls = new URL[]{tempDir.toUri().toURL()};
// ClassLoader cl = new URLClassLoader(urls);
// ClassLoader cl = sc.getClassLoader();
AbstractJavaSourceClassLoader cl = this.compilerFactory.newJavaSourceClassLoader();
cl.setSourceFinder(new MapResourceFinder(Collections.singletonMap("Nuke.java", code.getBytes("UTF-8"))));

Class<?> clazz = cl.loadClass("Nuke");
Object o = clazz.getDeclaredConstructor().newInstance();
System.out.println(o);
}

private static String
getClassCode(String body) {
return (
""
+ "import java.util.Arrays;\n\n"
+ "public class Nuke {"
+ body
+ "}"
);
}

private static String
getClassBody2(int numAssignments) {
String template = (
""
+ "public Object[] nuke() {\n"
+ "\tString bloat = \"some_bloat\";\n"
+ "\t//Lots of variables\n"
+ "%s"
+ "\t//Big array initialization\n"
+ "\treturn new Object[]\n"
+ "\t{\n"
+ "%s"
+ "\t};\n"
+ "}\n"
+ "\n"
+ ""
+ "public static void main(String[] args) throws Exception {"
+ "\tSystem.out.println(Arrays.toString(new Nuke().nuke()));"
+ "}"
);
StringBuilder assignments = new StringBuilder();
StringBuilder appends = new StringBuilder();
for (int i = 0; i < numAssignments; i++) {
assignments.append(String.format("\tfinal String current%s = bloat;\n", i));
appends.append(String.format("\t\tcurrent%s", i));
if (i < numAssignments - 1) {
appends.append(",");
}
appends.append("\n");
}
return String.format(template, assignments, appends);
}

// --------------------

@Test
public void
testIssue177() throws Exception {
Expand All @@ -1533,4 +1607,31 @@ interface Nukable {
+ ""
), "test2");
}

// TODO: Doesn't reproduce the problem!
@Test
public void
testIssue181() throws Exception {
this.assertCompilationUnitCookable(
""
+ "public\n"
+ "interface Main {\n"
+ " class Member {\n"
+ " Object i = new Object();\n"
+ " }\n"
+ "}\n"
);
}

@Test public void
testIssue185() throws Exception {
this.assertScriptCookable(
""
+ "java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));\n"
+ "String line;\n"
+ "while ((line = br.readLine()) != null) {\n"
+ " System.out.println(line);\n"
+ "}\n"
);
}
}

0 comments on commit 7f574ea

Please sign in to comment.