gecko-dev/js/rhino/docs/ScriptingJava.html

1493 lines
47 KiB
HTML
Raw Normal View History

2003-04-24 13:14:00 +00:00
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 10 (filtered)">
2003-04-24 13:14:00 +00:00
<title>Scripting Java</title>
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Helvetica;
panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
{font-family:Courier;
panose-1:2 7 4 9 2 2 5 2 4 4;}
@font-face
{font-family:Times;
panose-1:2 2 6 3 5 4 5 2 3 4;}
@font-face
{font-family:Geneva;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:6.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:black;}
h1
{margin-top:16.0pt;
margin-right:0in;
margin-bottom:8.0pt;
margin-left:0in;
text-align:justify;
page-break-after:avoid;
font-size:18.0pt;
font-family:Times;
}
h2
{margin-top:12.0pt;
margin-right:0in;
margin-bottom:6.0pt;
margin-left:0in;
text-align:justify;
page-break-after:avoid;
font-size:14.0pt;
font-family:Times;
}
h3
{margin-top:8.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
page-break-after:avoid;
font-size:10.0pt;
font-family:Times;
}
h4
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:3.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
page-break-after:avoid;
font-size:10.0pt;
font-family:Times;
}
h5
{margin-top:3.0pt;
margin-right:0in;
margin-bottom:3.0pt;
margin-left:0in;
text-align:justify;
page-break-after:avoid;
border:none;
padding:0in;
font-size:14.0pt;
font-family:Times;
color:fuchsia;}
h6
{margin-top:3.0pt;
margin-right:0in;
margin-bottom:3.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
page-break-after:avoid;
font-size:10.0pt;
font-family:Times;
color:fuchsia;}
p.MsoHeading7, li.MsoHeading7, div.MsoHeading7
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:.25in;
text-align:justify;
line-height:12.0pt;
page-break-after:avoid;
font-size:10.0pt;
font-family:Times;
color:fuchsia;
font-weight:bold;}
p.MsoHeading8, li.MsoHeading8, div.MsoHeading8
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:.5in;
text-align:justify;
line-height:12.0pt;
font-size:9.0pt;
font-family:Helvetica;
color:fuchsia;
font-weight:bold;}
p.MsoHeading9, li.MsoHeading9, div.MsoHeading9
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:.75in;
text-align:justify;
line-height:12.0pt;
font-size:9.0pt;
font-family:Helvetica;
color:fuchsia;
font-weight:bold;}
p.MsoFootnoteText, li.MsoFootnoteText, div.MsoFootnoteText
{margin-top:3.0pt;
margin-right:0in;
margin-bottom:3.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
font-size:9.0pt;
font-family:Times;
color:black;}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:6.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:black;}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:6.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:black;}
span.MsoFootnoteReference
{vertical-align:super;}
a:link, span.MsoHyperlink
{color:red;
font-style:italic;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p.MsoDocumentMap, li.MsoDocumentMap, div.MsoDocumentMap
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:6.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
background:navy;
font-size:10.0pt;
font-family:Geneva;
color:black;}
p.ListBullet, li.ListBullet, div.ListBullet
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:maroon;}
p.ListBullet0, li.ListBullet0, div.ListBullet0
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:maroon;}
p.ListBullet1, li.ListBullet1, div.ListBullet1
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:maroon;}
p.ListBullet2, li.ListBullet2, div.ListBullet2
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:maroon;}
p.ListNumber, li.ListNumber, div.ListNumber
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
text-indent:-.25in;
font-size:10.0pt;
font-family:Times;
color:navy;
layout-grid-mode:line;}
p.ListNumber0, li.ListNumber0, div.ListNumber0
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
text-indent:-.25in;
font-size:10.0pt;
font-family:Times;
color:navy;
layout-grid-mode:line;}
p.ListNumber1, li.ListNumber1, div.ListNumber1
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:navy;}
p.ListNumber2, li.ListNumber2, div.ListNumber2
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:navy;}
p.ListVariable, li.ListVariable, div.ListVariable
{margin-top:0in;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:green;}
p.ListVariable0, li.ListVariable0, div.ListVariable0
{margin-top:0in;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:purple;}
p.ListVariableTerm, li.ListVariableTerm, div.ListVariableTerm
{margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:purple;}
span.bold
{font-family:Times;
color:red;
font-weight:bold;}
p.CellBody, li.CellBody, div.CellBody
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:0in;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:navy;}
p.CellCode, li.CellCode, div.CellCode
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:0in;
font-size:9.0pt;
font-family:Courier;
color:navy;}
p.CellHeading, li.CellHeading, div.CellHeading
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:0in;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:navy;
font-weight:bold;}
p.CellSubhead, li.CellSubhead, div.CellSubhead
{margin-top:2.0pt;
margin-right:0in;
margin-bottom:2.0pt;
margin-left:0in;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:navy;
font-weight:bold;}
p.ChapterTitle, li.ChapterTitle, div.ChapterTitle
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:1.5in;
margin-left:0in;
text-align:right;
font-size:24.0pt;
font-family:Times;
color:red;}
p.ChapterLabel, li.ChapterLabel, div.ChapterLabel
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:24.0pt;
margin-left:0in;
text-align:right;
page-break-before:always;
font-size:48.0pt;
font-family:Times;
color:red;
font-weight:bold;}
p.Code, li.Code, div.Code
{margin-top:0in;
margin-right:-67.7pt;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
border:none;
padding:0in;
font-size:9.0pt;
font-family:"Courier New";
color:green;
letter-spacing:-.5pt;}
p.CodeEmphasis, li.CodeEmphasis, div.CodeEmphasis
{margin-top:0in;
margin-right:-67.7pt;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
border:none;
padding:0in;
font-size:9.0pt;
font-family:"Courier New";
color:green;
letter-spacing:-.5pt;
font-weight:bold;}
p.CodeNum, li.CodeNum, div.CodeNum
{margin-top:0in;
margin-right:-67.7pt;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
text-indent:-.25in;
line-height:10.0pt;
border:none;
padding:0in;
font-size:9.0pt;
font-family:"Courier New";
color:green;
letter-spacing:-.5pt;}
p.Comment, li.Comment, div.Comment
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:blue;
text-decoration:underline;}
span.emphasis
{font-family:Times;
color:red;
font-style:italic;}
p.Epigraph, li.Epigraph, div.Epigraph
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:0in;
text-align:right;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:black;
font-style:italic;}
p.EpigraphAuthor, li.EpigraphAuthor, div.EpigraphAuthor
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:0in;
text-align:right;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:black;}
p.EpigraphCitation, li.EpigraphCitation, div.EpigraphCitation
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:24.0pt;
margin-left:0in;
text-align:right;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:black;
font-style:italic;}
p.ExampleTitle, li.ExampleTitle, div.ExampleTitle
{margin-top:4.0pt;
margin-right:-.2in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:center;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:green;
font-style:italic;}
p.FigureHolder, li.FigureHolder, div.FigureHolder
{margin-top:6.0pt;
margin-right:0in;
margin-bottom:0in;
margin-left:0in;
margin-bottom:.0001pt;
text-align:center;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:maroon;}
p.FigureTitle, li.FigureTitle, div.FigureTitle
{margin-top:4.0pt;
margin-right:.5in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:center;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:maroon;
font-style:italic;}
p.ListSimple, li.ListSimple, div.ListSimple
{margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:blue;}
p.ListVariableTerm0, li.ListVariableTerm0, div.ListVariableTerm0
{margin:0in;
margin-bottom:.0001pt;
text-align:justify;
line-height:12.0pt;
page-break-after:avoid;
font-size:10.0pt;
font-family:Times;
color:green;}
span.literal
{font-family:Courier;
color:red;}
p.Note, li.Note, div.Note
{margin-top:6.0pt;
margin-right:.5in;
margin-bottom:6.0pt;
margin-left:.5in;
text-align:justify;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:teal;}
p.NoteCode, li.NoteCode, div.NoteCode
{margin-top:0in;
margin-right:.5in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:10.0pt;
border:none;
padding:0in;
font-size:9.0pt;
font-family:Courier;
color:teal;}
p.NoteListBullet, li.NoteListBullet, div.NoteListBullet
{margin-top:4.0pt;
margin-right:.5in;
margin-bottom:4.0pt;
margin-left:.75in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:teal;}
p.NoteListNumber, li.NoteListNumber, div.NoteListNumber
{margin-top:4.0pt;
margin-right:.5in;
margin-bottom:4.0pt;
margin-left:.75in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:teal;}
p.NoteWarning, li.NoteWarning, div.NoteWarning
{margin-top:6.0pt;
margin-right:.5in;
margin-bottom:6.0pt;
margin-left:.5in;
text-align:justify;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:purple;}
p.NoteWarningCode, li.NoteWarningCode, div.NoteWarningCode
{margin-top:0in;
margin-right:.5in;
margin-bottom:0in;
margin-left:.5in;
margin-bottom:.0001pt;
line-height:10.0pt;
border:none;
padding:0in;
font-size:9.0pt;
font-family:Courier;
color:purple;}
p.NoteWarningListBullet, li.NoteWarningListBullet, div.NoteWarningListBullet
{margin-top:4.0pt;
margin-right:.5in;
margin-bottom:4.0pt;
margin-left:.75in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:purple;}
p.NoteWarningListNumber, li.NoteWarningListNumber, div.NoteWarningListNumber
{margin-top:4.0pt;
margin-right:.5in;
margin-bottom:4.0pt;
margin-left:.75in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:purple;}
span.onlineitem
{font-family:Times;
color:red;
font-style:italic;}
p.Quote, li.Quote, div.Quote
{margin-top:4.0pt;
margin-right:.25in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
font-size:9.0pt;
font-family:Times;
color:teal;}
p.RefSynopsis, li.RefSynopsis, div.RefSynopsis
{margin-top:4.0pt;
margin-right:0in;
margin-bottom:4.0pt;
margin-left:0in;
text-align:justify;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:fuchsia;}
p.RefPurpose, li.RefPurpose, div.RefPurpose
{margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
text-indent:-.25in;
line-height:10.0pt;
font-size:10.0pt;
font-family:Times;
color:fuchsia;}
span.replaceable
{font-family:Courier;
color:red;
font-style:italic;}
p.SidebarBody, li.SidebarBody, div.SidebarBody
{margin-top:4.0pt;
margin-right:.25in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:justify;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:teal;}
p.SidebarCode, li.SidebarCode, div.SidebarCode
{margin-top:0in;
margin-right:.25in;
margin-bottom:0in;
margin-left:.25in;
margin-bottom:.0001pt;
text-align:justify;
line-height:10.0pt;
border:none;
padding:0in;
font-size:9.0pt;
font-family:Courier;
color:teal;}
p.SidebarListBullet, li.SidebarListBullet, div.SidebarListBullet
{margin-top:4.0pt;
margin-right:.25in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
page-break-after:avoid;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:teal;}
p.SidebarListNumber, li.SidebarListNumber, div.SidebarListNumber
{margin-top:4.0pt;
margin-right:.25in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:justify;
text-indent:-.25in;
line-height:12.0pt;
border:none;
padding:0in;
font-size:10.0pt;
font-family:Times;
color:teal;
layout-grid-mode:line;}
p.SidebarTitle, li.SidebarTitle, div.SidebarTitle
{margin-top:4.0pt;
margin-right:.25in;
margin-bottom:4.0pt;
margin-left:.25in;
text-align:center;
border:none;
padding:0in;
font-size:18.0pt;
font-family:Times;
color:teal;}
span.subscript
{font-family:Times;
color:red;
position:relative;
top:3.0pt;}
span.superscript
{color:red;
position:relative;
top:-3.0pt;}
span.Symbol
{font-family:Symbol;
color:red;
border:none;}
p.TableTitle, li.TableTitle, div.TableTitle
{margin-top:12.0pt;
margin-right:.5in;
margin-bottom:4.0pt;
margin-left:.5in;
text-align:center;
line-height:12.0pt;
font-size:10.0pt;
font-family:Times;
color:navy;
font-style:italic;}
span.technicalitalic
{font-family:Times;
color:red;
font-style:italic;}
span.userinput
{font-family:Courier;
color:red;
font-weight:bold;}
span.userinputreplaceable
{font-family:Courier;
color:red;
font-weight:bold;
font-style:italic;}
/* Page Definitions */
@page Section1
{size:8.5in 11.0in;
margin:1.5in 2.0in 1.5in 2.0in;}
div.Section1
{page:Section1;}
/* List Definitions */
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
-->
</style>
2003-04-24 13:14:00 +00:00
</head>
<body lang=EN-US link=red vlink=purple>
<div class=Section1>
2003-04-24 13:14:00 +00:00
<p class=ChapterTitle>Scripting Java</p>
2003-04-24 13:14:00 +00:00
<p class=MsoNormal>This paper shows how to use Rhino to reach beyond JavaScript
into Java.<a href="#_ftn1" name="_ftnref1" title=""><span
class=MsoFootnoteReference><span class=MsoFootnoteReference><span
style='font-size:10.0pt;font-family:Times;color:black'>[1]</span></span></span></a>
Scripting Java has many uses. It allows us to write powerful scripts quickly by
making use of the many Java libraries available. We can test Java classes by
writing scripts. We can also aid our Java development by using scripting for <span
class=emphasis><span style='color:red'>exploratory programming</span></span>.
Exploratory programming is the process of learning about what a library or API
can do by writing quick programs that use it. As we will see, scripting makes
this process easier.</p>
2003-04-24 13:14:00 +00:00
<p class=MsoNormal>Note that the ECMA standard doesn<73>t cover communication with
2003-04-24 13:14:00 +00:00
Java (or with any external object system for that matter). All the
functionality covered in this chapter should thus be considered an extension.</p>
2003-04-24 13:14:00 +00:00
<h1>Accessing Java packages and classes</h1>
2003-04-24 13:14:00 +00:00
<p class=MsoNormal>Every piece of Java code is part of a class. Every Java
2003-04-24 13:14:00 +00:00
class is part of a package. In JavaScript, however, scripts exist outside of
any package hierarchy. How then, do we access classes in Java packages?</p>
<p class=MsoNormal>Rhino defines a top-level variable named <span
class=technicalitalic><span style='color:red'>Packages</span></span>. The
properties of the <span class=technicalitalic><span style='color:red'>Packages</span></span>
variable are all the top-level Java packages, such as <span
class=technicalitalic><span style='color:red'>java</span></span> and <span
class=technicalitalic><span style='color:red'>com</span></span>.<2E> For example,
we can access the value of the <span class=technicalitalic><span
style='color:red'>java</span></span> package:</p>
2003-04-24 13:14:00 +00:00
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
2003-04-24 13:14:00 +00:00
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; Packages.java</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[JavaPackage java]</p>
</div>
<p class=MsoNormal>As a handy shortcut, Rhino defines a top-level variable <span
class=technicalitalic><span style='color:red'>java</span></span> that is
equivalent to <span class=technicalitalic><span style='color:red'>Packages.java</span></span>.
2003-04-24 13:14:00 +00:00
So the previous example could be even shorter:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; java</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[JavaPackage java]</p>
</div>
2003-04-24 13:14:00 +00:00
<p class=MsoNormal>We can access Java classes simply by stepping down the
2003-04-24 13:14:00 +00:00
package hierarchy:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
2003-04-24 13:14:00 +00:00
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; java.io.File</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[JavaClass java.io.File]</p>
</div>
<p class=MsoNormal>If your scripts access a lot of different Java classes it
2003-04-24 13:14:00 +00:00
can get awkward to use the full package name of the class every time. Rhino
provides a top-level function <span class=technicalitalic><span
style='color:red'>importPackage</span></span> that serves the same purpose as
Java<EFBFBD>s <span class=technicalitalic><span style='color:red'>import</span></span>
declaration. For example, we could import all of the classes in the <span
class=technicalitalic><span style='color:red'>java.io </span></span>package and
access class <span class=technicalitalic><span style='color:red'>java.io.File</span></span>
using just the name <span class=technicalitalic><span style='color:red'>File</span></span>:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt;
importPackage(java.io)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; File</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[JavaClass java.io.File]</p>
</div>
<p class=MsoNormal>Here <span class=technicalitalic><span style='color:red'>importPackage(java.io)</span></span>
makes all the classes in the <span class=technicalitalic><span
style='color:red'>java.io</span></span> package<67> (such as <span
class=technicalitalic><span style='color:red'>File</span></span>) available at
the top level. It<49>s equivalent in effect to the Java declaration <span
class=technicalitalic><span style='color:red'>import java.io.*;</span></span>.</p>
<p class=MsoNormal>It<EFBFBD>s important to note that Java imports <span
class=technicalitalic><span style='color:red'>java.lang.*</span></span>
implicitly, while Rhino does not. The reason is that JavaScript has its own
top-level objects <span class=technicalitalic><span style='color:red'>Boolean</span></span>,
<span class=technicalitalic><span style='color:red'>Math</span></span>, <span
class=technicalitalic><span style='color:red'>Number</span></span>, <span
class=technicalitalic><span style='color:red'>Object</span></span>, and <span
class=technicalitalic><span style='color:red'>String</span></span> that are
different from the classes by those names defined in the <span
class=technicalitalic><span style='color:red'>java.lang</span></span> package.
Because of this conflict, it<69>s a good idea not to use <span
class=technicalitalic><span style='color:red'>importPackage</span></span> on
the <span class=technicalitalic><span style='color:red'>java.lang</span></span>
package. </p>
<p class=MsoNormal>One thing to be careful of is Rhino<6E>s handling of errors in
specifying Java package or class names. If <span class=technicalitalic><span
style='color:red'>java.MyClass</span></span> is accessed, Rhino attempts to
load a class named <span class=technicalitalic><span style='color:red'>java.MyClass</span></span>.
If that load fails, it assumes that <span class=technicalitalic><span
style='color:red'>java.MyClass</span></span> is a package name, and no error is
2003-04-24 13:14:00 +00:00
reported:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; java.MyClass</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[JavaPackage
java.MyClass]</p>
</div>
2003-04-24 13:14:00 +00:00
<p class=MsoNormal>Only if you attempt to use this object as a class will an
2003-04-24 13:14:00 +00:00
error be reported.</p>
<h1>Working with Java objects</h1>
<p class=MsoNormal>Now that we can access Java classes, the next logical step
is to create an object. This works just as in Java, with the use of the <span
class=technicalitalic><span style='color:red'>new</span></span> operator:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; new java.util.Date()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>Thu Jan 24 16:18:17 EST 2002</p>
</div>
<p class=MsoNormal>If we store the new object in a JavaScript variable, we can
then call methods on it:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; f = new
java.io.File(&quot;test.txt&quot;)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>test.txt</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; f.exists()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>true</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; f.getName()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>test.txt</p>
</div>
<p class=MsoNormal>Static methods and fields can be accessed from the class
object itself:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; java.lang.Math.PI</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>3.141592653589793</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt;
java.lang.Math.cos(0)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>1</p>
</div>
<p class=MsoNormal>In JavaScript, unlike Java, the method by itself is an
2003-04-24 13:14:00 +00:00
object and can be evaluated as well as being called. If we just view the method
object by itself we can see the various overloaded forms of the method:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; f.listFiles</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>function listFiles() {/*</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>java.io.File[]
listFiles()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>java.io.File[]
listFiles(java.io.FilenameFilter)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>java.io.File[]
listFiles(java.io.FileFilter)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>*/}</p>
</div>
<p class=MsoNormal>This output shows that the <span class=technicalitalic><span
style='color:red'>File</span></span> class defines three overloaded methods <span
class=technicalitalic><span style='color:red'>listFiles</span></span>: one that
takes no arguments, another with a <span class=technicalitalic><span
style='color:red'>FilenameFilter</span></span> argument, and a third with a <span
class=technicalitalic><span style='color:red'>FileFilter</span></span>
argument. All the methods return an array of <span class=technicalitalic><span
style='color:red'>File</span></span> objects. Being able to view the parameters
and return type of Java methods is particularly useful in exploratory
programming where we might be investigating a method and are unsure of the
parameter or return types. </p>
<p class=MsoNormal>Another useful feature for exploratory programming is the
ability to see all the methods and fields defined for an object. Using the
JavaScript <span class=technicalitalic><span style='color:red'>for..in</span></span>
construct, we can print out all these values:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; for (i in f) {
print(i) }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>exists</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>parentFile</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>mkdir</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>toString</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>wait</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><span class=replaceable><span
style='color:red'>[44 others]</span></span></p>
</div>
<p class=MsoNormal>Note that not only the methods of the <span
class=technicalitalic><span style='color:red'>File</span></span> class are
listed, but also the methods inherited from the base class <span
class=technicalitalic><span style='color:red'>java.lang.Object</span></span>
(like <span class=technicalitalic><span style='color:red'>wait</span></span>).
This makes it easier to work with objects in deeply nested inheritance
hierarchies since you can see all the methods that are available for that
2003-04-24 13:14:00 +00:00
object.</p>
<p class=MsoNormal>Rhino provides another convenience by allowing properties of
JavaBeans to be accessed directly by their property names. A JavaBean property <span
class=technicalitalic><span style='color:red'>foo</span></span> is defined by
the methods <span class=technicalitalic><span style='color:red'>getFoo</span></span>
and <span class=technicalitalic><span style='color:red'>setFoo</span></span>.
Additionally, a boolean property of the same name can be defined by an <span
class=technicalitalic><span style='color:red'>isFoo</span></span> method.<a
href="#_ftn2" name="_ftnref2" title=""><span class=MsoFootnoteReference><span
class=MsoFootnoteReference><span style='font-size:10.0pt;font-family:Times;
color:black'>[2]</span></span></span></a> For example, the following code
actually calls the <span class=technicalitalic><span style='color:red'>File</span></span>
object<EFBFBD>s <span class=technicalitalic><span style='color:red'>getName</span></span>
and <span class=technicalitalic><span style='color:red'>isDirectory</span></span>
methods.</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; f.name</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>test.txt</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; f.directory</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>false</p>
</div>
<h1>Calling overloaded methods</h1>
<p class=MsoNormal>The process of choosing a method to call based upon the
types of the arguments is called <span class=emphasis><span style='color:red'>overload
resolution</span></span>. In Java, overload resolution is performed at compile
time, while in Rhino it occurs at runtime. This difference is inevitable given
JavaScript<EFBFBD>s use of dynamic typing as was discussed in Chapter 2: since the
type of a variable is not known until runtime, only then can overload
2003-04-24 13:14:00 +00:00
resolution occur.</p>
<p class=MsoNormal>As an example, consider the following Java class that
defines a number of overloaded methods and calls them.</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>public class Overload {</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> public String
f(Object o) { return &quot;f(Object)&quot;; }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> public String
f(String s) { return &quot;f(String)&quot;; }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> public String f(int
i)<29><><EFBFBD> { return &quot;f(int)&quot;; }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> public String
g(String s, int i) { </p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> return
&quot;g(String,int)&quot;; </p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> public String g(int
i, String s) { </p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> return
&quot;g(int,String)&quot;; </p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> public static void
main(String[] args) {</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Overload o = new
Overload();</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Object[] a = new
Object[] { new Integer(3), &quot;hi&quot;, Overload.class };</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> for (int i=0; i
&lt; a.length; i++)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
System.out.println(o.f(a[i]));</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>}</p>
</div>
<p class=MsoNormal>When we compile and execute the program, it produces the
output</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>f(Object)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>f(Object)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>f(Object)</p>
</div>
<p class=MsoNormal>However, if we write a similar script </p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>var o = new
Packages.Overload()</p>
</div>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>var a = [ 3,
&quot;hi&quot;, Packages.Overload ];</p>
</div>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>for (var i=0; i &lt;
a.length; i++) </p>
</div>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'><EFBFBD><EFBFBD><EFBFBD> print(o.f(a[i]));</p>
</div>
<p class=MsoNormal>and execute it, we get the output</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>f(int)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>f(String)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>f(Object)</p>
</div>
<p class=MsoNormal>Because Rhino selects an overloaded method at runtime, it
calls the more specific type that matches the argument. Meanwhile Java selects
the overloaded method purely on the type of the argument at compile time. </p>
<p class=MsoNormal>Although this has the benefit of selecting a method that may
be a better match for each call, it does have an impact on performance since
more work is done at each call. In practice this performance cost hasn<73>t been
noticeable in real applications.</p>
<p class=MsoNormal>Because overload resolution occurs at runtime, it can fail
at runtime. For example, if we call <span class=technicalitalic><span
style='color:red'>Overload</span></span><EFBFBD>s method <span class=technicalitalic><span
style='color:red'>g</span></span> with two integers we get an error because
neither form of the method is closer to the argument types than the other:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; o.g(3,4)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js:
&quot;&lt;stdin&gt;&quot;, line 2: The choice of Java method Overload.g
matching JavaScript argument types (number,number) is ambiguous; candidate
methods are: class java.lang.String g(java.lang.String,int), class
java.lang.String g(int,java.lang.String)</p>
</div>
<p class=MsoNormal>A more precise definition of overloading semantics can be
found at <span class=onlineitem><span style='color:red'><a
href="http://www.mozilla.org/js/liveconnect/lc3_method_overloading.html">http://www.mozilla.org/js/liveconnect/lc3_method_overloading.html</a></span></span>.</p>
<h1>Implementing Java interfaces</h1>
<p class=MsoNormal>Now that we can access Java classes, create Java objects,
and access fields, methods, and properties of those objects, we have a great
deal of power at our fingertips. However, there are a few instances where that
is not enough: many APIs in Java work by providing interfaces that clients must
implement. One example of this is the <span class=technicalitalic><span
style='color:red'>Thread</span></span> class: its constructor takes a <span
class=technicalitalic><span style='color:red'>Runnable</span></span> that
contains a single method <span class=technicalitalic><span style='color:red'>run</span></span>
2003-04-24 13:14:00 +00:00
that will be called when the new thread is started. </p>
<p class=MsoNormal>To address this need, Rhino provides the ability to create
new Java objects that implement interfaces. First we must define a JavaScript
object with function properties whose names match the methods required by the
Java interface. To implement a <span class=technicalitalic><span
style='color:red'>Runnable</span></span>, we need only define a single method <span
class=technicalitalic><span style='color:red'>run</span></span> with no
parameters. If you remember from Chapter 3, it is possible to define a
JavaScript object with the <span class=technicalitalic><span style='color:red'>{
</span></span><span class=replaceable><span style='color:red'>propertyName</span></span><span
class=technicalitalic><span style='color:red'>: </span></span><span
class=replaceable><span style='color:red'>value</span></span><span
class=technicalitalic><span style='color:red'> <20> }</span></span> notation. We
can use that syntax here in combination with a function expression to define a
JavaScript object with a <span class=technicalitalic><span style='color:red'>run</span></span>
method:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; obj = { run:
function () { print(&quot;\nrunning&quot;); } }</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[object Object]</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; obj.run()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>&nbsp;</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>running</p>
</div>
<p class=MsoNormal>Now we can create an object implementing the <span
class=technicalitalic><span style='color:red'>Runnable</span></span> interface
by <20>constructing<6E> a <span class=technicalitalic><span style='color:red'>Runnable</span></span>:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; r = new
java.lang.Runnable(obj);</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[object JavaObject]</p>
</div>
<p class=MsoNormal>In Java it is not possible to use the <span
class=technicalitalic><span style='color:red'>new</span></span> operator on an
interface because there is no implementation available. Here Rhino gets the
implementation from the JavaScript object <span class=technicalitalic><span
style='color:red'>obj</span></span>. Now that we have an object implementing <span
class=technicalitalic><span style='color:red'>Runnable</span></span>, we can
create a <span class=technicalitalic><span style='color:red'>Thread</span></span>
and run it. The function we defined for <span class=technicalitalic><span
style='color:red'>run </span></span>will be called on a new thread.</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; t = new
java.lang.Thread(r)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>Thread[Thread-2,5,main]</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; t.start()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt;</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>running</p>
</div>
<p class=MsoNormal>The final <span class=literal><span style='color:red'>js</span></span>
prompt and the output from the new thread may appear in either order, depending
on thread scheduling. Rhino also allows the JavaScript object literal and the
interface creation expression to be combined for a more convenient syntax:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; r = new
java.lang.Runnable(){run: function() { print(&quot;\nrunning&quot;); } };</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[object JavaObject]</p>
</div>
<p class=MsoNormal>Behind the scenes, Rhino generates the bytecode for a new
Java class that implements <span class=technicalitalic><span style='color:red'>Runnable</span></span>
and forwards all calls to its <span class=technicalitalic><span
style='color:red'>run</span></span> method over to an associated JavaScript
object. The object that implements this class is called a <span class=emphasis><span
style='color:red'>Java adapter</span></span>. Because the forwarding to
JavaScript occurs at runtime, it is possible to delay defining the methods
implementing an interface until they are called. While omitting a required
method is bad practice for programming in the large, it<69>s useful for small
scripts and for exploratory programming.</p>
<h1>The <span class=technicalitalic>JavaAdapter</span> constructor</h1>
<p class=MsoNormal>In the previous section we created Java adapters using the <span
class=technicalitalic><span style='color:red'>new</span></span> operator with
Java interfaces. This approach has its limitations: it<69>s not possible to
implement multiple interfaces, nor can we extend non-abstract classes. For
these reasons there is a <span class=technicalitalic><span style='color:red'>JavaAdapter</span></span>
constructor. </p>
<p class=MsoNormal>The syntax of the <span class=technicalitalic><span
style='color:red'>JavaAdapter</span></span> constructor is</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>new JavaAdapter(<span
class=replaceable><span style='color:red'>javaIntfOrClass</span></span>, <span
class=replaceable><span style='color:red'>[javaIntfOrClass</span></span>,<span
class=replaceable><span style='color:red'> ...]</span></span> <span
class=replaceable><span style='color:red'>javascriptObject</span></span>)</p>
</div>
<p class=MsoNormal>Each <span class=replaceable><span style='color:red'>javaIntfOrClass</span></span>
is an interface to implement or a class to extend. (Only one <span
class=replaceable><span style='color:red'>javaIntfOrClass</span></span> may be
a class, of course.) The <span class=replaceable><span style='color:red'>javascriptObject</span></span>
is the JavaScript object containing the methods that will be called from the
Java adapter. </p>
<p class=MsoNormal>In practice there<72>s little need to call the <span
class=technicalitalic><span style='color:red'>JavaAdapter</span></span>
constructor directly. Most of the time the previous syntaxes using the <span
class=technicalitalic><span style='color:red'>new</span></span> operator will
be sufficient.</p>
<h1>Creating Java arrays</h1>
<p class=MsoNormal>Rhino provides no special syntax for creating Java arrays.
You must use the <span class=technicalitalic><span style='color:red'>java.lang.reflect.Array</span></span>
class for this purpose. To create an array of five Java strings you would make
the following call:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; a =
java.lang.reflect.Array.newInstance(java.lang.String, 5);</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[Ljava.lang.String;@7ffe01</p>
</div>
<p class=MsoNormal>To create an array of primitive types, we must use the
special TYPE field defined in the associated object class in the <span
class=technicalitalic><span style='color:red'>java.lang</span></span> package.
For example, to create an array of bytes, we must use the special field <span
class=technicalitalic><span style='color:red'>java.lang.Byte.TYPE</span></span>:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; a =
java.lang.reflect.Array.newInstance(java.lang.Character.TYPE, 2);</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>[C@7a84e4</p>
</div>
<p class=MsoNormal>The resulting value can then be used anywhere a Java array
2003-04-24 13:14:00 +00:00
of that type is allowed.</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; a[0] = 104</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>104</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; a[1] = 105</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>105</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; new
java.lang.String(a)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>hi</p>
</div>
<h1>Java strings and JavaScript strings</h1>
<p class=MsoNormal>It<EFBFBD>s important to keep in mind that Java strings and
JavaScript strings are <span class=emphasis><span style='color:red'>not</span></span>
the same. Java strings are instances of the type <span class=technicalitalic><span
style='color:red'>java.lang.String</span></span> and have all the methods
defined by that class. JavaScript strings have methods defined by <span
class=technicalitalic><span style='color:red'>String.prototype</span></span>.
The most common stumbling block is <span class=technicalitalic><span
style='color:red'>length</span></span>, which is a method of Java strings and a
dynamic property of JavaScript strings:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; javaString = new
java.lang.String(&quot;Java&quot;)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>Java</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; jsString =
&quot;JavaScript&quot;</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>JavaScript</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt;
javaString.length()</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>4</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; jsString.length</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>10</p>
</div>
<p class=MsoNormal>Rhino provides some help in reducing the differences between
the two types. First, you can pass a JavaScript string to a Java method that
requires a Java string and Rhino will perform the conversion. We actually saw
this feature in action on the call to the <span class=technicalitalic><span
style='color:red'>java.lang.String</span></span> constructor in the preceding
2003-04-24 13:14:00 +00:00
example.</p>
<p class=MsoNormal>Rhino also makes the JavaScript methods available to Java
strings if the java.lang.String class doesn<73>t already define them. For example:</p>
<div style='border:none;border-left:solid windowtext 1.0pt;padding:0in 0in 0in 2.0pt;
margin-left:.25in;margin-right:-67.7pt'>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>js&gt; javaString.match(/a.*/)</p>
<p class=Code style='margin:0in;margin-bottom:.0001pt'>ava</p>
</div>
</div>
2003-04-24 13:14:00 +00:00
<div><br clear=all>
2003-04-24 13:14:00 +00:00
<hr align=left size=1 width="33%">
<div id=ftn1>
2003-04-24 13:14:00 +00:00
<p class=MsoFootnoteText style='margin-top:3.0pt;margin-right:67.7pt;
margin-bottom:3.0pt;margin-left:-.25in'><a href="#_ftnref1" name="_ftn1"
title=""><span class=MsoFootnoteReference><span class=MsoFootnoteReference><span
style='font-size:9.0pt;font-family:Times;color:black'>[1]</span></span></span></a>
The ability to call Java from JavaScript was first implemented as part of a
Netscape browser technology called <span class=emphasis><span style='color:
red'>LiveConnect</span></span>. However, since that technology also encompassed
communication with browser plugins, and since the way of calling JavaScript
from Java in Rhino is entirely different, that term won<6F>t be used in this paper.</p>
</div>
<div id=ftn2>
<p class=MsoFootnoteText style='margin-top:3.0pt;margin-right:67.7pt;
margin-bottom:3.0pt;margin-left:-.25in'><a href="#_ftnref2" name="_ftn2"
title=""><span class=MsoFootnoteReference><span class=MsoFootnoteReference><span
style='font-size:9.0pt;font-family:Times;color:black'>[2]</span></span></span></a>
For more information on JavaBeans, see <span class=emphasis><span
style='color:red'>Developing Java Beans</span></span> by Robert Englander.</p>
</div>
</div>
2003-04-24 13:14:00 +00:00
</body>
2003-04-24 13:14:00 +00:00
</html>