Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static Decompiler<?> valueOf(String engineName) {
*
* Gets the default decompiler (currently Vineflower)
* @since 4.2.1
* @return the default decompiler (currently Vineflower)
* @return the default decompiler
*/
public static Decompiler<?> getDefault() {
return valueOf("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package com.heliosdecompiler.transformerapi.common;

import org.jetbrains.java.decompiler.main.extern.IResultSaver;

import java.util.HashMap;
import java.util.Map;
import java.util.jar.Manifest;

import jd.core.DecompilationResult;

public abstract class AbstractResultSaver {
public abstract class AbstractResultSaver implements IResultSaver {

private static final String UNEXPECTED = "Unexpected";

Expand Down Expand Up @@ -57,6 +59,7 @@ public void saveFolder(String path) {
public void copyFile(String source, String path, String entryName) {
}

@Override
public void saveClassFile(String path, String qualifiedName, String entryName, String content, int[] mapping) {
if (mapping != null) {
lineNumbers = true;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public class FileLoader implements Loader {

protected static final Pattern CLASS_SUFFIX_PATTERN = Pattern.compile("\\.class$");

private final Path rootDirectory;

private final HashMap<String, byte[]> map = new HashMap<>();

public FileLoader(String rootLocation, String pkg, String className) throws IOException {
Objects.requireNonNull(rootLocation, "rootLocation");
Objects.requireNonNull(className, "className");

Path rootDirectory = Paths.get(rootLocation);
this.rootDirectory = Paths.get(rootLocation);
Validate.isTrue(Files.isDirectory(rootDirectory), "Not a directory: " + rootDirectory);

String topLevelTypeName = stripClassSuffix(className);
Expand Down Expand Up @@ -95,11 +97,27 @@ protected String makeEntryName(String entryName) {

@Override
public byte[] load(String internalName) throws IOException {
return map.get(stripClassSuffix(internalName));
String key = stripClassSuffix(internalName);
byte[] data = map.get(key);
if (data != null) {
return data;
}
Path classFile = classFilePath(key);
if (!Files.isRegularFile(classFile)) {
return data;
}
data = Files.readAllBytes(classFile);
map.put(key, data);
return data;
}

@Override
public boolean canLoad(String internalName) {
return map.containsKey(stripClassSuffix(internalName));
String key = stripClassSuffix(internalName);
return map.containsKey(key) || Files.isRegularFile(classFilePath(key));
}

protected Path classFilePath(String internalName) {
return rootDirectory.resolve(internalName + ".class");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public byte[] load(String internalName) throws IOException {
}
}
}
return null;
return classContents;
}

private static ZipFile openZipFile(String classpathEntry) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2026 Nicolas Baumann (@nbauma109)
*
* Licensed under the Apache License, Version 2.0.
*/

package com.heliosdecompiler.transformerapi.common;

import java.util.Map;

import jd.core.DecompilationResult;
import jd.core.links.DeclarationData;
import jd.core.links.HyperlinkReferenceData;
import jd.core.links.ReferenceData;

/**
* Shared helpers for writing declarations and hyperlinks into a {@link DecompilationResult}.
*
* @since 4.2.3
*/
public final class ResultLinkSupport {

/**
* Normalized symbol target used before writing JD-style link entries.
*/
public record LinkTarget(String typeName, String name, String descriptor) {
}

private ResultLinkSupport() {
}

/**
* Register a declaration using the key format expected by JD-style consumers.
*/
public static void addDeclaration(DecompilationResult result, int start, int length, LinkTarget target) {
DeclarationData data = new DeclarationData(start, length, target.typeName(), target.name(), target.descriptor());
result.addDeclaration(declarationKey(target), data);
if (target.name() == null) {
result.addTypeDeclaration(start, data);
}
}

/**
* Register a hyperlink backed by a cached reference object.
*/
public static void addReference(DecompilationResult result, Map<String, ReferenceData> cache, int start, int length, LinkTarget target, String scopeInternalName) {
ReferenceData reference = reference(result, cache, target, scopeInternalName);
result.addHyperLink(start, new HyperlinkReferenceData(start, length, reference));
}

/**
* Resolve or create a reference so repeated mentions reuse the same instance.
*/
public static ReferenceData reference(DecompilationResult result, Map<String, ReferenceData> cache, LinkTarget target, String scopeInternalName) {
// A stable cache key keeps the result model compact when the same target is referenced many times.
String key = declarationKey(target) + '-' + scopeInternalName;
return cache.computeIfAbsent(key, ignored -> {
ReferenceData reference = new ReferenceData(target.typeName(), target.name(), target.descriptor(), scopeInternalName);
reference.setEnabled(true);
result.addReference(reference);
return reference;
});
}

/**
* Build the declaration lookup key used by {@link DecompilationResult#getDeclarations()}.
*/
public static String declarationKey(LinkTarget target) {
if (target.name() == null) {
return target.typeName();
}
return target.typeName() + '-' + target.name() + '-' + target.descriptor();
}

/**
* Measure the identifier length at a source offset.
*/
public static int identifierLength(String code, int start) {
int end = start;
while (end < code.length() && Character.isJavaIdentifierPart(code.charAt(end))) {
end++;
}
return end - start;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ default ClassStruct readClassAndInnerClasses(Map<String, byte[]> importantData,
}
}
}
if (classNode.superName != null && !"java/lang/Object".equals(classNode.superName) && loader.canLoad(classNode.superName)) {
importantData.putAll(readClassAndInnerClasses(importantData, loader, classNode.superName).importantData);
}
}
return new ClassStruct(fullClassName, importantData);
}
Expand Down Expand Up @@ -114,15 +117,17 @@ public IOutputSink createOutputSink(IResultSaver saver) {
return new IOutputSink() {
@Override
public void close() throws IOException {
// not used
}

@Override
public void begin() {
// not used
}

@Override
public void acceptOther(String path) {
// not used
// not used
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public CFRDataSource(Loader loader, byte[] data, String name, Options options) {

@Override
public void informAnalysisRelativePathDetail(String usePath, String classFilePath) {
// not used
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright 2026 Nicolas Baumann (@nbauma109)
* Copyright 2017 Sam Sun <github-contact@samczsun.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,6 +22,7 @@
import org.benf.cfr.reader.util.CfrVersionInfo;
import org.benf.cfr.reader.util.getopt.OptionsImpl;

import com.heliosdecompiler.transformerapi.common.BytecodeSourceLinker;
import com.heliosdecompiler.transformerapi.common.Loader;
import com.heliosdecompiler.transformerapi.decompilers.Decompiler;

Expand All @@ -30,6 +32,9 @@
import java.util.Scanner;
import jd.core.DecompilationResult;

/**
* Provides a gateway to the CFR decompiler.
*/
public class CFRDecompiler extends Decompiler.AbstractDecompiler implements Decompiler<CFRSettings> {

public CFRDecompiler(String name) {
Expand All @@ -39,6 +44,7 @@ public CFRDecompiler(String name) {
@Override
public DecompilationResult decompile(Loader loader, String internalName, CFRSettings settings) throws IOException {
StopWatch stopWatch = StopWatch.createStarted();
ClassStruct classStruct = readClassAndInnerClasses(loader, internalName);
CFROutputStreamFactory sink = new CFROutputStreamFactory();
String entryPath = internalName + ".class";
OptionsImpl options = new OptionsImpl(settings.getSettings());
Expand All @@ -55,6 +61,8 @@ public DecompilationResult decompile(Loader loader, String internalName, CFRSett
}
DecompilationResult decompilationResult = new DecompilationResult();
decompilationResult.setDecompiledOutput(resultCode);
// CFR emits text only, so link metadata is reconstructed from the source after decompilation.
BytecodeSourceLinker.link(decompilationResult, resultCode, classStruct.fullClassName(), classStruct.importantData());
time = stopWatch.getTime();
return decompilationResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright 2026 Nicolas Baumann (@nbauma109)
* Copyright 2017 Sam Sun <github-contact@samczsun.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -23,6 +24,7 @@
import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider;
import org.jetbrains.java.decompiler.struct.StructContext;

import com.heliosdecompiler.transformerapi.common.BytecodeSourceLinker;
import com.heliosdecompiler.transformerapi.common.Loader;
import com.heliosdecompiler.transformerapi.decompilers.Decompiler;

Expand All @@ -33,7 +35,7 @@
import jd.core.DecompilationResult;

/**
* Provides a gateway to the Fernflower decompiler
* Provides a gateway to the Fernflower decompiler.
*/
public class FernflowerDecompiler extends Decompiler.AbstractDecompiler implements Decompiler<FernflowerSettings> {

Expand Down Expand Up @@ -63,7 +65,10 @@ public DecompilationResult decompile(Loader loader, String internalName, Fernflo
baseDecompiler.clearContext();
}
String key = classStruct.fullClassName();
decompilationResult.setDecompiledOutput(saver.getResults().get(key));
String source = saver.getResults().get(key);
decompilationResult.setDecompiledOutput(source);
// Fernflower does not expose token callbacks, so links are synthesized from source and bytecode.
BytecodeSourceLinker.link(decompilationResult, source, key, classStruct.importantData());
}
time = stopWatch.getTime();
return decompilationResult;
Expand Down
Loading
Loading