为什么这个代码不编译与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()defaultvalue does not match minimum requirements;
Class<?> [] groups()default {};
Class< ;? extends Payload> [] payload()default {};在eclipse中编译但无法在sun / oracle编译器中编译:
}
>
> MinTimeValueCo.java:19:表达式的非法开始
> [javac]})
> [javac] ^
> [javac] 1错误
这是因为后的逗号而发生的MinTimeDoubleCoListConstraintValidator.class ,
。
当我删除逗号,它工作正常:
@Constraint(validatedBy = {
MinTimeIntCoConstraintValidator.class,
MinTimeIntCoListConstraintValidator.class,
MinTimeDoubleCoConstraintValidator.class,
MinTimeDoubleCoListConstraintValidator.class
}
我使用jdk 1.6.0.10。
你知道为什么 这是Java 6的 javac $ c $中的一个错误c>。 JLS在某些地方允许尾随逗号,而Eclipse编译器遵循这里的标准,而Java 6从不允许在任何地方使用尾随逗号。
您可以尝试使用 javac
从Java 7使用选项 -source 6 -target 6
(获取Java 6兼容的字节代码)。如果错误仍然存在,请提交。它可能会修复。
the following code:
@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 {};
}
compiled in eclipse but fails to compile in sun/oracle compiler:
> MinTimeValueCo.java:19: illegal start of expression
> [javac] })
> [javac] ^
> [javac] 1 error
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
})
I am using jdk 1.6.0.10.
Do you know why this is illegal and compiling in eclipse?
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.
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屋!