Comments (8)
Default methods aren't static, you must access them through an instance. I added an example how to do it.
from ognl.
@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.
Ah... right :D Yet I added another example and it works, see this test and the interface
from ognl.
@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.
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.
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:
- Calling
Ognl.parseExpression("@ognl.test.objects.StaticInterface@staticMethod()");
, - 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 executeOgnl.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.
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.
@lukaszlenart Thank you very much.
from ognl.
Related Issues (20)
- Should this null-check be here? HOT 2
- Varargs method not found if no varargs are given HOT 2
- Developer Guide uses deprecated code HOT 4
- The 3.2.18 version of ognl.Ognl#getValue(..) is not easy to use at all HOT 3
- "Two vararg methods with same score" for resteasy ClientWebTarget.request() HOT 13
- Move OGNL under a dedicated organisation HOT 10
- java.lang.ArithmeticException: / by zero HOT 3
- Illegal reflective access warning when using "_parameter.containsKey() and Collections.singletonMap HOT 6
- OGNL issues with java 16 and 17 HOT 9
- Struts2 2.5 and struts2-jquery-plugin HOT 2
- In 3.3.3 (and before) compileExpression fails without add-opens on Java 17 HOT 13
- 如何解决安全问题。How ognl solve safety problem HOT 5
- Ognl.createDefaultContext(Object, ClassResolver) throws "IllegalArgumentException" HOT 4
- method with varargs invoked result exception: java.lang.ArrayStoreException HOT 6
- Most unit tests no longer running? HOT 2
- Request for posting of Maven signing key(s) HOT 2
- Dependency Dashboard
- Ognl choosing method on unexported class rather than exported interface
- The class `ASTLess` should be public.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ognl.