bc2json

Convert java bytecode to json representation

License

License

Categories

Categories

JSON Data
GroupId

GroupId

com.github.racaljk
ArtifactId

ArtifactId

bc2json
Last Version

Last Version

1.4.1
Release Date

Release Date

Type

Type

jar
Description

Description

bc2json
Convert java bytecode to json representation
Project URL

Project URL

https://github.com/racaljk/bc2json
Source Code Management

Source Code Management

https://github.com/racaljk/bc2json

Download bc2json

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.racaljk/bc2json/ -->
<dependency>
    <groupId>com.github.racaljk</groupId>
    <artifactId>bc2json</artifactId>
    <version>1.4.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.racaljk/bc2json/
implementation 'com.github.racaljk:bc2json:1.4.1'
// https://jarcasting.com/artifacts/com.github.racaljk/bc2json/
implementation ("com.github.racaljk:bc2json:1.4.1")
'com.github.racaljk:bc2json:jar:1.4.1'
<dependency org="com.github.racaljk" name="bc2json" rev="1.4.1">
  <artifact name="bc2json" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.racaljk', module='bc2json', version='1.4.1')
)
libraryDependencies += "com.github.racaljk" % "bc2json" % "1.4.1"
[com.github.racaljk/bc2json "1.4.1"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.5

Project Modules

There are no modules declared in this project.

Java bytecode to JSON representation

codebeat badge Maven Central

bc2json could convert *.class bytecode to json representation so that we can use it as a human readable and also the world's most popular IR(ok, that's json) for further use. Parsing behaviors are based on jvm8 specification. All elements on specification would be parsed into json.

Quick start

Step 0: Recommend to introduce bc2json and its dependencies by maven

<dependency>
  <groupId>com.github.racaljk</groupId>
  <artifactId>bc2json</artifactId>
  <version>1.4.1/version>
</dependency>

Step 1: You can load bytecode from local file

B2Json b2Json = B2Json.fromFilePath("/path/to/Test.class");

Optional step 2: and then set some options

b2Json.withOption(Option.PRETTY_PRINTING);
b2Json.withOption(Option.MORE_READABLE);

Step 3: Got it

String result = b2Json.toJsonString();
System.out.println(result);
// Or persist the result string to disk
b2Json.toJsonFile("test.json");

Demonstrations of options

You can set these options according to your demands

  • Without any options
{"magic":-889275714,"minor_version":0,"major_version":52,"constants":{"slotsNum":"37","slots":[
{"index":1,"type":"parser.classfile.constantpool.ConstantMethodRefInfo","method_class":"java/lang/Object",
"method_name":"\u003cinit\u003e","method_type":"()V"},...]},"access_flag":33,"this_class":"Test",
"super_class":"java/lang/Object","interfaces":[],"fields":[{"field_name":"greeting","field_type":
"Ljava/lang/String;","access_flag":"26"}],"methods":[{"method_name":"\u003cinit\u003e","method_type"
:"()V","method_flag":"1","method_opcode":"42,183,0,1,177"},...],"classfile_attributes":[{"attribute_name":
"SourceFileAttribute","source_file":"Test.java"},...]}
  • Option.PRETTY_PRINTING
{
  "magic": -889275714,
  "minor_version": 0,
  "major_version": 52,
  "constants": {
    "slotsNum": "37",
    "slots": [
      {
        "index": 1,
        "type": "parser.classfile.constantpool.ConstantMethodRefInfo",
        "method_class": "java/lang/Object",
        "method_name": "\u003cinit\u003e",
        "method_type": "()V"
      },
      ...
    ]
  },
  "access_flag": 33,
  "this_class": 26,
  "super_class": 30,
  "interfaces": [],
  "fields": [
    {
      "field_name": "greeting",
      "field_type": "Ljava/lang/String;",
      "access_flag": "26"
    }
  ],
  "methods": [
    {
      "method_name": "\u003cinit\u003e",
      "method_type": "()V",
      "method_flag": "1",
      "method_opcode": "42,183,0,1,177"
    },
    ...
  ],
  "classfile_attributes": [
    {
      "attribute_name": "SourceFileAttribute",
      "source_file": "Test.java"
    },
    ...
  ]
}
  • Option.PRETTY_PRINTING and Option.MORE_READABLE
{
  "magic": "cafebabe",
  "version": "52.0",
  "constants": {
    "slotsNum": "37",
    "slots": [
      {
        "index": 1,
        "type": "parser.classfile.constantpool.ConstantMethodRefInfo",
        "method_class": "java.lang.Object",
        "method": " \u003cinit\u003e()"
      },
      ...
    ]
  },
  "access_flag": "public super ",
  "this_class": "Test",
  "super_class": "java.lang.Object",
  "interfaces": [],
  "fields": [
    {
      "field": "private static final java.lang.String greeting"
    }
  ],
  "methods": [
    {
      "method_signature": "public \u003cinit\u003e()",
      "method_opcode": "aload_0,invokespecial,nop,aconst_null,return"
    },
    ...
  ],
  "classfile_attributes": [
    {
      "attribute_name": "SourceFileAttribute",
      "source_file": "Test.java"
    },
    ...
  ]
}

Documentation

See its javadoc for full details.

License

Yes, I love the MIT LICENSE

Versions

Version
1.4.1
1.4.0
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0