精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照.docx
- 文档编号:4620386
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:21
- 大小:1.83MB
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照.docx
《精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照.docx》由会员分享,可在线阅读,更多相关《精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照.docx(21页珍藏版)》请在冰点文库上搜索。
精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的JavaAPI中英文双语对照
英文2646单词,14808字符(字符就是印刷符),中文3875汉字。
JavaAPIsforNashorn
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 精品文档504关于计算机专业JAVA语言类有关的外文文献翻译成品Nashorn的Java API中英文双语对照 精品 文档 5
链接地址:https://www.bingdoc.com/p-4620386.html