Search in sources :

Example 1 with PrimaryExpressionIsClassStaticFieldException

use of org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException in project datanucleus-core by datanucleus.

the class JavaQueryCompiler method compileResult.

public Expression[] compileResult() {
    if (result == null) {
        return null;
    }
    Node[] node = parser.parseResult(result);
    Expression[] expr = new Expression[node.length];
    for (int i = 0; i < node.length; i++) {
        ExpressionCompiler comp = new ExpressionCompiler();
        comp.setSymbolTable(symtbl);
        comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
        // Find the last child of this node, and check for an alias (type = NAME)
        String alias = null;
        Node aliasNode = null;
        List<Node> childNodes = node[i].getChildNodes();
        if (childNodes != null && childNodes.size() > 0) {
            Node lastChild = childNodes.get(childNodes.size() - 1);
            if (lastChild.getNodeType() == NodeType.NAME) {
                // Alias node
                aliasNode = lastChild;
            }
        }
        if (aliasNode != null) {
            alias = (String) aliasNode.getNodeValue();
            node[i].removeChildNode(aliasNode);
        }
        if (candidateAliasOrig != null) {
            swapCandidateAliasNodeName(node[i]);
        }
        if (parameterSubtitutionMap != null) {
            node[i] = swapSubqueryParameters(node[i]);
        }
        expr[i] = comp.compileExpression(node[i]);
        if (alias != null) {
            expr[i].setAlias(alias);
        }
        try {
            expr[i].bind(symtbl);
        } catch (PrimaryExpressionIsClassLiteralException peil) {
            // PrimaryExpression should be swapped for a class Literal
            expr[i] = peil.getLiteral();
            expr[i].bind(symtbl);
        } catch (PrimaryExpressionIsClassStaticFieldException peil) {
            // PrimaryExpression should be swapped for a static field Literal
            Field fld = peil.getLiteralField();
            try {
                // Get the value of the static field
                Object value = fld.get(null);
                expr[i] = new Literal(value);
                expr[i].bind(symtbl);
            } catch (Exception e) {
                throw new NucleusUserException("Error processing static field " + fld.getName(), e);
            }
        } catch (PrimaryExpressionIsVariableException pive) {
            // PrimaryExpression should be swapped for an implicit variable
            expr[i] = pive.getVariableExpression();
            expr[i].bind(symtbl);
        }
        if (expr[i] instanceof PrimaryExpression) {
            String id = ((PrimaryExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        } else if (expr[i] instanceof ParameterExpression) {
            String id = ((ParameterExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        } else if (expr[i] instanceof VariableExpression) {
            String id = ((VariableExpression) expr[i]).getId();
            if (isKeyword(id)) {
                throw new NucleusUserException(Localiser.msg("021052", getLanguage(), id));
            }
        }
    }
    return expr;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryExpressionIsClassLiteralException(org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) PrimaryExpressionIsClassStaticFieldException(org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException) QueryCompilerSyntaxException(org.datanucleus.store.query.QueryCompilerSyntaxException) PrimaryExpressionIsVariableException(org.datanucleus.query.expression.PrimaryExpressionIsVariableException) NucleusException(org.datanucleus.exceptions.NucleusException) PrimaryExpressionIsClassLiteralException(org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) PrimaryExpressionIsClassStaticFieldException(org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException) Field(java.lang.reflect.Field) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) PrimaryExpressionIsVariableException(org.datanucleus.query.expression.PrimaryExpressionIsVariableException) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Aggregations

Field (java.lang.reflect.Field)1 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 Expression (org.datanucleus.query.expression.Expression)1 ExpressionCompiler (org.datanucleus.query.expression.ExpressionCompiler)1 Literal (org.datanucleus.query.expression.Literal)1 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)1 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)1 PrimaryExpressionIsClassLiteralException (org.datanucleus.query.expression.PrimaryExpressionIsClassLiteralException)1 PrimaryExpressionIsClassStaticFieldException (org.datanucleus.query.expression.PrimaryExpressionIsClassStaticFieldException)1 PrimaryExpressionIsVariableException (org.datanucleus.query.expression.PrimaryExpressionIsVariableException)1 VariableExpression (org.datanucleus.query.expression.VariableExpression)1 QueryCompilerSyntaxException (org.datanucleus.store.query.QueryCompilerSyntaxException)1