Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
zlataovce committed Jun 18, 2024
1 parent 912dbe7 commit 1fbc4eb
Show file tree
Hide file tree
Showing 14 changed files with 3,902 additions and 4,574 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const fs = require("fs");
const { decompile } = require("./cfr.js"); // get it from the dist/ directory or jsDelivr

const data = fs.readFileSync("./your/package/HelloWorld.class"); // read a class file
console.log(decompile(data, {
console.log(decompile("your/package/HelloWorld", {
source: (name) => {
/* provide any additional classes for analysis here */
/* provide classes for analysis here, including the one you want to decompile */

console.log(name); /* internal name, e.g. java/lang/Object */
return null; /* class not available */
return name === "your/package/HelloWorld" ? data : null /* class not available */;
},
options: {
/* see https://github.com/leibnitz27/cfr/blob/master/src/org/benf/cfr/reader/util/getopt/OptionsImpl.java#L274 */
Expand Down
30 changes: 0 additions & 30 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
`java-library`
alias(libs.plugins.blossom)
alias(libs.plugins.teavm) // order matters?
}

Expand Down Expand Up @@ -39,32 +38,3 @@ tasks {
dependsOn("copyDist")
}
}

val Class<*>.bytes: ByteArray
get() = (classLoader ?: ClassLoader.getSystemClassLoader())
.getResourceAsStream(name.replace('.', '/') + ".class")!!
.use(`java.io`.InputStream::readAllBytes)

val ByteArray.base64String: String
get() = `java.util`.Base64.getEncoder().encodeToString(this)

fun net.kyori.blossom.SourceTemplateSet.classes(vararg klasses: kotlin.reflect.KClass<*>) {
properties.put("classes", klasses.associate { klass ->
klass.javaObjectType.name to klass.javaObjectType.bytes.base64String
})
}

sourceSets {
main {
blossom {
javaSources {
classes(
Object::class, Record::class, Enum::class,
Byte::class, Short::class, Int::class, Long::class,
Float::class, Double::class, Boolean::class, Character::class,
Void::class, String::class
)
}
}
}
}
8,121 changes: 3,869 additions & 4,252 deletions dist/cfr.js

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@
<input accept=".class" id="file-input" type="file">
<div id="file-content"></div>

<script src="https://cdn.jsdelivr.net/gh/leonardosnt/java-class-tools@master/dist/java-class-tools.min.js"></script>
<script type="module">
import { decompile } from "https://cdn.jsdelivr.net/gh/cephxdev/cfr@main/dist/cfr.js";

// const stripHeader = (s) => {
// const start = s.indexOf("/*\n");
// const end = s.indexOf(" */\n");
//
// if (start >= 0 && end > start) {
// return s.substring(0, start) + s.substring(end + 4);
// }
// return s;
// };
const decoder = new TextDecoder();
const readClassName = (buf) => {
const cf = new JavaClassTools.JavaClassFileReader().read(buf);
const cpEntry = cf.constant_pool[cf.constant_pool[cf.this_class].name_index];

return decoder.decode(new Uint8Array(cpEntry.bytes));
};

const input = document.getElementById("file-input");
input.value = null; // clear file input
Expand All @@ -75,14 +74,17 @@
if (file) {
const reader = new FileReader();
reader.onload = (evt) => {
const name = file.name.substring(0, file.name.indexOf(".class"));
const buf = new Uint8Array(evt.target.result);
const name = readClassName(buf);

const start = Date.now();
console.log(`Decompiling ${name}...`);

document.getElementById("file-content").textContent = // stripHeader(
decompile(new Int8Array(evt.target.result));
// );
document.getElementById("file-content").textContent = decompile(name, {
source: (n) => {
return n === name ? buf : null;
},
});

console.log(`Decompiled ${name} in ${Date.now() - start}ms.`);
};
Expand Down
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ teavm-core = { group = "org.teavm", name = "teavm-core", version.ref = "teavm" }

[plugins]
teavm = { id = "org.teavm", version.ref = "teavm" }
blossom = { id = "net.kyori.blossom", version = "2.1.0" }
23 changes: 0 additions & 23 deletions src/main/java-templates/dev/cephx/cfr/source/VMConstants.java.peb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.cephx.cfr.source;
package dev.cephx.cfr;

import org.benf.cfr.reader.api.ClassFileSource;
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair;
Expand All @@ -7,7 +7,7 @@
import java.util.List;
import java.util.function.Function;

public record SimpleClassSource(Function<String, byte[]> source) implements ClassFileSource {
record ClassSourceImpl(Function<String, byte[]> source) implements ClassFileSource {
@Override
public void informAnalysisRelativePathDetail(String usePath, String classFilePath) {
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/cephx/cfr/DecompilerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public interface DecompilerOptions extends JSObject {
@JSBody(script = "return this.source ? this.source : (() => { return null; });")
Source getSource();

default byte[] source(String name) {
return this.getSource().get(name);
}

interface Option extends JSObject {
@JSBody(script = "return this[0];")
String getName();
Expand Down
30 changes: 7 additions & 23 deletions src/main/java/dev/cephx/cfr/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package dev.cephx.cfr;

import dev.cephx.cfr.source.ByteArrayClassSource;
import dev.cephx.cfr.source.CompositeClassSource;
import dev.cephx.cfr.sink.OutputSinkFactoryImpl;
import dev.cephx.cfr.source.SimpleClassSource;
import dev.cephx.cfr.source.VMClassSource;
import org.benf.cfr.reader.api.CfrDriver;
import org.teavm.jso.JSByRef;
import org.teavm.jso.JSExport;
import org.teavm.jso.core.JSObjects;

Expand All @@ -16,31 +10,21 @@

public class Main {
@JSExport
public static String decompile(@JSByRef byte[] b, DecompilerOptions options) throws Throwable {
if (options == null || JSObjects.isUndefined(options)) {
options = JSObjects.create();
}
public static String decompile(String name, DecompilerOptions options) throws Throwable {
return decompile0(name, options == null || JSObjects.isUndefined(options) ? JSObjects.create() : options);
}

private static String decompile0(String name, DecompilerOptions options) throws Throwable {
final var sinkFactory = new OutputSinkFactoryImpl();
final var source = new ByteArrayClassSource(b);
new CfrDriver.Builder()
.withClassFileSource(
new CompositeClassSource(
source,
new SimpleClassSource(options.getSource()::get),
VMClassSource.INSTANCE
)
)
.withClassFileSource(new ClassSourceImpl(options::source))
.withOutputSink(sinkFactory)
.withOptions(
Arrays.stream(options.getOptions())
.collect(Collectors.toMap(
DecompilerOptions.Option::getName,
DecompilerOptions.Option::getValue
))
.collect(Collectors.toMap(DecompilerOptions.Option::getName, DecompilerOptions.Option::getValue))
)
.build()
.analyse(List.of(source.name()));
.analyse(List.of(name));

return sinkFactory.outputOrThrow();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.cephx.cfr.sink;
package dev.cephx.cfr;

import org.benf.cfr.reader.api.OutputSinkFactory;

import java.util.Collection;
import java.util.List;

public final class OutputSinkFactoryImpl implements OutputSinkFactory {
final class OutputSinkFactoryImpl implements OutputSinkFactory {
private Throwable exception;
private String output;

Expand Down
39 changes: 0 additions & 39 deletions src/main/java/dev/cephx/cfr/source/ByteArrayClassSource.java

This file was deleted.

96 changes: 0 additions & 96 deletions src/main/java/dev/cephx/cfr/source/ClassFileUtil.java

This file was deleted.

Loading

0 comments on commit 1fbc4eb

Please sign in to comment.