org.apache.jdo.impl.jdoql.jdoqlc
Class Optimizer

java.lang.Object
  extended byantlr.TreeParser
      extended byorg.apache.jdo.impl.jdoql.jdoqlc.Optimizer
All Implemented Interfaces:
OptimizerTokenTypes

public class Optimizer
extends antlr.TreeParser
implements OptimizerTokenTypes

This class defines the optimizer pass of the JDOQL compiler. It takes the typed AST as produced by the smenatic analysis and converts it into a simpler but equivalent typed AST.

Author:
Michael Bouschen

Field Summary
static java.lang.String[] _tokenNames
           
protected  ErrorMsg errorMsg
          The error message support class.
protected static I18NHelper msg
          I18N support
protected  boolean optimizeParameters
          Flag indicating whether query parameers should be included.
protected  ParameterTable paramtab
          The query parameter table
protected  PersistenceManagerInternal pm
          The persistence manager of the query instance.
 
Fields inherited from class antlr.TreeParser
_retTree, astFactory, ASTNULL, inputState, returnAST, tokenNames, traceDepth
 
Fields inherited from interface org.apache.jdo.impl.jdoql.jdoqlc.OptimizerTokenTypes
AND, ARG_LIST, ASCENDING, ASSIGN, BAND, BAND_ASSIGN, BNOT, BOOLEAN, BOOLEAN_LITERAL, BOR, BOR_ASSIGN, BYTE, BYTE_LITERAL, CANDIDATE_CLASS, CAST, CHAR, CHAR_LITERAL, COLLECTION_EQUAL, COLLECTION_NOT_EQUAL, COMMA, CONCAT, CONSTANT, CONTAINS, DEC, DESCENDING, DIV, DIV_ASSIGN, DOT, DOUBLE, DOUBLE_LITERAL, ENDS_WITH, EOF, EQUAL, ESC, EXPONENT, FALSE, FIELD_ACCESS, FLOAT, FLOAT_LITERAL, FLOATINGPOINT_SUFFIX, GE, GT, HEX_DIGIT, IDENT, IMPORT, IMPORT_ON_DEMAND, INC, INT, INT_LITERAL, IS_EMPTY, LE, LNOT, LONG, LONG_LITERAL, LPAREN, LT, MINUS, MINUS_ASSIGN, NAVIGATION, NEWLINE, NOT_EQUAL, NULL, NULL_TREE_LOOKAHEAD, OBJECT_EQUAL, OBJECT_NOT_EQUAL, OR, PARAMETER_ACCESS, PARAMETER_DECL, PLUS, PLUS_ASSIGN, QUERY_TREE, RPAREN, SEMI, SHORT, SHORT_LITERAL, SL, SR, STAR, STAR_ASSIGN, STARTS_WITH, STATIC_FIELD_ACCESS, STRING_LITERAL, THIS, TRUE, TYPE, UNARY_MINUS, UNARY_PLUS, UNICODE_CHAR, UNICODE_ESCAPE, VARIABLE_ACCESS, VARIABLE_DECL, WS
 
Constructor Summary
Optimizer()
           
 
Method Summary
 void binaryArithmeticExpr(antlr.collections.AST _t)
           
 void bitwiseExpr(antlr.collections.AST _t)
           
 void candidateClass(antlr.collections.AST _t)
           
