[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get Java Method Name

Posted on 2004-11-25
8
Medium Priority
?
187 Views
Last Modified: 2010-03-31
How can I determine which method of parent object or class instantiated another class if I'm in the new class. ?
0
Comment
Question by:ekartha
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 100 total points
ID: 12679046
YourClass object;

System.out.println("executing method of "+object.getClass());
object.someMethod();
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12679055
class Parent
{
    void print()
    {
           System.out.println("I am print method of parent");
    }    
}

class Child extends Parent
{
    void print()
    {
           System.out.println("I am print method of Child");
    }    
}


class Test
{
         public static void main(String s[])
         {
                     Parent p=new Parent();
                     System.out.println("Executing methods of "+p.getClass());
                        p.print();

                       p=new Child();
                     System.out.println("Executing methods of "+p.getClass());

                       p.print();
         }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12679070
To understand the functionality more change the test class something like this

import java.util.*;
class Test
{
         public static void main(String s[])
         {
                     Parent p=getRandomObject();
                     System.out.println("Executing methods of "+p.getClass());
                      p.print();
         }
          public static Parent getRandomObject()
          {
                     Random r=new Random();
                     int n=r.nextInt();
                     if(n%2==0) return new Parent();
                       return new Child();
          }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Assisted Solution

by:gnoon
gnoon earned 100 total points
ID: 12679166
This may help

class ChildClass
{
    ChildClass()
    {
        StackTraceElement[] e = new Throwable().getStackTrace();
        for(int i=0; i<e.length; i++)
            System.out.println(e[i].getClassName()+"."+e[i].getMethodName()+" was called.");
    }
}

class ParentClass
{
    ParentClass()
    {
    }
    void parentMethod()
    {
        ChildClass c = new ChildClass();
    }
}

public class TestClass
{
    public static void main(String[] args)
    {
        ParentClass p = new ParentClass();
        p.parentMethod();
    }
}
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12679209
The way I always think about when hearing this question is the stack-trace.  Throw an exception in your code, then catch it and get the stack trace.  

try{
     throw new MyDummyException();
     }catch(MyDummyException mde){  // or any exception, just good to make your own
       String caller = parseStackMethod(mde, "ThisMethodName");
      }

// rest of code.....

public String parseStackMethod(MyDummyException e, String summoninMethod){
   String s = "";
   StackTraceElement[] elements = e.getStackTrace();
   for(int i =0; i<elements.length; i++){
     // most likely the first element should be the one thrown, but just to be sure allow for differences
     if(elements[i].getClassName().equals(this.getClass().getName()) && elements[i].getMethodName().equals(summoninMethod)){
        // have found the method & class where exception is thrown now take the next element for the class you are looking for
        if((i+1)<elements.length){
           s = elements[i].getMethodName();
           break;
      }
   }
  return s;
}


or something like that
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 12679219
gnoon...I see that you had similar thoughts as I, just a bit faster on the typing than myself.  
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question