简体   繁体   English

Java Nashorn通用类声明

[英]Java Nashorn Generic Classes Declaration

Can anyone help me declare a class like this in Java Nashorn HttpEntity. 任何人都可以帮助我在Java Nashorn HttpEntity中声明这样的类。 Here is a sample script I am trying to run. 这是我尝试运行的示例脚本。

--This my declaration somewhere in the code-- -这是我在代码中的某处声明-

var JavascriptValidator = Java.type('ph.com.bdo.tbg.icos.validation.form.JavascriptValidator');
var HttpHeaders = Java.type('org.springframework.http.HttpHeaders');
var DateTimeFormatter = Java.type('java.time.format.DateTimeFormatter');
var Arrays = Java.type('java.util.Arrays');
var Date = Java.type('java.util.Date');
var MediaType = Java.type('org.springframework.http.MediaType');
var HttpEntity = Java.type('org.springframework.http.HttpEntity');
var UriComponentsBuilder = Java.type('org.springframework.web.util.UriComponentsBuilder');
var RestTemplate = Java.type('org.springframework.web.client.RestTemplate');

--This is a sample function I am running-- -这是我正在运行的示例函数-

    function init(){
            var headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("username", username);
            headers.add("password", password);

            var entity = new HttpEntity<String>(headers);

            // more code... 
        }

--Then I get the following error-- -然后出现以下错误-

FAILED: testFinalValidate
<eval>:73 TypeError: Can not create new object with constructor org.springframework.http.HttpEntity with the passed arguments; they do not match any of its method signatures.
at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185)
at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
at jdk.nashorn.internal.runtime.linker.NashornStaticClassLinker.checkNullConstructor(NashornStaticClassLinker.java:108)
at jdk.nashorn.internal.runtime.linker.NashornStaticClassLinker.getGuardedInvocation(NashornStaticClassLinker.java:96)
at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176)
at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:154)
at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:253)
at jdk.nashorn.internal.scripts.Script$Recompilation$2$2171$\^eval\_.init(<eval>:73)
at jdk.nashorn.internal.scripts.Script$Recompilation$1$1772A$\^eval\_.finalValidate(<eval>:59)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:639)
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.ScriptObjectMirror.call(ScriptObjectMirror.java:117)
at com.bdo.icos.util.BaseTest.runScript(BaseTest.java:194)
at com.bdo.icos.validation.SmartMoneyTest.testFinalValidate(SmartMoneyTest.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:54)
at org.testng.internal.InvokeMethodRunnable.run(InvokeMethodRunnable.java:44)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Please note that the code if converted into equivalent Java syntax it would compile and run successfully. 请注意,如果将代码转换为等效的Java语法,它将可以编译并成功运行。

Does the class org.springframework.http.HttpEntity have the correct public constructor accepting HttpHeaders ? 类org.springframework.http.HttpEntity是否具有接受HttpHeaders的正确公共构造函数? Note that only public constructors and methods callable from script. 请注意,只有公共构造函数和可从脚本调用的方法。 protected /package-private/private members are not accessible. 受保护的/ package-private / private成员不可访问。

Also this syntax-> 还有这个语法->

var entity = new HttpEntity<String>(headers);

won't work (appears to be cut-paste from java code) 将不起作用(似乎是从Java代码中剪切粘贴的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM