精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx

上传人:b****3 文档编号:6787711 上传时间:2023-05-07 格式:DOCX 页数:21 大小:1.83MB
下载 相关 举报
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第1页
第1页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第2页
第2页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第3页
第3页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第4页
第4页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第5页
第5页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第6页
第6页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第7页
第7页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第8页
第8页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第9页
第9页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第10页
第10页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第11页
第11页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第12页
第12页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第13页
第13页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第14页
第14页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第15页
第15页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第16页
第16页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第17页
第17页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第18页
第18页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第19页
第19页 / 共21页
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx

《精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx》由会员分享,可在线阅读,更多相关《精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx(21页珍藏版)》请在冰点文库上搜索。

精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照Word文件下载.docx

Inthischapter,youwilllearn:

WhatJavaAPIsforNashornare

HowtoinstantiatetheNashornenginedirectly

HowtopassoptionstotheNashornengineinJavacodeandonthecommand-line

HowtoshareglobalsamongscriptcontextsinaNashornengine

Howtoadd,update,delete,andreadpropertiesofscriptobjectsinJavacode

HowtocreatescriptobjectsandinvoketheirmethodsinJavacode

HowtoinvokescriptfunctionsfromJavacode

HowtoconvertscriptdatestoJavadates

WhatAreJavaAPIsforNashorn?

ItisstraightforwardtouseJavaclassesinNashornscripts.SometimesyoumaywanttouseNashornobjectsinJavacode.YoumaypassNashornobjectstoJavacodeorJavacodemayevaluateaNashornscriptandretrievethereferenceofNashornobjects.WhenNashornobjectscrosstheboundary(script-to-Java),theyneedtoberepresentedasobjectsofaJavaclassandyoushouldbeabletousethemlikeanyotherJavaobjects.

IfyourapplicationusesonlyNashorn,totakefulladvantageoftheNashornengineyoumaywanttouseoptionsandextensionsavailableinNashorn.YouwillneedtoinstantiatetheNashornengineinJavacode,usingtheclassesthatarespecifictotheNashornengine,notusingtheclassesintheJavaScriptingAPI.TheJavaAPIsforNashornprovideJavaclassesandinterfacesthatletyoudealwiththeNashornscriptingengineandNashornobjectsdirectlyinJavacode.Figure12-1depictsaclassdiagramofthoseclassesandinterfacesthatyouaresupposedtouseintheclientcodewhenyouaredealingwiththeNashornengine.Theyareinthenjdk.nashorn.api.scriptingpackage.

NotethattheNashornscriptingengineinternallyusesalotofotherclassesinotherpackages.However,youarenotsupposedtousetheminyourapplicationdirectly,exceptforclassesfromthejdk.nashorn.api.scriptingpackage.Thewebpageathttps:

//wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notescontainsthelinksforthedocumentationforthejdk.nashorn.api.scriptingpackage.

IwilldiscusssomeNashorn-specificJavaclassesandinterfacesindetailinsubsequentsections.Table12-1liststheclassesandinterfacesinthejdk.nashorn.api.scriptingpackagewiththeirdescriptions.

InstantiatingtheNashornEngine

Inpreviouschapters,youhavebeeninstantiatingtheNashornengineinJavacodeusingthestandardJavaScriptingAPI.TheNashornengineprovidesseveralcustomfeatures.Totakeadvantageofthosefeatures,youwillneedtoinstantiatetheNashornenginedirectly.First,youwillneedtocreateanobjectoftheNashornScriptEngineFactoryclass,andthencalloneoftheoverloadedversionsofthegetScriptEngine()methodtocreateaNashornengine:

Bydefault,theNashornenginefactorycreatesaNashornenginewiththe--dump-on-erroroptionenabled.IwillshowyouhowtosetotheroptionsfortheNashornengineinthenextexample.Becauseofthe--no-javaoption,youcannotuseanyJavaclassesinsidethescriptsexecutedbythisengine.The–strictoptionwillforcetheenginetoexecuteallscriptsinstrictmode.ThefollowingsnippetofcodecreatesaNashornengineusingaclassfilter,whichisaninstanceoftheClassFilterinterface,torestricttheusageofanyclassesfromthecom.jdojopackageanditssubpackages.NoticethattheClassFilterinterfacewasaddedinJDK8u40;

itisnotavailableinJDK8.

ClassFilterisafunctionalinterface.ItsmethodispassedthefullyqualifiednameoftheJavaclassthattheNashornscriptistryingtouse.Ifthemethodreturnstrue,theclasscanbeusedinthescript;

otherwise,theclasscannotbeusedinthescript.

ThefollowingisalistoftheoverloadedversionsofthegetScriptEngine()methodoftheNashornScriptEngineFactoryclass:

SharingtheEngineGlobals

Bydefault,theNashornenginemaintainstheglobalobjectsperscriptcontext.Inthisdiscussion,theterm“globals”meanstheglobalvariablesanddeclarationsstoredintheglobalscopeoftheNashornscript,whichyourefertobythisinthetop-levelscripts.Donotmixuptheglobalscopebindingsinthescriptcontextwiththeglobalsinthescript.Thescriptglobalsaresearchedfirstwhenyoureferenceanyvariablesorcreatevariablesinyourscript.Ifyoureferenceavariableinascriptthatisnotfoundinthescriptglobals,theenginewillsearchforitintheglobalscopebindingsofthescriptcontext.Forexample,thescriptobjectsObject,Math,String,andsoonarepartofthescriptglobals.ConsidertheprogramanditsoutputinListing12-1.

Theprogramperformsthefollowingsteps:

1.CreatesaNashornenginewithdefaultoptions.

2.Executesascriptusingtheeval()methodthatusesthedefaultscriptcontextoftheengine.Thescriptcreatesaglobalvariablenamedmsgthatisstoredinthescriptglobals.Youcanrefertothevariableusingitssimplenamemsgorthis.msgintheglobalscope.

3.Executesascriptusingtheeval()methodthatusesthedefaultscriptcontextoftheenginethatprintsthevalueofthemsgvariable.Thefirstlineintheoutputconfirmsthatthecorrectvalueofthemsgvariableisprinted.

4.Executesascriptusingtheeval()methodthatusesanewscriptcontext.Thescripttriestoprintthevalueoftheglobalvariablemsg.Thesecondlineintheoutput,byprintingundefined,confirmsthatthevariablenamedmsgdoesnotexistinthescriptglobals.

ThisisthedefaultbehavioroftheNashornengine.Itcreatesafreshcopyoftheglobalsperscriptcontext.Ifyouwanttousetheglobalsofthedefaultcontext(ifyouwanttoshareglobals),youcandosobycopyingtheenginescopeBindingsofthedefaultcontexttoyournewscriptcontext.Listing12-2usesthisapproachtosharethescriptglobalsbetweentwoscriptcontexts.

The–-global-per-engineoptionfortheNashornengineaccomplishesthesamethingsasinthepreviousexample.Itsharesthescriptglobalsamongallscriptcontexts.Listing12-3showshowtosetthisoptionfortheengine.Theoutputconfirmsthattheengineisusingonlyonecopyofglobalstoexecuteallscripts.

UsingScriptObjectsinJavaCode

WhenobjectsandvaluesfromNashorncrossthescript-JavaboundarytoenterJavacode,theyneedtoberepresentedasJavaobjectsandvalues.Table12-2liststhemappingofclassesbetweenscriptobjectsandtheircorrespondingJavaobjects.

Notethatyouarenotsupposedtousetheclassesandinterfacesinthejdk.nashorn.internalpackageanditssubpackages.Theyarelistedinthetableonlyforinformationalpurposes.Inthistable,themappingfortheNashornNumbertypeisshownasjava.lang.Numbertype.AlthoughNashorntriestopassjava.lang.Integerandjava.lang.

Doubleobjectsfornumericvaluewheneverpossible,itisnotreliabletodependonsuchspecializedJavatypes.Youshouldexpectajava.lang.NumberinstancewhenaNashornNumberispassedfromascripttoJavacode.Whenevernecessary,valuesofNumberandBooleantypesinNashornareconvertedtothecorrespondingJavaprimitivetypes,suchasint,double,boolean,andsoon.

InNashorn,ascriptobjectisrepresentedasaninstanceofthejdk.nashorn.internal.runtime.ScriptObjectclass.WhenthescriptobjectispassedtoJavacode,itiswrappedinaScriptObjectMirrorobject.BeforeJDK8u40,ifyoupassedascriptobjecttoaJavamethodthatdeclareditsparameterasjava.lang.Objecttype,aScriptObjectwaspassed,notaScriptObjectMirror.JDK8u40haschangedthisbehaviortopassascriptobjecttoJavacodeasaScriptObjectMirroreverytime.Ifyouhaddeclaredthemethod’sparameterasJSObjectorScriptObjectMirrorinJava,NashornalwayspassedthescriptobjecttoJavaasaScriptObjectMirror.

TheScriptObjectMirrorclassimplementstheJSObjectandBindingsinterfaces.Theclassaddsmoremethodstodealwithscriptobjects.YoucanstorethereferenceofascriptobjectinJavacodeinanyofthethreetypesofvariables:

JSObject,Bindings,orScriptObjectMirror.Whattypeyouusedependsonyourneeds.UsingtheScriptObjectMirrortypegivesyoumoreflexibilityandaccesstoallfeaturesofthescriptobject.Table12-3containsthelistofmethodsdeclaredintheJSObjectinterface.

IwillshowhowtousethesemethodsandsomemethodsoftheScriptObjectMirrorclassinthesubsequentsection.TheJSObjectinterfacecanbeimplementedbyanyJavaclass.Objectsofsuchaclasscanbeusedjustasiftheyarescriptobjectsandcanusethesyntaxobj.func(),obj[prop],deleteobj.prop,andsoontoworkwiththeirmethodsandpropertiesinscripts.

UsingPropertiesofScriptObjects

TheScriptObjectMirrorclassimplementsJSObjectandBindingsinterfaces.Youcanusemethodsofbothinterfacestoaccessproperties.YoucanusethegetMember()andgetSlot()methodsofJSObjecttoreadthenamedandindexedpropertiesofthescriptobject.Youcanusetheget()methodofBindingstogetthevalueofaproperty.YoucanusethesetMember()andsetSlot()methodsofJSObjecttoaddandupdateaproperty.Theput()methodofBindingsletsyoudothesame.

YoucanusethehasMember()andhasSlot()methodsofJSObjecttocheckwhetheranamedpropertyandanindexedpropertyexist.Atthesametime,youcanusethecontainsKey()methodoftheBindingsinterfacetocheckwhetherapropertyexists.YoucanthinkoftheJSObjectandBindingsinterfacesimplementedbytheScriptObjectMirrorclassprovidingtwoviewsofthesamescriptobject—theformerasasimpleobjectandthelatterasamap.

Listing12-4

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2