Code Monkey home page Code Monkey logo

asmsupport's Introduction

asmsupport

Build Status Coverage Status Maven central openhub Codacy Badge License (LGPL version 3.0)

English Version

Java字节码操作类库

asmsupport是一个字节码操作类库,它能够让程序员非常简单的在动态创建和修改类,该框架是基于asm开发的,不同与asm的是,它避免了直接操作jvm指令,栈和局部变量。更多内容可见文档

模块

模块 描述
asmsupport-thrid 这个模块的包含了asmsupport使用的第三方依赖包的源码,这些源码在asmsupport中都重新定义了命名空间,这样的好处就是实现零依赖,同时在开发过程中避免了因为使用asmsupport而产生包冲突
asmsupport-standard 这个模块是asmsupport的标准,可以基于这个api标准完成一个新的asmsupport的实现
asmsupport-core 这个模块是asmsupport-standard的一个核心实现
asmsupport-client 这个模块是对asmsupport-core的一个高层封装,实现了很多简便的方式,比如链式调用,同时也实现了asmsupport-standard标准.
asmsupport-grammar 这个模块是支持对java语法直接编译。
asmsupport-issues 这个模块包含了所有asmsupport以往的issue的测试类,这个包的作用可以在后续开发中确保运行'maven test'的时候同一问题不再出现。
asmsupport-sample 这个模块包含了一些asmsupport的实例, 比如动态代理,json序列化等等.
asmsupport-lesson 这个模块包含了一些asmsupport基本教程实例.

Maven坐标

<dependency>
    <groupId>cn.wensiqun</groupId>
    <artifactId>asmsupport-client</artifactId>
    <version>x.x.x</version>
</dependency>

特点

ASMSupport最大的特点是将JAVA语法中的每一个操作和概念都赋予了一个具体的抽象(比如if程序块,四则运算,局部变量,都抽象出具体的类),最终构建出一个类似语法树的结构,这样带来有如下好处:

  1. 屏蔽JVM指令
  2. 以面向对象的方式兼容JAVA语法,开发简单
  3. 由于将操作和概念都抽象成类,方便将一个大的字节码构建工程模块化。

不同

ASMSupport是基于asm开发的,相比于ASM,ASMSupport屏蔽JVM字节码相关的细节,对于开发人员来说无需了解JVM字节码相关内容。

比如在ASM中你要使用局部变量是需要通过下标来访问的,相同位置的下标在不同的指令位置可能对应不同的局部变量,这样使用ASM就非常困难,而在ASMSupport中将每一个局部变量抽象成一个具体的对象,在任何位置使用该变量都能够轻松访问到。

BECL同样是基于asm开发的,他本身也是指令级别的字节码操作类库,所以ASMSupport和BECL的不同点和ASM一样。

javassist是源码级别的字节码操作类库,ASMSupport则是介于字节码和源码级别中间的字节码操作类库。使用javassist的好处就是非常直观易懂,基本没有门槛,但由于是源码级别,很多问题在编译时候无法暴露出来,并且对字节码构建过程的模块化也不是特别方便,由于javassit是基于源码的,所以其字节码生成过程中首先要做词法分析,语法分析,语义分析三个步骤后得到语法树,再通过语法树构建字节码。ASMSupport其实是在编码的过程中就已经将语法树结构确定下来,但在ASMSupport中并不是标准的语法树,所以为什么说ASMSupport则是介于字节码和源码级别中间的字节码操作类库。

第一个例子

代码中我们将使用ASMSupport生成如下代码:

public class FirstCase {
    
    public static void main(String[] args) {
        System.out.println("Hello ASMSupport.");
    }
    
}

字节码构建流程图

字节码构建流程图

进阶实例

在asmsupport-sample模块下提供了两种实例,1.json序列化,2.实现动态代理。

字节码实现json序列化

JSON的实例在包cn.wensiqun.asmsupport.sample.client.json下,运行cn.wensiqun.asmsupport.sample.client.json.demo.Runner类,在target/asmsupport-test-generated下查看生成的类。

字节码实现动态代理

Proxy的实例在包cn.wensiqun.asmsupport.sample.client.proxy下,运行cn.wensiqun.asmsupport.sample.client.proxy.demo.Runner类,在target/asmsupport-test-generated下查看生成的类。

许可

ASMSupport使用的是GNU Lesser General Public License (LGPL)许可。

asmsupport's People

Contributors

wensiqun avatar woate avatar bingoohuang avatar ninng avatar

Watchers

James Cloos avatar

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.