Skip to content

Commit

Permalink
Use annotations for example host objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
hns committed Nov 18, 2011
1 parent 18b914a commit b0e27fe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
20 changes: 13 additions & 7 deletions examples/Counter.java
Expand Up @@ -37,26 +37,32 @@
* ***** 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;

// 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;
}
41 changes: 28 additions & 13 deletions examples/File.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -123,7 +128,8 @@ public String getClassName() {
*
* Used to define the "name" property.
*/
public String jsGet_name() {
@JSGetter
public String getName() {
return name;
}

Expand All @@ -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()]);
Expand All @@ -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();
}

Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -242,7 +255,7 @@ public void jsFunction_close() throws IOException {
@Override
protected void finalize() {
try {
jsFunction_close();
close();
}
catch (IOException e) {
}
Expand All @@ -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
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions examples/Foo.java
Expand Up @@ -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.
Expand Down Expand Up @@ -117,7 +119,8 @@ public String getClassName() {
*
* Resets the counter to 0.
*/
public void jsFunction_resetCounter() {
@JSFunction
public void resetCounter() {
counter = 0;
}

Expand All @@ -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++;
}

Expand All @@ -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();
Expand Down

0 comments on commit b0e27fe

Please sign in to comment.