Skip to content

Instantly share code, notes, and snippets.

@RayStarkMC
Created October 11, 2020 07:22
Show Gist options
  • Save RayStarkMC/6f56832dc4e0b188f44a47807adf8585 to your computer and use it in GitHub Desktop.
Save RayStarkMC/6f56832dc4e0b188f44a47807adf8585 to your computer and use it in GitHub Desktop.
キャプチャメソッドの使用例
public static void main(String[] args) {
Function<?, ?> f = Function.identity();
Object obj = new Object();
// これは出来ない
// f.apply((?)obj);
//これは出来る
Object obj2 = captureMethod(f, obj);
}
@SuppressWarnings("unchecked")
private static <T, R> Object captureMethod(Function<T, R> f, Object o) {
//ここで無検査キャスト(ただしoがT型だと証明できる場合に限る!!!!)
return f.apply((T)o);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment