Почему Map.Entry.getValue неизвестен в java-движке java? - PullRequest
1 голос
/ 16 ноября 2011
@Test
public void testB() throws ScriptException, NoSuchMethodException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "function one(map){\n" + "for(entry in map){\n"
            + "entry.getValue();\n" + "entry.getKey();\n" + "}}";
    HashMap map = new HashMap();
    map.put("keyOne", "valueOne");
    String functionName = "one";

    engine.eval(script);

    Invocable inv = (Invocable) engine;

    inv.invokeFunction(functionName, map);
}

StackTrace

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: 
Cannot find function getValue. (<Unknown source>#3) in <Unknown source> at line number 3
at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:184)
at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:142
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Я предполагаю, что есть кое-что, о чем я не знаю, что мне нужно сделать, чтобы заставить это работать, и я не могу найти документально подтвержденный.

1 Ответ

1 голос
/ 17 ноября 2011

Во-первых, нам нужно перебрать entrySet() карты после получения ее в виде массива через map.toArray().
Затем map [entry] Entry объект с getKey() и getValue() методами.
Интуитивно понятный, когда вы знаете, что делать.

@Test
public void testC() throws ScriptException, NoSuchMethodException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    String script = "function one(map){\n"
            + "var entries=map.entrySet().toArray();\n"
            + "for(entry in entries){\n"
            + "print( entries[entry].getKey());\n"
            + "print( entries[entry].getValue());\n" + "}}";
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("keyOne", "valueOne");
    String functionName = "one";
    engine.eval(script);
    Invocable inv = (Invocable) engine;
    inv.invokeFunction(functionName, map);
}
...