Name of the method.

Good day,
Within a class, the name of the class can bet determined via a call to the "getClass().getName()" method, right ?
How do we get the name of the method itself ?
================
class magic {
magician () {
xx = getClass().getName(); // --> Will give "magic"
// How do we get "magician" ?
================
Regards

Good day,
By all means, I'm far from being an expert or even being to point to consider myself "at ease" with Java.
Simply put, I have few applets sources that are using the "simple" things such as the following :
"public boolean mouseEnter(Event Event1, int int2, int int3)"
When compiled with SDK v1.3, there is a mention that the API is deprecated. I looked into the deprecated APIs and functions documentation, and I must admit, I'm completely lost in that jungle!
What I would like to see/have, is how it is being done using the "new" or "proper" way. The lack of basic real and simple examples is a tad irritating, at least for me.
As far as I can see, the Java language is not quite easy to learn for self-training people and taking Java classes is not part of my budget for now.
The above is only on example.
Anyhow, as far as getting the "Method Name", I guess the idea of adding it to my //#INC line would be the easiest and most performance effective way.
As far as the doclet approach with JavaDoc, I think this is a good idea is good, however I'm far from being at the stage of creating such thing.
Adding a new "throwable()" and getting the stacktrace is something to consider, however for the moment being I just want to apply the KISS rule, so I'm not throwing that idea away. ;-)
I'll look at the different samples that came with the JDK, however I would appreciate if some of you could provide me with links to simple samples that are "respecting" the JDK v1.3/v1.4 calling conventions, would that be for the mouse example mentioned above, or some other of those "deprecated" things.
I know for a fact that some of them are requiring a simply syntax adjustment, I can easily cope with that. Some others are a bit "fancier" to adapt, such as the alternate approach to eliminate a call to the "threadname.stop()" method. That one was easy to do simply because there was an example in the docs, that I was lucky enough to find. For some others, I simply need help to figure it out, so this is why I'm enquiring for some assistance and help.
Best regards.

Similar Messages

  • How can i get the name of the method or exception handle

    i just start to programm.
    i want to handle exceptions.
    there is a try catch procedure.
    i want that the catch procedures tells me which method has occured the exception.
    so i thought i could handle it like this
    public void getAllConstraints(Connection _conn){
    try{
    catch (SQLException e){
    handleException("SQLException", e);
    public void handleException(String exceptionName,
    Exception e){
    System.out.println(exceptionName + " occured in method " +
    GETMETHODENNAME);
    System.out.println("Exception name: ");
    System.err.println(e);
    the exceptionname should say for example SQLException, IOException or somthing like this.

    You can use the getStackTrace() method:
    StackTraceElement stack[] = new Exception().getStackTrace();
    // stack[0] is the current method
    // stack[1] is the calling method
    // etc.

  • What's the method name for convert integer to ASCII

    Hi, all:
    I need a method which can convert the integer value to the ASCII character. I just can't find this kind of method. If someone know the name of the method or where I need to find it, please tell me. Thanks in advance!
    he

    Its most simple to use a cast:
    int someValue;
    char someCharacter;
    someValue = 65;
    someCharacter = (char) someValue;
    System.out.println (someCharacter);
    this should display 'A' if I'm not mistaken.
    Jon

  • How is the Method Parameter Identified in this Code Snippet ?

    package methods;
    public class Me10 {
         public static void main(String[] args) {
              short y=6;
              long z=7;
              go(y);
              go(z);
         public static void go(Short s)
              System.out.println("SHORT");
         public static void go(Long l)
              System.out.println("LONG");
         public static void go(int i)
              System.out.println("INT");
    O/P :
    INT
    LONGEven when y is declared as short why does the INT method get called ?
    What can we do so that when i pass y , short method will be called ?
    Thabks in Advance.

    as far as I understand, method parameters are identified per JLS section 15.12.2 'Determine Method Signature':
    "...This step uses the name of the method and the types of the argument expressions to locate methods that are both _accessible_ and applicable, that is, declarations that can be correctly invoked on the given arguments. There may be more than one such method, in which case the _most specific_ one is chosen. The descriptor (signature plus return type) of the most specific method is one used at run time to perform the method dispatch...
    ...The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error..." [...click here to continue reading if you're interested|http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2|JLS 15.12.2 'Determine Method Signature']
    As for calling 'Short' method for 'y' value, I'd probably use something like go(new Short(y))

  • How do I alter the bytes of a Class file to add calls to the methods?

    If i had the bytes of a class file, and I wanted to alter the bytes that constitute each method for the class so that it included a call to the security manager, how would i do it?
    1. How would I know which bytes were the opening of a method?
    2. how would I know what the name of the method is?
    3. How would I create bytes for something like:
       SecurityManager sm = System.getSecurityManager().checkPermission(thismeth, subject);
    4. I assume that if by some miracle I can do the above, then all I have to do is call defineClass(...) in ClassLoader and send it the new bytes, right?
    Thanks to all!

    OK, if it will help anyone get me the answers here, I found a class on the internet that can read a class file and tell you where in the bytes a method occurs and what its name is, and how long it is. What I need now is how to convert a call into the correct manner of bytes.
    For example, so I could add the bytes that would do:
       System.out.println("Added!");
    The class that reads a class file:
    /* Inspector.java by Mark D. LaDue */
    /* June 24, 1997 */
    /* Copyright (c) 1997 Mark D. LaDue
       You may study, use, modify, and distribute this example for any purpose.
       This example is provided WITHOUT WARRANTY either expressed or implied.  */
    /* This Java application analyzes the entries in the constant pool and locates
       the code arrays in a Java class file. Each entry in the constant pool
       yields the following information:
       Index     Tag     Reference(s)/Value(s)
       where "Index" is its position within the class file's constant pool,
       "Tag" is the official tag number for that type of entry, and
       "Reference(s)/Value(s)" contains the constant pool information
       according to the entry's type.  (See Lindholm and Yellin's "The Java
       Virtual Machine Specification" for details.)  For each code array in
       the class file, its starting byte, its total length, and the name of
       the method in which it occurs are given.  Combining this information
       with the information yielded by the humble "javap" utility gives one
       sufficient information to hack the code arrays in Java class files. */
    import java.io.*;
    class Inspector {
        public static void main(String[] argv) {
            int fpointer = 8; // Where are we in the class file?
            int cp_entries = 1; // How big is the constant pool?
            int Code_entry = 1; // Where is the entry that denotes "Code"?
            int num_interfaces = 0; // How many interfaces does it use?
            int num_fields = 0; // How many fields are there?
            int num_f_attributes = 0; // How many attributes does a field have?
            int num_methods = 0; // How many methods are there?
            int num_m_attributes = 0; // How many attributes does a method have?
            int[] tags; // Tags for the constant pool entries
            int[] read_ints1; // References for some constant pool entries
            int[] read_ints2; // References for some constant pool entries
            long[] read_longs; // Values for some constant pool entries
            float[] read_floats; // Values for some constant pool entries
            double[] read_doubles; // Values for some constant pool entries
            StringBuffer[] read_strings; // Strings in some constant pool entries
            int[] method_index;
            long[] code_start;
            long[] code_length;
    // How on earth do I use this thing?
            if (argv.length != 1) {
                System.out.println("Try \"java Inspector class_file.class\"");
                System.exit(1);
    // Start by opening the file for reading
            try {
                RandomAccessFile victim = new RandomAccessFile(argv[0], "r");
    // Skip the magic number and versions and start looking at the class file
                victim.seek(fpointer);
    // Determine how many entries there are in the constant pool
                cp_entries = victim.readUnsignedShort();
                fpointer += 2;
    // Set up the arrays of useful information about the constant pool entries
                tags = new int[cp_entries];
                read_ints1 = new int[cp_entries];
                read_ints2 = new int[cp_entries];
                read_longs = new long[cp_entries];
                read_floats = new float[cp_entries];
                read_doubles = new double[cp_entries];
                read_strings = new StringBuffer[cp_entries];
    //Initialize these arrays
                for (int cnt = 0; cnt < cp_entries; cnt++) {
                    tags[cnt] = -1;
                    read_ints1[cnt] = -1;
                    read_ints2[cnt] = -1;
                    read_longs[cnt] = -1;
                    read_floats[cnt] = -1;
                    read_doubles[cnt] = -1;
                    read_strings[cnt] = new StringBuffer();
    // Look at each entry in the constant pool and save the information in it
                for (int i = 1; i < cp_entries; i++) {
                    tags[i] = victim.readUnsignedByte();
                    fpointer++;
                    int skipper = 0;
                    int start = 0;
                    int test_int = 0;
                    switch (tags) {
    case 3: read_ints1[i] = victim.readInt();
    fpointer += 4;
    break;
    case 4: read_floats[i] = victim.readFloat();
    fpointer += 4;
    break;
    case 5: read_longs[i] = victim.readLong();
    fpointer += 8;
    i++;
    break;
    case 6: read_doubles[i] = victim.readDouble();
    fpointer += 8;
    i++;
    break;
    case 7:
    case 8: read_ints1[i] = victim.readUnsignedShort();
    fpointer += 2;
    break;
    case 9:
    case 10:
    case 11:
    case 12: read_ints1[i] = victim.readUnsignedShort();
    fpointer += 2;
    victim.seek(fpointer);
    read_ints2[i] = victim.readUnsignedShort();
    fpointer += 2;
    break;
    // This is the critical case - determine an entry in the constant pool where
    // the string "Code" is found so we can later identify the code attributes
    // for the class's methods
    case 1: skipper = victim.readUnsignedShort();
    start = fpointer;
    fpointer += 2;
    victim.seek(fpointer);
    for (int cnt = 0; cnt < skipper; cnt++) {
    int next = victim.readUnsignedByte();
    switch (next) {
    case 9: read_strings[i].append("\\" + "t");
    break;
    case 10: read_strings[i].append("\\" + "n");
    break;
    case 11: read_strings[i].append("\\" + "v");
    break;
    case 13: read_strings[i].append("\\" + "r");
    break;
    default: read_strings[i].append((char)next);
    break;
    victim.seek(++fpointer);
    victim.seek(start);
    if (skipper == 4) {
    fpointer = start + 2;
    victim.seek(fpointer);
    test_int = victim.readInt();
    if (test_int == 1131373669) {Code_entry = i;}
    fpointer = fpointer + skipper;
    else {fpointer = start + skipper + 2;}
    break;
    victim.seek(fpointer);
    // Skip ahead and see how many interfaces the class implements
    fpointer += 6;
    victim.seek(fpointer);
    num_interfaces = victim.readUnsignedShort();
    // Bypass the interface information
    fpointer = fpointer + 2*(num_interfaces) + 2;
    victim.seek(fpointer);
    // Determine the number of fields
    num_fields = victim.readUnsignedShort();
    // Bypass the field information
    fpointer += 2;
    victim.seek(fpointer);
    for (int j=0; j<num_fields; j++) {
    fpointer += 6;
    victim.seek(fpointer);
    num_f_attributes = victim.readUnsignedShort();
    fpointer = fpointer + 8*(num_f_attributes) + 2;
    victim.seek(fpointer);
    // Determine the number of methods
    num_methods = victim.readUnsignedShort();
    fpointer += 2;
    // Set up the arrays of information about the class's methods
    method_index = new int[num_methods];
    code_start = new long[num_methods];
    code_length = new long[num_methods];
    //Initialize these arrays
    for (int cnt = 0; cnt < num_methods; cnt++) {
    method_index[cnt] = -1;
    code_start[cnt] = -1;
    code_length[cnt] = -1;
    // For each method determine the index of its name and locate its code array
    for (int k=0; k<num_methods; k++) {
    fpointer += 2;
    victim.seek(fpointer);
    method_index[k] = victim.readUnsignedShort();
    fpointer += 4;
    victim.seek(fpointer);
    // Determine the number of attributes for the method
    num_m_attributes = victim.readUnsignedShort();
    fpointer += 2;
    // Test each attribute to see if it's code
    for (int m=0; m<num_m_attributes; m++) {
    int Code_test = victim.readUnsignedShort();
    fpointer += 2;
    // If it is, record the location and length of the code array
    if (Code_test == Code_entry){
    int att_length = victim.readInt();
    int next_method = fpointer + att_length + 4;
    fpointer += 8;
    victim.seek(fpointer);
    code_length[k] = victim.readInt();
    code_start[k] = fpointer + 5;
    fpointer = next_method;
    victim.seek(fpointer);
    // Otherwise just skip it and go on to the next method
    else {
    fpointer = fpointer + victim.readInt() + 4;
    victim.seek(fpointer);
    // Print the information about the Constant Pool
    System.out.println("There are " + (cp_entries - 1) + " + 1 entries in the Constant Pool:\n");
    System.out.println("Index\t" + "Tag\t" + "Reference(s)/Value(s)\t");
    System.out.println("-----\t" + "---\t" + "---------------------\t");
    for (int i = 0; i < cp_entries; i++) {
    switch (tags[i]) {
    case 1: System.out.println(i + "\t" + tags[i] + "\t" + read_strings[i].toString());
    break;
    case 3: System.out.println(i + "\t" + tags[i] + "\t" + read_ints1[i]);
    break;
    case 4: System.out.println(i + "\t" + tags[i] + "\t" + read_floats[i]);
    break;
    case 5: System.out.println(i + "\t" + tags[i] + "\t" + read_longs[i]);
    break;
    case 6: System.out.println(i + "\t" + tags[i] + "\t" + read_doubles[i]);
    break;
    case 7:
    case 8: System.out.println(i + "\t" + tags[i] + "\t" + read_ints1[i]);
    break;
    case 9:
    case 10:
    case 11:
    case 12: System.out.println(i + "\t" + tags[i] + "\t" + read_ints1[i] + " " + read_ints2[i]);
    break;
    System.out.println();
    // Print the information about the methods
    System.out.println("There are " + num_methods + " methods:\n");
    for (int j = 0; j < num_methods; j++) {
    System.out.println("Code array in method " + read_strings[method_index[j]].toString() + " of length " + code_length[j] + " starting at byte " + code_start[j] + ".");
    System.out.println();
    // All the changes are made, so close the file and move along
    victim.close();
    } catch (IOException ioe) {}

  • How to get the method names in a class

    hi  friends,
    Could any of you tell me the report or function module  to display all the method names in a given class.
    thanks in advance.
    regards,
    kumar

    Hi Kumar ,
    Open ur class in SE24 transaction ,there is a tab for methods ..
    u'll get the list of methods form there ... n in source u'll get their operative details
    Regards
    Renu

  • How to get the name of a method?

    How can I get the name of a method, before measure its time of execution?

    [url
    http://java.sun.com/developer/JDCTechTips/2003/tt0318.
    html#2]DISCOVERING THE CALLING METHOD NAMEThis article uses Throwable.getStackTrace() which has been around longer than the one I mentioned (Thread.getStackTrace()):
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Throwable.html#getStackTrace()
    I actually meant to show Throwable.getStackTrace() in my original post but when I did a search on it Thread's was the first one I came across.

  • Getting the name of the executing method

    Hello,
    I'm looking for a way to get, in one method, an String with its name. For example, if the method's name is "testOne", I'm looking for getting a String with the name "testOne" in a sentence of the method.
    Is there any way?
    Thx

    Is this a good way to get it?
    StackTraceElement pila[]=Thread.currentThread().getStackTrace();
    String methodName=pila[pila.length-1].getMethodName();
    There may be a lot of threads in my program, and I must be sure that this will work fine in all situations.

  • How to get the current class name in static method?

    Hi,
    I'd like to get the current class name in a static method. This class is intended to be extended. So I would expect that the subclass need not to override this method and at the runtime, the method can get the subclass name.
    getClass() doesn't work, because it is not a static method.
    I would suggest Java to make getClass() static. It makes sense.
    But in the mean time, does anybody give an idea to work around it?
    Thank you,
    Bill

    Why not create an instance in a static method and use getClass() of the instance?
    public class Test {
       public static Class getClassName() {
          return new Test().getClass();

  • Get the method name

    hi
    can anyone know how to get the name of method from its catch block.
    For example
    i have a class called Hello.java
    and inside that a method called display();
    and i have a written the code inside a try catch block.so when an exception occurs i want to create a log file
    i got the class name using this.getClass().getSimpleName()
    so is there any way to get the method name , where the catch is written. (here in example i should get it as "display")
    plz help me
    im in great need.
    hope u understood my doubt.
    rgds

    Use a logging framework, and name the loggers well & use meaningful messages?
    Or create a Throwable, [fill in|http://java.sun.com/javase/6/docs/api/java/lang/Throwable.html#fillInStackTrace()] the stack trace, get the required StackTraceElement and [get the method|http://java.sun.com/javase/6/docs/api/java/lang/StackTraceElement.html#getMethodName()] from this.

  • How can i get the name of a method

    i have a method which is calling another method.
    how can this submethod get the name from the main method?
    Thanks
    Thorsten

    There was a post about this a while ago, but I can't find it.
    Check out the getStackTrace method of Throwable (not the printStackTrace method). getStackTrace returns a StackTraceElement array. Each StackTraceElement has a getMethodName() method. In the returned array, the StackTraceElement at index 0 is the stack frame that the Throwable was created in, and the element at index 1 is the stack frame that your method was called from.
        public String getCurrentMethodName () {
            StackTraceElement[] st = (new Throwable()).getStackTrace();
            // Index 0 is the stack frame of "getCurrentMethodName"
            // Index 1 is the stack frame of the method that called this.
            // Index 2 is the stack frame of the method that called THAT.
            // Note that st[1] should always exist because this method
            // will always be called from another method (main, at very
            // least).  
            return st[1].getMethodName();
        public void myMethod () {
            System.out.println("The name of this method is " + getCurrentMethodName());
        };Hope that helps. I didn't test it, but it should work.
    Jason

  • Where will i get the method code and method name in Bpc

    Dear Experts,
    Thanks for watching this thread at the time of creating Consolidation methods they have
    NEW
    HOLDING
    GLOBAL
    PROPORTIONAL
    EQUITY
    lEAVING (END OF THE YEAR)
    LEAVING(DURING THE YEAR)
    Where will i get the method Code and method name.
    Can you please guide me.
    Regards,
    Srinivasan.

    The consolidation methods types you have listed are the standard ones  provided by BPC for Consolidation Method types.
    You can set the Consolidation Methods in the BPC Administration Console under Business Rules Library -> Consolidation Methods.
    There you can see some defaults - you can remove these and set your own if you prefer.
    You should read the BPC340 Training guide.

  • What is the method name?

    Hello:
    I understood that i have to override the paintComponent method.
    But my question is that, what is the method declaration by which shall i set the background image? where image name is winter.jpg
    I will be highly grateful to u if u kindly give me the proper answer.

    The answer was already given to you in your other post.
    icewalker2g gave you this site http://www.jguru.com/faq/view.jsp?EID=9691
    The code:
    import javax.swing.*;
    import java.awt.*;
    public class BackgroundSample {
      public static void main(String args[]) {
        JFrame frame = new JFrame("Background Example");
        final ImageIcon imageIcon = new ImageIcon("draft.gif"); // <--- change this image to your image
        JTextArea textArea = new JTextArea() {
          Image image = imageIcon.getImage();
          Image grayImage = GrayFilter.createDisabledImage(image);
          {setOpaque(false);}  // instance initializer
          public void paintComponent (Graphics g) {
            g.drawImage(grayImage, 0, 0, this);
            super.paintComponent(g);
        JScrollPane scrollPane = new JScrollPane(textArea);
        Container content = frame.getContentPane();
        content.add(scrollPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(3);
        frame.setSize(250, 250);
        frame.setVisible(true);
    }In the future please read your post and don't multipost and crosspost.

  • Junit : How can I get the method name (say testMyAddress) that failed

    My code is below
              TestSuite masterSuite = new TestSuite(testClass);
              TestResult result = new TestResult();
              masterSuite.run(result);
                   Enumeration errors = result.errors();
                   while (errors.hasMoreElements())
                        TestFailure error = (TestFailure)errors.nextElement();
                        Test test = error.failedTest();
    /*will give me the class name but how can I get the method that threw the exception.
    I can get fName (that contains the method name) field to get the method,but being private I cannot hold of the field.
    Wondering if there is any easy way to get the method name that threw exception,without writing unneccessary code
                        Class c1 = test.getClass();
    thx
    m

    getting all methods is no good!
    My test class looks like this
    MyTestClass{
    testGoodData(){
    asserttrue(.....);
    testBadData(){
    asserttrue(.....);
    testNullData(){
    asserttrue(.....);
    someHelperMethod(){
    thx
    m

  • How to find the parameters names of a method  using reflection?

    Hi,
    Using the function getParameterTypes(), it is possible to find all parameter types from a Method. However, is there a way to find all parameters NAMES of a Method?
    If the asnwer is no, is there an easy way to obtain this information using the java source code (I mean, that doesn't need to create a parser)?
    Thanks

    The same way. Primitive types, even though they do not extend java.lang.Object, and are not objects, still have an associated Class object

Maybe you are looking for

  • How to UPGRADE for FREE from Snow Leopard yo Lion

    Bought a new iMac on 18/11/2011. Came with Operating System 10.6.8 Have now found out that I should have had the Lion OS installed by updating for free. Have tried for days now but no luck, I still have to pay for upgrading. Does anybody out there ha

  • Lightroom 5.4 to Lightroom Mobile sync is soooo slow - why no local sync?

    I am testing the new Lightroom 5.4 with Lightoom Mobile. What really annoys me is the fact that all photos are going up to the Creative Cloud. I have private pictures which should be private. Always. No upload. No Cloud sync. It is important! Speed i

  • Why won't my MacBook connect to my wireless internet at home?

    I'll try to give as many details as I can: I'm a freshman in highschool & have a MacBook from my school that I use for the year. It's just a regular MacBook, not an air or anything. In class the other day, my computer froze up completely, so I used t

  • Calibrating my Monitor question

    Hello everyone I am trying to calibrate my JVC monitor i am following the instructions to a link mentioned in one of the threads i generated a color bar in finalcut and brought it to the timeline i see it on my monitor, i turned the chroma all the wa

  • FS10N & FBL3N mismatch

    Hi all, I guess this is a common question asked in this forum. But I am asking this questoin just for confirmation . My system is SAP 4.7 Sup.Pack 29. Initially FS10N and FBL3N used to show correct values. But when I do the Archiving of FI_DOCUMNT ob