protected String generateGetter()

in tapestry-framework/src/org/apache/tapestry/enhance/javassist/OGNLExpressionCompiler.java [298:372]


    protected String generateGetter(OgnlContext context, CompiledExpression compiled)
            throws Exception
    {
        String pre = "";
        String post = "";
        String body;
        String getterCode;

        context.setRoot(compiled.getRoot());
        context.setCurrentObject(compiled.getRoot());
        context.remove(PRE_CAST);

        try
        {
            getterCode = compiled.getExpression().toGetSourceString(context, compiled.getRoot());
        } catch (NullPointerException e)
        {
            if (_log.isDebugEnabled())
                _log.warn("NullPointer caught compiling getter, may be normal ognl method artifact.", e);

            throw new UnsupportedCompilationException("Statement threw nullpointer.");
        }

        if (getterCode == null || getterCode.trim().length() <= 0
                                  && !ASTVarRef.class.isAssignableFrom(compiled.getExpression().getClass()))
        {
            getterCode = "null";
        }

        String castExpression = (String) context.get(PRE_CAST);

        if (context.getCurrentType() == null
            || context.getCurrentType().isPrimitive()
            || Character.class.isAssignableFrom(context.getCurrentType())
            || Object.class == context.getCurrentType())
        {
            pre = pre + " ($w) (";
            post = post + ")";
        }

        String rootExpr = !getterCode.equals("null") ? getRootExpression(compiled.getExpression(), compiled.getRoot(), context) : "";

        String noRoot = (String) context.remove("_noRoot");
        if (noRoot != null)
            rootExpr = "";

        createLocalReferences(context, generateClassFab(compiled), compiled.getGetterMethod().getParameterTypes());

        if (OrderedReturn.class.isInstance(compiled.getExpression()) && ((OrderedReturn) compiled.getExpression()).getLastExpression() != null)
        {
            body = "{ "
                   + (ASTMethod.class.isInstance(compiled.getExpression()) || ASTChain.class.isInstance(compiled.getExpression()) ? rootExpr : "")
                   + (castExpression != null ? castExpression : "")
                   + ((OrderedReturn) compiled.getExpression()).getCoreExpression()
                   + " return " + pre + ((OrderedReturn) compiled.getExpression()).getLastExpression()
                   + post
                   + ";}";

        } else
        {
            body = "{ return " + pre
                   + (castExpression != null ? castExpression : "")
                   + rootExpr
                   + getterCode
                   + post
                   + ";}";
        }

        body = body.replaceAll("\\.\\.", ".");

        if (_log.isDebugEnabled())
            _log.debug("Getter Body: ===================================\n" + body);

        return body;
    }