Skip to content

mrsalwater/Kapteyn

Repository files navigation

Kapteyn

A Java library for reverse engineering Java class files and jars. It contains a class file parser for accurate class file representation and a modern and fast bytecode decompiler. It is currently under development.

Note: Currently only capable of parsing Java 8 files

Features

How to build

git clone https://github.com/mrsalwater/Kapteyn.git
cd Kapteyn
./gradlew build

How to use

Class File Decompiling

public static String decompile(byte[] bytes) throws ClassFileException {
    ClassFileParser classFileParser = new ClassFileParser(bytes);
    ClassFile classFile = classFileParser.parse();

    ByteCodeParser byteCodeParser = new ByteCodeParser(classFile);
    ByteCodeFile byteCodeFile = byteCodeParser.parse();

    return byteCodeFile.getSource();
}

Class File Parsing

ClassFileParser classFileParser = new ClassFileParser(bytes);
ClassFile classFile = classFileParser.parse();

Class File Version

if (ClassFileVersion.match(classFile.getMajorVersion()) == ClassFileVersion.JAVA_8) {
    /* ... */
}

Constant Pool

ConstantPool constantPool = classFile.getConstantPool();
String thisClassName = constantPool.getUTF8(classFile.getThisClass()).getValue();
String superClassName = constantPool.getUTF8(classFile.getSuperClass()).getValue();

Access Flags

AccessFlags accessFlags = new AccessFlags(classFile.getAccessFlags());
if (accessFlags.isPublic() && accessFlags.isStatic()) {
    /* ... */
}

Interfaces

for (int index : classFile.getInterfaces()) {
    String interfaceName = constantPool.getUTF8(constantPool.getClass(index).getNameIndex()).getValue();
    /* ... */
}

Fields

for (Field field : classFile.getFields()) {
    String fieldName = constantPool.getUTF8(field.getNameIndex()).getValue();
    String fieldDescriptor = constantPool.getUTF8(field.getDescriptorIndex()).getValue();
    /* ... */
}

Methods

for (Method method : classFile.getMethods()) {
    String methodName = constantPool.getUTF8(method.getNameIndex()).getValue();
    String methodDescriptor = constantPool.getUTF8(method.getDescriptorIndex()).getValue();
    /* ... */
}

Attributes

Attributes classAttributes = classFile.getAttributes();
if (classAttributes.has(AttributeRuntimeVisibleAnnotations.class)) {
    AttributeRuntimeVisibleAnnotations annotations = classAttributes.get(AttributeRuntimeVisibleAnnotations.class);
    /* ... */
}

References

  • Java documentation
    The official defining document of the Java Virtual Machine
    Authors: Tim Lindholm, Frank Yellin, Gilad Bracha, Alex Buckley (2015-02-13)
  • Fernflower
    "Fernflower is the first actually working analytical decompiler for Java and probably for a high-level programming language in general"
    Author: JetBrains
  • Procyon
    "Procyon is a suite of Java metaprogramming tools focused on code generation and analysis"
    Author: Stefan Steiger
  • JD-Core
    "JD-Core is a Java decompiler written in Java"
    Author: Emmanuel Dupuy

This software is licensed under the MIT License
Copyright 2020 mrsalwater

About

A Java library for reverse engineering Java class files and Jars

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages