-
Notifications
You must be signed in to change notification settings - Fork 4
NEW @W-21102140@ - Add PMD AST dump API for programmatic AST generation #434
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
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cd70561
NEW @W-21102126@ - PMD java wrapper for ast-dump (#423)
aruntyagiTutu 4cde80c
NEW @W-21102140@ - pmdwrapper for ast dump typescript pmd wrapper (#…
aruntyagiTutu f821c96
New @W-21102140@ - engine api for astdump (#425)
aruntyagiTutu df3acc9
Merge branch 'dev' into feature/pmd-ast-dump
aruntyagiTutu d52a38a
Add comprehensive tests for PMD AST dump and remove implementation plan
aruntyagiTutu ce4978e
Fix OOM risk: Remove unnecessary file content loading in AST dump
aruntyagiTutu 61cf998
Keep PmdEngine and CpdEngine internal - don't export classes
aruntyagiTutu 73b8cf3
Re-export PmdEngine for AST generation API access
aruntyagiTutu f867f41
Merge branch 'dev' into feature/pmd-ast-dump
aruntyagiTutu e439bea
updated vars name
aruntyagiTutu 5596c58
return e.message in place of full trace
aruntyagiTutu 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
...ne/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumpInputData.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,22 @@ | ||
| package com.salesforce.sfca.pmdwrapper; | ||
|
|
||
| /** | ||
| * Input data structure for AST dump command | ||
| */ | ||
| public class PmdAstDumpInputData { | ||
| /** | ||
| * The language of the file to dump AST for (e.g., "apex", "xml", "visualforce") | ||
| */ | ||
| public String language; | ||
|
|
||
| /** | ||
| * Single file to generate AST for | ||
| */ | ||
| public String fileToDump; | ||
|
|
||
| /** | ||
| * Character encoding for reading the file | ||
| * Defaults to "UTF-8" if not specified | ||
| */ | ||
| public String encoding = "UTF-8"; | ||
| } |
26 changes: 26 additions & 0 deletions
26
...gine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumpResults.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,26 @@ | ||
| package com.salesforce.sfca.pmdwrapper; | ||
|
|
||
| import com.salesforce.sfca.shared.ProcessingError; | ||
|
|
||
| /** | ||
| * Results structure for AST dump command. | ||
| * Contains either the AST (if successful) or an error (if failed), but never both. | ||
| */ | ||
| public class PmdAstDumpResults { | ||
| /** | ||
| * Full path to the file that was processed | ||
| */ | ||
| public String file; | ||
|
|
||
| /** | ||
| * The AST representation in XML format | ||
| * This is populated if the AST generation was successful, null otherwise | ||
| */ | ||
| public String ast; | ||
|
|
||
| /** | ||
| * Error details if AST generation failed | ||
| * This is populated if the AST generation failed, null otherwise | ||
| */ | ||
| public ProcessingError error; | ||
| } |
122 changes: 122 additions & 0 deletions
122
...md-engine/pmd-cpd-wrappers/src/main/java/com/salesforce/sfca/pmdwrapper/PmdAstDumper.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,122 @@ | ||
| package com.salesforce.sfca.pmdwrapper; | ||
|
|
||
| import com.salesforce.sfca.shared.ProcessingError; | ||
| import net.sourceforge.pmd.lang.Language; | ||
| import net.sourceforge.pmd.lang.LanguageRegistry; | ||
| import net.sourceforge.pmd.util.treeexport.TreeExportConfiguration; | ||
| import net.sourceforge.pmd.util.treeexport.TreeExporter; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.PrintStream; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * Core class that performs AST dumping using PMD's TreeExporter API | ||
| */ | ||
| public class PmdAstDumper { | ||
|
|
||
| /** | ||
| * Dumps the AST for a single file in XML format | ||
| * | ||
| * @param inputData Input data containing language, file path, and encoding | ||
| * @return Results containing either the AST (if successful) or error details (if failed) | ||
| */ | ||
| public PmdAstDumpResults dump(PmdAstDumpInputData inputData) { | ||
| validateInputData(inputData); | ||
|
|
||
| PmdAstDumpResults results = new PmdAstDumpResults(); | ||
| results.file = inputData.fileToDump; | ||
|
|
||
| try { | ||
| System.out.println("Generating AST for file '" + inputData.fileToDump + "' with language '" + inputData.language + "'"); | ||
|
|
||
| // Verify file exists and is valid (lightweight validation without reading content) | ||
| Path filePath = Paths.get(inputData.fileToDump); | ||
| validateFilePath(filePath, inputData.encoding); | ||
|
|
||
| // Get language | ||
| Language language = LanguageRegistry.PMD.getLanguageById(inputData.language); | ||
| if (language == null) { | ||
| throw new RuntimeException("Language not supported: " + inputData.language); | ||
| } | ||
|
|
||
| // Create TreeExportConfiguration | ||
| TreeExportConfiguration config = new TreeExportConfiguration(); | ||
| config.setLanguage(language); | ||
| config.setFormat("xml"); // Always XML format for v1 | ||
aruntyagiTutu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| config.setFile(filePath); | ||
|
|
||
| // Capture output to string | ||
| ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
| PrintStream capturedPrintStream = new PrintStream(outputStream, true, StandardCharsets.UTF_8); | ||
| PrintStream originalOut = System.out; | ||
|
|
||
| try { | ||
aruntyagiTutu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Redirect System.out to capture XML output | ||
| System.setOut(capturedPrintStream); | ||
|
|
||
| // Create and export AST (TreeExporter writes to System.out) | ||
| TreeExporter exporter = new TreeExporter(config); | ||
| exporter.export(); | ||
|
|
||
| // Get the XML output | ||
| results.ast = outputStream.toString(StandardCharsets.UTF_8); | ||
|
|
||
| } finally { | ||
| // Restore original System.out | ||
| System.setOut(originalOut); | ||
| capturedPrintStream.close(); | ||
| } | ||
|
|
||
| System.out.println("Successfully generated AST for file '" + inputData.fileToDump + "'"); | ||
|
|
||
| } catch (Exception e) { | ||
| // Store processing error | ||
| System.err.println("Error generating AST for file '" + inputData.fileToDump + "': " + e.getMessage()); | ||
aruntyagiTutu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ProcessingError error = new ProcessingError(); | ||
| error.file = inputData.fileToDump; | ||
| error.message = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(); | ||
| error.detail = e.toString(); | ||
| results.error = error; | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Validates the input data | ||
| */ | ||
| private void validateInputData(PmdAstDumpInputData inputData) { | ||
| if (inputData.language == null || inputData.language.trim().isEmpty()) { | ||
| throw new RuntimeException("The 'language' field is required"); | ||
| } | ||
| if (inputData.fileToDump == null || inputData.fileToDump.trim().isEmpty()) { | ||
| throw new RuntimeException("The 'fileToDump' field is required"); | ||
| } | ||
| if (inputData.encoding == null || inputData.encoding.trim().isEmpty()) { | ||
| inputData.encoding = "UTF-8"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates file path and encoding without reading file content. | ||
| * This avoids loading large files into memory unnecessarily. | ||
| * PMD's TreeExporter will handle reading the file content. | ||
| */ | ||
| private void validateFilePath(Path filePath, String encoding) throws IOException { | ||
| if (!Files.exists(filePath)) { | ||
| throw new IOException("File not found: " + filePath); | ||
| } | ||
| if (!Files.isRegularFile(filePath)) { | ||
aruntyagiTutu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new IOException("Not a regular file: " + filePath); | ||
| } | ||
|
|
||
| // Validate encoding by attempting to get the Charset (throws if invalid) | ||
| Charset.forName(encoding); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Do we need to log this instead?
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.
We are intentionally using System.out.println here to maintain consistency
with the rest of the codebase (see PmdRuleDescriber.java:99 and
CpdRunner.java:55). The project uses slf4j-nop and relies on stdout for
inter-process communication with the TypeScript side.
am I missing something @namrata111f ?
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.
Understood, was not aware that inter-process communication relies on stdout. Just curious for debugging do we log these stdout on the TypeScript side?