[Checkers] Anonymous Constructors

Mahmood Ali mahmood at MIT.EDU
Mon Jun 23 16:49:45 EDT 2008


This email is to address the anonymous constructor item in the TODO  
file.  It states:

> Mahmood said (around 3/1/2008):
>   4. NullPointerException and StackOverflow when visiting anonymous
>   constructors for inner classes.
>   I haven't investigated the issue thoroughly and it might actually a
>   combination of the previous ones.
>   For now, I disabled the checks for the assignments made within
>   anonymous constructors.
> This should be fixed.  (Is it already fixed?)
> Perhaps if the regular javac typechecker issues a warning/error,  
> then the
> JSR 308 javac should not run the pluggable checkers.

The issue mentioned there was fixed. Yet, there is a bit of  
complications, so I wanted to address them here.

Anonymous constructors are constructors inserted by the compiler  
according to JLS (http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html# 
), which have exactly one method that invokes their super class  

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) {

Now the inner constructor super invocation doesn't type check, since  
x0 is not specified to be @Odd.

I just made a check-in that in the case of new class initiation, the  
framework would type check 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.  There is an  
accompanied test for it too.

- Mahmood

More information about the checkers mailing list