0

There is an ASTNode subclass called TypeLiteral.

The description of this type of code says that it looks like

( Type | void ) . class

I don't believe I have ever seen anything like this.

Can anyone provide an example of a TypeLiteral, and perhaps additionally of it being used in a Java program?

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

1 Answers1

2

There is a class called Class that represents the class of an object. It contains various useful methods for introspection on that class and objects of that class. One way to get a class object is to use the Object method getClass(). But if you want to refer to a Class by name of its class, you use a class literal.

Some examples:

class Foo {
  void method(Object b) {
    if(b.getClass() == Foo.class) {
      Foo f = Foo.class.cast(b);
    }
    Foo.class.getResourceAsStream(...);
    Foo.class.getMethod("method", Object.class);
  }
}
Russell Zahniser
  • 16,188
  • 39
  • 30
  • 1
    Can you explain? What does `.class` do? Also, why is it called a `TypeLiteral`? Where is the `Type` and what is `Literal` about it? – CodyBugstein Jun 11 '13 at 20:39