RefactorFirst icon indicating copy to clipboard operation
RefactorFirst copied to clipboard

Feature: Gradle Plugin

Open FxMorin opened this issue 3 months ago • 2 comments

Initial gradle plugin support. This is a rough implementation of the gradle plugin. I likely won't have any more time to work on this for a while. All code examples are written with Kotlin DSL

Building the jar

If you aren't using a published version of the plugin, you need to create the jar manually. Otherwise you can skip this

  • mvn install
  • ./refactor-first-gradle-plugin/gradlew shadowJar
  • Take the jar from ./refactor-first-gradle-plugin/build/libs/refactor-first-gradle-plugin-<version>-all.jar and place it in your gradle project root.

Using the plugin

If you are using a jar:

In your project's build.gradle.kts you would add:

buildscript {
    dependencies {
        classpath(files("refactor-first-gradle-plugin-<version>-all.jar"))
    }
}

If you are using the gradle plugin portal:

In your project's build.gradle.kts you would add:

repositories {
    gradlePluginPortal()
}

Will probably also need to implement it.

Load the plugin

apply(plugin = "org.hjug.refactor-first")

Once you've synced gradle, you should see some new tasks within your root. image Run them like you would any other tasks.

TODO:

  • [ ] Update README.md with steps on how to use the gradle plugin
  • [ ] Add plugin-publish back
  • [ ] Figure out why the jar is so large
  • [ ] Test on many projects to make sure it works correctly

Attempt

I attempted to run the gradle plugin on my game engine, and got the following error:

