A library that allows you to read, manipulate and write Java bytecode (.class files)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.externalToolBuilders
src/main/java/org/macroing/cel4j/java/binary
.classpath
.gitignore
.project
COPYING
COPYING.LESSER
README.md
build.xml

README.md

CEL4J Java Binary (v. 0.1.0)

CEL4J Java Binary is a library that allows you to read, manipulate and write Java bytecode (.class files).

It's part of the Code Engineering Library for Java (CEL4J) project hosted by Macroing.org.

Supported Features

  • A set of APIs that define a model of a .class file.
  • An API that models descriptors and signatures.
  • An API with NodeFilters to filter parts of the .class file model.
  • An API to read .class files.
  • A support API with a bunch of useful methods.
  • An API with extensions of NodeHierarchicalVisitor and NodeVisitor.

Examples

The following example reads a ClassFile and finds all its method_infos with an access flag of ACC_STATIC set.

import java.util.List;

import org.macroing.cel4j.java.binary.classfile.ClassFile;
import org.macroing.cel4j.java.binary.classfile.MethodInfo;
import org.macroing.cel4j.java.binary.filter.MethodInfoFilters;
import org.macroing.cel4j.java.binary.node.NodeFilter;
import org.macroing.cel4j.java.binary.reader.ClassFileReader;

public class ReadClassFileExample {
    public static void main(String[] args) {
        ClassFileReader classFileReader = ClassFileReader.newInstance();
        
        ClassFile classFile = classFileReader.readClassFile(String.class);
        
        NodeFilter nodeFilter = MethodInfoFilters.newStaticMethodInfoFilter();
        
        List<MethodInfo> methodInfos = NodeFilter.filter(classFile, nodeFilter, MethodInfo.class);
    }
}

The following example prints out all instructions from all method_infos in a ClassFile using Java 8 Lambdas.

import org.macroing.cel4j.java.binary.classfile.attributeinfo.CodeAttribute;
import org.macroing.cel4j.java.binary.reader.ClassFileReader;

public class PrintInstructionsExample {
    public static void main(String[] args) {
        ClassFileReader.newInstance().readClassFile(String.class).getMethodInfos().forEach(methodInfo -> {
            methodInfo.getAttributeInfos().forEach(attributeInfo -> {
                if(attributeInfo instanceof CodeAttribute) {
                    System.out.println();
                    
                    CodeAttribute codeAttribute = CodeAttribute.class.cast(attributeInfo);
                    codeAttribute.getInstructions().forEach(instruction -> {
                        System.out.println(instruction);
                    });
                }
            });
        });
    }
}

The following example demonstrates how you can find out about the descriptors and signatures in a ClassFile structure.

import org.macroing.cel4j.java.binary.classfile.ClassFile;
import org.macroing.cel4j.java.binary.reader.ClassFileReader;
import org.macroing.cel4j.java.binary.support.ClassFiles;

public class DescriptorAndSignatureExample {
    public static void main(String[] args) {
        ClassFile classFile = ClassFileReader.newInstance().readClassFile(java.util.Collection.class);
        ClassFiles.getAllSignatures(classFile).forEach(signature -> System.out.println(signature.toExternalForm()));
        ClassFiles.getAllFieldDescriptors(classFile).forEach(fieldDescriptor -> System.out.println(fieldDescriptor.toExternalForm()));
        ClassFiles.getAllMethodDescriptors(classFile).forEach(methodDescriptor -> System.out.println(methodDescriptor.toExternalForm()));
        ClassFiles.getClassSignature(classFile).ifPresent(signature -> System.out.println(signature.toExternalForm()));
    }
}

Dependencies

Note

This library has not been properly released yet. This means, even though it says it's version 1.0.0 in the build.xml file and all Java source code files, it should not be treated as such. When this library gets released, it will be tagged and available on the "releases" page.