cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a Java Map in Javascript

iblanco
Confirmed Champ
Confirmed Champ
Does anyone know how I can create a Java Map in Javascript, it seems like this should work:


var a = new java.util.HashMap();
a.put("key1", "value1");
a.put("key2", "value2");

But it doesn't work because although the new sentence runs flawlessly "a" is just null.

I see that Alfresco's scripts use always the "ScriptableHashMap" type as a result type for many of their functions, so I tried:


var a = Packages.org.alfresco.repo.jscript.ScriptableHashMap();

But with the same sad result.

Is there any reason why Rhino or Alfresco's Rhino integration doesn't allow the creation of maps ? Something security policyes related or somehting like that ?
4 REPLIES 4

smicyk
Champ in-the-making
Champ in-the-making
Hi,

in second example you should use new operator


var a = new Packages.org.alfresco.repo.jscript.ScriptableHashMap();

but why not to use simple JavaScript Array?


var a = new Array();
a["key1"] = "value1";
a["key2"] = "value2";

Thanks,
smicyk

iblanco
Confirmed Champ
Confirmed Champ
The lack of the "new" was a typo in the post, I have tried with the new but no luck.
I want to use a Map because I've exposed the ruleService as a JavaScript object and it requires a Map in some of its methods, and the javascript array does not work as a replacement.

Thanks.

stamat
Champ in-the-making
Champ in-the-making
Hey, sorry I'm like three years late Smiley Tongue But anyway if someone else stumbles upon this, I've written a simple wrapper around native hash table to match the functionality of Java Map. Note that Java Map isnt the same as Java Hash Map, cause Map has a linear seek of keys by value, and a HashMap has a hash table of value to key structure, so seek time is so much faster.

Anyway you can find Javascript Map class on this link: http://stamat.wordpress.com/2013/06/23/javascript-map-class/

Dont forget to leave feedback! Thanks for giving it a chance Smiley Very Happy

mdavid_cu
Champ in-the-making
Champ in-the-making
I've to use reflection for using native QNname Property Map.

<javascript>


//imports

ContextLoader = Packages.org.springframework.web.context.ContextLoader;
NamespaceService = Packages.org.alfresco.service.namespace.NamespaceService;
QName = Packages.org.alfresco.service.namespace.QName;
Class = java.lang.Class;

/**
* Gets the Bean instance object. You neet to declare a global variable e.g. NamespaceService = Packages.org.alfresco.service.namespace.NamespaceService;
*/
function srv(bname) {    
    return ContextLoader.getCurrentWebApplicationContext().getBean(bname, this[bname]);   
}

function main(){
   
    // Class reflection to a PropertyMap Alfresco implementation
    // PropertyMap  extends HashMap<QName, Serializable> so it avoids the HashMap<T> definitions
    var c = Class.forName("org.alfresco.util.PropertyMap");

    // Get a Method Object Reflexion for use the natural HashMap#put implementation
    var putMethod = c.getMethod("put", Class.forName("java.lang.Object"), Class.forName("java.lang.Object"));
   
    // ~ var propMap = {};
    var propMap = c.newInstance();

    // ~ propMap ['customSmiley Tonguerop'] = 'some val';
    putMethod.invoke(propMap, QName.createQName('customSmiley Tonguerop', srv("NamespaceService")), 'some val');
}

main(); 


</javascript>