Skip to content

Commit 5ca7d26

Browse files
committed
Add test for a JavacTool-based compiler that doesn't use standard JavaFileObjects
1 parent 3e10e78 commit 5ca7d26

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import javax.lang.model.element.Modifier;
2+
import javax.lang.model.element.NestingKind;
3+
import javax.tools.JavaCompiler;
4+
import javax.tools.JavaFileObject;
5+
import javax.tools.ToolProvider;
6+
import java.io.*;
7+
import java.net.URI;
8+
import java.util.List;
9+
import java.util.Objects;
10+
11+
public class Compiler {
12+
public static void main(String[] args) {
13+
14+
JavaCompiler.CompilationTask jc = ToolProvider.getSystemJavaCompiler().getTask(
15+
null, null, null, null, null,
16+
List.of(
17+
new JavaFileObject() {
18+
@Override
19+
public Kind getKind() {
20+
return Kind.SOURCE;
21+
}
22+
23+
@Override
24+
public boolean isNameCompatible(String simpleName, Kind kind) {
25+
return Objects.equals(simpleName, "Main");
26+
}
27+
28+
@Override
29+
public NestingKind getNestingKind() {
30+
return null;
31+
}
32+
33+
@Override
34+
public Modifier getAccessLevel() {
35+
return null;
36+
}
37+
38+
@Override
39+
public URI toUri() {
40+
return URI.create("https://nonesuch.imaginary/somedir/Main.java");
41+
}
42+
43+
@Override
44+
public String getName() {
45+
return "Main.java";
46+
}
47+
48+
@Override
49+
public InputStream openInputStream() throws IOException {
50+
return new ByteArrayInputStream(this.getCharContent(true).toString().getBytes());
51+
}
52+
53+
@Override
54+
public OutputStream openOutputStream() throws IOException {
55+
throw new IOException("No output allowed");
56+
}
57+
58+
@Override
59+
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
60+
return new StringReader(this.getCharContent(ignoreEncodingErrors).toString());
61+
}
62+
63+
@Override
64+
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
65+
return "public class Main { }";
66+
}
67+
68+
@Override
69+
public Writer openWriter() throws IOException {
70+
throw new IOException("No output allowed");
71+
}
72+
73+
@Override
74+
public long getLastModified() {
75+
return 0;
76+
}
77+
78+
@Override
79+
public boolean delete() {
80+
return false;
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "In-memory file with URI " + this.toUri();
86+
}
87+
}
88+
)
89+
);
90+
91+
jc.call();
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Main |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import commands
2+
3+
def test(codeql, java):
4+
commands.run("javac Compiler.java")
5+
codeql.database.create(command = "java Compiler")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from Class c
4+
where c.fromSource()
5+
select c.getName()

0 commit comments

Comments
 (0)