Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use annotations for example host objects.

  • Loading branch information...
commit b0e27fec80cb7e85b04debe32b1f84dee51eb178 1 parent 18b914a
@hns hns authored
Showing with 49 additions and 23 deletions.
  1. +13 −7 examples/Counter.java
  2. +28 −13 examples/File.java
  3. +8 −3 examples/Foo.java
View
20 examples/Counter.java
@@ -37,6 +37,9 @@
* ***** END LICENSE BLOCK ***** */
import org.mozilla.javascript.*;
+import org.mozilla.javascript.annotations.JSFunction;
+import org.mozilla.javascript.annotations.JSConstructor;
+import org.mozilla.javascript.annotations.JSGetter;
public class Counter extends ScriptableObject {
private static final long serialVersionUID = 438270592527335642L;
@@ -44,19 +47,22 @@
// The zero-argument constructor used by Rhino runtime to create instances
public Counter() { }
- // Method jsConstructor defines the JavaScript constructor
- public void jsConstructor(int a) { count = a; }
+ // @JSConstructor annotation defines the JavaScript constructor
+ @JSConstructor
+ public Counter(int a) { count = a; }
// The class name is defined by the getClassName method
@Override
public String getClassName() { return "Counter"; }
- // The method jsGet_count defines the count property.
- public int jsGet_count() { return count++; }
+ // The method getCount defines the count property.
+ @JSGetter
+ public int getCount() { return count++; }
- // Methods can be defined using the jsFunction_ prefix. Here we define
- // resetCount for JavaScript.
- public void jsFunction_resetCount() { count = 0; }
+ // Methods can be defined the @JSFunction annotation.
+ // Here we define resetCount for JavaScript.
+ @JSFunction
+ public void resetCount() { count = 0; }
private int count;
}
View
41 examples/File.java
@@ -37,6 +37,10 @@
* ***** END LICENSE BLOCK ***** */
import org.mozilla.javascript.*;
+import org.mozilla.javascript.annotations.JSConstructor;
+import org.mozilla.javascript.annotations.JSFunction;
+import org.mozilla.javascript.annotations.JSGetter;
+
import java.io.*;
import java.util.List;
import java.util.ArrayList;
@@ -95,6 +99,7 @@ public File() {
* Otherwise System.in or System.out is assumed as appropriate
* to the use.
*/
+ @JSConstructor
public static Scriptable jsConstructor(Context cx, Object[] args,
Function ctorObj,
boolean inNewExpr)
@@ -123,7 +128,8 @@ public String getClassName() {
*
* Used to define the "name" property.
*/
- public String jsGet_name() {
+ @JSGetter
+ public String getName() {
return name;
}
@@ -138,12 +144,13 @@ public String jsGet_name() {
* @exception IOException if an error occurred while accessing the file
* associated with this object
*/
- public Object jsFunction_readLines()
+ @JSFunction
+ public Object readLines()
throws IOException
{
List<String> list = new ArrayList<String>();
String s;
- while ((s = jsFunction_readLine()) != null) {
+ while ((s = readLine()) != null) {
list.add(s);
}
String[] lines = list.toArray(new String[list.size()]);
@@ -160,7 +167,8 @@ public Object jsFunction_readLines()
* associated with this object, or EOFException if the object
* reached the end of the file
*/
- public String jsFunction_readLine() throws IOException {
+ @JSFunction
+ public String readLine() throws IOException {
return getReader().readLine();
}
@@ -171,7 +179,8 @@ public String jsFunction_readLine() throws IOException {
* associated with this object, or EOFException if the object
* reached the end of the file
*/
- public String jsFunction_readChar() throws IOException {
+ @JSFunction
+ public String readChar() throws IOException {
int i = getReader().read();
if (i == -1)
return null;
@@ -189,7 +198,8 @@ public String jsFunction_readChar() throws IOException {
* @exception IOException if an error occurred while accessing the file
* associated with this object
*/
- public static void jsFunction_write(Context cx, Scriptable thisObj,
+ @JSFunction
+ public static void write(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws IOException
{
@@ -204,14 +214,16 @@ public static void jsFunction_write(Context cx, Scriptable thisObj,
* associated with this object
*
*/
- public static void jsFunction_writeLine(Context cx, Scriptable thisObj,
+ @JSFunction
+ public static void writeLine(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
throws IOException
{
write0(thisObj, args, true);
}
- public int jsGet_lineNumber()
+ @JSGetter
+ public int getLineNumber()
throws FileNotFoundException
{
return getReader().getLineNumber();
@@ -224,7 +236,8 @@ public int jsGet_lineNumber()
* @exception IOException if an error occurred while accessing the file
* associated with this object
*/
- public void jsFunction_close() throws IOException {
+ @JSFunction
+ public void close() throws IOException {
if (reader != null) {
reader.close();
reader = null;
@@ -242,7 +255,7 @@ public void jsFunction_close() throws IOException {
@Override
protected void finalize() {
try {
- jsFunction_close();
+ close();
}
catch (IOException e) {
}
@@ -251,7 +264,8 @@ protected void finalize() {
/**
* Get the Java reader.
*/
- public Object jsFunction_getReader() {
+ @JSFunction("getReader")
+ public Object getJSReader() {
if (reader == null)
return null;
// Here we use toObject() to "wrap" the BufferedReader object
@@ -264,10 +278,11 @@ public Object jsFunction_getReader() {
/**
* Get the Java writer.
*
- * @see File#jsFunction_getReader
+ * @see File#getReader
*
*/
- public Object jsFunction_getWriter() {
+ @JSFunction
+ public Object getWriter() {
if (writer == null)
return null;
Scriptable parent = ScriptableObject.getTopLevelScope(this);
View
11 examples/Foo.java
@@ -36,6 +36,8 @@
* ***** END LICENSE BLOCK ***** */
import org.mozilla.javascript.*;
+import org.mozilla.javascript.annotations.JSFunction;
+import org.mozilla.javascript.annotations.JSGetter;
/**
* An example host object class.
@@ -117,7 +119,8 @@ public String getClassName() {
*
* Resets the counter to 0.
*/
- public void jsFunction_resetCounter() {
+ @JSFunction
+ public void resetCounter() {
counter = 0;
}
@@ -127,7 +130,8 @@ public void jsFunction_resetCounter() {
* If "setCounter" had been defined in this class, the runtime would
* call the setter when the property is assigned to.
*/
- public int jsGet_counter() {
+ @JSGetter
+ public int getCounter() {
return counter++;
}
@@ -147,7 +151,8 @@ public int jsGet_counter() {
*
* @see org.mozilla.javascript.ScriptableObject#getTopLevelScope
*/
- public static Object jsFunction_varargs(Context cx, Scriptable thisObj,
+ @JSFunction
+ public static Object varargs(Context cx, Scriptable thisObj,
Object[] args, Function funObj)
{
StringBuffer buf = new StringBuffer();
Please sign in to comment.
Something went wrong with that request. Please try again.