com.google.clearsilver.jsilver.compiler
Class JavaExpression

java.lang.Object
  extended by com.google.clearsilver.jsilver.compiler.JavaExpression
Direct Known Subclasses:
JavaExpression.BooleanLiteralExpression, JavaExpression.StringExpression

public abstract class JavaExpression
extends Object

Represents a node of a Java expression. This class contains static helper methods for common types of expressions, or you can just create your own subclass.


Nested Class Summary
static class JavaExpression.BooleanLiteralExpression
          A JavaExpression to represent boolean literal values ('true' or 'false').
static class JavaExpression.StringExpression
           
static class JavaExpression.Type
          Simple type enumeration to allow us to compare the return types of expressions easily and cast expressions nicely.
 
Constructor Summary
JavaExpression(JavaExpression.Type type)
          Creates a typed expression.
 
Method Summary
static JavaExpression assign(JavaExpression.Type type, String name, JavaExpression value)
          A typed assignment (e.g.
static JavaExpression bool(boolean value)
          A boolean
static JavaExpression call(JavaExpression.Type type, String method, JavaExpression... params)
          A typed method call (e.g.
static JavaExpression call(String method, JavaExpression... params)
          An untyped method call (e.g.
static JavaExpression callFindVariable(JavaExpression expression, boolean create)
           
static JavaExpression callOn(JavaExpression.Type type, JavaExpression instance, String method, JavaExpression... params)
          A typed method call on an instance (e.g.
static JavaExpression callOn(JavaExpression instance, String method, JavaExpression... params)
          An untyped method call on an instance (e.g.
 JavaExpression cast(JavaExpression.Type destType)
          Cast this expression to the destination type (possibly a no-op)
static JavaExpression declare(JavaExpression.Type type, String name, JavaExpression value)
          A typed assignment with declaration (e.g.
 JavaExpression.Type getType()
          Gets the type of this expression (or null if unknown).
static JavaExpression increment(JavaExpression.Type type, JavaExpression accumulator, JavaExpression incr)
          An increment statement (e.g.
static JavaExpression infix(JavaExpression.Type type, String operator, JavaExpression left, JavaExpression right)
          An infix expression (e.g.
static JavaExpression inlineIf(JavaExpression.Type type, JavaExpression query, JavaExpression trueExp, JavaExpression falseExp)
          A three term inline if expression (e.g.
static JavaExpression integer(int value)
          An integer.
static JavaExpression integer(String value)
          An integer.
static JavaExpression literal(JavaExpression.Type type, String value)
          A literal expression (e.g.
static JavaExpression macro(String value)
           
static JavaExpression prefix(JavaExpression.Type type, String operator, JavaExpression expression)
          An prefix expression (e.g.
static JavaExpression string(String value)
          A Java string (e.g.
static JavaExpression symbol(JavaExpression.Type type, String value)
          A typed symbol (e.g.
static JavaExpression symbol(String value)
          An untyped symbol (e.g.
 String toString()
           
abstract  void write(PrintWriter out)
          Implementations use this to output the expression as Java code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JavaExpression

public JavaExpression(JavaExpression.Type type)
Creates a typed expression. Typed expressions allow for greater optimization by avoiding unnecessary casting operations.

Parameters:
type - the Type of the expression. Must be from the enum above and represent a primitive or a Class name or void.
Method Detail

cast

public JavaExpression cast(JavaExpression.Type destType)
Cast this expression to the destination type (possibly a no-op)


getType

public JavaExpression.Type getType()
Gets the type of this expression (or null if unknown).


write

public abstract void write(PrintWriter out)
Implementations use this to output the expression as Java code.


toString

public String toString()
Overrides:
toString in class Object

call

public static JavaExpression call(String method,
                                  JavaExpression... params)
An untyped method call (e.g. doStuff(x, "y")).


call

public static JavaExpression call(JavaExpression.Type type,
                                  String method,
                                  JavaExpression... params)
A typed method call (e.g. doStuff(x, "y")).


callOn

public static JavaExpression callOn(JavaExpression instance,
                                    String method,
                                    JavaExpression... params)
An untyped method call on an instance (e.g. thingy.doStuff(x, "y")). We assume it returns VOID and thus there is no return value.


callOn

public static JavaExpression callOn(JavaExpression.Type type,
                                    JavaExpression instance,
                                    String method,
                                    JavaExpression... params)
A typed method call on an instance (e.g. thingy.doStuff(x, "y")).


string

public static JavaExpression string(String value)
A Java string (e.g. "hello\nworld").


integer

public static JavaExpression integer(String value)
An integer.


integer

public static JavaExpression integer(int value)
An integer.


bool

public static JavaExpression bool(boolean value)
A boolean


symbol

public static JavaExpression symbol(String value)
An untyped symbol (e.g. myVariable).


symbol

public static JavaExpression symbol(JavaExpression.Type type,
                                    String value)
A typed symbol (e.g. myVariable).


macro

public static JavaExpression macro(String value)

assign

public static JavaExpression assign(JavaExpression.Type type,
                                    String name,
                                    JavaExpression value)
A typed assignment (e.g. stuff = doSomething()).


declare

public static JavaExpression declare(JavaExpression.Type type,
                                     String name,
                                     JavaExpression value)
A typed assignment with declaration (e.g. String stuff = doSomething()). Use this in preference when declaring variables from typed expressions.


infix

public static JavaExpression infix(JavaExpression.Type type,
                                   String operator,
                                   JavaExpression left,
                                   JavaExpression right)
An infix expression (e.g. (a + b) ).


prefix

public static JavaExpression prefix(JavaExpression.Type type,
                                    String operator,
                                    JavaExpression expression)
An prefix expression (e.g. (-a) ).


inlineIf

public static JavaExpression inlineIf(JavaExpression.Type type,
                                      JavaExpression query,
                                      JavaExpression trueExp,
                                      JavaExpression falseExp)
A three term inline if expression (e.g. (a ? b : c) ).


increment

public static JavaExpression increment(JavaExpression.Type type,
                                       JavaExpression accumulator,
                                       JavaExpression incr)
An increment statement (e.g. a += b). The difference with infix is that this does not wrap the expression in parentheses as that is not a valid statement.


literal

public static JavaExpression literal(JavaExpression.Type type,
                                     String value)
A literal expression (e.g. anything!). This method injects whatever string it is given into the Java code - use only in cases where there can be no ambiguity about how the string could be interpreted by the compiler.


callFindVariable

public static JavaExpression callFindVariable(JavaExpression expression,
                                              boolean create)


Copyright © 2010-2012 Google. All Rights Reserved.