The error
Caused by: java.lang.NoSuchMethodError: 'void org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.<init>(java.util.UUID, org.openrewrite.java.tree.Space, org.openrewrite.marker.Markers, org.openrewrite.java.tree.VariableDeclarator, java.util.List, org.openrewrite.java.tree.JLeftPadded, org.openrewrite.java.tree.JavaType$Variable)' at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitVariables(ReloadableJava21ParserVisitor.java:1765) at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitVariable(ReloadableJava21ParserVisitor.java:1679) at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitVariable(ReloadableJava21ParserVisitor.java:76) at com.sun.tools.javac.tree.JCTree$JCVariableDecl.accept(JCTree.java:1040) at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92) at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.convert(ReloadableJava21ParserVisitor.java:1839) ... 156 more org.openrewrite.java.JavaParsingException: Failed to convert for the following cursor stack:--- BEGIN PATH --- JCCompilationUnit(sourceFile = /path/to/some/class.java) --- END PATH ---
at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.reportJavaParsingException(ReloadableJava21ParserVisitor.java:1875)
at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.convert(ReloadableJava21ParserVisitor.java:1842)
at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.convertAll(ReloadableJava21ParserVisitor.java:1935)
at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitCompilationUnit(ReloadableJava21ParserVisitor.java:632)
at org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitCompilationUnit(ReloadableJava21ParserVisitor.java:76)
at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:623)
at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
at org.openrewrite.java.isolated.ReloadableJava21Parser.lambda$parseInputs$1(ReloadableJava21Parser.java:189)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:212)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) //7 internal lines
at org.hjug.graphbuilder.JavaGraphBuilder.processWithOpenRewrite(JavaGraphBuilder.java:77)
at org.hjug.graphbuilder.JavaGraphBuilder.getClassReferences(JavaGraphBuilder.java:38)
at org.hjug.cbc.CycleRanker.generateClassReferencesGraph(CycleRanker.java:30)
at org.hjug.cbc.CycleRanker.performCycleAnalysis(CycleRanker.java:40)
at org.hjug.refactorfirst.report.SimpleHtmlReport.generateReport(SimpleHtmlReport.java:208)
at org.hjug.refactorfirst.report.SimpleHtmlReport.execute(SimpleHtmlReport.java:109)
at org.hjug.gradlereport.SimpleHtmlReportTask.generate(SimpleHtmlReportTask.java:23) //54 internal lines
I suspected this is likely due to the fact that the project is written in Java 22. However, I than also got this error in another project:
The error
org.openrewrite.java.JavaParsingException: Failed symbol entering or attribution at org.openrewrite.java.isolated.ReloadableJava21Parser.parseInputsToCompilerAst(ReloadableJava21Parser.java:244) at org.openrewrite.java.isolated.ReloadableJava21Parser.parseInputs(ReloadableJava21Parser.java:173) at org.openrewrite.java.Java21Parser.parseInputs(Java21Parser.java:39) at org.openrewrite.Parser.parse(Parser.java:59) at org.hjug.graphbuilder.JavaGraphBuilder.processWithOpenRewrite(JavaGraphBuilder.java:76) at org.hjug.graphbuilder.JavaGraphBuilder.getClassReferences(JavaGraphBuilder.java:38) at org.hjug.cbc.CycleRanker.generateClassReferencesGraph(CycleRanker.java:30) at org.hjug.cbc.CycleRanker.performCycleAnalysis(CycleRanker.java:40) at org.hjug.refactorfirst.report.SimpleHtmlReport.generateReport(SimpleHtmlReport.java:208) at org.hjug.refactorfirst.report.SimpleHtmlReport.execute(SimpleHtmlReport.java:109) at org.hjug.gradlereport.HtmlReportTask.generate(HtmlReportTask.java:23) //123 internal lines Caused by: java.lang.AssertionError at com.sun.tools.javac.util.Assert.error(Assert.java:155) at com.sun.tools.javac.util.Assert.checkNonNull(Assert.java:62) at com.sun.tools.javac.util.ListBuffer.append(ListBuffer.java:127) at com.sun.tools.javac.comp.Attr$1.visitYield(Attr.java:1637) at com.sun.tools.javac.tree.JCTree$JCYield.accept(JCTree.java:1677) at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:50) at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:58) at com.sun.tools.javac.comp.Attr.lambda$visitSwitchExpression$7(Attr.java:1644) //24 internal lines at org.openrewrite.java.isolated.ReloadableJava21Parser.parseInputsToCompilerAst(ReloadableJava21Parser.java:240) ... 135 more org.openrewrite.internal.RecipeRunException: java.lang.NullPointerException: Cannot invoke "org.openrewrite.java.tree.JRightPadded.getElement()" because "this.containing" is null at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:290) at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:157) at org.openrewrite.java.JavadocVisitor.javaVisitorVisit(JavadocVisitor.java:38) at org.openrewrite.java.JavadocPrinter.visitInlinedValue(JavadocPrinter.java:148) at org.openrewrite.java.JavadocPrinter.visitInlinedValue(JavadocPrinter.java:29) at org.openrewrite.java.tree.Javadoc$InlinedValue.acceptJavadoc(Javadoc.java:282) at org.openrewrite.java.tree.Javadoc.accept(Javadoc.java:39) at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250) //33 internal lines at org.openrewrite.java.isolated.ReloadableJava21Parser.lambda$parseInputs$1(ReloadableJava21Parser.java:192) at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133) //7 internal lines at org.hjug.graphbuilder.JavaGraphBuilder.processWithOpenRewrite(JavaGraphBuilder.java:77) at org.hjug.graphbuilder.JavaGraphBuilder.getClassReferences(JavaGraphBuilder.java:38) at org.hjug.cbc.CycleRanker.generateClassReferencesGraph(CycleRanker.java:30) at org.hjug.cbc.CycleRanker.performCycleAnalysis(CycleRanker.java:40) at org.hjug.refactorfirst.report.SimpleHtmlReport.generateReport(SimpleHtmlReport.java:208) at org.hjug.refactorfirst.report.SimpleHtmlReport.execute(SimpleHtmlReport.java:109) at org.hjug.gradlereport.HtmlReportTask.generate(HtmlReportTask.java:23 //112 internal lines
This project is written in Java 21. It gave some results, but the data was clearly missing most classes.

I than attempted it with https://github.com/Mojang/DataFixerUpper and got the same issues.
I'm not sure if this is gradle related or not.

FxMorin avatar Sep 15 '25 19:09 FxMorin

Thank you so much @FxMorin! This is a tremendous gift - I will pick up work on this once I've finished working on the Directed Feedback Arc & Vertex Set detection I'm working on at the moment.

jimbethancourt avatar Sep 16 '25 13:09 jimbethancourt

I will try to take @aalmiray's contribution and incorporate your work into his so it all builds together without needing to copy jars.

jimbethancourt avatar Sep 16 '25 13:09 jimbethancourt