org.mozilla.javascript.annotations.JSFunction Java Examples

The following examples show how to use org.mozilla.javascript.annotations.JSFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Foo.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * An example of a variable-arguments method.
 *
 * All variable arguments methods must have the same number and
 * types of parameters, and must be static. <p>
 * @param cx the Context of the current thread
 * @param thisObj the JavaScript 'this' value.
 * @param args the array of arguments for this call
 * @param funObj the function object of the invoked JavaScript function
 *               This value is useful to compute a scope using
 *               Context.getTopLevelScope().
 * @return computes the string values and types of 'this' and
 * of each of the supplied arguments and returns them in a string.
 *
 * @see org.mozilla.javascript.ScriptableObject#getTopLevelScope
 */
@JSFunction
public static Object varargs(Context cx, Scriptable thisObj,
                                        Object[] args, Function funObj)
{
    StringBuffer buf = new StringBuffer();
    buf.append("this = ");
    buf.append(Context.toString(thisObj));
    buf.append("; args = [");
    for (int i=0; i < args.length; i++) {
        buf.append(Context.toString(args[i]));
        if (i+1 != args.length)
            buf.append(", ");
    }
    buf.append("]");
    return buf.toString();
}
 
Example #2
Source File: File.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read a character.
 *
 * @exception IOException if an error occurred while accessing the file
 *            associated with this object, or EOFException if the object
 *            reached the end of the file
 */
@JSFunction
public String readChar() throws IOException {
    int i = getReader().read();
    if (i == -1)
        return null;
    char[] charArray = { (char) i };
    return new String(charArray);
}
 
Example #3
Source File: ScriptableObject.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private static String getPropertyName(String methodName,
                                      String prefix,
                                      Annotation annotation) {
    if (prefix != null) {
        return methodName.substring(prefix.length());
    }
    String propName = null;
    if (annotation instanceof JSGetter) {
        propName = ((JSGetter) annotation).value();
        if (propName == null || propName.length() == 0) {
            if (methodName.length() > 3 && methodName.startsWith("get")) {
                propName = methodName.substring(3);
                if (Character.isUpperCase(propName.charAt(0))) {
                    if (propName.length() == 1) {
                        propName = propName.toLowerCase();
                    } else if (!Character.isUpperCase(propName.charAt(1))){
                        propName = Character.toLowerCase(propName.charAt(0))
                                + propName.substring(1);
                    }
                }
            }
        }
    } else if (annotation instanceof JSFunction) {
        propName = ((JSFunction) annotation).value();
    } else if (annotation instanceof JSStaticFunction) {
        propName = ((JSStaticFunction) annotation).value();
    }
    if (propName == null || propName.length() == 0) {
        propName = methodName;
    }
    return propName;
}
 
Example #4
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
private static void appendApiMethods(StringBuilder builder, Class<?> clazz, String namespace) {
	for (Method met : clazz.getMethods()) {
		if (met.getAnnotation(JSFunction.class) != null
				|| met.getAnnotation(JSStaticFunction.class) != null) {
			appendApiMethodDescription(builder, met, namespace);
		}
	}
	builder.append("\n");
}
 
Example #5
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
private static String[] getAllJsFunctions(Class<? extends ScriptableObject> clazz) {
	List<String> allList = new ArrayList<String>();
	for (Method met : clazz.getMethods()) {
		if (met.getAnnotation(JSFunction.class) != null) {
			allList.add(met.getName());
		}
	}
	return allList.toArray(blankArray);
}
 
Example #6
Source File: ScriptableObject.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private static String getPropertyName(String methodName,
                                      String prefix,
                                      Annotation annotation) {
    if (prefix != null) {
        return methodName.substring(prefix.length());
    }
    String propName = null;
    if (annotation instanceof JSGetter) {
        propName = ((JSGetter) annotation).value();
        if (propName == null || propName.length() == 0) {
            if (methodName.length() > 3 && methodName.startsWith("get")) {
                propName = methodName.substring(3);
                if (Character.isUpperCase(propName.charAt(0))) {
                    if (propName.length() == 1) {
                        propName = propName.toLowerCase();
                    } else if (!Character.isUpperCase(propName.charAt(1))){
                        propName = Character.toLowerCase(propName.charAt(0))
                                + propName.substring(1);
                    }
                }
            }
        }
    } else if (annotation instanceof JSFunction) {
        propName = ((JSFunction) annotation).value();
    } else if (annotation instanceof JSStaticFunction) {
        propName = ((JSStaticFunction) annotation).value();
    }
    if (propName == null || propName.length() == 0) {
        propName = methodName;
    }
    return propName;
}
 
Example #7
Source File: File.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the Java writer.
 *
 * @see File#getReader
 *
 */
@JSFunction
public Object getWriter() {
    if (writer == null)
        return null;
    Scriptable parent = ScriptableObject.getTopLevelScope(this);
    return Context.javaToJS(writer, parent);
}
 
Example #8
Source File: File.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the Java reader.
 */
@JSFunction("getReader")
public Object getJSReader() {
    if (reader == null)
        return null;
    // Here we use toObject() to "wrap" the BufferedReader object
    // in a Scriptable object so that it can be manipulated by
    // JavaScript.
    Scriptable parent = ScriptableObject.getTopLevelScope(this);
    return Context.javaToJS(reader, parent);
}
 
