| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.tools.fd.runtime; |
| 18 |
|
| 19 |
import static com.android.tools.fd.common.Log.logging; |
| 20 |
|
| 21 |
import com.android.annotations.NonNull; |
| 22 |
import com.android.annotations.Nullable; |
| 23 |
import com.android.tools.fd.common.Log; |
| 24 |
|
| 25 |
import java.lang.reflect.Constructor; |
| 26 |
import java.lang.reflect.Field; |
| 27 |
import java.lang.reflect.InvocationTargetException; |
| 28 |
import java.lang.reflect.Method; |
| 29 |
import java.util.NoSuchElementException; |
| 30 |
import java.util.logging.Level; |
| 31 |
import java.util.logging.Logger; |
| 32 |
|
| 33 |
/** |
| 34 |
* Generic Instant Run services. must not depend on Android APIs. |
| 35 |
* |
| 36 |
* TODO: transform this static methods into interface/implementation. |
| 37 |
*/ |
| 38 |
@SuppressWarnings("unused") |
| 39 |
public class AndroidInstantRuntime { |
| 40 |
|
| 41 |
protected interface Logging { |
| 42 |
void log(@NonNull Level level, @NonNull String string); |
| 43 |
|
| 44 |
boolean isLoggable(@NonNull Level level); |
| 45 |
|
| 46 |
void log(@NonNull Level level, @NonNull String string, @Nullable Throwable throwable); |
| 47 |
} |
| 48 |
|
| 49 |
public static void setLogger(final Logger logger) { |
| 50 |
|
| 51 |
logging = new Log.Logging() { |
| 52 |
@Override |
| 53 |
public void log(@NonNull Level level, @NonNull String string) { |
| 54 |
logger.log(level, string); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public boolean isLoggable(@NonNull Level level) { |
| 59 |
return logger.isLoggable(level); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void log(@NonNull Level level, @NonNull String string, |
| 64 |
@Nullable Throwable throwable) { |
| 65 |
logger.log(level, string, throwable); |
| 66 |
} |
| 67 |
}; |
| 68 |
} |
| 69 |
|
| 70 |
@Nullable |
| 71 |
public static Object getStaticPrivateField(Class targetClass, String fieldName) { |
| 72 |
return getPrivateField(null /* targetObject */, targetClass, fieldName); |
| 73 |
} |
| 74 |
|
| 75 |
public static void setStaticPrivateField( |
| 76 |
@NonNull Object value, @NonNull Class targetClass, @NonNull String fieldName) { |
| 77 |
setPrivateField(null /* targetObject */, value, targetClass, fieldName); |
| 78 |
} |
| 79 |
|
| 80 |
public static void setPrivateField( |
| 81 |
@Nullable Object targetObject, |
| 82 |
@Nullable Object value, |
| 83 |
@NonNull Class targetClass, |
| 84 |
@NonNull String fieldName) { |
| 85 |
|
| 86 |
try { |
| 87 |
Field declaredField = getField(targetClass, fieldName); |
| 88 |
declaredField.set(targetObject, value); |
| 89 |
} catch (IllegalAccessException e) { |
| 90 |
if (logging != null) { |
| 91 |
logging.log(Level.SEVERE, |
| 92 |
String.format("Exception during setPrivateField %s", fieldName), e); |
| 93 |
} |
| 94 |
throw new RuntimeException(e); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Nullable |
| 99 |
public static Object getPrivateField( |
| 100 |
@Nullable Object targetObject, |
| 101 |
@NonNull Class targetClass, |
| 102 |
@NonNull String fieldName) { |
| 103 |
|
| 104 |
try { |
| 105 |
Field declaredField = getField(targetClass, fieldName); |
| 106 |
return declaredField.get(targetObject); |
| 107 |
} catch (IllegalAccessException e) { |
| 108 |
if (logging != null) { |
| 109 |
logging.log(Level.SEVERE, |
| 110 |
String.format("Exception during%1$s getField %2$s", |
| 111 |
targetObject == null ? " static" : "", |
| 112 |
fieldName), e); |
| 113 |
} |
| 114 |
throw new RuntimeException(e); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
@NonNull |
| 120 |
private static Field getField(Class target, String name) { |
| 121 |
Field declareField = getFieldByName(target, name); |
| 122 |
if (declareField == null) { |
| 123 |
throw new RuntimeException(new NoSuchElementException(name)); |
| 124 |
} |
| 125 |
declareField.setAccessible(true); |
| 126 |
return declareField; |
| 127 |
} |
| 128 |
|
| 129 |
public static Object invokeProtectedMethod(Object receiver, |
| 130 |
Object[] params, |
| 131 |
Class[] parameterTypes, |
| 132 |
String methodName) throws Throwable { |
| 133 |
|
| 134 |
if (logging!=null && logging.isLoggable(Level.FINE)) { |
| 135 |
logging.log(Level.FINE, String.format("protectedMethod:%s on %s", methodName, receiver)); |
| 136 |
} |
| 137 |
try { |
| 138 |
Method toDispatchTo = getMethodByName(receiver.getClass(), methodName, parameterTypes); |
| 139 |
if (toDispatchTo == null) { |
| 140 |
throw new RuntimeException(new NoSuchMethodException(methodName)); |
| 141 |
} |
| 142 |
toDispatchTo.setAccessible(true); |
| 143 |
return toDispatchTo.invoke(receiver, params); |
| 144 |
} catch (InvocationTargetException e) { |
| 145 |
// The called method threw an exception, rethrow |
| 146 |
throw e.getCause(); |
| 147 |
} catch (IllegalAccessException e) { |
| 148 |
logging.log(Level.SEVERE, String.format("Exception while invoking %s", methodName), e); |
| 149 |
throw new RuntimeException(e); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
public static Object invokeProtectedStaticMethod( |
| 154 |
Object[] params, |
| 155 |
Class[] parameterTypes, |
| 156 |
String methodName, |
| 157 |
Class receiverClass) throws Throwable { |
| 158 |
|
| 159 |
if (logging!=null && logging.isLoggable(Level.FINE)) { |
| 160 |
logging.log(Level.FINE, |
| 161 |
String.format("protectedStaticMethod:%s on %s", methodName, receiverClass.getName())); |
| 162 |
} |
| 163 |
try { |
| 164 |
Method toDispatchTo = getMethodByName(receiverClass, methodName, parameterTypes); |
| 165 |
if (toDispatchTo == null) { |
| 166 |
throw new RuntimeException(new NoSuchMethodException( |
| 167 |
methodName + " in class " + receiverClass.getName())); |
| 168 |
} |
| 169 |
toDispatchTo.setAccessible(true); |
| 170 |
return toDispatchTo.invoke(null /* target */, params); |
| 171 |
} catch (InvocationTargetException e) { |
| 172 |
// The called method threw an exception, rethrow |
| 173 |
throw e.getCause(); |
| 174 |
} catch (IllegalAccessException e) { |
| 175 |
logging.log(Level.SEVERE, String.format("Exception while invoking %s", methodName), e); |
| 176 |
throw new RuntimeException(e); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public static <T> T newForClass(Object[] params, Class[] paramTypes, Class<T> targetClass) |
| 181 |
throws Throwable { |
| 182 |
Constructor declaredConstructor; |
| 183 |
try { |
| 184 |
declaredConstructor = targetClass.getDeclaredConstructor(paramTypes); |
| 185 |
} catch (NoSuchMethodException e) { |
| 186 |
logging.log(Level.SEVERE, "Exception while resolving constructor", e); |
| 187 |
throw new RuntimeException(e); |
| 188 |
} |
| 189 |
declaredConstructor.setAccessible(true); |
| 190 |
try { |
| 191 |
return targetClass.cast(declaredConstructor.newInstance(params)); |
| 192 |
} catch (InvocationTargetException e) { |
| 193 |
// The called method threw an exception, rethrow |
| 194 |
throw e.getCause(); |
| 195 |
} catch (InstantiationException e) { |
| 196 |
logging.log(Level.SEVERE, String.format("Exception while instantiating %s", targetClass), e); |
| 197 |
throw new RuntimeException(e); |
| 198 |
} catch (IllegalAccessException e) { |
| 199 |
logging.log(Level.SEVERE, String.format("Exception while instantiating %s", targetClass), e); |
| 200 |
throw new RuntimeException(e); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
private static Field getFieldByName(Class<?> aClass, String name) { |
| 205 |
|
| 206 |
if (logging!= null && logging.isLoggable(Level.FINE)) { |
| 207 |
logging.log(Level.FINE, String.format("getFieldByName:%s in %s", name, aClass.getName())); |
| 208 |
} |
| 209 |
|
| 210 |
Class<?> currentClass = aClass; |
| 211 |
while (currentClass != null) { |
| 212 |
try { |
| 213 |
return currentClass.getDeclaredField(name); |
| 214 |
} catch (NoSuchFieldException e) { |
| 215 |
// ignored. |
| 216 |
} |
| 217 |
currentClass = currentClass.getSuperclass(); |
| 218 |
} |
| 219 |
return null; |
| 220 |
} |
| 221 |
|
| 222 |
private static Method getMethodByName(Class<?> aClass, String name, Class[] paramTypes) { |
| 223 |
|
| 224 |
if (aClass == null) { |
| 225 |
return null; |
| 226 |
} |
| 227 |
|
| 228 |
Class<?> currentClass = aClass; |
| 229 |
while (currentClass != null) { |
| 230 |
try { |
| 231 |
return currentClass.getDeclaredMethod(name, paramTypes); |
| 232 |
} catch (NoSuchMethodException e) { |
| 233 |
// ignored. |
| 234 |
} |
| 235 |
currentClass = currentClass.getSuperclass(); |
| 236 |
if (currentClass!= null && logging!=null && logging.isLoggable(Level.FINE)) { |
| 237 |
logging.log(Level.FINE, String.format( |
| 238 |
"getMethodByName:Looking in %s now", currentClass.getName())); |
| 239 |
} |
| 240 |
|
| 241 |
} |
| 242 |
return null; |
| 243 |
} |
| 244 |
|
| 245 |
public static void trace(String s) { |
| 246 |
if (logging != null) { |
| 247 |
logging.log(Level.FINE, s); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
public static void trace(String s1, String s2) { |
| 252 |
if (logging != null) { |
| 253 |
logging.log(Level.FINE, String.format("%s %s", s1, s2)); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
public static void trace(String s1, String s2, String s3) { |
| 258 |
if (logging != null) { |
| 259 |
logging.log(Level.FINE, String.format("%s %s %s", s1, s2, s3)); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
public static void trace(String s1, String s2, String s3, String s4) { |
| 264 |
if (logging != null) { |
| 265 |
logging.log(Level.FINE, String.format("%s %s %s %s", s1, s2, s3, s4)); |
| 266 |
} |
| 267 |
} |
| 268 |
} |