A reflection framework that uses annotations and interfaces to eliminate redundant code for reflection.
import android.app.Application;
import reflectx.IProxyClass;
import reflectx.annotations.Source;
@Source("android.app.ActivityThread")
public interface IActivityThread extends IProxyClass {
IActivityThread currentActivityThread();
String currentPackageName();
Application currentApplication();
}
// ...
import reflectx.ProxyFactory;
import reflectx.mapping.ProxyClassMapping; // Generated by the annotation processor.
public class MainActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// It only needs to be initialized once.
ProxyClassMapping.init();
// Proxy "class android.app.ActivityThread".
IActivityThread am = ProxyFactory.proxy(IActivityThread.class);
// Call the static method and wrap the return result.
am = am.currentActivityThread();
// Call the member method.
Application currentApplication = am.currentApplication();
Assert.assertEquals(currentApplication, getApplication());
String currentPackageName = am.currentPackageName();
Assert.assertEquals(currentPackageName, getPackageName());
}
}
project/build.gradle
buildscript {
ext.reflectx_version = 'todo'
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
app/build.gradle
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation "me.zpp0196:reflectx:${reflectx_version}"
annotationProcessor "me.zpp0196.reflectx:compiler:${reflectx_version}"
}
Kotlin is also supported
apply plugin: 'kotlin-kapt'
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation "me.zpp0196:reflectx:${reflectx_version}"
kapt "me.zpp0196.reflectx:compiler:${reflectx_version}"
}
Copyright 2020-2021 zpp0196
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.