Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7035953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:21:57+00:00 2026-05-28T01:21:57+00:00

Disclaimer: I have 10 years’ experience programming, but 8 of which is in PHP

  • 0

Disclaimer: I have 10 years’ experience programming, but 8 of which is in PHP (loosely typed) — I have been using Java now for 4 days 🙂

In java, I need to get the value of an annotation for a method in the call stack. As far as I can tell, I do this with the Method object. From the call stack, I have retrieved the names of the class and method (strings). This is the (abbreviated) code that I am using…

Calling Method:

public class myClass
{
   @Path( "some/path/value" )
   public void myMethod( String someArg ) { ... }
}

Retrieval Code:

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
String callingMethodName = trace[depth].getMethodName();
String callingClassName = trace[depth].getClassName();

Class[] signature = new Class[1];
signature[0] = String.class;

Class callingClass = Class.forName( callingClassName );
Method callingMethod = callingClass.getMethod( callingMethodName, signature );
Path annotation = callingMethod.getAnnotation( Path.class );

This works like a charm, successfully returning the value of the @Path annotation ("some/path/value")

However, if you notice, I had to supply a signature of the method I was looking for. As you can see in the class code, there is only 1 method with that name, so, theoretically, the signature of the method should be irrelevant, right? As far as I can tell from various docs/blogs/examples, I should be able to call getMethod( ) with either no 2nd argument, or with null for the 2nd argument, but if I use the following:

Class callingClass = Class.forName( callingClassName );
Method callingMethod = callingClass.getMethod( callingMethodName );

Or even:

Class callingClass = Class.forName( callingClassName );
Method callingMethod = callingClass.getMethod( callingMethodName, null );

I get a NoSuchMethodException. Am I doing something wrong here? Should I be taking a different approach altogether?

In this particular situation, the calling class/method never uses polymorphism so there is only ever 1 signature. However, said signature is not known (unless that can also be determined by data available/derivable from Thread.currentThread( )), so I need a way to get a Method object without knowing the signature.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:21:58+00:00Added an answer on May 28, 2026 at 1:21 am

    No. The signature matters. If you don’t know the exact signature, but know the name, get all the methods and iterate over them until you get a hit:

    Method method = null;
    for (Method m : c.getDeclaredMethods()) {
        if (m.getName().equals(callingMethodName)) {
            method = m;
            break;
        }
    }
    // variable "method" is the first that matched name, or null if not found
    

    Also, since java 1.5, Class.getMethod(String, Class...) is a varargs method, so you don’t need the java cruft that both the question and other answer has, ie:

    This works, but avoid this crap (the old, hard way):

    Class[] signature = new Class[1];
    signature[0] = String.class;
    Method callingMethod = callingClass.getMethod( callingMethodName, signature);
    

    Prefer this (new way):

    Method callingMethod = callingClass.getMethod( callingMethodName, String.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I don't have a whole ton of experience with Java Generics, but my
Disclaimer: I have no experience using YUI at all. I was wondering how the
(Disclaimer) MY php experience is approx 2 hours old and I have know idea
First of all, a disclaimer: I have experience in other languages, but am still
Disclaimer: I have no Twitter API experience nor have I used Twitter until today
Disclaimer: I have not used RoR, and I have not generated tests. But, I
First the disclaimer: I never learnt any programming in school, and just have to
Disclaimer: This is not actually a programming question, but I feel the audience on
The Disclaimer First of all, I know this question (or close variations) have been
Disclaimer: completely new to Python from a PHP background Ok I'm using Python on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.