Re: Java 2.0 Grammar Problems

Thomas Maslen <maslen@pobox.com>
17 Feb 2001 01:32:52 -0500

          From comp.compilers

Related articles
Java 2.0 Grammar Problems schoettner@informatik.uni-ulm.de (Michael Schoettner) (2001-02-12)
Re: Java 2.0 Grammar Problems C.vanReeuwijk@twi.tudelft.nl (Kees van Reeuwijk) (2001-02-15)
Re: Java 2.0 Grammar Problems maslen@pobox.com (Thomas Maslen) (2001-02-17)
Re: Java 2.0 Grammar Problems C.vanReeuwijk@twi.tudelft.nl (Kees van Reeuwijk) (2001-02-23)
Re: Java 2.0 Grammar Problems pmb@dina.kvl.dk (Peter Bertelsen) (2001-02-25)
Re: Java 2.0 Grammar Problems Dr_Feriozi@prodigy.net (2001-03-01)
| List of all articles for this month |
From: Thomas Maslen <maslen@pobox.com>
Newsgroups: comp.compilers
Date: 17 Feb 2001 01:32:52 -0500
Organization: Distributed Systems Technology CRC
References: 01-02-040
Keywords: Java, parse
Posted-Date: 17 Feb 2001 01:32:52 EST

"Michael Schoettner" <schoettner@informatik.uni-ulm.de> writes:
[...]
>Primary:
> ( Expression )
> this [Arguments]
> super SuperSuffix
> Literal
> new Creator
> Identifier { . Identifier }[ IdentifierSuffix]
> BasicType BracketsOpt .class (*)
> void.class (**)
>
>(*) + (**) What are these productions good for?


They name Class instances that represent the primitive types (int, boolean,
float etcetera) and arrays thereof, and also void. These are used with
reflective methods such as


java.lang.Class.getDeclaredMethod(String, Class[])


although void.class probably only occurs as the return value of


java.lang.reflect.Method.getReturnType()


Class literals (anything with ".class") are syntactic sugar which the
compiler maps to other constructs:


void.class java.lang.Void.TYPE


int.class java.lang.Integer.TYPE


int[][][].class Class.forName("[[[I")


Foo.class Class.forName("fully.qualified.name.of.Foo")


Foo[][][].class Class.forName("[[[Lfully.qualified.name.of.Foo;")


In practice the call to Class.forName() is done once and then cached in a
static field, and ClassNotFoundException is mapped to NoClassDefFoundError.


You could argue that int.class et al and void.class are unnecessary,
since you can just as well write Integer.TYPE and Void.TYPE, but overall
I'm glad that the ".class" sugar exists because, unlike Class.forName(),
it can be checked at compile time.


Thomas Maslen
maslen@pobox.com


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.