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);
}
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;
}
}
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;
}
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;
}
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());
}
}
}
Aggregations