Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume that you want to check the method <code>doSomething(String, Object)</code>.</p> <p>You might try this:</p> <pre><code>boolean methodExists = false; try { obj.doSomething("", null); methodExists = true; } catch (NoSuchMethodError e) { // ignore } </code></pre> <p>This will not work, since the method will be resolved at compile-time.</p> <p>You really need to use reflection for it. And if you have access to the source code of the method you want to call, it's even better to create an interface with the method you want to call.</p> <p>[Update] The additional information is: There is an interface that may exist in two versions, an old one (without the wanted method) and a new one (with the wanted method). Based on that, I suggest the following:</p> <pre><code>package so7058621; import java.lang.reflect.Method; public class NetherHelper { private static final Method getAllowedNether; static { Method m = null; try { m = World.class.getMethod("getAllowedNether"); } catch (Exception e) { // doesn't matter } getAllowedNether = m; } /* Call this method instead from your code. */ public static boolean getAllowedNether(World world) { if (getAllowedNether != null) { try { return ((Boolean) getAllowedNether.invoke(world)).booleanValue(); } catch (Exception e) { // doesn't matter } } return false; } interface World { //boolean getAllowedNether(); } public static void main(String[] args) { System.out.println(getAllowedNether(new World() { public boolean getAllowedNether() { return true; } })); } } </code></pre> <p>This code tests whether the method <code>getAllowedNether</code> exists in the interface, so it doesn't matter whether the actual objects have the method or not.</p> <p>If the method <code>getAllowedNether</code> must be called very often and you run into performance problems because of that, I will have to think of a more advanced answer. This one should be fine for now.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload