Search in sources :

Example 1 with AnnotationManagerReflection

use of com.servoy.j2db.scripting.annotations.AnnotationManagerReflection 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)

Aggregations

AnnotationManagerReflection (com.servoy.j2db.scripting.annotations.AnnotationManagerReflection)1 JSReadonlyProperty (com.servoy.j2db.scripting.annotations.JSReadonlyProperty)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 MemberBox (org.mozilla.javascript.MemberBox)1 NativeJavaMethod (org.mozilla.javascript.NativeJavaMethod)1