3

So if I were logging and wanted to reference a class, it might not be a bad idea to use something like this:

log.warn("I did not find an instance of class "+SomeClass.class.getName());

This way a refactor would be guaranteed to update the string to use the correct name.

It would be really neat to be able to do this with Methods. You can't with Java 7, but perhaps something based on java 8 would work, like this:

log.warn("You forgot to call "+(SomeClass::aMethod).getName()+" before calling this method");

--it WOULD work great if SomeClass::aMethod was actually a method reference and not a Lambda.

Does anyone know of a way to make this work, perhaps extract the original method name from the lambda?

Bill K
  • 62,186
  • 18
  • 105
  • 157
  • Or https://stackoverflow.com/questions/2009732/type-safe-method-reflection-in-java – Savior Feb 08 '18 at 22:21
  • Or https://stackoverflow.com/questions/9864300/how-to-get-method-object-in-java-without-using-method-string-names – Savior Feb 08 '18 at 22:21
  • Or https://stackoverflow.com/questions/34121544/java-get-compile-time-safe-method-name – Savior Feb 08 '18 at 22:21
  • Or https://stackoverflow.com/questions/6960070/getting-a-method-object-for-a-class-with-compile-time-checking-in-java – Savior Feb 08 '18 at 22:22
  • 1
    "Easily" is debatable. [`Throwable#getStatckTrace`](https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#getStackTrace--) will generate a current stack trace, which you can use to extract the class and method names of the current call stack - this is not a cheap call – MadProgrammer Feb 08 '18 at 22:24
  • 1
    Are you using a `Logging` API like `Log4J` or the inbuilt logging API? Because they provide this functionality already – MadProgrammer Feb 08 '18 at 22:26
  • 1
    @MadProgrammer I don't think they want the current executing method necessarily. I think they want a compile time, type safe way to refer to a method. – Savior Feb 08 '18 at 22:28
  • @Pillar I'm glad someone knows what they want - they why not simply just use `Reflection`- but *"This way a refactor would be guaranteed to update the string to use the correct name."* won't work, as the method name would have changed to something that can't be referenced :P – MadProgrammer Feb 08 '18 at 22:30
  • I do not want the currently running method name, I want to put in a reference to a second method using java 8's closure syntax... I could not find a matching answer in the supplied answers yet, but I'm still looking. All those questions, so far, seem to predate java 8 – Bill K Feb 08 '18 at 22:32
  • It's not my level and I don't have a full understanding of that topic, but I think that here is an attempt to get a result you try to achieve: http://in.relation.to/2016/04/14/emulating-property-literals-with-java-8-method-references/ You might also want to take a look at comments section there (Thomas Darimont attempt). – Przemysław Moskal Feb 08 '18 at 23:15
  • @PrzemysławMoskal Thank you, that was a perfect answer for this question--why it's difficult and even an attempt to overcome the problem (Not practical, but a great example). I really appreciate it. – Bill K Feb 09 '18 at 18:32
  • @BillK Great that I could help some way, even though I don't understand much of what is going on there, I've seen that a result that they achieved there could be somehow similar to what you are looking for :-) – Przemysław Moskal Feb 09 '18 at 18:43
  • @PrzemysławMoskal The answer proposed that a method reference have the same abilities as a .class which is exactly what I was wishing for, and confirmed that it is unattainable in current java. The comment had an amazing hack that let you get at meta-data and get what I want, but it's severely limited to method types that have a predefined interface, but that's exactly the type of hack I was asking about (even though it's not quite enough...). Anyway, you'd have gotten the accepted answer if I hadn't been "Duped" – Bill K Feb 09 '18 at 19:31
  • @BillK Great to hear that, your comment clears this topic a bit for me :) Feel free to provide an answer to questions listed here as duped by your question, as it might be helpful for others looking for an answer there. Your answer will be much more valuable than my attempt as you are much more experienced than I am. Just let me know some way, that you've provided an answer to give me a chance to upvote it. Have a nice day! – Przemysław Moskal Feb 09 '18 at 22:51

0 Answers0