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 7585201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:07:08+00:00 2026-05-30T19:07:08+00:00

I am using Reflections to obtain a method from a class that is annotated

  • 0

I am using Reflections to obtain a method from a class that is annotated with a specific annotation. Once I get the list of methods in the class, I loop through the methods and if the method matches a specific return type, I want to invoke that method. For testing purposes I know that the method I am getting returns a String.

Reflections reflections = new Reflections(new ConfigurationBuilder()
        .setScanners(new TypesScanner(), new TypeElementsScanner())
        .setUrls(ClasspathHelper.forPackage("stressball"))
);

Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(DependantClass.class);
System.out.println(annotated);

for(Class<?> clazz : annotated) {
    for(Method method : clazz.getMethods()) {
        if(method.isAnnotationPresent(DependantResource.class)) {
            if(method.getReturnType() == String.class) {
                System.out.println(method.invoke(method,(Object[]) null));
            }                   
        }
    }
}

This is the method I am attempting to invoke

@DependantResource
public String showInjector() {
    return "This is an injector";
}

I keep getting the following error and I know it has everything to do with the object I am passing into invoke, but is the method from the loop not the object I am supposed to be passing?

Exception in thread "main" java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at stressball.test.DefaultTest.main(DefaultTest.java:35)
  • 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-30T19:07:10+00:00Added an answer on May 30, 2026 at 7:07 pm

    This is not correct:

    method.invoke(method,(Object[]) null)
    

    You should instantiate an object first, and then make a call. Something like:

    method.invoke(clazz.newInstance(), (Object[]) null)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using reflection it is possible to obtain the call stack (apart from that
Using reflection, how can I get all types that implement an interface with C#
I am using reflection class to invoke some methods which are on the some
I'm trying to get a class memeber variable list at run time. I know
i am trying to call the MotionEvent.obtain Methode using the reflections library. But so
I have a List of List<String> 's which I get from a external API
I'm using reflection to get some member names as strings. I'm using a method
I've been successfully using MEF to obtain exported class instances. However, I came across
Using reflection I want to retrieve only the properties that have both a get
I have a simple question. Is there a way ( using reflections I suppose

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.