Giter VIP home page Giter VIP logo

Comments (8)

lukaszlenart avatar lukaszlenart commented on September 15, 2024

Default methods aren't static, you must access them through an instance. I added an example how to do it.

from ognl.

hengyunabc avatar hengyunabc commented on September 15, 2024

@lukaszlenart Thank you for your reply. The example above does not invoke the default method; it calls a static method of an interface. It can be called directly without creating an object.

System.err.println(III.staticMethod()); // ok

from ognl.

lukaszlenart avatar lukaszlenart commented on September 15, 2024

Ah... right :D Yet I added another example and it works, see this test and the interface

from ognl.

hengyunabc avatar hengyunabc commented on September 15, 2024

@lukaszlenart Thanks.

It seems that using Ognl.compileExpression works correctly, using Ognl.parseExpression error.

        OgnlContext context = Ognl.createDefaultContext(null, new DefaultMemberAccess(false));
        Root root = new Root();
        context.setRoot(new Object());

        Object parseExpression = Ognl.parseExpression("@ognl.test.objects.StaticInterface@staticMethod()");

        Node compileExpression = Ognl.compileExpression(context, root,
                "@ognl.test.objects.StaticInterface@staticMethod()");

        // ok
        System.out.println(Ognl.getValue(compileExpression, context, root)); // or
        // error
        System.out.println(Ognl.getValue(parseExpression, context)); //error

output:

static
Exception in thread "main" ognl.OgnlException: No method accessor for interface ognl.test.objects.StaticInterface

from ognl.

lukaszlenart avatar lukaszlenart commented on September 15, 2024

I'm not sure if I understand the problem - parseExpression is rather used to validate the expression, it isn't used to compile the expression into an executable form

from ognl.

hengyunabc avatar hengyunabc commented on September 15, 2024

Sorry, maybe I didn't explain the situation clearly. I'll try my best to clarify.

Initially, I thought the following invocation would succeed:

        OgnlContext context = Ognl.createDefaultContext(null, new DefaultMemberAccess(false));
        Root root = new Root();
        context.setRoot(new Object());
        
        // javadoc: Convenience method that combines calls to parseExpression and getValue.
        Ognl.getValue("@ognl.test.objects.StaticInterface@staticMethod()", root); // error

However, it fails.

Based on the specific code execution process, I found that it is:

  1. Calling Ognl.parseExpression("@ognl.test.objects.StaticInterface@staticMethod()");,
  2. Then calling Ognl.getValue(parseExpression, context).

In the example you provided: https://github.com/orphan-oss/ognl/blob/master/src/test/java/ognl/test/StaticsAndConstructorsTest.java#L51

It can execute successfully because in the testcase, it explicitly calls Ognl.compileExpression

Ognl.compileExpression(context, root,
                "@ognl.test.objects.StaticInterface@staticMethod()");

Then it executes Ognl.getValue(compileExpression, context, root), so it can succeed.


So, my question is:

  • ognl.Ognl.getValue(String, Object), should it be able to execute Ognl.getValue("@ognl.test.objects.StaticInterface@staticMethod()", root) successfully?
  • Because other expressions can be executed successfully, for example: Ognl.getValue("@ognl.test.objects.StaticInterface@class", root).

from ognl.

lukaszlenart avatar lukaszlenart commented on September 15, 2024

class is threaten in some special way and from Ognl perspective class is a static field which is represented by ASTSTaticField class, while staticMethod() is a static method represented by ASTStaticMethod class - you cannot compare behaviour of those two.

Please also take a look on the compileExpression implementation:

    public static Node compileExpression(OgnlContext context, Object root, String expression) throws Exception {
        Node expr = (Node) Ognl.parseExpression(expression);

        OgnlRuntime.compileExpression(context, expr, root);

        return expr;
    }

You can notice the expression is parsed in the first step and then compiled. Just parsing the expression isn't enough, it works for class because there are special cases to cover such behaviour -> OgnlRuntime#getStaticField()

I would say the Ognl behaves properly, parsed expression isn't compiled - it's like Java source code, it should be parsable to be compiled into binary code.

from ognl.

hengyunabc avatar hengyunabc commented on September 15, 2024

@lukaszlenart Thank you very much.

from ognl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.