protected  JDOQLAST checkAnd(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check an AND operation (BAND, AND) for constant operands that could be optimized.
protected  JDOQLAST checkBinaryMinusOp(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check a binary minus operation (MINUS) for constant operands that could be optimized.
protected  JDOQLAST checkBinaryPlusOp(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check a binary plus operation (PLUS) for constant operands that could be optimized.
protected  JDOQLAST checkCollectionEqualityOp(JDOQLAST op, JDOQLAST left, JDOQLAST right, boolean negate)
          Check a collection equality operation (COLLECTION_EQUAL, COLLECTION_NOT_EQUAL) for constant operands that could be optimized.
protected  JDOQLAST checkConcatOp(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check a string concatenation operation (CONCAT) for constant operands that could be optimized.
protected  JDOQLAST checkDivisionOp(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check a binary division operation (DIV) for constant operands that could be optimized.
protected  JDOQLAST checkEqualityOp(JDOQLAST op, JDOQLAST left, JDOQLAST right, boolean negate)
          Check a equality operation (EQUAL, NOT_EQUAL) for constant operands that could be optimized.
protected  JDOQLAST checkLogicalNotOp(JDOQLAST op, JDOQLAST arg)
          Check a logical not operation (LNOT) for a constant operand that could be optimized.
protected  JDOQLAST checkMultiplicationOp(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check a binary multiplication operation (STAR) for constant operands that could be optimized.
protected  JDOQLAST checkObjectEqualityOp(JDOQLAST op, JDOQLAST left, JDOQLAST right, boolean negate)
          Check a object equality operation (OBJECT_EQUAL, OBJECT_NOT_EQUAL) for constant operands that could be optimized.
protected  JDOQLAST checkOr(JDOQLAST op, JDOQLAST left, JDOQLAST right)
          Check an OR operation (BOR, OR) for constant operands that could be optimized.
protected  JDOQLAST checkUnaryMinusOp(JDOQLAST op, JDOQLAST arg)
          Check a unary minus operation (UNARY_MINUS) for a constant operand that could be optimized.
 void complementExpr(antlr.collections.AST _t)
           
 void conditionalExpr(antlr.collections.AST _t)
           
 void contains(antlr.collections.AST _t)
           
 void declareParameter(antlr.collections.AST _t)
           
 void declareVariable(antlr.collections.AST _t)
           
 void endsWith(antlr.collections.AST _t)
           
 void expression(antlr.collections.AST _t)
           
 void fieldAccess(antlr.collections.AST _t)
           
 void filter(antlr.collections.AST _t)
           
private  java.lang.reflect.Field getAccessibleField(JavaField javaField)
          Method executing TypeSupport.getAccessibleField in a doPrivileged block.
protected  java.math.BigDecimal getBigDecimalValue(java.lang.Object value)
          Converts the specified value into a BigDecimal value.
protected  java.math.BigInteger getBigIntegerValue(java.lang.Object value)
          Converts the specified value into a BigInteger value.
private  java.lang.Object getFieldValue(JavaField javaField, PersistenceManagerInternal pm, java.lang.Object object)
          Returns the value of the specified field of the specified object.
private  java.lang.Object getStaticFieldValue(JavaField javaField)
          Returns the value of the specified static field.
private  JDOQLAST handleBooleanValueEqExpr(JDOQLAST op, java.lang.Object value, JDOQLAST expr, boolean negate)
          This method is called in the case of an equality operation having a boolean constant operand and a non constant operand.
private  JDOQLAST handleValueAndExpr(JDOQLAST op, java.lang.Object value, JDOQLAST expr)
          This method is called in the case of an AND operation having at least one constant operand.
protected  JDOQLAST handleValueEqValue(JDOQLAST op, JDOQLAST left, JDOQLAST right, boolean negate)
          This method is called in the case of an equality operation having two constant operands.
private  JDOQLAST handleValueOrExpr(JDOQLAST op, java.lang.Object value, JDOQLAST expr)
          This method is called in the case of an OR operation having at least one constant operand.
 void init(PersistenceManagerInternal pm, ParameterTable paramtab, ErrorMsg errorMsg)
           
 void integralLiteral(antlr.collections.AST _t, boolean negate)
           
protected  boolean isBooleanValueAST(JDOQLAST ast)
          Returns true if the specified AST represents a constant boolean value.
 void isEmpty(antlr.collections.AST _t)
           
 void literal(antlr.collections.AST _t)
           
 void navigation(antlr.collections.AST _t)
           
 void ordering(antlr.collections.AST _t)
           
 void orderSpec(antlr.collections.AST _t)
           
 void parameterAccess(antlr.collections.AST _t)
           
 void parameters(antlr.collections.AST _t)
           
protected static char parseChar(java.lang.String text)
          Converts the string argument into a single char.
 void primary(antlr.collections.AST _t)
           
 void query(antlr.collections.AST _t)
           
 void relationalExpr(antlr.collections.AST _t)
           
 void reportError(antlr.RecognitionException ex)
           
 void reportError(java.lang.String s)
           
private  java.lang.String skipLongTypeSuffix(java.lang.String txt)
          Returns the specified string w/o a long type suffix 'l' or 'L' or the specified string if there is no such suffix.
 void startsWith(antlr.collections.AST _t)
           
 void staticFieldAccess(antlr.collections.AST _t)
           
 void unaryArithmeticExpr(antlr.collections.AST _t)
           
 void variableAccess(antlr.collections.AST _t)
           
 void variables(antlr.collections.AST _t)
           
 
Methods inherited from class antlr.TreeParser
getAST, getASTFactory, getTokenName, getTokenNames, match, match, matchNot, panic, reportWarning, setASTFactory, setASTNodeClass, setASTNodeType, traceIn, traceIndent, traceOut
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pm

protected PersistenceManagerInternal pm
The persistence manager of the query instance.


errorMsg

protected ErrorMsg errorMsg
The error message support class.


paramtab

protected ParameterTable paramtab
The query parameter table


optimizeParameters

protected boolean optimizeParameters
Flag indicating whether query parameers should be included.


msg

protected static final I18NHelper msg
I18N support


_tokenNames

public static final java.lang.String[] _tokenNames
Constructor Detail

Optimizer

public Optimizer()
Method Detail

init

public void init(PersistenceManagerInternal pm,
                 ParameterTable paramtab,
                 ErrorMsg errorMsg)

reportError

public void reportError(antlr.RecognitionException ex)

reportError

public void reportError(java.lang.String s)

parseChar

protected static char parseChar(java.lang.String text)
Converts the string argument into a single char.


checkAnd

protected JDOQLAST checkAnd(JDOQLAST op,
                            JDOQLAST left,
                            JDOQLAST right)
Check an AND operation (BAND, AND) for constant operands that could be optimized.

Parameters:
op - the AND operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkOr

protected JDOQLAST checkOr(JDOQLAST op,
                           JDOQLAST left,
                           JDOQLAST right)
Check an OR operation (BOR, OR) for constant operands that could be optimized.

Parameters:
op - the OR operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkEqualityOp

protected JDOQLAST checkEqualityOp(JDOQLAST op,
                                   JDOQLAST left,
                                   JDOQLAST right,
                                   boolean negate)
Check a equality operation (EQUAL, NOT_EQUAL) for constant operands that could be optimized.

Parameters:
op - the equality operator
left - the left operand
right - the right operand
negate - true for not equal operation, false otherwise
Returns:
optimized JDOQLAST

checkObjectEqualityOp

protected JDOQLAST checkObjectEqualityOp(JDOQLAST op,
                                         JDOQLAST left,
                                         JDOQLAST right,
                                         boolean negate)
Check a object equality operation (OBJECT_EQUAL, OBJECT_NOT_EQUAL) for constant operands that could be optimized.

Parameters:
op - the object equality operator
left - the left operand
right - the right operand
negate - true for not equal operation, false otherwise
Returns:
optimized JDOQLAST

checkCollectionEqualityOp

protected JDOQLAST checkCollectionEqualityOp(JDOQLAST op,
                                             JDOQLAST left,
                                             JDOQLAST right,
                                             boolean negate)
Check a collection equality operation (COLLECTION_EQUAL, COLLECTION_NOT_EQUAL) for constant operands that could be optimized.

Parameters:
op - the collection equality operator
left - the left operand
right - the right operand
negate - true for not equal operation, false otherwise
Returns:
optimized JDOQLAST

checkLogicalNotOp

protected JDOQLAST checkLogicalNotOp(JDOQLAST op,
                                     JDOQLAST arg)
Check a logical not operation (LNOT) for a constant operand that could be optimized.

Parameters:
op - the logical not operator
arg - the operand
Returns:
optimized JDOQLAST

checkBinaryPlusOp

protected JDOQLAST checkBinaryPlusOp(JDOQLAST op,
                                     JDOQLAST left,
                                     JDOQLAST right)
Check a binary plus operation (PLUS) for constant operands that could be optimized.

Parameters:
op - the plus operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkConcatOp

protected JDOQLAST checkConcatOp(JDOQLAST op,
                                 JDOQLAST left,
                                 JDOQLAST right)
Check a string concatenation operation (CONCAT) for constant operands that could be optimized.

Parameters:
op - the concat operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkBinaryMinusOp

protected JDOQLAST checkBinaryMinusOp(JDOQLAST op,
                                      JDOQLAST left,
                                      JDOQLAST right)
Check a binary minus operation (MINUS) for constant operands that could be optimized.

Parameters:
op - the minus operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkMultiplicationOp

protected JDOQLAST checkMultiplicationOp(JDOQLAST op,
                                         JDOQLAST left,
                                         JDOQLAST right)
Check a binary multiplication operation (STAR) for constant operands that could be optimized.

Parameters:
op - the multiplication operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkDivisionOp

protected JDOQLAST checkDivisionOp(JDOQLAST op,
                                   JDOQLAST left,
                                   JDOQLAST right)
Check a binary division operation (DIV) for constant operands that could be optimized.

Parameters:
op - the division operator
left - the left operand
right - the right operand
Returns:
optimized JDOQLAST

checkUnaryMinusOp

protected JDOQLAST checkUnaryMinusOp(JDOQLAST op,
                                     JDOQLAST arg)
Check a unary minus operation (UNARY_MINUS) for a constant operand that could be optimized.

Parameters:
op - the unary minus operator
arg - the operand
Returns:
optimized JDOQLAST

getBigDecimalValue

protected java.math.BigDecimal getBigDecimalValue(java.lang.Object value)
Converts the specified value into a BigDecimal value.

Parameters:
value - value to be converted
Returns:
BigDecimal representation

getBigIntegerValue

protected java.math.BigInteger getBigIntegerValue(java.lang.Object value)
Converts the specified value into a BigInteger value.

Parameters:
value - value to be converted
Returns:
BigInteger representation

handleValueEqValue

protected JDOQLAST handleValueEqValue(JDOQLAST op,
                                      JDOQLAST left,
                                      JDOQLAST right,
                                      boolean negate)
This method is called in the case of an equality operation having two constant operands. It calculates the result of this constant operation and returns a JDOQLAST node representing a constant boolean value.

Parameters:
op - the equality operator
left - the left operand
right - the right operand
negate - true for not equal operation, false otherwise
Returns:
optimized JDOQLAST

handleBooleanValueEqExpr

private JDOQLAST handleBooleanValueEqExpr(JDOQLAST op,
                                          java.lang.Object value,
                                          JDOQLAST expr,
                                          boolean negate)
This method is called in the case of an equality operation having a boolean constant operand and a non constant operand. It returns the non constant operand either as it is or inverted, depending on the equality operation.

Parameters:
op - the equality operator
value - the contant boolean value
expr - the non constant operand
negate - true for not equal operation, false otherwise
Returns:
optimized JDOQLAST

handleValueAndExpr

private JDOQLAST handleValueAndExpr(JDOQLAST op,
                                    java.lang.Object value,
                                    JDOQLAST expr)
This method is called in the case of an AND operation having at least one constant operand. If the constant operand evaluates to true it returns the other operand. If it evaluates to false it returns an AST representing the constant boolean value false.

Parameters:
op - the AND operator
value - the value of the contsnat operand
expr - the other operand
Returns:
optimized JDOQLAST

handleValueOrExpr

private JDOQLAST handleValueOrExpr(JDOQLAST op,
                                   java.lang.Object value,
                                   JDOQLAST expr)
This method is called in the case of an OR operation having at least one constant operand. If the constant operand evaluates to false it returns the other operand. If it evaluates to true it returns an AST representing the constant boolean value true.

Parameters:
op - the AND operator
value - the value of the constant operand
expr - the other operand
Returns:
optimized JDOQLAST

isBooleanValueAST

protected boolean isBooleanValueAST(JDOQLAST ast)
Returns true if the specified AST represents a constant boolean value.


skipLongTypeSuffix

private java.lang.String skipLongTypeSuffix(java.lang.String txt)
Returns the specified string w/o a long type suffix 'l' or 'L' or the specified string if there is no such suffix.


getFieldValue

private java.lang.Object getFieldValue(JavaField javaField,
                                       PersistenceManagerInternal pm,
                                       java.lang.Object object)
Returns the value of the specified field of the specified object. The method uses StateManager methods to access the field value, if it is a managed field of a persistent instance. Otherwise it uses reflection.

Parameters:
javaField - the field representation
pm - the pm used in case of a managed field of a persistent instance
object - the instance
Returns:
the field value

getStaticFieldValue

private java.lang.Object getStaticFieldValue(JavaField javaField)
Returns the value of the specified static field.

Parameters:
javaField - the field representation
Returns:
the field value

getAccessibleField

private java.lang.reflect.Field getAccessibleField(JavaField javaField)
Method executing TypeSupport.getAccessibleField in a doPrivileged block.


query

public final void query(antlr.collections.AST _t)
                 throws antlr.RecognitionException
Throws:
antlr.RecognitionException

candidateClass

public final void candidateClass(antlr.collections.AST _t)
                          throws antlr.RecognitionException
Throws:
antlr.RecognitionException

parameters

public final void parameters(antlr.collections.AST _t)
                      throws antlr.RecognitionException
Throws:
antlr.RecognitionException

variables

public final void variables(antlr.collections.AST _t)
                     throws antlr.RecognitionException
Throws:
antlr.RecognitionException

ordering

public final void ordering(antlr.collections.AST _t)
                    throws antlr.RecognitionException
Throws:
antlr.RecognitionException

filter

public final void filter(antlr.collections.AST _t)
                  throws antlr.RecognitionException
Throws:
antlr.RecognitionException

declareParameter

public final void declareParameter(antlr.collections.AST _t)
                            throws antlr.RecognitionException
Throws:
antlr.RecognitionException

declareVariable

public final void declareVariable(antlr.collections.AST _t)
                           throws antlr.RecognitionException
Throws:
antlr.RecognitionException

orderSpec

public final void orderSpec(antlr.collections.AST _t)
                     throws antlr.RecognitionException
Throws:
antlr.RecognitionException

expression

public final void expression(antlr.collections.AST _t)
                      throws antlr.RecognitionException
Throws:
antlr.RecognitionException

primary

public final void primary(antlr.collections.AST _t)
                   throws antlr.RecognitionException
Throws:
antlr.RecognitionException

bitwiseExpr

public final void bitwiseExpr(antlr.collections.AST _t)
                       throws antlr.RecognitionException
Throws:
antlr.RecognitionException

conditionalExpr

public final void conditionalExpr(antlr.collections.AST _t)
                           throws antlr.RecognitionException
Throws:
antlr.RecognitionException

relationalExpr

public final void relationalExpr(antlr.collections.AST _t)
                          throws antlr.RecognitionException
Throws:
antlr.RecognitionException

binaryArithmeticExpr

public final void binaryArithmeticExpr(antlr.collections.AST _t)
                                throws antlr.RecognitionException
Throws:
antlr.RecognitionException

unaryArithmeticExpr

public final void unaryArithmeticExpr(antlr.collections.AST _t)
                               throws antlr.RecognitionException
Throws:
antlr.RecognitionException

complementExpr

public final void complementExpr(antlr.collections.AST _t)
                          throws antlr.RecognitionException
Throws:
antlr.RecognitionException

integralLiteral

public final void integralLiteral(antlr.collections.AST _t,
                                  boolean negate)
                           throws antlr.RecognitionException
Throws:
antlr.RecognitionException

literal

public final void literal(antlr.collections.AST _t)
                   throws antlr.RecognitionException
Throws:
antlr.RecognitionException

parameterAccess

public final void parameterAccess(antlr.collections.AST _t)
                           throws antlr.RecognitionException
Throws:
antlr.RecognitionException

variableAccess

public final void variableAccess(antlr.collections.AST _t)
                          throws antlr.RecognitionException
Throws:
antlr.RecognitionException

staticFieldAccess

public final void staticFieldAccess(antlr.collections.AST _t)
                             throws antlr.RecognitionException
Throws:
antlr.RecognitionException

fieldAccess

public final void fieldAccess(antlr.collections.AST _t)
                       throws antlr.RecognitionException
Throws:
antlr.RecognitionException

navigation

public final void navigation(antlr.collections.AST _t)
                      throws antlr.RecognitionException
Throws:
antlr.RecognitionException

contains

public final void contains(antlr.collections.AST _t)
                    throws antlr.RecognitionException
Throws:
antlr.RecognitionException

startsWith

public final void startsWith(antlr.collections.AST _t)
                      throws antlr.RecognitionException
Throws:
antlr.RecognitionException

endsWith

public final void endsWith(antlr.collections.AST _t)
                    throws antlr.RecognitionException
Throws:
antlr.RecognitionException

isEmpty

public final void isEmpty(antlr.collections.AST _t)
                   throws antlr.RecognitionException
Throws:
antlr.RecognitionException