redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.08k stars 440 forks source link

VSCode implicitly import java.lang.StringTemplate.STR which conflict with org.yaml.snakeyaml.nodes.Tag.STR #3510

Open renanfranca opened 8 months ago

renanfranca commented 8 months ago

VSCode implicitly import java.lang.StringTemplate.STR which conflict with org.yaml.snakeyaml.nodes.Tag.STR

Environment
Steps To Reproduce
  1. Clone the open source project https://github.com/jhipster/jhipster-lite
  2. Import the java project
  3. Go to file tech.jhipster.lite.module.infrastructure.secondary.YamlFileSpringPropertiesHandler.java
  4. See the compilation error (Type mismatch: cannot convert from StringTemplate.Processor<String,RuntimeException> to Tag)

[Screenshot] image

Current Result

I have to change the code only to run the project: FROM:

  private Node buildScalarNode(Object value) {
    Tag tag = STR;
    if (value instanceof Integer || value instanceof Long) {
      tag = INT;
    } else if (value instanceof Double || value instanceof Float) {
      tag = FLOAT;
    } else if (value instanceof Boolean) {
      tag = BOOL;
    }

    return new ScalarNode(tag, value.toString(), null, null, DumperOptions.ScalarStyle.PLAIN);
  }

TO:

  private Node buildScalarNode(Object value) {
    Tag tag = Tag.STR;
    if (value instanceof Integer || value instanceof Long) {
      tag = INT;
    } else if (value instanceof Double || value instanceof Float) {
      tag = FLOAT;
    } else if (value instanceof Boolean) {
      tag = BOOL;
    }

    return new ScalarNode(tag, value.toString(), null, null, DumperOptions.ScalarStyle.PLAIN);
  }
Expected Result

Is there a configuration to solve this problem without need to explicitly use Tag.STR?

Additional Informations
rgrunber commented 8 months ago

Reproducible in Eclipse & javac !

Eclipse image

Tag.java

package org.example;
public class Tag {
    public static final String STR = "test";
}

Main.java

package org.sample;
import static org.example.Tag.STR;
public class Main {
    public static void main(String[] args) {
        String value = STR;
        System.out.println(value);
    }
}
/usr/lib/jvm/java-21-openjdk/bin/javac --enable-preview --release 21 org/sample/Main.java 
org/sample/Main.java:5: error: reference to STR is ambiguous
        String value = STR;
                       ^
  both variable STR in StringTemplate and variable STR in Tag match
1 error

I think this comes down to how String Templates are defined in (JEP 430). It says :

STR is a public static final field that is automatically imported into every Java source file.

So STR becomes a kind of "restricted keyword". I think it always needs to be fully qualified now.

fbricon commented 8 months ago

@rgrunber javac doesn't fail without the "--enable-preview" flag. Neither does IJ IDEA. So the bug here is to expose STR when preview features are disabled. Definitely an upstream JDT issue.

rgrunber commented 8 months ago

String Templates are a preview feature in Java 21 though. If --enable-preview is not included, it's reasonable to assume the STR field doesn't get automatically injected, hence no ambiguity.

Update: Ok, after thinking a bit ( :stuck_out_tongue_winking_eye: ) I see your point. So users could use Java 21 (and eventually Java 22, it'll be preview there also) and if we disabled --enable-preview, it would continue to work. But what happens when the feature becomes finalized (ie. Java 23?). Then we can't protect them from the name clash.

fbricon commented 8 months ago

But what happens when the feature becomes finalized (ie. Java 23?). Then we can't protect them from the name clash.

Yes, but that's a Java 23 problem.

rgrunber commented 8 months ago

You're right. I forgot that managed projects (like Maven, Gradle) don't have preview features enabled by default. If preview features are disabled, and this is occuring then it's a bug in JDT Core.