Methref
Methref is a tool that gives you strong-type references to method names.
Sometimes you need to refer to a method in our code using their names. Usually, in this cases, method names are stored as strings and they are not immune to method name changes and typing errors.
Methref is a tiny and cool utility, build on Proxetta, that provides strongly-typed references to method names. Here is how it works.

Simple way

1
Methref.of(Str.class).name(Str::boo);
2
// returns String: 'boo'
Copied!
First, we create Methref object and call method name on method reference. Could it be simpler?
If a method has arguments, just pass whatever: usually null or zeros:
1
Methref.of(Str.class).name((str) -> str.hello(null, 0));
2
// returns 'hello'
Copied!
Two important things to remember:
  1. 1.
    Although there is a target method invocation, method code is NOT
    executed! Proxy just returns null or a method name if methods return type is a String.
  2. 2.
    Create new Methref where needed. However, they can be stored and reused.

Separate way

With Methref you can actually do a lot of magic :) Previous, simple usage can be split into several steps:
1
Methref m = Methref.of(Str.class);
2
​
3
// get proxy
4
Str str = m.proxy();
5
​
6
// invoke method
7
str.foo();
8
​
9
// get invoked method name
10
String name = m.lastName();
Copied!
This approach is made for cases when method invocation happens in different place of your code, where you don't want to expose Methref.

Detect Methref

Imagine that you are creating several Methref in one place of code. Then, a user of your code will call a method on one of those proxies, but you don't have control over which. There is a way to detect the Methref from the proxy by calling method isMyProxy. Furthermore, there is a static method lastName(instance) that works like the Methref#lastName().

Where this can be used?

As I said, it can be used for magic :) Believe it or not, with Methref you can create, for example, strongly typed SQL-alike syntax directly with your code.
Enjoy!
Last modified 8mo ago