openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.03k stars 302 forks source link

GroovyParser issue with statically imported constants #4070

Open Jnb1234 opened 4 months ago

Jnb1234 commented 4 months ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Gradle plugin, and my project is a single module project.

plugins {
    id 'maven-publish'
    id 'groovy'
    id 'idea'
    id 'net.researchgate.release' version '2.8.1'
    id "com.github.ben-manes.versions" version "0.36.0"
    id "com.gorylenko.gradle-git-properties" version "2.3.2"
    id 'java-library'
    id 'org.openrewrite.rewrite' version '6.8.4'
}

rewrite {
    activeRecipe("org.openrewrite.java.RemoveUnusedImports")
}

What is the smallest, simplest way to reproduce the problem?

package openrewrite.issues

import com.example.demo.AnotherClass

import static com.example.demo.AnotherClass.SOME_CONSTANT1

class ConstantIssue {
    AnotherClass anotherClass = new AnotherClass()
    public void showIssue() {
        Map a = new HashMap()
//        a.put(AnotherClass.SOME_CONSTANT1, "value")  // No parsing error
        a.put(SOME_CONSTANT1, "value")  // parsing error
    }
}

package com.example.demo;

public class AnotherClass {
    public static final String SOME_CONSTANT1 = "someConstant";
    public static final String SOME_CONSTANT2 = "someConstant";
    public static final String SOME_CONSTANT3 = "someConstant";
}

What did you expect to see?

No parsing issue

What did you see instead?

Get parsing issue

What is the full stack trace of any errors you encountered?

There were problems parsing some source files, run with --info to see full stack traces
There were problems parsing src\test\groovy\openrewrite\issues\ConstantIssue.groovy
All sources parsed, running active recipes: org.openrewrite.java.RemoveUnusedImports
Applying recipes would make no changes. No report generated.

Are you interested in contributing a fix to OpenRewrite?

timtebeek commented 4 months ago

Thanks for the very clear reproduction sample. Looks like you're running these recipes against some Groovy projects today; thanks for reporting the issues that you find. Up to now we had mostly focused on Gradle support through Groovy, but of course we shouldn't limit ourselves to that use case only, so then any real world use and reported issues help.

Jnb1234 commented 4 months ago

This issue has shown up in almost every parsing issue. I am hitting several Map issues as well... Hopefully will have an issue for this sometime today.