com.blankj.utilcode.util.ReflectUtils Java Examples
The following examples show how to use
com.blankj.utilcode.util.ReflectUtils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void testProxy() { assertEquals("abc", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(0)); assertEquals("bc", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(1)); assertEquals("c", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(2)); assertEquals("a", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(0, 1)); assertEquals("b", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(1, 2)); assertEquals("c", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(2, 3)); assertEquals("abc", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(0)); assertEquals("bc", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(1)); assertEquals("c", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(2)); assertEquals("a", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(0, 1)); assertEquals("b", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(1, 2)); assertEquals("c", ReflectUtils.reflect((Object) "abc").proxy(Test9.class).substring(2, 3)); }
Example #2
Source File: GlideHook.java From DoraemonKit with Apache License 2.0 | 6 votes |
/** * hook requestListeners字段 * * @param singleRequest * @return */ public static void proxy(Object singleRequest) { try { List<RequestListener> requestListeners = null; if (singleRequest instanceof SingleRequest) { requestListeners = ReflectUtils.reflect(singleRequest).field("requestListeners").get(); } //可能存在用户没有引入okhttp的情况 if (requestListeners == null) { requestListeners = new ArrayList<>(); requestListeners.add(new DokitGlideRequestListener()); } else { requestListeners.add(new DokitGlideRequestListener()); } if (singleRequest instanceof SingleRequest) { ReflectUtils.reflect(singleRequest).field("requestListeners",requestListeners); } } catch (Exception e) { e.printStackTrace(); } }
Example #3
Source File: PicassoHook.java From DoraemonKit with Apache License 2.0 | 6 votes |
/** * 注入到com.squareup.picasso.Request 构造方法中 */ public static void proxy(Object request) { try { if (request instanceof Request) { Request requestObj = (Request) request; List<Transformation> transformations = requestObj.transformations; if (transformations == null) { transformations = new ArrayList<>(); transformations.add(new DokitPicassoTransformation(requestObj.uri, requestObj.resourceId)); } else { transformations.clear(); transformations.add(new DokitPicassoTransformation(requestObj.uri, requestObj.resourceId)); } ReflectUtils.reflect(request).field("transformations", transformations); } } catch (Exception e) { e.printStackTrace(); } }
Example #4
Source File: TencentLocationListenerProxy.java From DoraemonKit with Apache License 2.0 | 6 votes |
@Override public void onLocationChanged(TencentLocation tencentLocation, int i, String s) { if (GpsMockManager.getInstance().isMocking()) { try { //tencentLocation 的 对象类型为TxLocation //LogHelper.i(TAG, "matched==onLocationChanged==" + tencentLocation.toString()); //b 为fb类型 Object b = ReflectUtils.reflect(tencentLocation).field("b").get(); //a 为lat ReflectUtils.reflect(b).field("a", GpsMockManager.getInstance().getLatitude()); //b 为lng ReflectUtils.reflect(b).field("b", GpsMockManager.getInstance().getLongitude()); //LogHelper.i(TAG, "b===>" + b.toString()); } catch (Exception e) { e.printStackTrace(); } } if (tencentLocationListener != null) { tencentLocationListener.onLocationChanged(tencentLocation, i, s); } }
Example #5
Source File: PlatformHttpHook.java From DoraemonKit with Apache License 2.0 | 6 votes |
/** * @param builder 真实的对象为DidiHttpClient.Builder * @param didiHttpClient 真实的对象为DidiHttpClient */ public static void performDidiHttpOneParamBuilderInit(Object builder, Object didiHttpClient) { try { if (builder instanceof DidiHttpClient.Builder) { DidiHttpClient.Builder localBuild = (DidiHttpClient.Builder) builder; List<Interceptor> interceptors = removeDuplicate(localBuild.interceptors()); List<Interceptor> networkInterceptors = removeDuplicate(localBuild.networkInterceptors()); ReflectUtils.reflect(localBuild).field("interceptors", interceptors); ReflectUtils.reflect(localBuild).field("networkInterceptors", networkInterceptors); //Log.i("Doraemon", "====performDidiHttpOneParamBuilderInit==="); } } catch (Exception e) { Log.i("Doraemon", "" + e.getMessage()); } }
Example #6
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void fieldFinalAdvanced() { ReflectUtils.reflect(Test8.class) .field("S_DATA", ReflectUtils.reflect(Test8.class).newInstance()) .field("S_DATA") .field("I_DATA", ReflectUtils.reflect(Test8.class).newInstance()) .field("I_DATA") .field("F_INT1", 1) .field("F_INT2", 1) .field("SF_INT1", 2) .field("SF_INT2", 2); assertEquals(2, Test8.SF_INT1); assertEquals(new Integer(2), Test8.SF_INT2); assertEquals(0, Test8.S_DATA.F_INT1); assertEquals(new Integer(0), Test8.S_DATA.F_INT2); assertEquals(1, Test8.S_DATA.I_DATA.F_INT1); assertEquals(new Integer(1), Test8.S_DATA.I_DATA.F_INT2); }
Example #7
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void fieldAdvanced() { ReflectUtils.reflect(Test1.class) .field("S_DATA", ReflectUtils.reflect(Test1.class).newInstance()) .field("S_DATA") .field("I_DATA", ReflectUtils.reflect(Test1.class).newInstance()) .field("I_DATA") .field("I_INT1", 1) .field("S_INT1", 2); assertEquals(2, Test1.S_INT1); assertEquals(null, Test1.S_INT2); assertEquals(0, Test1.S_DATA.I_INT1); assertEquals(null, Test1.S_DATA.I_INT2); assertEquals(1, Test1.S_DATA.I_DATA.I_INT1); assertEquals(null, Test1.S_DATA.I_DATA.I_INT2); }
Example #8
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void newInstance() { assertEquals( "", ReflectUtils.reflect(String.class).newInstance().get() ); assertEquals( "abc", ReflectUtils.reflect(String.class).newInstance("abc").get() ); assertEquals( "abc", ReflectUtils.reflect(String.class).newInstance("abc".getBytes()).get() ); assertEquals( "abc", ReflectUtils.reflect(String.class).newInstance("abc".toCharArray()).get() ); assertEquals( "b", ReflectUtils.reflect(String.class).newInstance("abc".toCharArray(), 1, 1).get() ); }
Example #9
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void fieldFinal() { // instance field Test8 test11 = new Test8(); ReflectUtils.reflect(test11).field("F_INT1", 1); assertEquals(1, ReflectUtils.reflect(test11).field("F_INT1").get()); ReflectUtils.reflect(test11).field("F_INT2", 1); assertEquals(1, ReflectUtils.reflect(test11).field("F_INT2").get()); ReflectUtils.reflect(test11).field("F_INT2", null); assertNull(ReflectUtils.reflect(test11).field("F_INT2").get()); // static field ReflectUtils.reflect(Test8.class).field("SF_INT1", 1); assertEquals(1, ReflectUtils.reflect(Test8.class).field("SF_INT1").get()); ReflectUtils.reflect(Test8.class).field("SF_INT2", 1); assertEquals(1, ReflectUtils.reflect(Test8.class).field("SF_INT2").get()); ReflectUtils.reflect(Test8.class).field("SF_INT2", null); assertNull(ReflectUtils.reflect(Test8.class).field("SF_INT2").get()); }
Example #10
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void newInstanceAmbiguity() { Test2 test; test = ReflectUtils.reflect(Test2.class).newInstance().get(); assertEquals(null, test.n); assertEquals(Test2.ConstructorType.NO_ARGS, test.constructorType); test = ReflectUtils.reflect(Test2.class).newInstance("abc").get(); assertEquals("abc", test.n); assertEquals(Test2.ConstructorType.OBJECT, test.constructorType); test = ReflectUtils.reflect(Test2.class).newInstance(new Long("1")).get(); assertEquals(1L, test.n); assertEquals(Test2.ConstructorType.NUMBER, test.constructorType); test = ReflectUtils.reflect(Test2.class).newInstance(1).get(); assertEquals(1, test.n); assertEquals(Test2.ConstructorType.INTEGER, test.constructorType); test = ReflectUtils.reflect(Test2.class).newInstance('a').get(); assertEquals('a', test.n); assertEquals(Test2.ConstructorType.OBJECT, test.constructorType); }
Example #11
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
@Test public void methodAmbiguity() { Test3 test; test = ReflectUtils.reflect(Test3.class).newInstance().method("method").get(); assertEquals(null, test.n); assertEquals(Test3.MethodType.NO_ARGS, test.methodType); test = ReflectUtils.reflect(Test3.class).newInstance().method("method", "abc").get(); assertEquals("abc", test.n); assertEquals(Test3.MethodType.OBJECT, test.methodType); test = ReflectUtils.reflect(Test3.class).newInstance().method("method", new Long("1")).get(); assertEquals(1L, test.n); assertEquals(Test3.MethodType.NUMBER, test.methodType); test = ReflectUtils.reflect(Test3.class).newInstance().method("method", 1).get(); assertEquals(1, test.n); assertEquals(Test3.MethodType.INTEGER, test.methodType); test = ReflectUtils.reflect(Test3.class).newInstance().method("method", 'a').get(); assertEquals('a', test.n); assertEquals(Test3.MethodType.OBJECT, test.methodType); }
Example #12
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void methodSuper() { TestHierarchicalMethodsSubclass subclass = new TestHierarchicalMethodsSubclass(); assertEquals( TestHierarchicalMethodsBase.PUBLIC_RESULT, ReflectUtils.reflect(subclass).method("pub_base_method", 1).get() ); assertEquals( TestHierarchicalMethodsBase.PRIVATE_RESULT, ReflectUtils.reflect(subclass).method("very_priv_method").get() ); }
Example #13
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void methodVoid() { // instance methods Test4 test4 = new Test4(); assertEquals( test4, ReflectUtils.reflect(test4).method("i_method").get() ); // static methods assertEquals( Test4.class, ReflectUtils.reflect(Test4.class).method("s_method").get() ); }
Example #14
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void methodDeclaring() { TestHierarchicalMethodsSubclass subclass = new TestHierarchicalMethodsSubclass(); assertEquals( TestHierarchicalMethodsSubclass.PRIVATE_RESULT, ReflectUtils.reflect(subclass).method("priv_method", 1).get() ); TestHierarchicalMethodsBase baseClass = new TestHierarchicalMethodsBase(); assertEquals( TestHierarchicalMethodsBase.PRIVATE_RESULT, ReflectUtils.reflect(baseClass).method("priv_method", 1).get() ); }
Example #15
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void fieldPrivate() { class Foo { private String bar; } Foo foo = new Foo(); ReflectUtils.reflect(foo).field("bar", "FooBar"); assertThat(foo.bar, Matchers.is("FooBar")); assertEquals("FooBar", ReflectUtils.reflect(foo).field("bar").get()); ReflectUtils.reflect(foo).field("bar", null); assertNull(foo.bar); assertNull(ReflectUtils.reflect(foo).field("bar").get()); }
Example #16
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void fieldPrivateStaticFinal() { assertEquals(1, ReflectUtils.reflect(TestPrivateStaticFinal.class).field("I1").get()); assertEquals(1, ReflectUtils.reflect(TestPrivateStaticFinal.class).field("I2").get()); ReflectUtils.reflect(TestPrivateStaticFinal.class).field("I1", 2); ReflectUtils.reflect(TestPrivateStaticFinal.class).field("I2", 2); assertEquals(2, ReflectUtils.reflect(TestPrivateStaticFinal.class).field("I1").get()); assertEquals(2, ReflectUtils.reflect(TestPrivateStaticFinal.class).field("I2").get()); }
Example #17
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void _toString() { Object object = new Object() { @Override public String toString() { return "test"; } }; assertEquals(ReflectUtils.reflect(object).toString(), object.toString()); }
Example #18
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void _equals() { Object object = new Object(); ReflectUtils a = ReflectUtils.reflect(object); ReflectUtils b = ReflectUtils.reflect(object); ReflectUtils c = ReflectUtils.reflect(object); assertTrue(b.equals(a)); assertTrue(a.equals(b)); assertTrue(b.equals(c)); assertTrue(a.equals(c)); //noinspection ObjectEqualsNull assertFalse(a.equals(null)); }
Example #19
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void methodNullArguments() { Test6 test9 = new Test6(); ReflectUtils.reflect(test9).method("put", "key", "value"); assertTrue(test9.map.containsKey("key")); assertEquals("value", test9.map.get("key")); ReflectUtils.reflect(test9).method("put", "key", null); assertTrue(test9.map.containsKey("key")); assertNull(test9.map.get("key")); }
Example #20
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void methodPrivate() { // instance methods Test5 test8 = new Test5(); assertEquals( test8, ReflectUtils.reflect(test8).method("i_method").get() ); // static methods assertEquals( Test5.class, ReflectUtils.reflect(Test5.class).method("s_method").get() ); }
Example #21
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void newInstanceWithPrivate() { Test7 t1 = ReflectUtils.reflect(Test7.class).newInstance(1).get(); assertEquals(1, (int) t1.i); assertNull(t1.s); Test7 t2 = ReflectUtils.reflect(Test7.class).newInstance("a").get(); assertNull(t2.i); assertEquals("a", t2.s); Test7 t3 = ReflectUtils.reflect(Test7.class).newInstance("a", 1).get(); assertEquals(1, (int) t3.i); assertEquals("a", t3.s); }
Example #22
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void newInstancePrivate() { assertNull(ReflectUtils.reflect(PrivateConstructors.class).newInstance().field("string").get()); assertEquals( "abc", ReflectUtils.reflect(PrivateConstructors.class).newInstance("abc").field("string").get() ); }
Example #23
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Test public void reflect() { Assert.assertEquals( ReflectUtils.reflect(Object.class), ReflectUtils.reflect("java.lang.Object", ClassLoader.getSystemClassLoader()) ); assertEquals( ReflectUtils.reflect(Object.class), ReflectUtils.reflect("java.lang.Object") ); assertEquals( ReflectUtils.reflect(String.class).get(), ReflectUtils.reflect("java.lang.String").get() ); assertEquals( Object.class, ReflectUtils.reflect(Object.class).get() ); assertEquals( "abc", ReflectUtils.reflect((Object) "abc").get() ); assertEquals( 1, ReflectUtils.reflect(1).get() ); }
Example #24
Source File: InjectManagerService.java From AndroidQuick with MIT License | 5 votes |
private static void injectColorById(ViewManager viewManager, Object object, Field field) { ByColor colorById = field.getAnnotation(ByColor.class); if (colorById != null) { int colorId = colorById.value(); int color = viewManager.getColor(colorId); try { ReflectUtils.reflect(object).field(field.getName(), color); } catch (Exception e) { e.printStackTrace(); } } }
Example #25
Source File: InjectManagerService.java From AndroidQuick with MIT License | 5 votes |
private static void injectStringById(ViewManager viewManager, Object object, Field field) { ByString stringById = field.getAnnotation(ByString.class); if (stringById != null) { int stringId = stringById.value(); String string = viewManager.getString(stringId); try { ReflectUtils.reflect(object).field(field.getName(), string); } catch (Exception e) { e.printStackTrace(); } } }
Example #26
Source File: InjectManagerService.java From AndroidQuick with MIT License | 5 votes |
private static void injectViewById(ViewManager viewManager, Object object, Field field) { ByView viewById = field.getAnnotation(ByView.class); if (viewById != null) { int viewId = viewById.value(); View view = viewManager.findViewById(viewId); try { ReflectUtils.reflect(object).field(field.getName(), view); } catch (Exception e) { e.printStackTrace(); } } }
Example #27
Source File: OkHttpHook.java From DoraemonKit with Apache License 2.0 | 5 votes |
/** * @param builder 真实的对象为okHttpClient.Builder * @param okHttpClient 真实的对象为okHttpClient */ public static void performOkhttpOneParamBuilderInit(Object builder, Object okHttpClient) { try { if (builder instanceof OkHttpClient.Builder) { OkHttpClient.Builder localBuild = (OkHttpClient.Builder) builder; List<Interceptor> interceptors = removeDuplicate(localBuild.interceptors()); List<Interceptor> networkInterceptors = removeDuplicate(localBuild.networkInterceptors()); ReflectUtils.reflect(localBuild).field("interceptors", interceptors); ReflectUtils.reflect(localBuild).field("networkInterceptors", networkInterceptors); } } catch (Exception e) { Log.i("Doraemon", "" + e.getMessage()); } }
Example #28
Source File: DokitGlideTransform.java From DoraemonKit with Apache License 2.0 | 5 votes |
@NonNull @Override public Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) { try { if (mWrap != null) { resource = mWrap.transform(context, resource, outWidth, outHeight); } if (PerformanceSpInfoConfig.isLargeImgOpen()) { String url = ""; if (mRequestBuilder instanceof RequestBuilder) { if (ReflectUtils.reflect(mRequestBuilder).field("model").get() instanceof String) { url = ReflectUtils.reflect(mRequestBuilder).field("model").get(); } else if (ReflectUtils.reflect(mRequestBuilder).field("model").get() instanceof Integer) { url = "" + ReflectUtils.reflect(mRequestBuilder).field("model").get(); } } Bitmap bitmap = resource.get(); double imgSize = ConvertUtils.byte2MemorySize(bitmap.getByteCount(), MemoryConstants.MB); LargePictureManager.getInstance().saveImageInfo(url, imgSize, bitmap.getWidth(), bitmap.getHeight(), "Glide"); } } catch (Exception e) { if (mWrap != null) { resource = mWrap.transform(context, resource, outWidth, outHeight); } } return resource; }
Example #29
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 4 votes |
@Test public void method() { // instance methods assertEquals( "", ReflectUtils.reflect((Object) " ").method("trim").get() ); assertEquals( "12", ReflectUtils.reflect((Object) " 12 ").method("trim").get() ); assertEquals( "34", ReflectUtils.reflect((Object) "1234").method("substring", 2).get() ); assertEquals( "12", ReflectUtils.reflect((Object) "1234").method("substring", 0, 2).get() ); assertEquals( "1234", ReflectUtils.reflect((Object) "12").method("concat", "34").get() ); assertEquals( "123456", ReflectUtils.reflect((Object) "12").method("concat", "34").method("concat", "56").get() ); assertEquals( 2, ReflectUtils.reflect((Object) "1234").method("indexOf", "3").get() ); assertEquals( 2.0f, (float) ReflectUtils.reflect((Object) "1234").method("indexOf", "3").method("floatValue").get(), 0.0f ); assertEquals( "2", ReflectUtils.reflect((Object) "1234").method("indexOf", "3").method("toString").get() ); // static methods assertEquals( "true", ReflectUtils.reflect(String.class).method("valueOf", true).get() ); assertEquals( "1", ReflectUtils.reflect(String.class).method("valueOf", 1).get() ); assertEquals( "abc", ReflectUtils.reflect(String.class).method("valueOf", "abc".toCharArray()).get() ); assertEquals( "abc", ReflectUtils.reflect(String.class).method("copyValueOf", "abc".toCharArray()).get() ); assertEquals( "b", ReflectUtils.reflect(String.class).method("copyValueOf", "abc".toCharArray(), 1, 1).get() ); }
Example #30
Source File: ReflectUtilsTest.java From AndroidUtilCode with Apache License 2.0 | 4 votes |
@Test public void field() { // instance field Test1 test1 = new Test1(); ReflectUtils.reflect(test1).field("I_INT1", 1); assertEquals(1, ReflectUtils.reflect(test1).field("I_INT1").get()); ReflectUtils.reflect(test1).field("I_INT2", 1); assertEquals(1, ReflectUtils.reflect(test1).field("I_INT2").get()); ReflectUtils.reflect(test1).field("I_INT2", null); assertNull(ReflectUtils.reflect(test1).field("I_INT2").get()); // static field ReflectUtils.reflect(Test1.class).field("S_INT1", 1); assertEquals(1, ReflectUtils.reflect(Test1.class).field("S_INT1").get()); ReflectUtils.reflect(Test1.class).field("S_INT2", 1); assertEquals(1, ReflectUtils.reflect(Test1.class).field("S_INT2").get()); ReflectUtils.reflect(Test1.class).field("S_INT2", null); assertNull(ReflectUtils.reflect(Test1.class).field("S_INT2").get()); // hierarchies field TestHierarchicalMethodsSubclass test2 = new TestHierarchicalMethodsSubclass(); ReflectUtils.reflect(test2).field("invisibleField1", 1); assertEquals(1, ReflectUtils.reflect(test2).field("invisibleField1").get()); ReflectUtils.reflect(test2).field("invisibleField2", 1); assertEquals(1, ReflectUtils.reflect(test2).field("invisibleField2").get()); ReflectUtils.reflect(test2).field("invisibleField3", 1); assertEquals(1, ReflectUtils.reflect(test2).field("invisibleField3").get()); ReflectUtils.reflect(test2).field("visibleField1", 1); assertEquals(1, ReflectUtils.reflect(test2).field("visibleField1").get()); ReflectUtils.reflect(test2).field("visibleField2", 1); assertEquals(1, ReflectUtils.reflect(test2).field("visibleField2").get()); ReflectUtils.reflect(test2).field("visibleField3", 1); assertEquals(1, ReflectUtils.reflect(test2).field("visibleField3").get()); }