Error in ColdFusion Javacast array to class from loadPaths - java

I am trying to use Elasticsearch's Java API.
I am trying to create a RestClientBuilder.
Host=createObject("java", "org.apache.http.HttpHost").init(variables.HostName, variables.Port);
Node=createObject("java", "org.elasticsearch.client.Node").init(Host);
RestClient=createObject("java", "org.elasticsearch.client.RestClient").builder(Javacast("org.elasticsearch.client.Node[]", [Node])).build();
I get the error
Cannot convert the value to Java array because type org.elasticsearch.client.Node is unknown.
Also if I just try to use:
RestClient=createObject("java", "org.elasticsearch.client.RestClient").builder(Javacast("org.apache.http.HttpHost[]", [Host]));
I get the following error
Either there are no methods with the specified method name and
argument types or the builder method is overloaded with argument types
that ColdFusion cannot decipher reliably. ColdFusion found 0 methods
that match the provided arguments. If this is a Java object and you
verified that the method exists, use the javacast function to reduce
ambiguity.
This I assume is because ColdFusion doesn't play nicely with varargs

I found a workaround using this method
https://www.bennadel.com/blog/1980-tojava---a-coldfusion-user-defined-function-for-complex-java-casting.htm
I believe there is a bug with Javacast and javaSettings loadPaths not being used.
coldfusion.runtime.Cast$UnknownTypeException: Cannot convert the value
to Java array because type org.elasticsearch.client.Node is unknown.
at coldfusion.runtime.Cast.toJavaArray(Cast.java:1602)
Additionally if I try to perform the actiuons that the UDF takes
local.javaClass = createObject("java", "org.apache.http.HttpHost");
local.HostArrayReflect = createObject("java", "java.lang.reflect.Array");
local.HostArray = local.HostArrayReflect.newInstance(
local.javaClass.GetClass()
, JavaCast( "int", ArrayLen(local.Hosts))
);
for (i=0; i LT ArrayLen(local.Hosts); i=i+1) {
local.HostArrayReflect.Set(local.HostArray, JavaCast("int", i), local.Hosts[i]);
}
I get the error
An exception occurred while instantiating a Java object. The class
must not be an interface or an abstract class. If the class has a
constructor that accepts an argument, you must call the constructor
explicitly using the init(args) method. Error :
org.apache.http.HttpHost
java.lang.NoSuchMethodException: org.apache.http.HttpHost.() at
java.lang.Class.getConstructor0(Class.java:3082) at
java.lang.Class.newInstance(Class.java:412) at
coldfusion.runtime.java.JavaProxy.createObjectWithDefaultConstructor(JavaProxy.java:209)
at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:92)
This happens when I try to run getClass(), but in the UDF there is no issue. A coworker tried to run this on Lucee and it seems to have worked, so I believe there is a bug in CF related to this.

Related

XSL: Cannot convert argument/return type in call to method

I got XSL file which i'm trying to generate over my app.
The code goes like this:
TransformerFactory factory = TransformerFactory.newInstance();
Source styleSheetLoc = new ResourceSource(styleSheetLocation);
Templates t = factory.newTemplates(styleSheetLoc); // <<< throwing the Exception
return t.newTransformer();
The exception is:
ERROR: 'Cannot find external method 'com.am.caretalks.util.XsltUtils.getResourceString' (must be public).'
FATAL ERROR: 'Cannot convert argument/return type in call to method 'com.am.caretalks.util.XsltUtils.getResourceString(node-set, node-set, int)''
javax.xml.transform.TransformerConfigurationException: Cannot convert argument/return type in call to method 'com.am.caretalks.util.XsltUtils.getResourceString(node-set, node-set, int)'
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:990)
at com.am.caretalks.sendreport.CreatePDF.buildTransformer(CreatePDF.java:1147)
at com.am.caretalks.sendreport.CreatePDF.createProviderEngagementPDF(CreatePDF.java:608)
at com.am.caretalks.sendreport.CreatePDF.createProviderEngagementPDF(CreatePDF.java:586)
at com.am.caretalks.admin.EngagementReportExportServiceImpl.exportAndUpload(EngagementReportExportServiceImpl.java:131)
at com.am.caretalks.admin.EngagementReportExportServiceImpl$$FastClassBySpringCGLIB$$38327504.invoke(<generated>)
EDIT:
This is the called function from the XSL file
public static String getResourceString(final String messageKey, final String locale, final String customerIndex) {
.
.
}
I searched the web and I found a solution that tells me to use only objects (not primitives) while calling to a function from my XSL file to my java code and due to that i'm getting these error, but nothing did helped me here.
The big issue here that I don't understand what is the error and how to solve it, what do I need to do according the thrown Exception?
Any other suggestions to fix this issue are welcome
The error message suggests that you are passing inappropriate arguments to the function. It appears the method expects (string, string, string) and you are passing (node-set, node-set, int). I don't know Xalan well, but try doing an explicit conversion of the supplied arguments to the required type by calling number() or string().

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.

