Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: libadblockplus-android-webview/src/org/adblockplus/libadblockplus/android/webview/ReflectionUtils.java

Issue 29351744: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Left Patch Set: Created Sept. 8, 2016, 12:52 p.m.
Right Patch Set: changed packages, now using AdblockEngine (original ABPEngine), improved demo app Created Oct. 25, 2016, 11:20 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
LEFTRIGHT
(no file at all)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.libadblockplus.android.webview;
19
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Method;
22
23 public class ReflectionUtils
24 {
25 public static Object extractProperty(Object target, String field)
26 {
27 return extractProperty(target, target.getClass(), field);
28 }
29
30 public static Object extractProperty(Object target, Class clazz, String field)
31 {
32 try
33 {
34 Field f = clazz.getDeclaredField(field);
35 if (!f.isAccessible())
36 {
37 f.setAccessible(true);
38 }
39 return f.get(target);
40 }
41 catch (NoSuchFieldException e)
42 {
43 return extractProperty(target, clazz.getSuperclass(), field);
44 }
45 catch (Exception e)
46 {
47 return null;
48 }
49 }
50
51 public static Object extractProperty(Object target, String []fields)
52 {
53 Object eachTarget = target;
54 for (int i = 0; i < fields.length; i++)
55 {
56 eachTarget = extractProperty(eachTarget, eachTarget.getClass(), fields[i]) ;
57 if (eachTarget == null)
58 {
59 return null;
60 }
61 }
62 return eachTarget;
63 }
64
65 public static boolean injectProperty(Object target, String field, Object value )
66 {
67 try
68 {
69 Field f = target.getClass().getDeclaredField(field);
70 if (!f.isAccessible())
71 {
72 f.setAccessible(true);
73 }
74 f.set(target, value);
75 return true;
76 }
77 catch (Exception e)
78 {
79 return false;
80 }
81 }
82
83 public static boolean invokeMethod(Object target, String methodName, Object[] args)
84 {
85 Method[] methods = target.getClass().getDeclaredMethods();
86 for (Method eachMethod : methods)
87 {
88 if (eachMethod.getName().equals(methodName))
89 {
90 try
91 {
92 if (!eachMethod.isAccessible())
93 {
94 eachMethod.setAccessible(true);
95 }
96 eachMethod.invoke(target, args);
97 return true;
98 }
99 catch (Exception e)
100 {
101 return false;
102 }
103 }
104 }
105 return false;
106 }
107 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld