Guava 6 - Reflection Guava 对反射相关的操作提供封装。
运行时捕获类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 List <String> stringList = Lists.newArrayList();List <Integer> intList = Lists.newArrayList();boolean result = stringList.getClass().isAssignableFrom(intList.getClass());assertTrue(result); TypeToken <List<String>> stringListToken = new TypeToken <List<String>>() {};TypeToken <List<Integer>> integerListToken = new TypeToken <List<Integer>>() {};TypeToken <List<? extends Number>> numberTypeToken = new TypeToken <List<? extends Number >>() {};assertFalse(stringListToken.isSubtypeOf(integerListToken)); assertFalse(numberTypeToken.isSubtypeOf(integerListToken)); assertTrue(integerListToken.isSubtypeOf(numberTypeToken));
Invokable 对 Method 和 Constructor 的操作提供了方便的 api.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Modifier.isPublic(method.getModifiers()); !(Modifier.isPrivate(method.getModifiers()) || Modifier.isPublic(method.getModifiers())); !(Modifier.isFinal(method.getModifiers()) || Modifiers.isPrivate(method.getModifiers()) || Modifiers.isStatic(method.getModifiers()) || Modifiers.isFinal(method.getDeclaringClass().getModifiers())) invokable.isPublic(); invokable.isPackagePrivate(); invokable.isOverridable(); for (Annotation annotation : method.getParameterAnnotations()[0 ]) { if (annotation instanceof Nullable) { return true ; } } return false ;invokable.getParameters().get(0 ).isAnnotationPresent(Nullable.class);
Dynamic Proxies 1 2 3 4 5 6 7 Foo foo = (Foo) Proxy.newProxyInstance( Foo.class.getClassLoader(), new Class <?>[] {Foo.class}, invocationHandler); Foo foo = Reflection.newProxy(Foo.class, invocationHandler);
Title: Guava 6 - Reflection
Author: Jiandong
Date: 2018-08-05
Last Update: 2026-06-16
Blog Link: https://mjd507.github.io/2018/08/05/Guava-Guide-6/
Copyright Declaration: Please refer carefully, most of the content I have not fully mastered.