DexPatcher / dexpatcher-gradle

Modify Android applications at source-level in Android Studio
https://dexpatcher.github.io/
GNU General Public License v3.0
83 stars 17 forks source link

Replace the DexTag type with a parameter annotation. #8

Closed Lanchon closed 9 years ago

Lanchon commented 9 years ago

This is an embarrassing mistake. I designed and implemented DexPatcher in a couple of days, and I had never really used Java annotations before.

Because in Java you cannot rename a constructor, if you want to invoke the original constructor from a replacement constructor, you need to add a tag parameter to overload it instead. DexPatcher 1.0.0 recognizes a tag parameter by detecting the specific hardcoded type 'lanchon.dexpatcher.annotation.DexTag', which is bundled in the annotations Jar, and must be included in final executable.

This idiom is typically used to patch a constructor:

@DexEdit
MyClass(String data, DexTag tag) {}

@DexAdd
MyClass(String data) {
    this(filterData(data), DexTag.TAG);
    doMoreStuff();
}

I used a tag type instead of a parameter annotation because I thought that parameter annotations were a Java 1.8 feature, when in fact they are supported in Java 1.7. The downside to this is that an app can easily detect that it has been patched by DexPatcher by checking for this tag type.

To avoid this problem, the tagging mechanism should be changed to a parameter annotation applicable to any type (including primitive types). A suggested name for the annotation is 'DexTagParameter'. Alternatively, the existing 'DexIgnore' annotation could be overloaded for this use.

The end result would look like this:

@DexEdit
MyClass(String data, @DexTagParameter int tag) {}

@DexAdd
MyClass(String data) {
    this(filterData(data), 0);
    doMoreStuff();
}

It might be better if the 'DexTag' name goes unused to guarantee compile time errors in old code. But most code would reference 'DexTag.TAG' anyway, and if not the resulting patch would fail to apply, so this is not hard requirement.

Lanchon commented 9 years ago

moved to DexPatcher-tool.