我遇到了以下 javac 编译失败,其中 javac 无法识别具有公共枚举的静态嵌套类上的注释。一旦我将枚举移出静态嵌套类,编译错误就解决了。有谁知道为什么 javac 失败了?这是一个java编译器错误吗?还是有我不知道的Java细微差别?
下面是一个独立的测试用例。
编译失败:
package test;
import test.AnnotationBug.NestedClassWithEnum.ParticipantType;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.googlecode.objectify.annotation.Embed;
public class AnnotationBug {
ParticipantType type;
@Embed
@Data
@NoArgsConstructor
public static final class NestedClassNoEnum {
}
@Embed
@Data
@NoArgsConstructor
public static final class NestedClassWithEnum {
ParticipantType type;
public enum ParticipantType {
ORGANIZER,
REGISTERED,
WAIT_LISTED
}
}
}
编译输出:
$ javac -classpath /home/avaliani/projects/jars/objectify-4.0b2.jar:/home/avaliani/projects/jars/lombok.jar test/AnnotationBug.java
test/AnnotationBug.java:20: error: cannot find symbol
@Embed
^
symbol: class Embed
location: class AnnotationBug
test/AnnotationBug.java:21: error: cannot find symbol
@Data
^
symbol: class Data
location: class AnnotationBug
test/AnnotationBug.java:22: error: cannot find symbol
@NoArgsConstructor
^
symbol: class NoArgsConstructor
location: class AnnotationBug
编译:
package test;
// import test.AnnotationBug.NestedClassWithEnum.ParticipantType;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.googlecode.objectify.annotation.Embed;
public class AnnotationBug {
ParticipantType type;
@Embed
@Data
@NoArgsConstructor
public static final class NestedClassNoEnum {
}
@Embed
@Data
@NoArgsConstructor
public static final class NestedClassWithEnum {
ParticipantType type;
}
public enum ParticipantType {
ORGANIZER,
REGISTERED,
WAIT_LISTED
}
}
编译没有错误:
$ javac -classpath /home/avaliani/projects/jars/objectify-4.0b2.jar:/home/avaliani/projects/jars/lombok.jar test/AnnotationBug.java
需要指出的事情:
1)注意编译失败的行号。解析 NestedClassNoEnum 的注解没有问题。
2)Java版本:
$ java -version
java version "1.7.0_21"
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-0ubuntu0.12.10.1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)