添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Could anybody please let me know when should we use:

Object.getClass().getMethod (action, null);

I found out that this belongs to the Java reflection package. I am asking this question because, in our existing application, we have this code, but I can't find out what's its use.

Method action = null;
 try {
    action = getClass().getMethod (action, null);
 catch (NoSuchMethodException x) {
 return (TAPResponse) action.invoke (this, null);

What is this doing in this case?

And why is the return statement in such a way? What will be returned?

I doubt that this is the code in your existing application, as getMethod expects a method name as String not a method object. – stryba Feb 24, 2012 at 12:25 Hi , Thank you very much for the replies , but could anybody please tell me why the return type is in such a way ?? (Because invoke is part of java.lang.reflect.Method class , and what is TAPResponse?? here – user663724 Feb 24, 2012 at 12:47 @Kiran Please can you fix your code. What you've written above will not compile, and without an accurate code snippet we can hardly speculate about what you mean. – kittylyst Feb 24, 2012 at 12:58 Cut and paste what you've written above into an IDE in a scratch class. It will not compile. Several people have now pointed this out. Just try it out and see for yourself. – kittylyst Feb 24, 2012 at 13:20

Your code is incorrect and won't compile.

Look at the Javadoc for Class. It only has one method called getMethod:

getMethod(String name, Class<?>... parameterTypes)

It doesn't have a method called getMethod() which has a Method object as first parameter.

I hope you're missing a part of the code there, otherwise the call would be:

action = getClass().getMethod(null, null);

And that would get you a NullPointerException. http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getMethod(java.lang.String, java.lang.Class...)

getMethod finds a Method in the given class with the first argument being the Method name, and the remaining arguments being the parameter types.

What your code does is, it tries to obtain a method of your class by a given name (that shouldn't be action... really, was it a copy paste problem?), and invokes it on your class current instance (this).

This method accepts no arguments, so null is passed as the second argument of both getMethod and invoke (it's redundant, it can be omited).

I doubt this code compiles. Look at java.lang.Class#getMethod(...)

  public Method getMethod(String name,
                    Class... parameterTypes)
             throws NoSuchMethodException,
                    SecurityException

It takes the name of public method in that class, and type of parameter that the said method takes. It returns the Method object that can be invoked by passing an instance of the Object this method belongs and the parameters that it takes.

This is what your code tries to do. (unsuccessfully, it seems)

The other comments talk about the documentation for the java.lang.Method. In terms of what the code means:

In the code that you have specified, you are basically trying to get the method that is named "actionName" (I think that is a typo btw, anyway) and then invoke that method. This is useful if you want to avoid doing a lot of if-else.

For example, instead of:

if ("first".equals(actionName)) first();

else if ("second".equals(actionName)) second();

.....

you can replace it with the code that you have pasted. That way you do not have to modify any code and just add a new method and it will be supported as a new action.

As long as you make sure that the "actionName" is sanitized (for example, you don't end up calling something like deleteAll), this is a good approach.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.