Java if object has method

Java: check if a class exists and call a specific method if it exists

Is there a way to do the following? Check if a class exists (in the same package) and if it does exist, check if a particular method exists, and if so, calling it? Say that I have class X. In some method of class X, I want to do the following:

Whether it’s reasonable depends on your requirements. In most cases it’s not reasonable. In the rare cases it is reasonable, you would most likely understand exactly what you’re doing and why you’re doing it.

6 Answers 6

Is such a thing possible? And is doing such a thing reasonable? Thanks.

Of course it is possible.
If you develop a program or a library that has to discover dynamically some classes, it is a very reasonable thing.
If it is not the case, it could not be.

If your need makes sense, you should ask you an additional question : should you invoke a static or instance method ?

Here is a sample example with both solutions :

ReflectionClass that contains the logic using reflection :

import java.lang.reflect.Method; public class ReflectionCalls < public static void main(String[] args) < new ReflectionCalls(); >public ReflectionCalls() < callMethod(true); callMethod(false); >private void callMethod(boolean isInstanceMethod) < String className = "DiscoveredClass"; String staticMethodName = "methodStatic"; String instanceMethodName = "methodInstance"; Class[] formalParameters = < int.class, String.class >; Object[] effectiveParameters = new Object[] < 5, "hello" >; String packageName = getClass().getPackage().getName(); try < Classclazz = Class.forName(packageName + "." + className); if (!isInstanceMethod) < Method method = clazz.getMethod(staticMethodName, formalParameters); method.invoke(null, effectiveParameters); >else < Method method = clazz.getMethod(instanceMethodName, formalParameters); Object newInstance = clazz.newInstance(); method.invoke(newInstance, effectiveParameters); >> catch (Exception e) < e.printStackTrace(); >> > 

DiscoveredClass (the class we manipulate in the example)

 package reflectionexp; public class DiscoveredClass < public static void methodStatic(int x, String string) < System.out.println("static method with " + x + " and " + string); >public void methodInstance(int x, String string) < System.out.println("instance method with " + x + " and " + string); >> 

instance method with 5 and hello

static method with 5 and hello

Источник

How To Check If A Method Exists At Runtime In Java?

What does try/catch have to do with method existence? Unless you are referring to a situation like with the Iterator interface where implementation of remove are permitted to throw a UnsupportedOperationException ?

I’m trying to see if a desired method that may not be present exists so that I know if I can call it. I thought I would be able to put the call inside a try catch, and catch an error if the method does not exist. In case it isn’t obvious I’m not exactly a Java pro!

7 Answers 7

I assume that you want to check the method doSomething(String, Object) .

boolean methodExists = false; try < obj.doSomething("", null); methodExists = true; >catch (NoSuchMethodError e) < // ignore >

This will not work, since the method will be resolved at compile-time.

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.

[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:
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; >>)); > > 

This code tests whether the method getAllowedNether exists in the interface, so it doesn’t matter whether the actual objects have the method or not.

If the method getAllowedNether 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.

Источник

How to check if a java class has a particular method in it?

I have an xml schema (generated automatically using trang) which keeps changing. These changes are not very elaborate. Only some elements are added or deleted from this schema. From this schema, I am generating java classes (using cxf) by which I will unmarshall the xml document. As schema changes, my auto-generated java classes also change. Again, as with schema, changes in java classes are not very big. For instance, if an element say elemA is added to schema; some related functions say getElemA() and setElemA() are added to auto-generated java class. Now how would I make sure that a particular function exists in these auto-generated classes? One solution is to hand-write the schema such that all possible elements of xml are covered. This is what I’ll ultimately do. But for now, I have not fixed the format of xml file. UPDATE : There is a possibility that a method getElemA() may be defined in auto-generated classes. I do not have control over the auto-generation of these classes. But in my main class, if have following code,

If method getElemA exists then ElemA elemA = getElemA() 

