英文:
What is the fully qualified class name of an enum defined locally within a method?
问题
在Java 16中,我们现在可以在方法内部本地定义枚举。这个功能是作为新的records功能的一部分而引入的。(顺便说一句,接口也可以是本地的。)
这样本地定义的枚举类的完全限定名是什么?
如果我要在单独的.java
文件中定义枚举,以下代码可以工作。我应该如何更改本地枚举的完全限定类名?
package work.basil.example.enums;
public class EnumMagic
{
public static void main ( String[] args )
{
EnumMagic app = new EnumMagic ( );
app.demo ( );
}
private void demo ( )
{
enum Shape { CIRCLE, SQUARE }
try
{
Class < ? > clazz = Class.forName ( "work.basil.example.enums.Shape" );
System.out.println ( "clazz = " + clazz );
}
catch ( ClassNotFoundException e ) { throw new RuntimeException ( e ); }
}
}
按照当前的编写方式,可以理解为我会得到一个java.lang.ClassNotFoundException: work.basil.example.enums.Shape
异常。
以下两者都不起作用:
"work.basil.example.enums.EnumMagic.Shape"
"work.basil.example.enums.EnumMagic.$Shape"
英文:
As of Java 16, we can now define an enum locally, within a method. This feature came as part of the work for the new records feature. (Interfaces too can be local, by the way.)
What is the fully-qualified name of such a locally-defined enum class?
The following code works if I were define the enum in a separate .java
file. How should I change this fully-qualified class name for a local enum?
package work.basil.example.enums;
public class EnumMagic
{
public static void main ( String[] args )
{
EnumMagic app = new EnumMagic ( );
app.demo ( );
}
private void demo ( )
{
enum Shape { CIRCLE, SQUARE }
try
{
Class < ? > clazz = Class.forName ( "work.basil.example.enums.Shape" );
System.out.println ( "clazz = " + clazz );
}
catch ( ClassNotFoundException e ) { throw new RuntimeException ( e ); }
}
}
As currently written, I understandably get an java.lang.ClassNotFoundException: work.basil.example.enums.Shape
.
Neither of these work:
"work.basil.example.enums.EnumMagic.Shape"
"work.basil.example.enums.EnumMagic.$Shape"
答案1
得分: 2
本地类、枚举和接口没有限定名称,只有一个简单名称。在您的情况下是:Shape
。来源:https://docs.oracle.com/javase/specs/jls/se15/preview/specs/local-statics-jls.html#jls-14.3。
英文:
Local classes, enums and interfaces don't have a qualified name. Just a simple name. In your case: Shape
From: https://docs.oracle.com/javase/specs/jls/se15/preview/specs/local-statics-jls.html#jls-14.3
答案2
得分: 1
我从Mar-Z的答案的评论中获取了以下信息。
Class#getName
您可以通过Java代码来查询所讨论的类的名称。调用Class#getName
,像这样:
Shape.class.getName()
示例:
System.out.println(Shape.class.getName());
> work.basil.example.enums.EnumMagic$1Shape
因此,我们可以将其插入到问题的代码中。
enum Shape { CIRCLE, SQUARE }
try
{
Class<?> clazz = Class.forName("work.basil.example.enums.EnumMagic$1Shape");
System.out.println("clazz = " + clazz);
}
catch (ClassNotFoundException e) { throw new RuntimeException(e); }
> clazz = class work.basil.example.enums.EnumMagic$1Shape
注意: 不幸的是,这个硬编码的类名字符串是脆弱的。名称可能会在您的代码的后续构建中发生更改。请参阅下面的评论中的讨论。
英文:
I gleaned the following info from the comments on the Answer by Mar-Z.
Class#getName
You can discover the name of the class in question by interrogating through Java code. Call Class#getName
, like this:
Shape.class.getName()
Example:
System.out.println ( Shape.class.getName() );
>work.basil.example.enums.EnumMagic$1Shape
So we can plug that into the Question’s code.
enum Shape { CIRCLE, SQUARE }
try
{
Class < ? > clazz = Class.forName ( "work.basil.example.enums.EnumMagic$1Shape" );
System.out.println ( "clazz = " + clazz );
}
catch ( ClassNotFoundException e ) { throw new RuntimeException ( e ); }
>clazz = class work.basil.example.enums.EnumMagic$1Shape
Caveat: Unfortunately, this hard-coded class name string is fragile. The name could change in later builds of your code. See discussion in Comments below.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论