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; ...@@ -11,7 +11,9 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
...@@ -74,33 +76,39 @@ public abstract class InternalJadxTest { ...@@ -74,33 +76,39 @@ public abstract class InternalJadxTest {
} }
public File getJarForClass(Class<?> cls) throws IOException { public File getJarForClass(Class<?> cls) throws IOException {
File classFile = getClassFile(cls); String path = cls.getPackage().getName().replace('.', '/');
String shortClsFileName = cls.getName().replace('.', '/') + ".class"; List<File> list = getClassFilesWithInners(cls);
File temp = File.createTempFile("jadx-tmp-", System.nanoTime() + ".jar"); File temp = File.createTempFile("jadx-tmp-", System.nanoTime() + ".jar");
JarOutputStream jo = new JarOutputStream(new FileOutputStream(temp)); JarOutputStream jo = new JarOutputStream(new FileOutputStream(temp));
add(classFile, shortClsFileName, jo); for (File file : list) {
add(file, path + "/" + file.getName(), jo);
}
jo.close(); jo.close();
temp.deleteOnExit(); temp.deleteOnExit();
return temp; return temp;
} }
private File getClassFile(Class<?> cls) { private List<File> getClassFilesWithInners(Class<?> cls) {
String path = cutPackage(cls) + ".class"; List<File> list = new ArrayList<File>();
URL resource = cls.getResource(path); String pkgName = cls.getPackage().getName();
if (resource == null) { URL pkgResource = ClassLoader.getSystemClassLoader().getResource(pkgName.replace('.', '/'));
fail("Class file not found: " + path); 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));
}
} }
if (!"file".equalsIgnoreCase(resource.getProtocol())) { } catch (URISyntaxException e) {
fail("Class is not stored in a file."); fail(e.getMessage());
} }
return new File(resource.getPath());
} }
return list;
private String cutPackage(Class<?> cls) {
String longName = cls.getName();
String pkg = cls.getPackage().getName();
return longName.substring(pkg.length() + 1, longName.length());
} }
private void add(File source, String entryName, JarOutputStream target) throws IOException { private void add(File source, String entryName, JarOutputStream target) throws IOException {
...@@ -114,8 +122,9 @@ public abstract class InternalJadxTest { ...@@ -114,8 +122,9 @@ public abstract class InternalJadxTest {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
while (true) { while (true) {
int count = in.read(buffer); int count = in.read(buffer);
if (count == -1) if (count == -1) {
break; break;
}
target.write(buffer, 0, count); target.write(buffer, 0, count);
} }
target.closeEntry(); 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