Call 'new' constructor Function from java - jxbrowser

Is it possible to call the new operator to create a new javascript object directly from java obtaining a JSObject?
for example, I know I can do:
browser.executeJavaScript("var myString = new String('Hello World');"); and then I could do: JSObject hello = browser.executeJavaScript("myString"), to get the myString object into java. Is it possible to to this in one call? Something like
JSObject hello = new JSString("Hello World")?
Thanks

You can use the following approach to instantiate JavaScript object on Java side:
JSObject object = browser.getJSContext().createObject();

Related

What is the point of JObject's CreateWriter if using the writer just throws ArgumentException?

I have a "legacy" method that takes a JsonWriter and dumps a bunch of json.. Out of this, I want a JObject instance, so I tried the following:
JObject myObj = new JObject();
using (var writer = myObj.CreateWriter())
{
TheLegacyMethod(writer);
}
// TODO: Do stuff with the nicely initialized JObject instance
The issue is that the first time "TheLegacyMethod" attempts to actually use the writer to do a:
writer.WriteStartObject();
I get an ArgumentException complaining that a JObject can't be added to a JObject.
I've used a smelly workaround, writing the Json to a StringBuilder first, then doing a JObject.Parse() on the result... but I would like to avoid this intermediate.
What is the point of JObject.CreateWriter() if you can't use it? Am I missing something? (I'm hoping I am).
Thanks,
Tyler
When writing values to an object the property name must be written before the value.

Create a copy of an object in flex whose constructor has required parameters

When using ObjectUtil.copy() on my object, I get the following error
Argument count mismatch on foo.bar::MyObject(). Expected 1, got 0.
MyObject's constructor takes in one parameter. Is there a way for me to clone, or copy it?
Use the ObjectUtil.clone method. That should do deep copies.
However, I'm not sure if that will solve the issue.
In Flex 4.5 Neither copy, nor clone, create a new version of the object using the new keyword; therefore the constructor is never called. Both do byte array copies. I'd be interested to see your code.
I use:
public static function clone(source:Object):* {
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
Never let me down.

TypeError: Error #1034: Type Coercion failed: cannot convert Object#1456c7b9 to mx.messaging.messages.IMessage

Im trying to connect a Flash client to BlazeDS. There has been some success with this from others using the vanilla BlazeDS setup. However I'm using the new Spring BlazeDS Integration from springsource and running aground.
The flash client actually seems to be working in that I can see the correct data in the body of the returned object, but for some reason unknown it fails casting as an IMessage. It fails in PollingChannel.as on this line with the subject line error
var messageList:Array = msg.body as Array;
for each (var message:IMessage in messageList) <--
On application load I register a whole bunch of classes like so
registerClassAlias( "flex.messaging.messages.RemotingMessage", RemotingMessage );
registerClassAlias("mx.messaging.messages.IMessage", IMessage);
etc..
my code is basically
var channelSet:mx.messaging.ChannelSet = new mx.messaging.ChannelSet();
var channel:mx.messaging.channels.AMFChannel = new AMFChannel("my-amf", "http://localhost:8400/SpringA/messagebroker/amf");
channelSet.addChannel(channel);
var consumer:mx.messaging.Consumer = new Consumer();
consumer.channelSet = channelSet;
consumer.destination = "simple-feed";
consumer.subscribe();
consumer.addEventListener(MessageEvent.MESSAGE, test);
private function test(event:IMessage)
{
trace("msg..");
// breakpoint never makes it here
}
I have a flex client which works 100% with same destination/channel.
The error in the title means that you, for some reason, got an object that is not implementing or extending the IMessage interface, therefore the loop can not cast it in this part:
for each (var message:IMessage in messageList){
Either you should somehow make sure that you don't add anything that is not extending or implementing IMessage, or check if the variable IS actually ext./imp. it. Also - if you want to do that, you will have to change the for each like this:
for each (var obj in messageList){
if (obj is IMessage){
var message:IMessage = obj as IMessage;
// DO STUFF HERE
}
}
Add this Object mapping:
registerClassAlias("flex.messaging.io.ObjectProxy", ObjectProxy);
If on your Java VO objects you have overridden the hashcode() method, this situation could happen.
Remove the hashcode() override (if you are able to).
See my blog for the backstory on how I discovered this. http://squaredi.blogspot.com/2013/12/remoting-landmine-without-stack-trace.html
I had the same error when trying to send an actionscript object to the backend. My problem was that my c# equivalent object was missing an public parameterless constructor.

Image to Object with as3

I'm trying to convert an image in my assets folder
"./assets/image1.png"
to type Object. It needs to be Object because that's what the function I'm using it in is expecting.
Any ideas what would be the simplest way to do this?
Do you mean something like :
[Embed(source="assets/logo.jpg")]
private var logo:Class;
private function init(e:Event):void
{
this.displayImage(logo as Object);
}
private function displayImage(img:Object):void
{
//Assuming you have an image control on stage with an instance
//name of "myImage"
myImage.source = img;
}
If the function you are passing the image to is expecting an Object object, you can in pass anything, it won't reject it. That doesn't mean the function will work correctly, though. Any value will be an Object (except for undefined, which will be accepted but coerced to null and maybe some other strange cases).
So, assuming you didn't write the function yourself, do you have any doc that describes what it expects? Or maybe you have the source code for it?. Otherwise, if the only thing you know about what this function expects is that the parameter must be of type Object... you're in trouble, I think.
Why don't you create a new Object containing the information about the image... including the path.
var obj:Object = new Object();
obj.path = "/assets/image.jpg";
obj.height = 32;
obj.width = 32;
trace(obj.path);
// or, if Flex
Alert.show(obj.path);
And then just pass the new Object into the function and access it like I have above.

Is this possible to invoke a constructor dynamically using relfection if there is no empty default constructor?

I'm using GetParameter to determine what parameters the constructor needs. I can get a list of them. Now I want to invoke the ctor. Is this possible if there is no empty one?
Which language?
For c# you could use
Activator.CreateInstance(typeof(X), constructorparm1, constructorparam2...)
Is this what you are looking for? This creates an instance of SqlConnection passing a string to the constructor. If you want to pass more values, simply add them to the array.
SqlConnection conn;
conn = (SqlConnection)System.Activator.CreateInstance(typeof(SqlConnection), new object[] { "Server=myserver" });
Console.WriteLine(conn.ConnectionString);

Resources