Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leonardosnt/java-class-tools
Read and write java class files in Node.js or in the browser.
https://github.com/leonardosnt/java-class-tools
assembler browser class disassembler java javascript node parser reader writer
Last synced: 4 months ago
JSON representation
Read and write java class files in Node.js or in the browser.
- Host: GitHub
- URL: https://github.com/leonardosnt/java-class-tools
- Owner: leonardosnt
- License: mit
- Created: 2017-02-09T19:40:07.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-19T18:08:49.000Z (over 1 year ago)
- Last Synced: 2024-09-29T13:10:41.923Z (4 months ago)
- Topics: assembler, browser, class, disassembler, java, javascript, node, parser, reader, writer
- Language: JavaScript
- Homepage:
- Size: 453 KB
- Stars: 37
- Watchers: 5
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# java-class-tools
[![Build status](https://ci.appveyor.com/api/projects/status/dwfu9sq51uhofyyv?svg=true)](https://ci.appveyor.com/project/leonardosnt/java-class-tools)
---
The structure of the ClassFile object returned by `JavaClassFileReader#read` and its _inner objects_ matches the [Java Virtual Machine Specification](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html), therefore you can use this specification as a guide on how to understand and change a Class file.## Install
```
npm install java-class-tools
```## Examples:
- [Browser usage example](https://rawgit.com/leonardosnt/java-class-tools/master/examples/browser.html) (List all methods and fields of a class file)
- [Node usage example](https://repl.it/@leonardosnt/jct-example) (Simple disassembler)
- [Try yourself](https://runkit.com/leonardosnt/java-class-tools-example) (_RunKit:_ you must login with GitHub to be able to fork it)Print method names (Node.js):
```javascript
const { JavaClassFileReader } = require('java-class-tools');
const { TextDecoder } = require('util');const reader = new JavaClassFileReader();
const classFile = reader.read('./Foo.class');
const textDecoder = new TextDecoder();classFile.methods.forEach(md => {
/**
* Method name in constant-pool.
*
* Points to a CONSTANT_Utf8_info structure: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.7
*/
const nameInConstantPool = classFile.constant_pool[md.name_index];// It is also possible to use the string_decoder builtin module.
const name = textDecoder.decode(new Uint8Array(nameInConstantPool.bytes));console.log(name);
});
```Print method names (Node.js):
```htmlfetch('https://gist.githubusercontent.com/leonardosnt/69207dd9bcae55c93ff8fe6546c92eef/raw/fa008a94f9bc208cfa593cf568f0c504e3b30413/Class.class')
.then(r => r.arrayBuffer())
.then(printAllMethods);
function printAllMethods(classData) {
const reader = new JavaClassTools.JavaClassFileReader();
const classFile = reader.read(classData);
const textDecoder = new TextDecoder();classFile.methods.forEach(method => {
/**
* Method name in constant-pool.
*
* Points to a CONSTANT_Utf8_info structure: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4.7
*/
const nameEntry = classFile.constant_pool[method.name_index];const name = textDecoder.decode(new Uint8Array(nameEntry.bytes));
console.log(name);
});
}```
## License
Copyright (C) 2017-2020 leonardosnt <>
Licensed under the MIT License. See LICENSE file in the project root for full license information.