Search in sources :

Example 1 with NativeJavaMethod

use of org.mozilla.javascript.NativeJavaMethod in project mayaa by seasarorg.

the class NativeDynaBeanTest method testMethod3.

@Test
public void testMethod3() {
    Object obj = _cx.evaluateString(_scope, "bean.remove", null, 0, null);
    Object o = JavaAdapter.convertResult(obj, Object.class);
    assertTrue(o instanceof NativeJavaMethod);
}
Also used : NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) ScriptableObject(org.mozilla.javascript.ScriptableObject) Test(org.junit.Test)

Example 2 with NativeJavaMethod

use of org.mozilla.javascript.NativeJavaMethod in project servoy-client by Servoy.

the class InstanceJavaMembers method makeBeanProperties.

/**
 * @see org.mozilla.javascript.JavaMembers#makeBeanProperties(Scriptable, boolean)
 */
@SuppressWarnings("nls")
@Override
protected void makeBeanProperties(boolean isStatic, boolean includePrivate) {
    Map<String, Object> ht = isStatic ? staticMembers : members;
    Map<String, Object> copy = new HashMap<String, Object>(ht);
    for (Entry<String, Object> entry : ht.entrySet()) {
        String name = entry.getKey();
        String newName = null;
        if (// $NON-NLS-1$
        name.startsWith("js_")) {
            newName = name.substring(3);
        } else {
            Object member = entry.getValue();
            if (member instanceof NativeJavaMethod) {
                if (((NativeJavaMethod) member).getMethods().length == 1) {
                    MemberBox mb = ((NativeJavaMethod) member).getMethods()[0];
                    if (mb.isMethod()) {
                        if (AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSReadonlyProperty.class)) {
                            newName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSReadonlyProperty.class).property();
                            if (newName == null || newName.length() == 0 && (entry.getKey().startsWith("get") || entry.getKey().startsWith("is"))) {
                                newName = entry.getKey().substring(entry.getKey().startsWith("get") ? 3 : 2);
                                // Make the bean property name.
                                char ch0 = newName.charAt(0);
                                if (Character.isUpperCase(ch0)) {
                                    if (newName.length() == 1) {
                                        newName = newName.toLowerCase();
                                    } else {
                                        char ch1 = newName.charAt(1);
                                        if (!Character.isUpperCase(ch1)) {
                                            newName = Character.toLowerCase(ch0) + newName.substring(1);
                                        }
                                    }
                                }
                            }
                        } else if (AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSGetter.class)) {
                            newName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSGetter.class).value();
                        } else if (AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSSetter.class)) {
                            newName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSSetter.class).value();
                        }
                    }
                }
                for (MemberBox mb : ((NativeJavaMethod) member).getMethods()) {
                    if (mb.isMethod() && AnnotationManagerReflection.getInstance().isAnnotationPresent(mb.method(), cl, JSFunction.class)) {
                        String funcName = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), cl, JSFunction.class).value();
                        if (funcName == null || funcName.length() == 0) {
                            funcName = entry.getKey();
                        }
                        // $NON-NLS-1$
                        newName = "jsFunction_".concat(funcName);
                        break;
                    }
                }
            }
        }
        if (newName != null && newName.length() > 0 && !newName.equals(name) && !Ident.checkIfJavascriptKeyword(newName)) {
            putNewValueMergeForDuplicates(copy, name, newName);
        }
    }
    ht = copy;
    if (isStatic) {
        staticMembers = ht;
    } else {
        members = ht;
    }
    super.makeBeanProperties(isStatic, includePrivate);
    copy = new HashMap<String, Object>(ht);
    for (Entry<String, Object> entry : ht.entrySet()) {
        String name = entry.getKey();
        if (// $NON-NLS-1$
        name.startsWith("jsFunction_")) {
            String newName = name.substring(11);
            if (!Ident.checkIfJavascriptKeyword(newName)) {
                putNewValueMergeForDuplicates(copy, name, newName);
            }
        } else {
            Object member = entry.getValue();
            if (member instanceof NativeJavaMethod && ((NativeJavaMethod) member).getMethods().length == 1) {
                MemberBox mb = ((NativeJavaMethod) member).getMethods()[0];
                if (mb.isMethod()) {
                    // make bean property
                    JSReadonlyProperty jsReadonlyProperty = AnnotationManagerReflection.getInstance().getAnnotation(mb.method(), mb.getDeclaringClass(), JSReadonlyProperty.class);
                    if (jsReadonlyProperty != null) {
                        String propertyName = jsReadonlyProperty.property();
                        if (propertyName == null || propertyName.length() == 0) {
                            propertyName = name;
                        }
                        Object oldValue = copy.put(propertyName, new BeanProperty(mb, null, null));
                        if (oldValue instanceof NativeJavaMethod) {
                            // allow the method to be called directly as well
                            String functionName = ((NativeJavaMethod) oldValue).getFunctionName();
                            if (!functionName.equals(propertyName)) {
                                copy.put(functionName, oldValue);
                                // but do not show it
                                addMethodToDelete(functionName);
                            }
                        }
                    }
                }
            }
        }
    }
    if (isStatic) {
        staticMembers = copy;
    } else {
        members = copy;
    }
}
Also used : MemberBox(org.mozilla.javascript.MemberBox) HashMap(java.util.HashMap) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty) NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) JSFunction(org.mozilla.javascript.annotations.JSFunction) JSGetter(org.mozilla.javascript.annotations.JSGetter)

