GithubHelp home page GithubHelp logo

smartdengg / interface-buoy Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 210 KB

ASM&AOP. Safely invoke the method declared within a Java interface.

License: MIT License

Java 77.54% Groovy 22.46%
gradle gradle-plugin groovy android asm aop interface-design gradle-android-plugin

interface-buoy's Introduction

interface-buoy

Kotlin and groovy provide very convenient operators, the operator ?. is only invoked when the reference object is not empty, but unfortunately, this utility operator is not provided in java.

I wrote a blog to analyze the principles of the ?. operators in kotlin and groovy. The kotlinc and groovyc compilers compile the ?. to the same effect as if (callback != null) {} bytecode.

Interface-buoy is an Android gradle plugin that uses ASM to dynamically modify java bytecode during the build process. Combine dynamic proxies and reflections to achieve the same effect as the '?.' effect.

Note that we only modify the method defined in the java interface, that is which will trigger the invokeinterface instruction at runtime.

Blog link : 在Java 中安全使用接口引用

Usage

Command Line

$ git clone [email protected]:SmartDengg/interface-buoy.git
$ cd interface-buoy/
$ ./gradlew build

AGP

Add Android gradle plugin dependency to your any application or library module's build.gradle.

buildscript {
    repositories {
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.github.SmartDengg.interface-buoy:interface-buoy-gradle:1.3.0'
    }
}

apply plugin: 'com.android.application' // or apply plugin: 'com.android.library'
apply plugin: 'com.smartdengg.interfacebuoy' // or apply plugin: 'interfacebuoy'

dependencies {
  implementation fileTree(include: ['*.jar'], dir: 'libs')
  implementation 'com.github.SmartDengg.interface-buoy:interface-buoy-runtime:1.3.0'
}

Optional

In the java code you can use BuoySettings.loggable = true to control whether to print the log, the default is not to print the log.

Support

If the invoked interface method has a return type but not a void type, its bytecode will not be modified. Currently only the void return type is supported.

  • void return type
  • other return type

Principle

Before:


invokeinterface #3,  2            // InterfaceMethod Callback.onProgress:(I)V


After:


invokestatic  #23                 // Method buoy$onProgress:(LCallback;I)V


static void buoy$onProgress(JavaSample$Callback, int);
Code:
   0: aload_0
   1: ldc           #25                 // String Callback
   3: ldc           #27                 // String Callback.onProgress:(int)void
   5: invokestatic  #33                 // Method com/smartdengg/interfacebuoy/compiler/InterfaceBuoy.proxy:(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
   8: iload_1
   9: invokeinterface #37,  2           // InterfaceMethod Callback.onProgress:(I)V
  14: return


  1. Replaces invokeinterface with invokestatic at compile time.

  2. Generated Buoy$onProgress method at compile time to prevent calling interface methods on an null object.

About me

email : [email protected]

blog : 小鄧子

weibo : -小鄧子-

License

See the LICENSE file for license rights and limitations (MIT).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.