IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

huangapple go评论123阅读模式
英文:

IntelliJ indicating that enhanced switch block doesn't work despite using Java 14

问题

我正在尝试使用在Java 14中标准化的增强switch块。旧版本如下:

switch (expression) {
    case OPTION1:
        foo();
        break;
    case OPTION2:
        bar();
        break;
}

但是由于我使用的是Java 14,我应该能够使用这个:

switch (expression) {
    case OPTION1 -> foo();
    case OPTION2 -> bar();
}

这是一个最小可复现示例(MRE):

public class Main {
    public static void main(String[] args) {
        String demo = Math.random() > 0.5 ? "Hello" : "World";
        switch (demo) {
            case "Hello" -> System.out.println("字符串包含Hello");
            case "World" -> System.out.println("字符串包含World");
        }
    }
}

当我编写该代码时,IntelliJ会给我以下警告:

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

不支持语言级别14的增强'switch'块

然而,尽管这个警告被突出显示为编译时错误,但这是不正确的。该代码可以正确编译,并且switch块按预期工作,因为我正在使用Java 14。我为什么会收到这个警告?

编辑:根据请求的项目结构

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

编辑:我的iml文件

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library>
        <CLASSES>
          <root url="jar://$USER_HOME$/Downloads/MathParser.org-mXparser-v.4.4.0/MathParser.org-mXparser-v.4.4.0/bin/jdk13/MathParser.org-mXparser-v.4.4.0-jdk13.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>
英文:

I am trying to use the enhanced switch block that was standardized in Java 14. The old version is this:

switch (expression) {
    case OPTION1:
        foo();
        break;
    case OPTION2:
        bar();
        break;
}

however since I am using Java 14, I should be able to use this:

switch (expression) {
    case OPTION1 -&gt; foo();
    case OPTION2 -&gt; bar();
}

Here is a MRE:

public class Main {
    public static void main(String[] args) {
        String demo = Math.random() &gt; 0.5 ? &quot;Hello&quot;: &quot;World&quot;;
        switch (demo) {
            case &quot;Hello&quot; -&gt; System.out.println(&quot;The string contained Hello&quot;);
            case &quot;World&quot; -&gt; System.out.println(&quot;The string contained World&quot;);
        }
    }
}

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

When I write that code, IntelliJ gives me the following warning:

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

> Enhanced 'switch' blocks are not supported at language level '14'

However, that warning, despite being highlighted in red to indicate a compile time error, is incorrect. The code compiles correctly and the switch block works as intended because I am using Java 14. Why am I getting this warning?

Edit: Project structure per request

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

Edit: My iml file

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;module type=&quot;JAVA_MODULE&quot; version=&quot;4&quot;&gt;
  &lt;component name=&quot;NewModuleRootManager&quot; inherit-compiler-output=&quot;true&quot;&gt;
    &lt;exclude-output /&gt;
    &lt;content url=&quot;file://$MODULE_DIR$&quot;&gt;
      &lt;sourceFolder url=&quot;file://$MODULE_DIR$/src&quot; isTestSource=&quot;false&quot; /&gt;
    &lt;/content&gt;
    &lt;orderEntry type=&quot;inheritedJdk&quot; /&gt;
    &lt;orderEntry type=&quot;sourceFolder&quot; forTests=&quot;false&quot; /&gt;
    &lt;orderEntry type=&quot;module-library&quot;&gt;
      &lt;library&gt;
        &lt;CLASSES&gt;
          &lt;root url=&quot;jar://$USER_HOME$/Downloads/MathParser.org-mXparser-v.4.4.0/MathParser.org-mXparser-v.4.4.0/bin/jdk13/MathParser.org-mXparser-v.4.4.0-jdk13.jar!/&quot; /&gt;
        &lt;/CLASSES&gt;
        &lt;JAVADOC /&gt;
        &lt;SOURCES /&gt;
      &lt;/library&gt;
    &lt;/orderEntry&gt;
  &lt;/component&gt;
&lt;/module&gt;

答案1

得分: 1

Here's the translated content:

首先使用IntelliJ 2020

通常造成这种情况的原因是SDK、JDK和语言级别不一致,

在项目结构的项目选项卡中检查项目和模块是否一致,如下所示(好的,我正在使用语言级别11)

项目显示openjdk-14
ide插入了java14.iml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_14" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/com" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>
英文:

First use intellij 2020

Normally the reason for this is the sdk, jdk and language level are not aligned,

Check in the project structure project tab that Project and Modules are consistent see below (ok I am using language level 11)

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。
IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

Project showing openjdk-14
IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

ide inserted java14.iml file contents

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;module type=&quot;JAVA_MODULE&quot; version=&quot;4&quot;&gt;
  &lt;component name=&quot;NewModuleRootManager&quot; LANGUAGE_LEVEL=&quot;JDK_14&quot; inherit-compiler-output=&quot;true&quot;&gt;
    &lt;exclude-output /&gt;
    &lt;content url=&quot;file://$MODULE_DIR$&quot;&gt;
      &lt;sourceFolder url=&quot;file://$MODULE_DIR$/src/com&quot; isTestSource=&quot;false&quot; /&gt;
    &lt;/content&gt;
    &lt;orderEntry type=&quot;inheritedJdk&quot; /&gt;
    &lt;orderEntry type=&quot;sourceFolder&quot; forTests=&quot;false&quot; /&gt;
  &lt;/component&gt;
&lt;/module&gt;

答案2

得分: 0

I have tried this and it is working just fine I think you might forget to add the package name

package com.company;

public class Main
{
    public static void main(String[] args) {
        
        String demo = Math.random() > 0.5 ? "Hello" : "World";
        switch (demo)
        {
            case "Hello" -> System.out.println("The string contained Hello");
            case "World" -> System.out.println("The string contained World");
        }
    }
}

This is a screenshot for my project structure is try to compare it with yours

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

英文:

I have tried this and it is working just fine I think you might forget to add the package name

package com.company;

public class Main
{
    public static void main(String[] args) {
        
        String demo = Math.random() &gt; 0.5 ? &quot;Hello&quot;: &quot;World&quot;;
        switch (demo)
        {
            case &quot;Hello&quot; -&gt; System.out.println(&quot;The string contained Hello&quot;);
            case &quot;World&quot; -&gt; System.out.println(&quot;The string contained World&quot;);
        }
    }
}

This is a screenshot for my project structure is try to compare it with yours

IntelliJ指示增强的开关块不起作用,尽管使用了Java 14。

huangapple
  • 本文由 发表于 2020年8月12日 01:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63363479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定