Example 3 with NativeJavaMethod

use of org.mozilla.javascript.NativeJavaMethod in project servoy-client by Servoy.

the class DeclaringClassJavaMembers method getMethodIds.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.mozilla.javascript.JavaMembers#getMethodIds(boolean)
	 */
@Override
public List getMethodIds(boolean isStatic) {
    List list = super.getMethodIds(isStatic);
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {
        String id = (String) iter.next();
        NativeJavaMethod method = getMethod(id, isStatic);
        if (method.getMethods()[0].getDeclaringClass() != clazz) {
            iter.remove();
        }
    }
    return list;
}
Also used : NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) Iterator(java.util.Iterator) List(java.util.List)

Example 4 with NativeJavaMethod

use of org.mozilla.javascript.NativeJavaMethod in project servoy-client by Servoy.

the class DefaultJavaScope method getJsFunctions.

public static Map<String, NativeJavaMethod> getJsFunctions(Class<?> clazz) {
    Map<String, NativeJavaMethod> jsFunctions = new HashMap<String, NativeJavaMethod>();
    try {
        for (Method method : clazz.getMethods()) {
            String name = null;
            if (// $NON-NLS-1$
            method.getName().startsWith("js_")) {
                name = method.getName().substring(3);
            } else if (// $NON-NLS-1$
            method.getName().startsWith("jsFunction_")) {
                name = method.getName().substring(11);
            } else {
                AnnotationManagerReflection annotationManager = AnnotationManagerReflection.getInstance();
                JSReadonlyProperty jsReadonlyProperty = annotationManager.getAnnotation(method, clazz, JSReadonlyProperty.class);
                if (jsReadonlyProperty != null) {
                    name = jsReadonlyProperty.property();
                    if (name == null || name.length() == 0) {
                        name = method.getName();
                    }
                } else if (annotationManager.isAnnotationPresent(method, clazz, JSFunction.class)) {
                    name = method.getName();
                }
            }
            if (name != null) {
                NativeJavaMethod nativeJavaMethod = jsFunctions.get(name);
                if (nativeJavaMethod == null) {
                    nativeJavaMethod = new NativeJavaMethod(method, name);
                } else {
                    nativeJavaMethod = new NativeJavaMethod(Utils.arrayAdd(nativeJavaMethod.getMethods(), new MemberBox(method), true), name);
                }
                jsFunctions.put(name, nativeJavaMethod);
            }
        }
    } catch (Exception e) {
        Debug.error(e);
    }
    return jsFunctions;
}
Also used : NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) MemberBox(org.mozilla.javascript.MemberBox) HashMap(java.util.HashMap) AnnotationManagerReflection(com.servoy.j2db.scripting.annotations.AnnotationManagerReflection) JSReadonlyProperty(com.servoy.j2db.scripting.annotations.JSReadonlyProperty) NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) Method(java.lang.reflect.Method)

Example 5 with NativeJavaMethod

use of org.mozilla.javascript.NativeJavaMethod in project servoy-client by Servoy.

the class InstanceJavaMembers method putNewValueMergeForDuplicates.

/**
 * @param copy
 * @param name
 * @param newName
 */
@SuppressWarnings("nls")
private void putNewValueMergeForDuplicates(Map<String, Object> copy, String name, String newName) {
    Object oldValue = copy.put(newName, copy.remove(name));
    if (oldValue != null) {
        Object newValue = copy.get(newName);
        if (oldValue instanceof NativeJavaMethod && newValue instanceof NativeJavaMethod) {
            MemberBox[] oldMethods = ((NativeJavaMethod) oldValue).getMethods();
            MemberBox[] newMethods = ((NativeJavaMethod) newValue).getMethods();
            copy.put(newName, new NativeJavaMethod(Utils.arrayJoin(oldMethods, newMethods), newName));
        } else {
            Debug.error("illegal state new_name '" + newName + "' from old_name '" + name + "' can't be merged:  " + oldValue + ", " + newValue, new RuntimeException());
        }
    }
}
Also used : NativeJavaMethod(org.mozilla.javascript.NativeJavaMethod) MemberBox(org.mozilla.javascript.MemberBox)

Aggregations

NativeJavaMethod (org.mozilla.javascript.NativeJavaMethod)8 MemberBox (org.mozilla.javascript.MemberBox)3 JSReadonlyProperty (com.servoy.j2db.scripting.annotations.JSReadonlyProperty)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1 Record (com.servoy.j2db.dataprocessing.Record)1 IScriptable (com.servoy.j2db.scripting.IScriptable)1 AnnotationManagerReflection (com.servoy.j2db.scripting.annotations.AnnotationManagerReflection)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigInteger (java.math.BigInteger)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ZipException (java.util.zip.ZipException)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 NativeArray (org.mozilla.javascript.NativeArray)1