-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for a JavacTool-based compiler that doesn't use standard Jav…
…aFileObjects
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
java/ql/integration-tests/java/javac-tool-custom-file/Compiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.NestingKind; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.ToolProvider; | ||
import java.io.*; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class Compiler { | ||
public static void main(String[] args) { | ||
|
||
JavaCompiler.CompilationTask jc = ToolProvider.getSystemJavaCompiler().getTask( | ||
null, null, null, null, null, | ||
List.of( | ||
new JavaFileObject() { | ||
@Override | ||
public Kind getKind() { | ||
return Kind.SOURCE; | ||
} | ||
|
||
@Override | ||
public boolean isNameCompatible(String simpleName, Kind kind) { | ||
return Objects.equals(simpleName, "Main"); | ||
} | ||
|
||
@Override | ||
public NestingKind getNestingKind() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Modifier getAccessLevel() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public URI toUri() { | ||
return URI.create("https://nonesuch.imaginary/somedir/Main.java"); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Main.java"; | ||
} | ||
|
||
@Override | ||
public InputStream openInputStream() throws IOException { | ||
return new ByteArrayInputStream(this.getCharContent(true).toString().getBytes()); | ||
} | ||
|
||
@Override | ||
public OutputStream openOutputStream() throws IOException { | ||
throw new IOException("No output allowed"); | ||
} | ||
|
||
@Override | ||
public Reader openReader(boolean ignoreEncodingErrors) throws IOException { | ||
return new StringReader(this.getCharContent(ignoreEncodingErrors).toString()); | ||
} | ||
|
||
@Override | ||
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { | ||
return "public class Main { }"; | ||
} | ||
|
||
@Override | ||
public Writer openWriter() throws IOException { | ||
throw new IOException("No output allowed"); | ||
} | ||
|
||
@Override | ||
public long getLastModified() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean delete() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "In-memory file with URI " + this.toUri(); | ||
} | ||
} | ||
) | ||
); | ||
|
||
jc.call(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
java/ql/integration-tests/java/javac-tool-custom-file/test.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
| Main | |
5 changes: 5 additions & 0 deletions
5
java/ql/integration-tests/java/javac-tool-custom-file/test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import commands | ||
|
||
def test(codeql, java): | ||
commands.run("javac Compiler.java") | ||
codeql.database.create(command = "java Compiler") |
5 changes: 5 additions & 0 deletions
5
java/ql/integration-tests/java/javac-tool-custom-file/test.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java | ||
|
||
from Class c | ||
where c.fromSource() | ||
select c.getName() |