Integrating java and .net dll using JNI

I am doing a small project it is a interoperability of java and .net dll.
Focus:
I just have a java file which calls the .net dll which is created by using C# and CPP and MCPP..
The program is just a hello world Program..
I just refer the below mentioned sites.
http://www.codeproject.com/Articles/378826/How-to-wrap-a-Csharp-library-for-use-in-Java
http://www.codeproject.com/Articles/13093/C-method-calls-within-a-Java-program
Finally I just get some ideas and finally did this with some errors.
Coding:
#using "mscorlib.dll"
#using "CSharpHelloWorld.netmodule"
using namespace System;
public __gc class HelloWorldC
{
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld __gc *t;
HelloWorldC() {
t = new CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};
Errors:
Error 1 error C4980: '__gc' : use of this keyword requires /clr:oldSyntax command line option
Error 2 error C3699: 'interior_ptr' : cannot use this indirection on type 'CSharpHelloWorld'
Error 3 error C2750: 'CSharpHelloWorld' : cannot use 'new' on the reference type; use 'gcnew' instead
Error 4 error C2440: '=' : cannot convert from 'CSharpHelloWorld *' to 'CSharpHelloWorld ^' 11 WindowsComponentProject
Error 5 error C2011: 'HelloWorldC' : 'class' type redefinition 6 WindowsComponentProject
Error 6 error C3699: '*' : cannot use this indirection on type 'HelloWorldC' 18 WindowsComponentProject
Error 7 error C2750: 'HelloWorldC' : cannot use 'new' on the reference type; use 'gcnew' instead 18 WindowsComponentProject
Error 8 error C2440: 'initializing' : cannot convert from 'HelloWorldC *' to 'HelloWorldC ^' 18 WindowsComponentProject
Error 9 error C2027: use of undefined type 'HelloWorldC' 21 WindowsComponentProject
Error 10 error C2227: left of '->callCSharpHelloWorld' must point to class/struct/union/generic type 21 WindowsComponentProject
I just seek some sites for the solution to change the properties of CLR but it doesn't works kindly help me over it.. thanks in advance !!!
You are using old Managed C++ syntax.
New one is called C++/CLI.
You create a reference type by prefixing the class declaration with ref keyword. Use ^ to declare a reference variable as in CSharpHelloWorld^ t.
gcnew is what you use to create an object in managed heap.
You should modify your class as shown below.
using namespace System;
public ref class HelloWorldC {
public:
// Provide .NET interop and garbage collecting to the pointer.
CSharpHelloWorld^ t;
HelloWorldC() {
t = gcnew CSharpHelloWorld();
// Assign the reference a new instance of the object
}
// This inline function is called from the C++ Code
void callCSharpHelloWorld() {
t->displayHelloWorld();
}
};

Undefined objects in JavaScript engine in Java

Executing the following code in Java7
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("js");
Bindings b = scriptEngine.createBindings();
b.put("x", true);
scriptEngine.eval("x&y", b);
I get the error
sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "b" is not defined. (<Unknown Source>#1) in <Unknown Source> at line number 1
Is there an option to evaluate to null/false for undefined objects, like in JavaScript?
I know that an option will be to do something like "this.x&this.y" instead of "x&y", but I don't have control over that string (user entered).
I browsed a little bit through the Rhino code and it seems that there's no such option.
In the end I will append "this." in front of each variable. This is not by far a desirable solution (I will not even accept my own answer :) ), but for the time being I have no other.

defining keys in play framework application conf file

I'm trying to add new keys in application.conf file in play framework 2.1. I have added the following keys:
gen.db.host=localhost
gen.db.port=27017
gen.db.name=test
When i start my application, it is throwing the following error:
Configuration error: Configuration error[application.conf: 46: port has type NUMBER rather than OBJECT]
......
......
......
Caused by: com.typesafe.config.ConfigException$WrongType: application.conf: 46: port has type NUMBER rather than OBJECT
I don't understand this issue. How can i resolve it? Also, is it a good practice to define new keys in application.conf file?
Thanks.
Put the port between double quotes:
gen.db.port="27017"
You have to use a colon
gen.dn.port:"26017"
Here's a link to the current documentation
http://www.playframework.com/documentation/2.0.x/Configuration
You are propably using wrong method to extract the data from config. I assume you use it like:
current.configuration.getConfig("gen.db.port")
But this method returns play.api.Configuration and expects an object to be under the path "gen.db.port" (as it is mentioned in the error). Since under the "gen.db.port" path you have a number, you should change the method to:
current.configuration.getNumber("gen.db.port")

Categories

Resources