0

How can I get the name of a method just like I can get the name of a class? (RandomClass.class.getName())

Hardcoding won't work because the obfuscator destroys it.

Reason: I'm injecting methods like this:

        MethodNode getLocalPlayer = GetterAdapter.insert(false, true, "getLocalPlayer", "Lvanquish/accessors/Player;", "client", "yD", "LQZ;");
        classNode.methods.add(getLocalPlayer);

        //class client implements my interface which contains the method getLocalPlayer

      public interface Client {

         public int[] getPlayerIndices();

         public Player getLocalPlayer();

         public Player[] getPlayers();

         public int getBaseX();

         public int getBaseY();

         public int getCameraX();

         public int getCameraY();

      }

        //when I obfuscate my files getLocalPlayer get's a name like a2
        //when you look above you see that the method name was hard code and so
        // will it create an error

Declared method wont work here, because I don't know the method name and there are like 4 vars of the same type.

Would this work or would it be a mess?

    @DataMap.varDetails(name = "getPlayerIndices")
public int[] getPlayerIndices();
user2997204
  • 1,344
  • 2
  • 12
  • 24
  • 3
    You can throw an exception, catch it and look at the stack trace. Just kidding, don't... – Eran Dec 22 '13 at 14:13
  • http://stackoverflow.com/questions/3864175/how-to-get-the-name-of-method-in-current-class?rq=1 – MGorgon Dec 22 '13 at 14:13
  • 1
    Why do you need to? (This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem).) – Oliver Charlesworth Dec 22 '13 at 14:15
  • Sorry, but could you say something more about why you don't want to use `getDeclaredMethod()`? – Pshemo Dec 22 '13 at 14:15
  • @Pshemo OP wants to find out a method's name, not find a method by a known name. – Marko Topolnik Dec 22 '13 at 14:17
  • 1
    @eran Your suggestion is actually the best way, IMO. Just one thing: no need to *throw* an exception, *creating* is all it takes. – Marko Topolnik Dec 22 '13 at 14:19
  • Often reflection is used where it isn't actually needed. Perhaps instead you could wrap each Client in some common interface, such as PlayerGetter? – VGR Dec 22 '13 at 14:50

2 Answers2

0

You would need each of the methods you want to refer to reflectively do something like this:

static String baseXName;

public int getBaseX() {
  if (baseXName == null)
    baseXName = detectMyName();
  ... the actual code of the method ...
}

and detectMyName() would, as @eran suggests in the first comment to your question, instantiate an Exception and retrieve the actual runtime name of the calling method from its stacktrace. You can avoid a tiny bit of overhead by obtaining the stacktrace without instantiating exception if you call Thread.currentThread().getStackTrace().

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

you can try this:

Thread.currentThread().getStackTrace();

it show you the stack of your calling method in current thread.
Best of luck :)