Skip to content
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

Issue #11604: allow 3rd party checks to print their own custom asts #12510

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rnveach
Copy link
Member

@rnveach rnveach commented Dec 8, 2022

Issue #11604

Currently 3rd parties have no way to provide their own AST to do some of the basics that Checkstyle does like printing the ASTs for users to understand it.

This provides a new option in the CLI so custom printers can be used. Other printers were very slightly reworked to this new method to show its usage.

It is hoped to use this method for other CLI functionality like printXpathBranch and printSuppressions.

@rnveach rnveach requested a review from romani December 8, 2022 22:58
@romani romani assigned pbludov and unassigned romani Dec 9, 2022
Copy link
Member

@pbludov pbludov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of minors

callTreeStringPrinter(filesToProcess.get(0),
Class.forName(options.customTreePrinterClass));
}
catch (ClassNotFoundException | IOException | CheckstyleException ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to catch CheckstyleException here. It just wraps one CheckstyleException with another:

com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to work with custom tree printer
	at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:308)
	at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:197)
	at com.puppycrawl.tools.checkstyle.Main.main(Main.java:132)
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to work with custom tree printer
	at com.puppycrawl.tools.checkstyle.Main.callTreeStringPrinter(Main.java:360)
	at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:304)
	... 2 more

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in the next push.

src/main/java/com/puppycrawl/tools/checkstyle/Main.java Outdated Show resolved Hide resolved
@rnveach rnveach force-pushed the issue_11604_tree_string_printer branch from 824310c to 1706e7b Compare December 11, 2022 16:33
if (suppressionLineColumnNumber != null || configurationFile != null
|| propertiesFile != null || outputPath != null
|| parseResult.hasMatchedOption(OUTPUT_FORMAT_OPTION)) {
result.add("Option '-t' cannot be used with other options.");
result.add("Tree Options cannot be used with other options.");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wording was identified as out of date and confusing for options where -t is not used.

Examples:

PS M:\> java -jar .\checkstyle-10.5.0-all.jar -s out -t .
Option '-t' cannot be used with other options.
PS M:\> java -jar .\checkstyle-10.5.0-all.jar -s out -T .
Option '-t' cannot be used with other options.
PS M:\> java -jar .\checkstyle-10.5.0-all.jar -s out -J .
Option '-t' cannot be used with other options.

@rnveach rnveach force-pushed the issue_11604_tree_string_printer branch from 1706e7b to c61064b Compare December 11, 2022 16:58
@rnveach rnveach force-pushed the issue_11604_tree_string_printer branch from c61064b to 8b48ac1 Compare December 12, 2022 00:14
@pbludov pbludov assigned strkkk and unassigned pbludov Dec 12, 2022
Copy link
Member

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What alternatives have been considered to solve this problem? Extension of CLI/API like this deserves it's own issue/discussion IMO. Won't we need to add "alternative" options for others, such as -g, -s, and -b?

@rnveach
Copy link
Member Author

rnveach commented Dec 12, 2022

Won't we need to add "alternative" options for others, such as -g, -s, and -b?

See the end of my first post at #12510 (comment) .
It is hoped to use this method for other CLI functionality like printXpathBranch and printSuppressions.

I was planning things in future PRs after this one.

deserves it's own issue

I can create the issue if that is what is needed.

What alternatives have been considered to solve this problem?

There is no alternative in Checkstyle as we don't allow any extension of the CLI. If this PR isn't acceptable, then there isn't a lot of good options.

3rd parties could create a hack of a check who's only function is to print the tree of the file it is given, which would require a configuration file for nothing more than specifying the tree printer which I am passing in as an argument in this PR.

Other than that, 3rd parties will have to duplicate Checkstyle's CLI as they will need the same options (plus their own) if we were to allow them to use checkstyle/contribution#604 .

@nrmancuso
Copy link
Member

There is no alternative in Checkstyle as we don't allow any extension of the CLI

Is allowing extension of CLI another option? Then people that want to extend CLI functionality can do whatever they want, and not be bothered by activities in main project.

@rnveach
Copy link
Member Author

rnveach commented Dec 13, 2022

Is allowing extension of CLI another option?

I am open to this option, but I am not fully seeing how to be honest.

Any extension is based picocli allowing this as it is the main controller of the CLI and probably some overridable methods to allow such custom execution.

https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L629
I am going to assume first this would have to be made a public class so 3rd parties can extend it. I am not sure if picocli will accept that.

https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L113-L114
Somehow this has to be overridable that 3rd parties can provide their new CLI instance with their new options.

https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L188
https://github.com/checkstyle/checkstyle/blob/master/src/main/java/com/puppycrawl/tools/checkstyle/Main.java#L195
Then we would need to override these remaining methods for the main logic and ensure they stay of main repo's way if a 3rd party option is provided. runCheckstyle method might also have to be made protected so 3rd parties can call it.

If this is possible, I am seeing this require main repo make classes public and methods protected to be able to do an extends on it.

And even if we do this, things like generateXpathSuppressionsFile will probably not be possible without even more hacking entry points. The prints are easiest to integrate hence why I started this PR with them. It may be possible if that section of code was method'ed off to the side.

@nrmancuso
Copy link
Member

If we want to inject dependencies, we could do something like:

public class Main {
    
    CliOptions clioptions; // injected, can read concrete class name from property file, bean, etc.
    CliRunner cliRunner; // injected, replaces `runCli` and other functionality, also can specify class via property file, bean, etc.

    public static void main(String... args){
        Main m = new Main();
        m.run(args);
    }

    private void run(String... args) {
        int e = cliRunner.execute(cliOptions);
        Runtime.getRuntime().exit(e);
    }
...

* Interface for printing ASTs to String.
*/
@FunctionalInterface
public interface TreeStringPrinter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont like this design, to be honest.

method name like printFileAst assumes that

  1. there is no return, similar to System.out.println() and similar methods.
  2. Result is the AST, which is weird since return type is String, which means any text.

Plus, name of the interface is confusing, since even existing implementations dont do any printing of the AST, they just convert it into string. Printing is performed outside, in Main.java line 369.

Therefore I suggest to rename it to something like FileConverter (or something like this) with meaningful method name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nrmancuso @strkkk @pbludov
Is there any agreement we can make on design before I continue? @nrmancuso Was mentioning extension over something being built into the project. For @strkkk 's comment, everything was originally printFileAst so thats why I went with this, but if we head down extension, then naming becomes not an issue as 3rd parties will be the ones to name it themselves.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make

private static class CliOptions {
a public class to allow extension of options? There is bound to be more things that some third party may need to do with the CLI, and this way they would not be blocked by limitations of checksytle CLI.

@nrmancuso nrmancuso removed their assignment Jan 11, 2023
@romani romani added the blocked label Mar 6, 2023
@romani
Copy link
Member

romani commented Mar 6, 2023

I do not like this extension of our API, i think it should be done completely in thirdparty projects such custom support.

@romani
Copy link
Member

romani commented Dec 1, 2023

no more ideas or points to reconcile disfigurements.
I am closing this PR.
We will focus in our api on what we need, thirparty projects can clone us and hack us as they want to get what they need, we will not make it as our contract to support.
We already have bunch of implementations and other unfortunate decisions in our API we need to focus on our needs only.

@romani romani closed this Dec 1, 2023
@romani romani reopened this Apr 27, 2024
@romani
Copy link
Member

romani commented Apr 27, 2024

can we use this property and print tree in format thatwe need at #14631 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants