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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:58:26+00:00 2026-05-28T00:58:26+00:00

I have methods like this: public File method1(){ method2() } public method2(){ do something..

  • 0

I have methods like this:

public File method1(){
    method2()
}

public method2(){
do something..
and get method1 return type(in this case File)
}

How do I get it? i tried like this..

Throwable t = new Throwable();
StackTraceElement[] elements = t.getStackTrace();

and get all the methods for the elements. And after that, getReturnType, but it doesn’t work. I also tried

public File method1(){
    method2(this.getClass());
}

public method2(Class<?> className){
   //problem here
}

But here the problem is that i can’t compare two elements, the one on the stack and the one from classname.getMethods().

Is there any way that I can send method return type to a method2? I need this because of making some history-like log. I know it can be done with aspectJ but I have to do it somehow like this.

EDIT:

The main problem I have is that I can get stack output, and see the method who called my method2 – that’s one fragment I need! Also I need that method’s return type, but the stack doesnt hold that information. Now, I can get all the methods from the class where the “method who called method2” is. The list of those methods, hold everything, return type, input parameters.. but that’s a pretty big list, 63 methods. So I have to compare them somehow to find out which one is the one FROM STACK. I can’t comapre them using name, because some differ with return type, hashcode is different – that’s where I’m stuck.

  • 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-28T00:58:26+00:00Added an answer on May 28, 2026 at 12:58 am

    Update

    If you really need to do this from a stack trace (which I would strongly recommend avoiding), I don’t think you can. The stack trace can tell you the class and method names, but it doesn’t include the method’s argument types, and so if the method is overloaded you can’t tell which one called method2.

    I recommend you revisit your design. If method2 needs to know the return type of the method that calls it, then the method in question should pass that information into method2. Attempting to gather that information from a runtime stack is not only inefficient, but it’s a design red flag.

    Example:

    public File method1(File f) {
        // ...
    
        method2(File.class);
    }
    
    public String method1(String s) {
        // ...
    
        method2(String.class);
    }
    
    public Foo method1(Foo f) {
        // ...
    
        method2(Foo.class);
    }
    

    There we have three overloads of method1. This is not a problem for method2, because each of them tells method2 what its return type is — or more accurately, I hope, what it needs method2 to create for it or whatever.

    Original answer

    For the specific case you list (esp. toward the end of your question), you can do this:

    public File method1(){
        method2(File.class);
    }
    

    File.class is the Class instance for the File class.

    For the general case of finding the type of the return value of a method in a class, you can use the reflection API. Get the Class instance for the class containing the method via Class.forName, look up the method on that Class instance using Class#getMethod, then use Method#getReturnType on that Method to find out the return type’s class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some existing web services I consume have methods that look something like this: List<Employee>
In my Spring context file I have something like this: <bean id=userCheck class=a.b.c.UserExistsCheck/> <aop:config>
I have an xml file that looks something like this <questions> <question> <text>What color
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have a method that looks like this: public static String escape(String text) {
I have a method in a url rewriting module that looks like this public
I have a method that looks like: T[] field; public Method(IList<T> argument) { this.field
I have methods in all of my models that look like this: def formatted_start_date
Ok so I have a number of methods that look like this:- which sorts

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.