为什么这段代码没有用 javac 编译,但在 eclipse 中没有错误? [英] why is this code not compiling with javac but has no errors in eclipse?
问题描述
以下代码:
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE })
@Constraint(validatedBy = {
MinTimeIntCoConstraintValidator.class,
MinTimeIntCoListConstraintValidator.class,
MinTimeDoubleCoConstraintValidator.class,
MinTimeDoubleCoListConstraintValidator.class,
})
@Documented
public @interface MinTimeValueCo
{
int value();
String message() default "value does not match minimum requirements";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default {};
}
在eclipse中编译但在sun/oracle编译器中编译失败:
compiled in eclipse but fails to compile in sun/oracle compiler:
> MinTimeValueCo.java:19: illegal start of expression
> [javac] })
> [javac] ^
> [javac] 1 error
这是因为 MinTimeDoubleCoListConstraintValidator.class,
后面的逗号造成的.
This happened because of the comma after MinTimeDoubleCoListConstraintValidator.class,
.
当我删除逗号时它工作正常:
when I removed the comma it works fine:
@Constraint(validatedBy = {
MinTimeIntCoConstraintValidator.class,
MinTimeIntCoListConstraintValidator.class,
MinTimeDoubleCoConstraintValidator.class,
MinTimeDoubleCoListConstraintValidator.class
})
我使用的是 jdk 1.6.0.10.
你知道为什么这是非法的并在 eclipse 中编译吗?
I am using jdk 1.6.0.10.
Do you know why this is illegal and compiling in eclipse?
推荐答案
这是 Java 6 的 javac
中的一个错误.JLS 允许尾随逗号在某些地方,Eclipse 编译器遵循此处的标准,而 Java 6 从不允许在任何地方使用尾随逗号.
This is a bug in Java 6's javac
. The JLS allows trailing commas in some places and the Eclipse compiler follows the standard here while Java 6 never allows trailing commas anywhere.
您可以尝试使用 Java 7 中的 javac
和选项 -source 6 -target 6
(以获得 Java 6 兼容的字节码)来编译您的代码.如果错误仍然存在,归档.它可能会得到修复.
You can try to compile your code with javac
from Java 7 with the options -source 6 -target 6
(to get Java 6 compatible byte code). If the bug is still there, file it. It might get fixed.
这篇关于为什么这段代码没有用 javac 编译,但在 eclipse 中没有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!