core tests: add inner classes in internal tests
This commit is contained in:
@@ -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);
|
||||
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());
|
||||
}
|
||||
}
|
||||
if (!"file".equalsIgnoreCase(resource.getProtocol())) {
|
||||
fail("Class is not stored in a file.");
|
||||
}
|
||||
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();
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
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 */")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user