Skip to content

Instantly share code, notes, and snippets.

@raphw
Last active January 19, 2016 13:22
Show Gist options
  • Save raphw/5fa5a20bb64f383320b2 to your computer and use it in GitHub Desktop.
Save raphw/5fa5a20bb64f383320b2 to your computer and use it in GitHub Desktop.
Receiver type overview
public class Foo<T> {
class Bar<S> {
Bar(Foo<T> Foo.this) {}
void bar(Foo<T>.Bar<S> this) {}
}
static class Qux<S> {
void qux(Qux<S> this) {}
}
void foo(Foo<T> this) {}
static void foobar() {}
static Class<?> baz() {
class Baz<S> {
void baz(Baz<S> this) {}
}
return Baz.class;
}
public static void main(String[] args) throws Exception {
System.out.println(Foo.class.getDeclaredConstructor().getAnnotatedReceiverType().getType()); // class Foo
System.out.println(Foo.class.getDeclaredMethod("foo").getAnnotatedReceiverType().getType()); // class Foo
System.out.println(Foo.class.getDeclaredMethod("foobar").getAnnotatedReceiverType()); // null
System.out.println(Foo.Bar.class.getDeclaredConstructor(Foo.class).getAnnotatedReceiverType().getType()); // class Foo
System.out.println(Foo.Bar.class.getDeclaredMethod("bar").getAnnotatedReceiverType().getType()); // class Foo.Bar
System.out.println(Foo.Qux.class.getDeclaredConstructor().getAnnotatedReceiverType().getType()); // class Foo
System.out.println(Foo.Qux.class.getDeclaredMethod("qux").getAnnotatedReceiverType().getType()); // class Foo.Qux
System.out.println(baz().getDeclaredConstructor().getAnnotatedReceiverType().getType()); // class Foo
System.out.println(baz().getDeclaredMethod("baz").getAnnotatedReceiverType().getType()); // class Foo.Baz
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment