Does ExpressionLanguage (EL) allow to call a method with a NULL argument? - java

I use Tomcat7 with JSF2. I use method invocations like
action="#{bean.method(22)}"
or
action="#{bean.method(var)}"
where var is an EL variable.
However, if var is null, or i try to call #{bean.method(null) i get an NPE and the method is not called.
Is there any way to pass a null argument to a method in EL?
Is there a literal for null in EL?
Thanks

I can reproduce this issue on Tomcat 7.0.22, but not on Glassfish 3.1.1. The NPE stacktrace hints that the Apache EL implementation is wrong here:
Caused by: java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at org.apache.el.util.ReflectionUtil.isAssignableFrom(ReflectionUtil.java:299)
at org.apache.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:172)
at org.apache.el.parser.AstValue.invoke(AstValue.java:251)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
It shouldn't have called Class#isAssinableFrom() with a null argument. Its javadoc also forbids that. I'd report it as a bug to the Tomcat guys over there at apache.org. Depending on what the EL 2.2 spec says, it should either been allowed (and thus the Class#isAssignableFrom() call should have been skipped), or been thrown as an ELException or one of its subclasses.

If possible (you have access to the bean method), you could change your method from int to Integer and then do a check for null before setting it.
method(int myInteger){
if(myInt == null){
return;
}
this.beanInt = myInteger;
}

I had the same issue on Apache 7.0.47. It disappeared after a update to 7.0.56.
So it probably was a bug, although I couldn't find any issue tracker entry.

Related

Issue in addPlugInSingleRowFunction upgrading Esper version from 5.3 to 8.3

In our existing application we are using Esper Version 5.3.
We have added few addPlugInSingleRowFunction() to use it in EPL as below --
final Configuration cepConfiguration = new Configuration();
cepConfiguration.addPlugInSingleRowFunction("toNumber", Double.class.getName(), "parseDouble");
cepConfiguration.addPlugInSingleRowFunction("toBoolean", Boolean.class.getName(), "parseBoolean");
This was working fine in 5.3 version.
Post upgrading to 8.3 above code changed as per Esper documentation --
cepConfiguration.getCompiler().addPlugInSingleRowFunction("toNumber", Double.class.getName(), "parseDouble");
cepConfiguration.getCompiler().addPlugInSingleRowFunction("toBoolean", Boolean.class.getName(), "parseBoolean");
But once the sendEventBean() method is called to send a Event to runtime we are seeing below exception every time.
Surprisingly events are getting matched as per the statements present in runtime even if below exception are coming. Though we are not sure whether some events are not matching or not.
Can someone please help on this?
applog.cls=com.espertech.esper.common.internal.epl.expression.dot.core.ExprDotNodeForgeStaticMethodEval,applog.mthd=staticMethodEvalHandleInvocationException,applog.line=228,applog.msg=Invocation exception when invoking method 'parseDouble' of class 'java.lang.Double' passing parameters [null] for statement 'stmt-0': NullPointerException : null,exc.stack=java.lang.NullPointerException\n\tat sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)\n\tat sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)\n\tat java.lang.Double.parseDouble(Double.java:538)\n\tat generated.StatementAIFactoryProvider_a4bd241445010f45474e4598e34521ca1b2836db_stmt450.m8(StatementAIFactoryProvider_a4bd241445010f45474e4598e34521ca1b2836db_stmt450.java:161)\n\tat generated.StatementAIFactoryProvider_a4bd241445010f45474e4598e34521ca1b2836db_stmt450$2.get(ANONYMOUS.java:148)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.FilterParamIndexEquals.matchEvent(FilterParamIndexEquals.java:32)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.FilterHandleSetNode.matchEvent(FilterHandleSetNode.java:100)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.EventTypeIndex.matchType(EventTypeIndex.java:178)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.EventTypeIndex.matchEvent(EventTypeIndex.java:124)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.FilterServiceBase.retryableMatchEvent(FilterServiceBase.java:179)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.FilterServiceBase.evaluateInternal(FilterServiceBase.java:96)\n\tat com.espertech.esper.runtime.internal.filtersvcimpl.FilterServiceLockCoarse.evaluate(FilterServiceLockCoarse.java:52)\n\tat com.espertech.esper.runtime.internal.kernel.service.EPEventServiceImpl.processMatches(EPEventServiceImpl.java:610)\n\tat com.espertech.esper.runtime.internal.kernel.service.EPEventServiceImpl.processWrappedEvent(EPEventServiceImpl.java:450)\n\tat com.espertech.esper.runtime.internal.kernel.thread.InboundUnitSendEvent.run(InboundUnitSendEvent.java:43)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat java.lang.Thread.run(Thread.java:748)
You could turn on compiler logging (config.getCompiler().getLogging().setEnableCode(true);) and make sure you have INFO level logging. You can inspect "StatementAIFactoryProvider_a4bd241445010f45474e4598e34521ca1b2836db_stmt450.m8" at line 161 to see what the problem is. Sounds like a null value gets passed to Double.parseDouble. But since I don't have the complete code its hard to say.

Why does Java 8's Nashorn engine in strict mode throw a java.lang.ClassCastException when calling apply() and passing the arguments object directly?

When I call eval (in strict mode) on a nashorn engine with the following script I get an exception:
var yfunc = function () {
(null).apply(null, arguments);
};
yfunc();
I've truncated my personal situation heavily. The "(null)" on line 2 can be replaced with anything between parenthesis or a local variable, either way just something that shouldn't throw a compile error, and it will yield the same result.
The issue seems to be explicitly that "arguments" is passed directly as the second argument of calling a method called "apply". Any of the following changes will undo the thrown exception:
Putting "arguments" in a variable first (but simply wrapping it in parenthesis doesn't work!)
Calling something other than apply
Passing "arguments" in a different argument slot when calling apply
Calling print() (with or without passing any arguments) as a preceding line of code inside yfunc() (weird huh?)
Defining more than 0 parameters for yfunc()
Binding yfunc first and then calling the bound method
Calling yfunc via Function.apply (not so much with Function.call!)
The Exception thrown is this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:4)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
When I call this method with an owner, the exception thrown changes. Example code:
var yfunc = {
method: function () {
(null).apply(null, arguments);
}
};
var x = yfunc.method();
Then the thrown exception looks like this:
Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to jdk.nashorn.internal.runtime.ScriptFunction
at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:5)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:449)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:406)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:402)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:155)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
I've reproduced the issue so far on specifically these environments:
windows 7 64bit -> jdk 1.8.0_60 64bit
windows 8 64bit -> jdk 1.8.0_131 64bit
I can't seem to find anything on the internet about similar issues. Do I need to report this to Oracle/OpenJDK?
Minor update
Added items 6 and 7 to list of "following changes will undo the thrown exception".
Final update
Bug filed: JDK-8184720
Yes, it appears to be a bug. Please file a bug.

Getting error .. Unable to parse EL function ${class} [duplicate]

Currenty I have a web project with JSF 1.2 and Facelets running in tomcat 6.0.18.0. I decided to upgrade the servlet container, thus i deployed in tomcat 7 and all seemed ok until we hit one view using my custome facelet functions.
javax.el.ELException: Failed to parse the expression [{pz:instanceof(object,'com.project.domain.MyClass')}]
Caused by: org.apache.el.parser.ParseException: Encountered " ":" ": "" at line 1, column 5. Was expecting one of:
"}" ...
"." ...
"[" ...
This error occurs when parsing the following code:
<ui:repeat var="object" value="#{objects}">
<ui:fragment rendered="#{pz:instanceof(object,'com.project.domain.MyClass')}">
...
If i understand correctly it throws an error because of the colon in the expression . I have tracked it down to the jasper-el that come with in the tomcat/lib directory, and if I replace jasper.jar and jasper-el.jar with the ones from tomcat 6.0.18 everythign works well.
Has anyone else had this problem before upgrading their tomcat? And How did they resolve it?
Could I deploy in production tomcat 7 with these jasper jar from tomcat 6, or could this cause further problems.
This is actually a misleading exception. It has a different underlying cause. The function name instanceof is invalid.
The EL 2.2 specification says the following:
1.14 Reserved Words
The following words are reserved for the language and must not be used as
identifiers.
and eq gt true instanceof
or ne le false empty
not lt ge null div mod
Note that many of these words are not in the language now, but they may be in the
future, so developers must avoid using these words.
and
1.19 Collected Syntax
...
Identifier ::= Java language identifier
...
Where the Java language identifier stands for keywords like instanceof, if, while, class, return, static, new, etc. They may not be used as variable/function names in EL. In case you have properties with those names, use the brace notation instead like so #{bean['class'].simpleName} instead of #{bean.class.simpleName}.
This was been fixed in Tomcat 7.0.4 or somewhere near before this version as indicated by issue 50147 wherein someone else pointed out the same problem as you have. So, to solve your problem, you have to rename your EL function name to for example isInstanceOf or something.
Add this line in catalina.properties ([tomcat folder]/conf), and it should fix the issue.
org.apache.el.parser.SKIP_IDENTIFIER_CHECK=true
However, you should not use the reserved words.
You can also try changing the syntax. I had the same exact problem with code that I was maintaining when we were moving from Tomcat 6 to 7. I had to change myobject.class.name to myobject['class'].name. After I made this change my code worked perfectly again.
Great hint, indeed! I had to change in my jspx ${instance.class.simpleName == ...} with ${instance['class'].simpleName eq ...}.
I was moving from vFabric on tomcat 6 to vFabric on tomcat 7

