This question already has answers here:
How to make a lambda expression define toString in Java 8?
(5 answers)
Naming(toString) Lambda-Expressions for Debugging purpose
(2 answers)
Closed 1 year ago.
In our application we have a couple of utility methods that take lambdas as argument to execute misc. activities and among them they then call the passed lambdas.
During development we occasionally had errors in the lambdas, so we emit the lambdas as part of the error message. The default toString() of a lambda at least emits the classname (which is already a BIG help! Otherwise we would never have found some of the issues) but only with a cryptic suffix e.g. "classname_here$$Lambda$2497/0x0000000800e75c40#463f3a95".
For better error logging it would be very helpful if one could give a lambda a more telling name. Is that somehow possible? Can one assign lambdas a name or some "human readable" identification? For classes that contain LOTS of lambdas it can be pretty hard to find out, WHICH lambda caused the issue, so that would be helpful.
Related
This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 3 years ago.
I've got a constructor that takes a parameter ArrayList<Setting> settings.
Now the problem is that I wrote the following call in a subclass:
super(new ArrayList<Setting>(){new Setting("", this, 0)});
This causes a lot of errors, the main one being Invalid method declaration; return type required, as well as '{' or ';' expected, Parameter expected, Unexpected token and Constructor Setting() is never used.
I tried switching to using regular arrays and it worked fine:
super(new Setting[]{new Setting("Exp Only", this, false)});
For now, I'm happy just using regular arrays, however I come across this error rather frequently, is there something I'm doing wrong or is this just the way it is, and if so, why?
Java has introduced of method since java 9. You can use it as below.
super(List.of(new Setting("Exp Only", this, false)));
This question already has answers here:
Which types can be used for Java annotation members?
(4 answers)
Closed 4 years ago.
Can I able to call a method which returns string inside an annotation.
If so please guide me how to achieve this?
I tried like this but this doesn't work for me.
#Description(value = Resource.getWord("key"))
An annotation only takes compile time constants (as they might be used during compile time), therefore you cannot make any calculation within the definition, as they are unknown during the compile time.
Allowed constant types are (taken from java-annotation-members):
Primitive
String
Class
Enum
Another Annotation
An array of any of the above
Possible solution for your situation:
As I understand you would like to localize the #Description content.
As this is only meant to be exposed to other developers anyway, you are safe to simply use English, in my opinion. Localization is for the end user, not the developer.
I can imagine an aspect being wired up to process methods annotated like this, where the "key" is in the annotation, and the aspect processing then uses the key at run time... but I'm not sure this is what you're looking for.
This question already has answers here:
Where can I find the Java JDK source code? [closed]
(11 answers)
Closed 6 years ago.
In java, is there any way to access the methods to classes that already you import? For example, is there a way to view the code for all the methods used for arrays? Such as the constructors, add(), remove(), size()? I have checked oracle, but there is no code, only method names and parameters. I understand how the methods work, but i'd like to see the actual code used.
Search for the JDK source code, depending on the version you want.
This question already has answers here:
Crossed out imported java packages
(2 answers)
Closed 8 years ago.
I tried to call the method .getHours() and Eclipse marks the name of a function with a line.
Why is this happening?
The method is deprecated, e.g., annotated with #Deprecated.
It means you probably shouldn't be using it, since support may disappear in the future.
That mark means that the method has been deprecated. This means the author of the code intends you to avoid that method, but doesn't remove it because of compatibility.
If you check the javadoc is possible the author has left documented an alternative way to perform the behavior.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Using Java Lib with Scala Reserved Words
I'm experimenting with Scala, and a Java library I'm using has a with method on one of its objects, but with is a keyword in Scala. How do I call this method from my Scala code?
From http://ofps.oreilly.com/titles/9780596155957/TypeLessDoMore.html#ReservedWords
Some Java methods use names that are reserved by Scala, e.g., java.util.Scanner.match. To avoid a compilation error, surround the name with single back quotes, e.g., java.util.Scanner.‵match‵.
(edited for formatting)