java - How to use Method Reference operator to get the reference of method -
i want reference annotation value in 1 class class. here -
class { private static final string static_string = b.class.getmethod("mymethod").getannotation(someannotation.class).name(); }
the problem here cannot use syntax because requires getmethod
wrapped in try-catch
not possible. although can use static
block create variable , gulp exception in catch
block, don't want because if in future method name changed, compiler won't throw error in static
block.
i wondering if like
private static final string static_string = (b::mymethod).getannotation(someannotation.class).name();
note: neither b
nor someannotation
source, come library.
i have 2 questions here
- is there better way value annotation, preferably without using external library google reflections or similar?
- how can use
::
instance ofjava.lang.reflect.method
? possible altogether? if yes, vice-versa possible, i.e. create supplier or mappermethod
's object?
for first question, have considered using getter?
private static string getsomeannotationname() { try { return b.class.getmethod("mymethod").getannotation(someannotation.class).name(); } catch (nosuchmethodexception e) { return null; } }
for second question, don't think getting method
::
possible. because methods got ::
represent implementations of functional interfaces, not methods. however, reverse possible. can create lambda this:
consumer<sometype> consumer = x -> { try { yourmethod.invoke(someobject, x) } catch (...) { // again have ignore exceptions here, unfortunately } };
Comments
Post a Comment