-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java: Add test for a JavacTool-based compiler that doesn't use standard JavaFileObjects #18305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
smowton
merged 2 commits into
github:main
from
smowton:smowton/admin/agent-extracted-file-test
Jan 14, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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() |
6 changes: 6 additions & 0 deletions
6
java/ql/lib/change-notes/2024-12-18-javac-tool-interception.md
This file contains hidden or 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,6 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * `JavacTool`-based compiler interception no longer requires an `--add-opens` directive when `FileObject.toUri` is accessible. | ||
| * `JavacTool`-based compiler interception no longer throws an exception visible to the program using `JavacTool` on failure to extract a file path from a passed `JavaFileObject`. | ||
| * `JavacTool`-based compiler interception now supports files that don't simply wrap a `file://` URL, such as a source file inside a JAR, or an in-memory file, but which do implement `getCharContent`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this get some kind of location based on
https://nonesuch.imaginary/somedir/Main.java?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the loc seen will be inside the db's scratch dir where the temporary is created. Almost necessarily the loc given by toURI or similar is going to be something vscode etc won't understand, like a jar-relative URI or the like; at least this location will be clickable, though considered outside the source-root. I haven't exposed it in this test because it isn't stable from test run to run (contains a temporary dirname).
I suggest we don't tackle this one at this juncture, since I'm not clear how we can give it a more useful location that makes sense to VSCode / another client, or the user.