Commit 36cfc9d1 authored by Skylot's avatar Skylot

core tests: add inner classes in internal tests

parent b2f189b5
......@@ -11,7 +11,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
......@@ -74,33 +76,39 @@ public abstract class InternalJadxTest {
}
public File getJarForClass(Class<?> cls) throws IOException {
File classFile = getClassFile(cls);
String shortClsFileName = cls.getName().replace('.', '/') + ".class";
String path = cls.getPackage().getName().replace('.', '/');
List<File> list = getClassFilesWithInners(cls);
File temp = File.createTempFile("jadx-tmp-", System.nanoTime() + ".jar");
JarOutputStream jo = new JarOutputStream(new FileOutputStream(temp));
add(classFile, shortClsFileName, jo);
for (File file : list) {
add(file, path + "/" + file.getName(), jo);
}
jo.close();
temp.deleteOnExit();
return temp;
}
private File getClassFile(Class<?> cls) {
String path = cutPackage(cls) + ".class";
URL resource = cls.getResource(path);
if (resource == null) {
fail("Class file not found: " + path);
}
if (!"file".equalsIgnoreCase(resource.getProtocol())) {
fail("Class is not stored in a file.");
private List<File> getClassFilesWithInners(Class<?> cls) {
List<File> list = new ArrayList<File>();
String pkgName = cls.getPackage().getName();
URL pkgResource = ClassLoader.getSystemClassLoader().getResource(pkgName.replace('.', '/'));
if (pkgResource != null) {
try {
String clsName = cls.getName();
File directory = new File(pkgResource.toURI());
String[] files = directory.list();
for (String file : files) {
String fullName = pkgName + "." + file;
if (fullName.startsWith(clsName)) {
list.add(new File(directory, file));
}
}
} catch (URISyntaxException e) {
fail(e.getMessage());
}
}
return new File(resource.getPath());
}
private String cutPackage(Class<?> cls) {
String longName = cls.getName();
String pkg = cls.getPackage().getName();
return longName.substring(pkg.length() + 1, longName.length());
return list;
}
private void add(File source, String entryName, JarOutputStream target) throws IOException {
......@@ -114,8 +122,9 @@ public abstract class InternalJadxTest {
byte[] buffer = new byte[1024];
while (true) {
int count = in.read(buffer);
if (count == -1)
if (count == -1) {
break;
}
target.write(buffer, 0, count);
}
target.closeEntry();
......
package jadx.tests.internal;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class TestInnerClass extends InternalJadxTest {
public static class TestCls {
public class Inner {
public class Inner2 {
}
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, containsString("Inner"));
assertThat(code, containsString("Inner2"));
// assertThat(code, not(containsString("this$0")));
// assertThat(code, not(containsString("super()")));
// assertThat(code, not(containsString("/* synthetic */")));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment