Code Monkey home page Code Monkey logo

javaclasslib's Introduction

JavaClassLib

This is a Java class file manipulation library that, unlike other bytecode libraries, directly deals with the constructs in the class file without any abstraction. It is still unfinished.

Examples

A generic example can be found here.

Creating a class

This will create a basic class named TheName extending some.super.Class compiled with Java version 11. Note that java versions less than 8 are not supported and using one will throw an exception.

JavaClass aClass = new JavaClass("TheName", "some/super/Class", 11);
aClass.setAccessFlags(ClassAccessFlags.PUBLIC);
Adding constant pool entries

Assuming we have the code from above, adding a constant pool entry is simple. For example, here we add a UTF8_info constant pool entry.

UTF8Entry entry = new UTF8Entry(aClass.getConstantPool(), "Write whatever you want in here");

The entry will automatically add itself to the constant pool on creation.

Adding methods

Here comes the juicy part: methods. Adding a method is slightly more complex.

Method m = new Method(aClass.getConstantPool(), "yourMethodName", "(Lsome/Type;)V");
m.setAccessFlags(MethodAccessFlags.PUBLIC);
aClass.getMethods().add(m);
Bytecode

Finally, bytecode! In Java, bytecode goes into a thing called a code attribute, which then goes into the method. Here's a sample:

CodeAttribute code = new CodeAttribute(new UTF8Entry(aClass.getConstantPool(), "Code"), 5, 2, 
    JvmInstructions.ALOAD.create(0),
    JvmInstructions.DUP.create()
);
m.getAttributes().add(code);

The code attribute has 4 arguments: a UTF8Entry pointing to the value Code (this is very important!), the max stack size, the max local variable size, and a list of bytecode instructions. In this case the instructions are aload 0, dup

Writing

What's the point of having a Java class if you can't write it? The JavaClass has a method write(OutputStream out) that writes the class in byte form. It can be used with a FileOutputStream, ByteArrayOutputStream, and all the other variants.

Todo

  • Add the rest of the bytecode instructions
  • Add more attributes
  • Add interface and field capabilities to classes
  • Add exception tables to the code attribute

javaclasslib's People

Contributors

seggan avatar

Watchers

 avatar  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.