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

Document how to write custom Refaster rules #640

Open
mikybars opened this issue May 21, 2023 · 3 comments
Open

Document how to write custom Refaster rules #640

mikybars opened this issue May 21, 2023 · 3 comments
Labels
documentation A documentation update

Comments

@mikybars
Copy link

Problem

I couldn't find any documentation on how to write and apply your own Refaster rules. I've tried adding and enabling your Refaster rule compiler to the build like this:

dependencies {
    annotationProcessor("tech.picnic.error-prone-support:refaster-compiler:$errorProneSupportVersion")
    // ...
}
tasks.withType<JavaCompile>().configureEach {
    options.compilerArgs.add("-Xplugin:RefasterRuleCompiler")
    // ...
}

Then I get my regular Java classes with @BeforeTemplate and @AfterTemplate compiled into .refaster files inside build/classes.

Unfortunately, even though I have the Refaster runner dependency as well, I cannot see any of my custom rules kicking in. I get all the error-prone-support checks tough.

Am I missing something here?

@Stephan202
Copy link
Member

Hey @almibarss, thanks for reaching out!

I have ~zero experience with Gradle, but it sounds like (a) the Refaster rule compiler is properly picked up, since .refaster files are generated and (b) the Refaster bug checker is properly picked up, since the rules from this project are active.

My guess is then that that perhaps these two configurations are part of the same module? I don't expect that to work, because the Refaster check loads all *.refaster classpath resources during compilation of the first Java file, while RefasterRuleCompiler will only afterwards generate the custom *.refaster files.

A setup such as this one should work:

  1. Define a module A that runs with -Xplugin:RefasterRuleCompiler, and place the custom Refaster rule classes in this module.
  2. Define a module B that contains refaster-compiler and module A on the annotation processor classpath.

This way, during compilation of B, the rules defined by A should kick in.

I hope this helps! If not, I'd be happy to have a look at some reproduction code. (Though, given that I know little about Gradle, and looking at my TODO list, I wouldn't be able to commit to a timeline.)

@Stephan202 Stephan202 added documentation A documentation update and removed new feature labels May 21, 2023
@mikybars
Copy link
Author

Hi @Stephan202, thank you for your prompt response!

Your guess was absolutely right. And I must admit that I was guessing sort of the same thing. The generation and collection of *.refaster files from the class path were kind of interleaving so I needed to introduce an explicit dependency via Gradle.

Here's the link to my repo where I've implemented a number of common scenarios such as custom rules:

https://github.com/almibarss/error-prone-gradle-demo

I still think there should exist some documentation somewhere because implementing your own rules sounds like a pretty essential feature and should be streamlined as much as possible.

@Stephan202
Copy link
Member

@almibarss happy it worked out!

You're absolutely right about documentation! If you don't mind I'll reopen this issue to track that task. (Otherwise I can file a separate ticket and relabel this one as question.)

@Stephan202 Stephan202 reopened this May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation A documentation update
Development

No branches or pull requests

2 participants