This code will always be there in my main class. If method getElemA() is generated in one of the auto-generated class then there is no problem. But if this method is not generated then compilers complain that this method does not exists in any of the class. Is there any way that I can make compiler not to complain about this function at compile time?

Источник

check if a class contains a method

I have a abstract class to create tools (like illustrator pen, selection etc). The idea is that users can create easily new tools if they like. Some tools have a method called, «draggingSelection». I wonder if there is a way to check if a class has that object, and if so, run it. (In this case draggingSelection returns a boolean) So far i can figure out if the method exists or not. I only can’t get it the method to run. I tried things with invoke but i fail at it. My method doesn’t requite any parameters. Could somone help.

public boolean draggingSelection() < Method[] meths = activeTool.getClass().getMethods(); for (int i = 0; i < meths.length; i++) < if (meths[i].getName().equals("draggingSelection")) < // how can i run it? //return meths[i].draggingSelection(); // wrong >> return false; > 

You can check it by reflection but a much better way is to declare an interface declaring public boolean draggingSelection() and use that. This way you get compile-time checks.

3 Answers 3

Better solution in my opinion is to check if given object’s class implements some interface.

However, to call draggingSelection method, do it on an object that you are testing:

activeTool.draggingSelection() 

I wouldn’t check. Just pass it as Draggable . This will give you compile time checks instead of annoying runtime checks.

You could do that via reflection, but a far better solution would be to have an interface that has all the relevant methods:

public interface SelectionAware

Once you have that, you have (at least) two options to use it:

  1. let your tools implement that interface and use myTool instanceof SelectionAware followed by a cast to call that method or
  2. let the tool explicitly register itself as a listener in some appropriate init method.

Option 1 is closer to what you attempted to do, but restricts the use of that interface and is not really clean code (because your code needs to «guess» if some tool implements some interface).

Option 2 is probably slightly more work (where/when to register/unregister the listener? . ), but is definitely the cleaner approach. It also has the advantage that the listeners are not restricted to being tools: anything could register such a listener.

Источник

How to tell if an object implements a method like toString()?

I am writing a testing library and I want to be able to handle objects that may not have been provided their own implementation of String toString() . In those cases, rather that using the useless default implementation, I’d like to use the Apache Commons-Lang ToStringBuilder instead. but would rather use an object’s implementation of toString() if available. How can I tell if an object has an implementation of toString() other than the default?

1 Answer 1

The best I have found is by using java.lang.Class.getDeclaredMethod(String,Class. ) and calling it on the object’s class hierarchy like so:

public static boolean objectHasToStringImplemented( final Object o ) < return classHasToStringImplemented( o.getClass() ); >public static boolean classHasToStringImplemented( final Class initialClass ) < ClassclassToCheck = initialClass; while( classToCheck != Object.class ) < if( classImplementsToString( classToCheck ) )< return true; >classToCheck = classToCheck.getSuperclass(); > return false; > private boolean classImplementsToString( final Class aClass )< try< aClass.getDeclaredMethod( "toString" ); return true; >catch( NoSuchMethodException e ) < return false; >> 

and then I sped it up by caching answers and not triggering exceptions

//NB: implementation is synchronized and thus thread-safe private static final Hashtable,Boolean> cachedAnswers = new Hashtable<>(); static < cachedAnswers.put( Object.class, Boolean.FALSE ); >public static Boolean objectHasToStringImplemented( final Object o ) < return classHasToStringImplemented( o.getClass() ); >public static Boolean classHasToStringImplemented( final Class aClass ) < for( Method m : aClass.getDeclaredMethods() )< if( isToString( m ) )< return Boolean.TRUE; >> return Boolean.FALSE; > private static Boolean isToString( final Method m )< if( m.getParameterCount() == 0 && m.getName().equals( "toString" ) && m.getReturnType().equals( String.class ) )< return Boolean.TRUE; >else < return Boolean.FALSE; >> 
Оцените статью