/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* Semantic.g
*
* Created on November 19, 2001
*/
{
}
/**
* This class defines the semantic analysis of the EJBQL compiler.
* Input of this pass is the AST as produced by the parser,
* that consists of EJBQLAST nodes.
* The result is a typed EJBQLAST tree.
*
* @author Michael Bouschen
* @author Shing Wai Chan
*/
class Semantic extends TreeParser;
{
importVocab = EJBQL;
buildAST = true;
defaultErrorHandler = false;
}
{
/** Name of the property to disable order by validation. */
public static final String DISABLE_ORDERBY_VALIDATION_PROPERTY =
"com.sun.jdo.spi.persistence.support.ejb.ejbqlc.DISABLE_ORDERBY_VALIDATION"; // NOI18N
/**
* Property to disable order by validation.
* Note, the default is false, meaning the compiler checks that select
* clause and orderby clause are compatible.
*/
private static final boolean DISABLE_ORDERBY_VALIDATION =
/** Symbol table handling names of variables and parameters. */
protected SymbolTable symtab;
/** Type info access helper. */
protected TypeSupport typeSupport;
/** Parameter info helper. */
protected ParameterSupport paramSupport;
/** result-type-mapping element from the DD. */
protected int resultTypeMapping;
/** Flag indicating finder or selector. */
protected boolean finderNotSelector;
/** Flag indicating have aggregate function or not. */
protected boolean isAggregate = false;
/** The ejb-name. */
/** I18N support. */
Semantic.class);
/**
* Initializes the semantic analysis.
* @param typeSupport type info access helper.
* @param paramSupport parameter info helper.
* @param resultTypeMapping result-type-mapping element from the DD
* @param finderNotSelector <code>true</code> for finder;
* <code>false</code> for selector
*/
{
this.symtab = new SymbolTable();
this.typeSupport = typeSupport;
this.paramSupport = paramSupport;
this.resultTypeMapping = resultTypeMapping;
this.finderNotSelector = finderNotSelector;
}
/** */
}
/** */
public void reportError(String s) {
}
//========= Internal helper methods ==========
/**
* Checks the return type and the type of the select clause expression
* of a finder method.
* <p>
* The return type of a finder must be one of the following:
* <ul>
* <li>java.util.Collection (multi-object finder)
* <li>java.util.Enumeration (EJB 1.1 multi-object finder)
* <li>the entity bean's remote interface (single-object finder)
* <li>the entity bean's local interface (single-object finder)
* </ul>
* The type of the select clause expression of a finder must be
* the entity bean's local or remote interface.
* @param selectClauseTypeInfo the type info of the select clause
* expression.
*/
private void checkFinderReturnType(
{
// The return type of a finder must be Collection or Enumeration or
// the entity bean's remote or local interface
}
// The type of the select clause expression must be the ejb name
// of this bean.
}
}
/**
* Implements type compatibility for selector. The method returns
* <code>true</code> if returnTypeInfo is compatible with
* selectClauseTypeInfo.
*/
private boolean isCompatibleSelectorSelectorReturnType(
{
if (isAggregate) {
} else {
}
}
/**
* Checks the return type and the type of the select clause expression
* of a selector method.
* <p>
* The return type of a selector must be one of the following:
* <ul>
* <li>java.util.Collection (multi-object selector)
* <li>java.util.Set (multi-object selector)
* <li>assignable from the type of the select clause expression
* (single-object selector)
* </ul>
* @param selectClauseTypeInfo the type info of the select clause
* expression.
*/
private void checkSelectorReturnType(
{
// The return type of a selector must be Collection or Set or
// assingable from the type of the select clause expression
"EXC_InvalidSelectorReturnType", //NOI18N
}
}
/**
* Checks the result-type-mapping element setting in the case of a finder
* method. Finder must not specify result-type-mapping.
*/
private void checkFinderResultTypeMapping()
{
"EXC_InvalidResultTypeMappingForFinder")); //NOI18N
}
}
/**
* Checks the setting of the result-type-mapping element for a
* selector. Only selectors returning a entity object may
* specify this.
* <p>
* The method checks the following error cases:
* <ul>
* <li>result-type-mapping is specified as Remote,
* but bean does not have remote interface
* <li>result-type-mapping is specified as Local,
* but bean does not have local interface
* <li>single-object selector returns remote interface,
* but result-type-mapping is not specified as Remote
* <li>single-object selector returns local interface,
* but result-type-mapping is specified as Remote
* <li>result-type-mapping is specified for a selector returning
* non-entity objects.
* </ul>
* @param selectClauseTypeInfo the type info of the select clause.
*/
private void checkSelectorResultTypeMapping(
{
// case: multi-object selector returning entity objects
// result-type-mapping is Remote =>
// bean must have remote interface
"EXC_InvalidRemoteResultTypeMappingForMultiSelector", //NOI18N
}
}
else {
// result-type-mapping is Local or not specified =>
// bean must have local interface
"EXC_InvalidLocalResultTypeMappingForMultiSelector", //NOI18N
}
}
}
// case: single-object selector returning remote interface
// result-type-mapping must be Remote
"EXC_InvalidLocalResultTypeMappingForSingleSelector")); //NOI18N
}
}
// case: single-object selector returning local interface
// result-type-mapping must be Local or not specified
"EXC_InvalidRemoteResultTypeMappingForSingleSelector")); //NOI18N
}
}
// cases: single-object and multi-object selector
// returning non-enity object(s)
// result-type-mapping must not be specified
"EXC_InvalidResultTypeMappingForSelector", //NOI18N
}
}
/**
* Checks that select clause and orderby clause are compatible.
* <p>
* The method checks the following error cases:
* <ul>
* <li>if the select clause is an identification variable or
* a single valued cmr path expression, then the orderby item
* must be a cmp field of the entity bean abstract schema
* type value returned by the SELECT clause
* <li>if the select clause is a cmp field, then
* orderby item must be empty or the same cmp field.
* </ul>
* @param select the select clause of the query
* @param orderby the orderby clause of the query
*/
{
// nothing to check if no orderby clause or
// if orderby validation is disabled
return;
}
// skip DISTINCT node, so selectReturnAST should be one of the following:
// Object(x), cmr-field, cmp-field
// it is illegal to be an aggregate function node
}
// share buf
"EXC_InvalidOrderbyItemForCMPSelect", //NOI18N
}
}
} else {
} else { // it must be an aggregate function node
"EXC_InvalidAggregateOrderby" //NOI18N
));
}
// share buf
"EXC_InvalidOrderbyItem", //NOI18N
}
}
}
}
/**
* Form a string representation of a dot expression and append to given
* StringBuffer.
* @param ast the AST node representing the root the of the expression
* @param buf the StringBuffer that will have result of path expression
* append
*/
//SW: We can write this method without recursion. Michael suggests to use
//recursion for readability.
return;
}
case CMP_FIELD_ACCESS:
case SINGLE_CMR_FIELD_ACCESS:
break;
default:
break;
}
}
/**
* Analyses a logical operation AND, OR
* @param op the logical operator
* @param leftAST left operand
* @param rightAST right operand
* @return the type info of the operator
*/
{
// handle error type
return typeSupport.errorType;
return common;
}
// if this code is reached a bitwise operator was used with invalid arguments
return typeSupport.errorType;
}
/**
* Analyses a equality operation (==, <>)
* @param op the relational operator
* @param leftAST left operand
* @param rightAST right operand
* @return the type info of the operator
*/
{
// handle error type
return typeSupport.errorType;
}
// check left hand side for literals and input params
return typeSupport.errorType;
}
else if (isInputParameter(leftAST)) {
return typeSupport.errorType;
}
// check operand types
return typeSupport.booleanType;
}
return typeSupport.booleanType;
}
return typeSupport.booleanType;
}
// the input parameter must be on right hand side of an equality
// expression ('?1' = e.department is not supported)
}
return typeSupport.booleanType;
}
// if this code is reached a conditional operator was used with invalid arguments
return typeSupport.errorType;
}
/**
* Analyses a relational operation (<, <=, >, >=)
* @param op the relational operator
* @param leftAST left operand
* @param rightAST right operand
* @return the type info of the operator
*/
{
// handle error type
return typeSupport.errorType;
}
// check left hand side for literals and input params
return typeSupport.errorType;
}
else if (isInputParameter(leftAST)) {
return typeSupport.errorType;
}
// check operand types
return typeSupport.booleanType;
}
// if this code is reached a conditional operator was used with invalid arguments
return typeSupport.errorType;
}
/**
* Analyses a binary arithmetic expression +, -, *, /.
* @param op the operator
* @param leftAST left operand
* @param rightAST right operand
* @return the type info of the operator
*/
{
// handle error type
return typeSupport.errorType;
}
return common;
}
// if this code is reached a conditional operator was used with invalid arguments
return typeSupport.errorType;
}
/**
* Returns the common type info for the specified operand types.
* This includes binary numeric promotion as specified in Java.
* @param left type info of left operand
* @param right type info of right operand
* @return the type info of the operator
*/
{
boolean wrapper = false;
// handle java.math.BigDecimal:
return left;
}
return right;
}
// handle java.math.BigInteger
// if right is floating point return BigDecimal,
// otherwise return BigInteger
}
// if left is floating point return BigDecimal,
// otherwise return BigInteger
}
wrapper = true;
}
wrapper = true;
}
// handle numeric types with arbitrary arithmetic operator
if (wrapper)
return promotedType;
}
}
// check for boolean wrapper class: if one of the operands has the
// type Boolean return Boolean, otherwise return boolean.
return typeSupport.booleanClassType;
else
return typeSupport.booleanType;
}
return right;
}
return left;
}
// not compatible types => return errorType
return typeSupport.errorType;
}
/**
* Analyses a unary expression (+ and -).
* @param op the operator
* @param argASTleftAST left operand
* @param rightAST right operand
* @return the type info of the operator
*/
{
// handle error type
return arg;
boolean wrapper = false;
wrapper = true;
}
if (wrapper)
return promotedType;
}
// if this code is reached a conditional operator was used with invalid arguments
return typeSupport.errorType;
}
/**
* Analyses a expression node that is expected to access a collection
* valued CMR field. It returns the element type of the collection valued
* CMR field.
* @param fieldAccess the field access node
* @return the type info of the operator
*/
{
return typeSupport.errorType;
}
}
/**
* Analyses a MEMBER OF operation.
* @param op the MEMBER OF operator
* @param value node representing the value to be tested
* @param col the collection
* @return the type info of the operator
*/
{
// handle error type
return typeSupport.errorType;
}
// check compatibility
}
// if this code is reached there is a compatibility problem
// with the value and the collection expr
return typeSupport.errorType;
}
/**
* Analyses the type of the element to be compatible with the type of the
* value expression in the sense that element type can be cast into value
* type without losing precision.
* For instance, element type can be a double and value type can be an
* integer.
* @param elementAST given element
* @param valueTypeInfo the type to be check for compatibility
* @return the type info of the elementAST or typeSupport.errorType
*/
{
// handle error type
return typeSupport.errorType;
}
return common;
}
// if this code is reached there is a compatibility problem
// with the value and the collection expr
return typeSupport.errorType;
}
/**
* Analyses whether paramAST can be associated to a ejbName.
* @param paramAST AST node corresponds to a PARAMETER
* @param ejbName name to be check with paramAST
* @return the type info of typeSupport.booleanType or typeSupport.errorType
*/
{
if (isInputParameter(paramAST)) {
"EXC_MultipleEJBNameParameter", // NOI18N
return typeSupport.errorType;
} else {
}
}
return typeSupport.booleanType;
}
/**
* Returns <code>true</code> if ast denotes a entity bena value.
*/
{
case SINGLE_CMR_FIELD_ACCESS:
case IDENTIFICATION_VAR:
return true;
case INPUT_PARAMETER:
}
return false;
}
/**
* Returns <code>true</code> if ast denotes a literal.
*/
{
return ((tokenType == INT_LITERAL) ||
(tokenType == LONG_LITERAL) ||
(tokenType == STRING_LITERAL) ||
(tokenType == FLOAT_LITERAL) ||
(tokenType == DOUBLE_LITERAL) ||
}
/**
* Returns <code>true</code> if ast denotes a input parameter access.
*/
{
}
/**
* The method checks the specified node being an expression of type String.
* @param expr the expression to be checked
* @return <code>true</code> if the specified expression has the type String.
*/
{
// handle error type
return true;
// expr must have the type String
return false;
}
// everything is ok => return true;
return true;
}
/**
* The method checks the specified node being an expression of
* type int or java.lang.Integer.
* @param expr the expression to be checked
* @return <code>true</code> if the specified expression has the type
* int or java.lang.Integer.
*/
{
// handle error type
return true;
// expr must have the type int or Integer
return false;
}
// everything is ok => return true;
return true;
}
/**
* The method checks the specified node being an expression of
* type double or java.lang.Double.
* @param expr the expression to be checked
* @return <code>true</code> if the specified expression has the type
* double or java.lang.Double.
*/
{
// handle error type
return true;
// expr must have the type double or Double
return false;
}
// everything is ok => return true;
return true;
}
/**
* The method checks the specified node being an expression of a number type
* (a numeric type or a number wrapper class).
* @param expr the expression to be checked
* @return <code>true</code> if the specified expression has a number type.
*/
{
// handle error type
return true;
// expr must have a number type
return false;
}
// everything is ok => return true;
return true;
}
/**
* The method checks the specified node being an expression of a number type
* (a numeric type or a number wrapper class).
* @param expr the expression to be checked
* @return <code>true</code> if the specified expression has a number or
* String type
*/
{
// handle error type
return true;
// expr must have a number type
"EXC_NumberOrStringExprExpected", //NOI18N
return false;
}
// everything is ok => return true;
return true;
}
/**
* The method checks whether the specified node denotes a valid abstract
* schema type.
* @param ident the node to be checked
* @return the type info for the abstract bean class of the specified
* abstract schema type.
*/
{
}
return typeInfo;
}
/**
* Returns true if the specified text is a string literal consisting of a
* single char. Escaped chars are counted as a single char such as \ uxxxx.
*/
{
int i = 0;
if (length == 0) {
// empty string
return false;
}
{
i++;
if (i == length) {
// string literal was '\'
return true;
}
// escaped char => check the next char
// unicode
i +=5;
}
i++;
i++;
i++;
}
}
}
i++;
i++;
}
}
else {
i++;
}
}
// check special EJBQL single quote char
i++;
i++;
}
}
else {
i++;
}
// reached end of text?
return (i == length);
}
/** Returns true if the specified char is an octal digit */
private boolean isOctalDigit(char c)
{
return ('0' <= c && c <= '7');
}
}
// rules
{
checkSelectOrderbyClause(#s, #o);
}
;
// ----------------------------------
// rules: from clause
// ----------------------------------
: #( FROM ( identificationVarDecl )+ )
;
;
{
}
}
;
{
// check abstract schema name
// check identification variable
}
}
;
// ----------------------------------
// rules: select clause
// ----------------------------------
{
if (finderNotSelector) {
}
else {
}
}
;
: DISTINCT
| // empty rule
{
// Insert DISTINCT keyword, in the case of a multi-object selector
// having java.util.Set as return type
if (!finderNotSelector &&
}
}
;
{
(decl instanceof IdentificationVariable)) {
}
else {
}
#o.setTypeInfo(typeInfo);
}
{
// check numeric type
"EXC_NumberExprExpected", //NO18N
}
isAggregate = true;
}
{
// check numeric type
"EXC_NumberExprExpected", //NO18N
}
isAggregate = true;
}
{
// check orderable type
"EXC_OrderableExpected", //NO18N
}
isAggregate = true;
}
{
// check orderable type
"EXC_OrderableExpected", //NO18N
}
isAggregate = true;
}
{
isAggregate = true;
}
;
: v:IDENT
{
(decl instanceof IdentificationVariable)) {
}
else {
}
#v.setTypeInfo(typeInfo);
}
;
// ----------------------------------
// rules: where clause
// ----------------------------------
: #( WHERE e:expression )
{
}
}
;
// ----------------------------------
// rules: order by clause
// ----------------------------------
: #( ORDER ( orderbyItem )+ )
| // empty rule
;
{
// check orderable type
"EXC_OrderableOrderbyClauseExpected", //NO18N
}
}
;
// ----------------------------------
// rules: expression
// ----------------------------------
| likeExpr
| inExpr
| function
| primary
;
{
}
{
}
;
{
}
{
}
{
}
{
}
{
}
{
}
;
{
}
{
}
{
}
{
}
;
{
}
{
}
{
else {
}
}
;
{
}
{
}
;
{
}
{
}
;
| p:inputParameter
{
#p.getText())); //NOI18N
}
}
;
: #( ESCAPE escapeCharacter )
| // empty rule
;
: s:STRING_LITERAL
{
// String must be single charater string literal =>
// either '<char>' or ''''
if (!isSingleCharacterStringLiteral(#s.getText())) {
}
}
| p:inputParameter
{
}
}
;
{
}
{
}
;
{
}
{
}
;
{
}
{
}
{
}
;
{
}
{
}
;
;
: concat
| length
| locate
| abs
| sqrt
| mod
;
{
}
;
{
}
;
{
}
;
{
}
;
{
}
;
{
}
;
{
}
;
: literal
;
;
{
if (fieldTypeInfo == null) {
// field is not known
}
else {
"ERR_MissingFieldInfo", //NOI18N
}
// field is not a relationship => cmp field
}
// field is a relationship of a collection type =>
// collection valued cmr field
}
else {
// field is a relationship of a non collection type =>
// single valued cmr field
#i.setType(SINGLE_CMR_FIELD);
}
}
#i.setTypeInfo(fieldTypeInfo);
}
;
;
: i:IDENT
{
// check for identification variables
}
else {
}
}
;
: p:pathExpression
{
int fieldTokenType = #p.getType();
if ((fieldTokenType != SINGLE_CMR_FIELD_ACCESS) &&
(fieldTokenType != CMP_FIELD_ACCESS)) {
}
}
;
: p:pathExpression
{
int fieldTokenType = #p.getType();
if ((fieldTokenType != CMP_FIELD_ACCESS)) {
#p.setType(CMP_FIELD_ACCESS);
}
}
;
: p:pathExpression
{
int fieldTokenType = #p.getType();
if (fieldTokenType != SINGLE_CMR_FIELD_ACCESS) {
}
}
;
: p:pathExpression
{
int fieldTokenType = #p.getType();
if (fieldTokenType != COLLECTION_CMR_FIELD_ACCESS) {
}
}
;
: ( inCollectionElement[valueExprTypeInfo] )+
;
: l:literal
{
}
| i:inputParameter
{
}
;
{
}
;