Skip to content

Instantly share code, notes, and snippets.

View 0xZhangKe's full-sized avatar
📎
Kotlin

ZhangKe 0xZhangKe

📎
Kotlin
View GitHub Profile
@0xZhangKe
0xZhangKe / gist:a090a07b84c3a63ade2a38b6816abded
Created March 29, 2021 01:51
adb show current Activity
adb shell dumpsys activity activities
@Target(ElementType.TYPE_USE)
public @interface Test {}
public class A {}
public class B extends @Test A {
public @Test int add(@Test int a, @Test int b) {
@Test int result = a + b;
System.out.println(result);
return result;
private fun groupingElementWithType(eleSet: Set<Element>): Map<TypeElement, ArrayList<Element>> {
val groupedElement = HashMap<TypeElement, ArrayList<Element>>()
for (item in eleSet) {
checkAnnotationLegal(item)
val enclosingElement = item.enclosingElement as TypeElement
if (groupedElement.keys.contains(enclosingElement)) {
groupedElement[enclosingElement]!!.add(item)
} else {
val list = ArrayList<Element>()
list += item
private fun checkAnnotationLegal(ele: Element) {
if (ele.kind != ElementKind.FIELD) {
throw RuntimeException("@BindView must in filed! $ele kind is ${ele.kind}")
}
val modifier = ele.modifiers
if (modifier.contains(Modifier.FINAL)) {
throw RuntimeException("@BindView filed can not be final!")
}
if (modifier.contains(Modifier.PRIVATE)) {
throw RuntimeException("@BindView filed can not be private")
private fun makeConstructor(typeElement: TypeElement): MethodSpec.Builder {
val typeMirror = typeElement.asType()
return MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(TypeName.get(typeMirror), "target")
}
for (itemView in elements) {
bindMethodBuilder.addStatement("target.${itemView} = " +
"target.findViewById(${itemView.getAnnotation(BindView::class.java).value})")
}
val typeBuilder = TypeSpec.classBuilder("${typeEle.simpleName}_Binding")
.addModifiers(Modifier.PUBLIC)
typeBuilder.addMethod(constructor)
// Generate a Java class file
val file = JavaFile.builder(getPackageName(classItem), typeBuilder.build())
.build()
file.writeTo(this.processingEnv.filer)
fun bind(activity: Activity) {
val targetClass = activity::class.java
val constructor = findBindingConstructorForClass(targetClass)
constructor?.newInstance(activity)
}
private fun findBindingConstructorForClass(cls: Class<*>?): Constructor<*>? {
if (cls == null) return null
var bindingConstructor: Constructor<*>? = null
val clsName = cls.name
client = new OkHttpClient();
Request request = new Request.Builder().url("your url").build();
// synchronously launch a request
Response syncResponse = client.newCall(request).execute();
// Asynchronously launch a request
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) { }
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { }
interface Interceptor {
@Throws(IOException::class)
fun intercept(chain: Chain): Response
}