Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6481655
Votes 4
Synopsis Parser confused by combination of parens and explicit type args
Category java:compiler
Reported Against
Release Fixed 7(b27)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 6318240 , 6665356
Submit Date 13-OCT-2006
Description
Consider the following program, Util.java:

public class Util {
    @SuppressWarnings("unchecked")
    public static <T> T cast(Object x) {
	return (T) x;
    }

    static {
	Util.<Object>cast(null);
	(Util.<Object>cast(null)).getClass();
    }
}

The first statement in the static initializer compiles without problems.  But the second statement produces the following error:

Util.java:9: illegal start of expression
        (Util.<Object>cast(null)).getClass();
              ^

Various experiments show that it is the combination of the parentheses and the explicit type argument that provokes the problem.
Posted Date : 2006-10-13 08:42:12.0
Work Around
N/A
Evaluation
A parser bug.
Posted Date : 2006-10-16 05:10:32.0

This bug is somehow related to CR 6665356. When the parser see an open parenthesis it continue parsing the rest of the expression disabling type-arguments parsing. This is because otherwise there would be ambiguity between the this expression

( e < 5 && e > 0) //combination of binary operators < and >

and this one:

(Foo<String>)  //generic cast expression

For this reason javac disables parsing of type-arguments when parsing the content of a parenthesized expression. When a generic cast is ecnountered however, javac has to reconstruct the type-arguments information that has not been parsed yet. 

In this example we have that this expression is parsed succesfully:

Util.<Object>cast(null);

while this one is not

(Util.<Object>cast(null));

The reason is that the parser is seeing an open parenthesis and, in order to avoid ambiguities, it disables type-argument parsing, as described above. In such a mode, there's no way to parse ".<Object>cast..." so the parser thinks that the end of the parenthesized expression is just after the '.', while it's not. 

Since generic method calls with explicit type-arguments are the only kind of expression such that the symbol '<' can follow an '.' , it is safe to temporary re-enable type-arguments parsing right after a '.' has been read (and re-disabling it after type-arguments --- if any --- have been parsed).
Posted Date : 2008-02-25 16:58:46.0
Comments
  
  Include a link with my name & email   

Submitted On 17-DEC-2006
brendanh
A similar example of the problem is shown in Simple.java:

import java.util.Collections;

public class Simple {

  public static void main(String [] args) {
      assert Collections.emptyList().size() == 0;
      assert (Collections.emptyList().size() == 0);

      assert Collections.<String>emptyList().size() == 0;
      assert (Collections.<String>emptyList().size() == 0);
  }
}

On Windows (1.5.0_09-b1) and OS X (1.5.0_06) the first 3 asserts parse without problems, but the 4th results in a compilation error:

Simple.java:10: illegal start of expression
      assert (Collections.<String>emptyList().size() == 0);
                          ^
Simple.java:10: < expected
      assert (Collections.<String>emptyList().size() == 0);
                                                          ^
2 errors



PLEASE NOTE: JDK6 is formerly known as Project Mustang