Git Product home page Git Product logo

groovy-bytecode-ast's Introduction

@Bytecode AST transformation

Build Status

This project adds a @Bytecode AST transformation to Groovy, which will let you write bytecode directly as a method body!

This is for educational purposes only. I would never use this in real production code.

Usage

Build script

If you use Gradle:

build.gradle
repositories {
   maven { url 'http://oss.jfrog.org/oss-release-local' }
}
dependencies {
    compile 'me.champeau.groovy:bytecode-xform:0.2.0'
}

Note that you might have to use a weird classloading trick like the one here if you face a classloader constraint violation.

But if you really insist on using Maven:

pom.xml
    <dependencies>
        <dependency>
            <groupId>me.champeau.groovy</groupId>
            <artifactId>bytecode-xform</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>jfrog</id>
            <url>http://oss.jfrog.org/oss-release-local</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

Code

Then you can start using it in your Groovy code like in this example:

@groovyx.ast.bytecode.Bytecode
int fib(int n) {
    l0:
    iload 1
    iconst_2
    if_icmpge l1
    iload 1
    _goto l2
    l1:
    aload 0
    iload 1
    iconst_2
    isub
    invokevirtual '.fib','(I)I'
    aload 0
    iload 1
    iconst_1
    isub
    invokevirtual '.fib', '(I)I'
    iadd
    l2:
    ireturn
}

A detailed explanation of the concept and why it was done can be found on some blog posts I wrote:

groovy-bytecode-ast's People

Contributors

glaforge avatar jasonnn avatar melix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

groovy-bytecode-ast's Issues

How to LDC a class?

In ASM, I can use this: methodNode.visitLdcInsn(Type.getType("Ljava/lang/Object;"));
In groovy-bytecode-ast, I've tried ldc Object.class, but get an error(No such property: value for class: org.codehaus.groovy.ast.expr.ClassExpression).
If I use ldc 'Ljava/lang/Object;', a literal "Ljava/lang/Object;" is provided.
So what should I do to pass a class?

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.