Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is this code not compiling with javac but has no errors in eclipse?

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?

like image 516
oshai Avatar asked Apr 30 '12 08:04

oshai


2 Answers

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.

like image 108
Aaron Digulla Avatar answered Sep 18 '22 12:09

Aaron Digulla


You have a , at the end of MinTimeDoubleCoListConstraintValidator.class, it is looking for another expression in the list.

like image 28
Deco Avatar answered Sep 18 '22 12:09

Deco