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
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,6 @@ $RECYCLE.BIN/
# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,73 +1,74 @@
package com.heliosdecompiler.transformerapi;

import org.apache.commons.io.IOUtils;
import org.jd.core.v1.loader.ClassPathLoader;
import org.junit.Test;

import com.heliosdecompiler.transformerapi.common.Loader;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;

import jd.core.DecompilationResult;

import static org.junit.Assert.assertEquals;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_CFR;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_PROCYON;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_FERNFLOWER;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_VINEFLOWER;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_JADX;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_JD_CORE_V0;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_JD_CORE_V1;

import static jd.core.preferences.Preferences.WRITE_METADATA;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_PROCYON;
import static com.heliosdecompiler.transformerapi.StandardTransformers.Decompilers.ENGINE_VINEFLOWER;
import static jd.core.preferences.Preferences.WRITE_LINE_NUMBERS;
import static jd.core.preferences.Preferences.WRITE_METADATA;
import static org.junit.Assert.assertEquals;

import jd.core.DecompilationResult;

public class StandardTransformersTest {

@Test
public void testDecompileCFR() throws Exception {
testDecompile("/HelloWorldCFR.txt", ENGINE_CFR, Map.of("showversion", "false"));
testDecompile("/TestCompactCFR.txt", ENGINE_CFR, Map.of("showversion", "false"));
}

@Test
public void testDecompileProcyon() throws Exception {
testDecompile("/HelloWorldProcyon.txt", ENGINE_PROCYON, Map.of("SuppressBanner", "true"));
testDecompile("/TestCompactProcyon.txt", ENGINE_PROCYON, Map.of("SuppressBanner", "true"));
}

@Test
public void testDecompileJADX() throws Exception {
testDecompile("/HelloWorldJADX.txt", ENGINE_JADX, Collections.emptyMap());
testDecompile("/TestCompactJADX.txt", ENGINE_JADX, Collections.emptyMap());
}

@Test
public void testDecompileFernflower() throws Exception {
testDecompile("/HelloWorldFernflower.txt", ENGINE_FERNFLOWER, Collections.emptyMap());
testDecompile("/TestCompactFernflower.txt", ENGINE_FERNFLOWER, Collections.emptyMap());
}

@Test
public void testDecompileVineflower() throws Exception {
testDecompile("/HelloWorldVineflower.txt", ENGINE_VINEFLOWER, Collections.emptyMap());
testDecompile("/TestCompactVineflower.txt", ENGINE_VINEFLOWER, Collections.emptyMap());
}

@Test
public void testDecompileJDCoreV0() throws Exception {
testDecompile("/HelloWorldJDCoreV0.txt", ENGINE_JD_CORE_V0, Collections.emptyMap());
testDecompile("/TestCompactJDCoreV0.txt", ENGINE_JD_CORE_V0, Collections.emptyMap());
}

@Test
public void testDecompileJDCoreV1() throws Exception {
testDecompile("/HelloWorldJDCoreV1.txt", ENGINE_JD_CORE_V1, Map.of(WRITE_LINE_NUMBERS, "false", WRITE_METADATA, "false"));
testDecompile("/TestCompactJDCoreV1.txt", ENGINE_JD_CORE_V1, Map.of(WRITE_LINE_NUMBERS, "false", WRITE_METADATA, "false"));
}

private void testDecompile(String path, String engineName, Map<String, String> preferences)
throws TransformationException, IOException, IllegalAccessException, InvocationTargetException {
ClassPathLoader classPathLoader = new ClassPathLoader();
Loader loader = new Loader(classPathLoader::canLoad, classPathLoader::load);
String internalName = HelloWorld.class.getName().replace('.', '/');
throws TransformationException, IOException, IllegalAccessException, InvocationTargetException, URISyntaxException {
URI resource = getClass().getResource("/test-compact-expand-inline.jar").toURI();
ZipLoader zipLoader = new ZipLoader(resource.toURL().openStream());
Loader loader = new Loader(zipLoader::canLoad, zipLoader::load, resource);
String internalName = "test/TestCompact";
DecompilationResult result = StandardTransformers.decompile(loader, internalName, preferences, engineName);
assertEqualsIgnoreEOL(getResourceAsString(path), result.getDecompiledOutput());
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/heliosdecompiler/transformerapi/ZipLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.heliosdecompiler.transformerapi;

import org.jd.core.v1.util.StringConstants;

import java.io.IOException;
import java.io.InputStream;

public class ZipLoader extends org.jd.core.v1.util.ZipLoader {

public ZipLoader(InputStream in) throws IOException {
super(in);
}

@Override
protected String makeEntryName(String entryName) {
return entryName;
}

@Override
public byte[] load(String internalName) throws IOException {
return getMap().get(internalName + StringConstants.CLASS_FILE_SUFFIX);
}

@Override
public boolean canLoad(String internalName) {
return getMap().containsKey(internalName + StringConstants.CLASS_FILE_SUFFIX);
}
}
10 changes: 0 additions & 10 deletions src/test/resources/HelloWorldCFR.txt

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/resources/HelloWorldFernflower.txt

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/resources/HelloWorldJADX.txt

This file was deleted.

9 changes: 0 additions & 9 deletions src/test/resources/HelloWorldJDCoreV0.txt

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/resources/HelloWorldJDCoreV1.txt

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/resources/HelloWorldProcyon.txt

This file was deleted.

7 changes: 0 additions & 7 deletions src/test/resources/HelloWorldVineflower.txt

This file was deleted.

161 changes: 161 additions & 0 deletions src/test/resources/TestCompactCFR.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Decompiled with CFR.
*/
package test;

public abstract class TestCompact {
public void method1(boolean flag) {
this.log("Test.method1 start");
if (flag) {
this.log("Test.method1 if");
} else {
this.log("Test.method1 else");
}
this.log("Test.method1 end");
}

public void method2() {
this.log("Test.method2 start");
try {
this.log("Test.method2 try");
}
catch (Exception e) {
this.log("Test.method2 catch");
}
finally {
this.log("Test.method2 finally");
}
this.log("Test.method2 end");
}

public void method3(int[] arr) {
this.log("Test.method3 start");
for (int i = 0; i < arr.length; ++i) {
this.log("Test.method3 for");
}
this.log("Test.method3 end");
}

public void method4(int i) {
this.log("Test.method4 start");
switch (i) {
case 0: {
this.log("Test.method4 case 0");
break;
}
case 1: {
this.log("Test.method4 case 1");
break;
}
case 2: {
this.log("Test.method4 case 2");
break;
}
case 3: {
this.log("Test.method4 case 3");
break;
}
default: {
this.log("Test.method4 default");
}
}
this.log("Test.method4 end");
}

public String method5(int i) {
this.log("Test.method5 start");
String s = switch (i) {
case 0 -> "Test.method4 case 0";
case 1 -> "Test.method4 case 1";
case 2 -> "Test.method4 case 2";
case 3 -> "Test.method4 case 3";
default -> throw new IllegalArgumentException();
};
return "return:" + s;
}

public void method5() {
this.log("Test.method5");
}

public void method6() {
this.log("Test.method6");
}

public abstract void log(String var1);

class Inner4 {
public Inner4() {
TestCompact.this.log("Inner4 constructor");
}

public void method1() {
TestCompact.this.log("Inner4.method1");
}

public void method2() {
TestCompact.this.log("Inner4.method2");
}

public boolean equals(Object o) {
throw new UnsupportedOperationException("equals");
}

public int hashCode() {
throw new UnsupportedOperationException("hashCode");
}

public String toString() {
throw new UnsupportedOperationException("toString");
}
}

class Inner3 {
public Inner3() {
TestCompact.this.log("Inner3 constructor");
}

public void method1() {
new Thread(new Runnable(){

@Override
public void run() {
TestCompact.this.log("Inner3.method1");
}
}).start();
}

public void method2() {
new Thread(() -> {
TestCompact.this.log("Inner3.method2a");
TestCompact.this.log("Inner3.method2b");
}).start();
}

public void method3() {
new Thread(() -> TestCompact.this.log("Inner3.method3")).start();
}
}

class Inner1 {
public Inner1() {
TestCompact.this.log("Inner1 constructor");
}

public void method1(boolean flag) {
TestCompact.this.log("Inner1.method1 start");
while (flag) {
TestCompact.this.log("Inner1.method1 while");
}
TestCompact.this.log("Inner1.method1 end");
}

public void method2(boolean flag) {
TestCompact.this.log("Inner1.method2 start");
do {
TestCompact.this.log("Inner1.method2 do while");
} while (flag);
TestCompact.this.log("Inner1.method2 end");
}
}
}
Loading
Loading