NullPointerException at java.util.logging.Logger.demandLogger

After upgrading from Java 6 to Java 8, my application throws the following exception:
com.mathworks.toolbox.javabuilder.MWException: Java exception occurred:
java.lang.NullPointerException
at java.util.logging.Logger.demandLogger(Logger.java:451)
at java.util.logging.Logger.getLogger(Logger.java:502)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.access$600(MWMCR.java:23)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mclFeval(MWMCR.java:833)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:731)
at com.sun.proxy.$Proxy2.mclFeval(Unknown Source)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.invoke(MWMCR.java:406)
at mDataEngine.mDataEngineMIF.volatility(mDataEngineMIF.java:7212)
This occurs when using the mathworks library, which in turn uses java.util.logging.Logger where the exception is thrown.
This can be solved by setting the following system property when starting the Java program:
-Dsun.util.logging.disableCallerCheck=true
More detailed information:
The reason for the NullpointerException seems to be explained here: http://www.infoq.com/news/2013/07/Oracle-Removes-getCallerClass
The method getCallerClass is used here in java.util.logging.Logger:
public static Logger More ...getLogger(String name) {
return demandLogger(name, null, Reflection.getCallerClass());
}
This leads to the variable caller to be null in the following code of java.util.logging.Logger:
if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
if (caller.getClassLoader() == null) {
return manager.demandSystemLogger(name, resourceBundleName);
}
}
return manager.demandLogger(name, resourceBundleName, caller);
By setting the systemvariable as explained above, the caller variable will not be used.
The Oracle bug JDK-8145302 NullPointerException at java.util.logging.Logger.demandLogger has been replaced with
JDK-8177325 Caller sensitive methods Logger.getLogger, Logger.getAnonymousLogger, will throw NPE if there is no caller on the stack.
The workaround in that bug report is listed as:
Workaround: use an auxiliary class in order to call Logger.getLogger instead of calling Logger::getLogger directly from JNI.
As you can see from your stacktrace:
java.lang.NullPointerException
at java.util.logging.Logger.demandLogger(Logger.java:451)
at java.util.logging.Logger.getLogger(Logger.java:502)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method)
...
the com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval is a JNI method calling java.util.logging.Logger.getLogger.
Mathworks should update the MWMCR class to include a java helper method to invoke getLogger and that helper method should be called from JNI instead of getLogger directly.

Why doesn't printStackTrace work in Clojure?

In both the Joy of Clojure and on Alex Miller's Pure Danger Tech blog-post it is recommended that you can print the last stack using something like the following:
(use 'clojure.stacktrace)
(java.util.Date. "foo")
(.printStackTrace *e 5)
But I can't get any of their examples to work, and instead just get
java.lang.NullPointerException: null
Reflector.java:26 clojure.lang.Reflector.invokeInstanceMethod
(Unknown Source) jtown$eval9755.invoke
What's up with this? .printStackTrace seems to be a Java function from the looks of it, so I am not sure why I am bringing clojure.stacktrace into my namespace, in the first place. I read through the clojure.stacktrace API, though, and see an e function, which seems similar too but is not the *e function, which is in core and is supposed to be binding to the last exception, but isn't. Could somebody straighten me out on the best way to check stack-traces?
There are some special vars available when using the REPL and
*e - holds the result of the last exception.
For instance:
core=> (java.util.Date. "foo")
IllegalArgumentException java.util.Date.parse (Date.java:615)
core=> (class *e)
java.lang.IllegalArgumentException
core=> (.printStackTrace *e)
java.lang.IllegalArgumentException
at java.util.Date.parse(Date.java:615)
<not included.....>
You are right, .printStackTrace is the java method that is invoked on the exception class. This is not very straightforward (since its java interop) so clojure.stacktrace namespace has some utilities about working with stack traces
So after
(use 'clojure.stacktrace)
you can use the stacktrace library instead of java interop:
core=> (print-stack-trace *e)
java.lang.IllegalArgumentException: null
at java.util.Date.parse (Date.java:615)
<not included.....>
Obviously in an app, instead of *e, you can do a try - catch and use the related functions as necessary
I use
(.printStackTrace *e *out*)
That seems to work.

Categories

Resources