0

I am developing a Java applet where I call a javascript function:

boolean isAllowed = (boolean) win.eval("isPointMarkCreationAllowed()");

and I would like to check if that function exists, like we do in javascript:

if (isPointMarkCreationAllowed == 'function')

is there anyway to do that in Java?

1
  • win.eval("isPointMarkCreationAllowed()"); What happens when you call that and the function does not exist? I would expect a JSException according to the JavaDocs. Commented Jan 28, 2013 at 14:58

2 Answers 2

2

Without actually having tried it, wouldn't

win.eval("typeof isPointMarkCreationAllowed == 'function'");

do exactly what you want and return a Boolean (true or false)?

Sign up to request clarification or add additional context in comments.

1 Comment

I suspect it would would the trick. If not, abstract out that code into a JS function that accepts the name of a function and returns true or false on evaluation.
-1

You can use reflection to test if a method exists.

For example if you have an object foo, you can get all the methods declared in the class of that object in the following:

Method[] methods = foo.getClass().getMethods();

This returns an array of the methods declared in the class.

Then just use a for loop to check if a specific method exists in the array returned

for (Method m : methods)
{
     if (m.getName().equals(someString))
     {
        //do something
     }
}

someString is the name of the method you're looking for, which is "isPointMarkCreationAllowed" in your case.

Use the following site to learn about reflections in Java http://docs.oracle.com/javase/tutorial/reflect/member/methodType.html

1 Comment

No, you can't use reflection to access or check JavaScript function from an applet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.