Example #9
Source File: File.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Close the file. It may be reopened.
 *
 * Implements a JavaScript function.
 * @exception IOException if an error occurred while accessing the file
 *            associated with this object
 */
@JSFunction
public void close() throws IOException {
    if (reader != null) {
        reader.close();
        reader = null;
    } else if (writer != null) {
        writer.close();
        writer = null;
    }
}
 
Example #10
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
@JSFunction
public long spawnCow(double x, double y, double z, String tex) {
	if (invalidTexName(tex)) {
		tex = "mob/cow.png";
	}
	long entityId = spawnEntityImpl((float) x, (float) y, (float) z, 11, tex);
	return entityId;
}
 
Example #11
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
@JSFunction
public long spawnChicken(double x, double y, double z, String tex) {
	if (invalidTexName(tex)) {
		tex = "mob/chicken.png";
	}
	long entityId = spawnEntityImpl((float) x, (float) y, (float) z, 10, tex);
	return entityId;
}
 
Example #12
Source File: File.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Read the remaining lines in the file and return them in an array.
 *
 * Implements a JavaScript function.<p>
 *
 * This is a good example of creating a new array and setting
 * elements in that array.
 *
 * @exception IOException if an error occurred while accessing the file
 *            associated with this object
 */
@JSFunction
public Object readLines()
    throws IOException
{
    List<String> list = new ArrayList<String>();
    String s;
    while ((s = readLine()) != null) {
        list.add(s);
    }
    String[] lines = list.toArray(new String[list.size()]);
    Scriptable scope = ScriptableObject.getTopLevelScope(this);
    Context cx = Context.getCurrentContext();
    return cx.newObject(scope, "Array", lines);
}
 
Example #13
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
@JSFunction
public double getPitch(Object entObj) {
	long ent;
	if (entObj == null || !(entObj instanceof Number)) {
		ent = getPlayerEnt();
	} else {
		ent = ((Number) entObj).longValue();
	}
	return nativeGetPitch(ent);
}
 
Example #14
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
@JSFunction
public double getYaw(Object entObj) {
	long ent;
	if (entObj == null || !(entObj instanceof Number)) {
		ent = getPlayerEnt();
	} else {
		ent = ((Number) entObj).longValue();
	}
	return nativeGetYaw(ent);
}
 
Example #15
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
@JSFunction
public long spawnPigZombie(double x, double y, double z, int item, String tex) {
	if (invalidTexName(tex)) {
		tex = null;
	}
	long entityId = spawnEntityImpl((float) x, (float) y, (float) z, 36, tex);
	if (item == 0 || !nativeIsValidItem(item)) item = 283; // gold sword
	nativeSetCarriedItem(entityId, item, 1, 0);
	return entityId;
}
 
Example #16
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 5 votes vote down vote up
@JSFunction
public long bl_spawnMob(double x, double y, double z, int typeId, String tex) {
	if (invalidTexName(tex)) {
		tex = null;
	}
	long entityId = spawnEntityImpl((float) x, (float) y, (float) z, typeId, tex);
	return entityId;
}
 
Example #17
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void preventDefault() {
	nativePreventDefault();
}
 
Example #18
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public int getCarriedItem() {
	return nativeGetCarriedItem(ITEMID);
}
 
Example #19
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void addItemInventory(int id, int amount, int damage) {
	if (!nativeIsValidItem(id)) throw new RuntimeException("invalid item id " + id);
	nativeAddItemInventory(id, amount, damage);
}
 
Example #20
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void rideAnimal(Object /* Flynn */rider, Object mount) {
	nativeRideAnimal(getEntityId(rider), getEntityId(mount));
}
 
Example #21
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public NativePointer getLevel() {
	return new NativePointer(nativeGetLevel());
	// TODO: WTF does this do?
}
 
Example #22
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void setTile(int x, int y, int z, int id, int damage) {
	NativeLevelApi.setTile(x, y, z, id, damage);
}
 
Example #23
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void clientMessage(String text) {
	wordWrapClientMessage(text);
}
 
Example #24
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void setNightMode(boolean isNight) {
	nativeSetNightMode(isNight);
}
 
Example #25
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public int getTile(int x, int y, int z) {
	return nativeGetTileWrap(x, y, z);
}
 
Example #26
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void setPositionRelative(Object ent, double x, double y, double z) {
	nativeSetPositionRelative(getEntityId(ent), (float) x, (float) y, (float) z);
}
 
Example #27
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void setRot(Object ent, double yaw, double pitch) {
	nativeSetRot(getEntityId(ent), (float) yaw, (float) pitch);
}
 
Example #28
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void bl_setMobSkin(Object entityId, String tex) {
	NativeEntityApi.setMobSkin(getEntityId(entityId), tex);
}
 
Example #29
Source File: ScriptManager.java    From MCPELauncher with Apache License 2.0 4 votes vote down vote up
@JSFunction
public void print(String str) {
	scriptPrint(str);
}
 
Example #30
Source File: DefineClassTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@JSFunction
public Object instanceFunction() {
    return "instanceFunction";
}