Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/marvin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Obser committed Sep 20, 2016
2 parents a9de1d6 + 04b7ba1 commit bcf8618
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 341 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jan 26 13:49:38 CET 2015
#Mon Sep 19 23:10:16 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
8 changes: 4 additions & 4 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.1.3'
}
}
apply plugin: 'com.android.library'
Expand All @@ -13,12 +13,12 @@ repositories {
}

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
minSdkVersion 19
targetSdkVersion 23
targetSdkVersion 24
versionCode 1
versionName "1.0"

Expand Down
28 changes: 22 additions & 6 deletions lib/src/main/java/de/larma/arthook/ArtHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public static void hook(Class clazz) {

public static OriginalMethod hook(Method method) {
if (!method.isAnnotationPresent(Hook.class))
throw new IllegalArgumentException("method must have MethodHook annotation");
throw new IllegalArgumentException("method must have @Hook annotation");

Method original;
Object original;
try {
original = findTargetMethod(method);
} catch (Throwable e) {
throw new RuntimeException("Can't find original method", e);
throw new RuntimeException("Can't find original method (" + method.getName() + ")", e);
}
String ident = null;
if (method.isAnnotationPresent(BackupIdentifier.class)) {
Expand All @@ -121,7 +121,19 @@ public static OriginalMethod hook(Method method) {
}

public static OriginalMethod hook(Method originalMethod, Method replacementMethod, String backupIdentifier) {
ArtMethod backArt = hook(originalMethod, replacementMethod);
return hook((Object) originalMethod, replacementMethod, backupIdentifier);
}

public static OriginalMethod hook(Object originalMethod, Method replacementMethod, String backupIdentifier) {
ArtMethod backArt;
if (originalMethod instanceof Method) {
backArt = hook((Method) originalMethod, replacementMethod);
} else if (originalMethod instanceof Constructor) {
backArt = hook((Constructor<?>) originalMethod, replacementMethod);
backArt.convertToMethod();
} else {
throw new RuntimeException("original method must be of type Method or Constructor");
}

Method backupMethod = (Method) backArt.getAssociatedMethod();
backupMethod.setAccessible(true);
Expand Down Expand Up @@ -172,19 +184,23 @@ private static int getQuickCompiledCodeSize(ArtMethod method) {
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt();
}

static Method findTargetMethod(Method method) throws NoSuchMethodException, ClassNotFoundException {
static Object findTargetMethod(Method method) throws NoSuchMethodException, ClassNotFoundException {
Hook hook = method.getAnnotation(Hook.class);
String[] split = hook.value().split("->");
return findTargetMethod(method, Class.forName(split[0]), split.length == 1 ? method.getName() : split[1]);
}

private static Method findTargetMethod(Method method, Class<?> targetClass, String methodName)
private static Object findTargetMethod(Method method, Class<?> targetClass, String methodName)
throws NoSuchMethodException {
Class<?>[] params = null;
if (method.getParameterTypes().length > 0) {
params = new Class<?>[method.getParameterTypes().length - 1];
System.arraycopy(method.getParameterTypes(), 1, params, 0, method.getParameterTypes().length - 1);
}
if (methodName.equals("()") || methodName.equals("<init>")) {
// Constructor
return targetClass.getConstructor(params);
}
try {
Method m = targetClass.getDeclaredMethod(methodName, method.getParameterTypes());
if (Modifier.isStatic(m.getModifiers())) return m;
Expand Down
Loading

0 comments on commit bcf8618

Please sign in to comment.