[Checkers] Anonymous Constructors

Michael Ernst mernst at csail.mit.edu
Sat Jun 28 14:08:38 EDT 2008


Mahmood-

Thanks for these very informative and helpful details.  You're doing a
great job of identifying, analyzing, and explaining these corner cases,
which I really appreciate!  And thanks for adding the test cases.

In fact, this email is so helpful that you might even want to insert the
main content of it (even including the examples, though "odd" makes the
argument sound like a number rather than a string) into a Java comment at
the appropriate place in the code.  Otherwise, someone else will run across
it, not have your knowledge and insight, and get confused.

Another solution, by the way (that you should perhaps note) is for the
javac compiler to copy the annotations to the generated constructors.
This is proposed in
  http://groups.csail.mit.edu/pag/jsr308/specification/java-annotation-design.html#sec:mods
What do you see as the tradeoffs between the two?  Should we change
the compiler rather than the framework?

And, can you add a test for bridge methods, analogous to the one you wrote
for anonymous constructors?

                     Thanks,

                    -Mike


> Anonymous constructors are constructors inserted by the compiler  
> according to JLS 15.9.5.1 (http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.9.5.1 
> ), which have exactly one method that invokes their super class  
> constructor.
> 
> They cannot type-check using any type system built on JSR308.  The  
> anonymous constructor does contain any of the annotations that the  
> real invoked constructor actually has.  Consider the following classes:
> 
>    // class with a constructor that only takes odd String
>    class A { A(@Odd String oddString) { } }
> 
>    void testExtendingA() {
>       new A("m") { };
>    }
> 
> The compiler will turn 'new A("m") { };' into:
>     new A("m") {
>        <init>(java.lang.String x0) {
>            super(x0);
>        }
>     };
> 
> Now the inner constructor super invocation doesn't type check, since  
> x0 is not specified to be @Odd.
> 
> In the case of new class instantiation, the  
> framework type checks the new class tree arguments against the  
> constructor arguments in the constructor in A rather than the  
> anonymous constructor that wouldn't have any annotations.



More information about the checkers mailing list