Skip to content

Commit

Permalink
Merge pull request #24 from moacirrf/23-windows-10-fail-to-decompile
Browse files Browse the repository at this point in the history
Fix problems on windows.
  • Loading branch information
moacirrf authored Jun 6, 2022
2 parents 8733f83 + 11fe229 commit 146ea81
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 68 deletions.
25 changes: 5 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
<licenseName>GNU GENERAL PUBLIC LICENSE 3.0</licenseName>
<licenseFile>LICENSE</licenseFile>
<!-- Path of Netbeans instalation.
<netbeansInstallation>${netbeansInstalationPath}</netbeansInstallation>
-->
<netbeansInstallation>${netbeansInstalationPath}</netbeansInstallation> -->
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -64,26 +63,12 @@

<!-- Junit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>


<!-- Netbeans modules -->
<dependency>
<groupId>org.netbeans.api</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.benf.cfr.reader.util.output.ProgressDumperNop;
import org.benf.cfr.reader.util.output.StringStreamDumper;
import org.benf.cfr.reader.util.output.SummaryDumper;
import static org.openide.util.Exceptions.printStackTrace;

/*
* Class entirely copied from cfr.jar
Expand Down Expand Up @@ -76,7 +77,12 @@ public ProgressDumper getProgressDumper() {

@Override
public ExceptionDumper getExceptionDumper() {
return null;
return new ExceptionDumper() {
@Override
public void noteException(String path, String comment, Exception e) {
printStackTrace(e);
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import com.machinezoo.noexception.Exceptions;
import com.mrf.javadecompiler.exception.ExceptionHandler;
import static java.io.File.separatorChar;
import java.net.URL;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.source.ClasspathInfo;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.FileUtil;
Expand Down Expand Up @@ -57,11 +55,7 @@ public static String extractName(FileObject file) {
if (nonNull(fileName)) {
return String.valueOf(fileName);
}
//when class is other places
if (isNull(file.getParent().getPath()) || file.getParent().getPath().isEmpty()) {
return file.getName();
}
return file.getParent().getPath() + separatorChar + file.getName();
return file.getPath();
}

private FileObject file;
Expand Down Expand Up @@ -96,6 +90,7 @@ public FileObject findResource(String internalName) {
}

private FileObject getClassIfOpenEditor(String internalName) throws FileStateInvalidException {
// find jar path from attribute
URL url = (URL) file.getAttribute(CLASSFILE_ROOT);
if (nonNull(url)) {
FileObject jarFile = FileUtil.toFileObject(FileUtil.archiveOrDirForURL(url));
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/mrf/javadecompiler/filesystems/TempDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,37 @@
public final class TempDir {

public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_java_decompiler";

/**
* Return directory where decompiled classes will be created.
*
* @return
*
* @return
*/
public static Path getTempDir() {
Path path = Paths.get(TEMP_DIR_PLUGIN);
if (!Files.exists(path)) {
wrap(ExceptionHandler::handleException)
.run(() -> Files.createDirectory(path));
.run(() -> {
if (!Files.exists(path)) {
Files.createDirectory(path);
}
});
}
return path;
}

public static void removeTempDir() {
clearTempFolder(getTempDir());
}

private static void clearTempFolder(Path path) {
Exceptions.wrap(ExceptionHandler::handleException).run(() -> {
if (Files.isDirectory(path) && Files.list(path).count() > 0) {
Files.list(path).forEach(it -> clearTempFolder(it));
}
path.toFile().setWritable(true);
Files.deleteIfExists(path);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private void writeToNewClass(FileObject file, String decompiled) {
wrap(ExceptionHandler::handleException).run(() -> {
Path newFile = Path.of(decompilerDir.toString(), file.getName().concat(".java"));
if (Files.exists(newFile)) {
newFile.toFile().setWritable(true);
Files.delete(newFile);
}
Files.write(newFile, decompiled.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,70 +24,84 @@
import java.nio.file.Path;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.io.TempDir;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.JarFileSystem;

/**
*
* @author moacirrf
*/
public class FileSystemHelperTest {

@TempDir
Path tempDir;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void test_extractName_when_class_file_is_opened_on_editor() throws IOException {
FileObject file = FileUtil.toFileObject(Files.createFile(tempDir.resolve("teste.java")).toFile());
file.setAttribute(CLASSFILE_BINARY_NAME, "teste");
Path tempDir = tempFolder.newFolder("temp").toPath();

String expResult = "teste";
FileObject file = FileUtil.toFileObject(Files.createFile(tempDir.resolve("test.java")).toFile());
file.setAttribute(CLASSFILE_BINARY_NAME, "test");

String expResult = "test";
String result = FileSystemHelper.extractName(file);

assertEquals(expResult, result);
}

@Test
public void test_extractName_when_class_file_is_on_a_simple_folder() throws IOException {
FileObject file = FileUtil.toFileObject(Files.createFile(tempDir.resolve("teste.java")).toFile());
Path tempDir = tempFolder.newFolder("temp").toPath();
FileObject file = FileUtil.toFileObject(Files.createFile(tempDir.resolve("test.class")).toFile());

String expResult = tempDir.toString() + File.separator + "teste";
String expResult = FileUtil.toFileObject(new File(tempDir.toFile() + File.separator + "test.class")).getPath();
String result = FileSystemHelper.extractName(file);

assertEquals(expResult, result);
}

@Test
public void test_extractName_when_class_file_is_inside_jar() throws IOException {
Path jar = tempDir.resolve("teste.jar");
Path tempDir = tempFolder.newFolder("temp").toPath();
Path jar = tempDir.resolve("test.jar");
createJarFile(jar);
JarFileSystem jarFileSystem = new JarFileSystem(jar.toFile());
FileObject file = jarFileSystem.findResource("teste.class");

String expResult = "teste";
String result = FileSystemHelper.extractName(file);
FileObject fileObject = FileUtil.getArchiveRoot(FileUtil.toFileObject(jar.toFile()));
FileObject file = fileObject.getFileSystem().findResource("test.class");

String expResult = "test.class";
String result = FileSystemHelper.extractName(file);
assertEquals(expResult, result);

}

@Test
public void test_findResource_inside_jar() throws IOException {
Path jar = tempDir.resolve("teste.jar");
createJarFile(jar);
FileSystemHelper instance = FileSystemHelper.of(new JarFileSystem(jar.toFile()).findResource("teste.class"));

FileObject result = instance.findResource("teste.class");
Path tempDir = tempFolder.newFolder("temp").toPath();
Path jar = tempDir.resolve("test.jar");
createJarFile(jar);

FileObject fileObject = FileUtil.getArchiveRoot(FileUtil.toFileObject(jar.toFile()));
FileObject file = fileObject.getFileSystem().findResource("test.class");

FileSystemHelper instance = FileSystemHelper.of(file);

FileObject result = instance.findResource("test.class");
assertNotNull(result);
}

private void createJarFile(Path fileName) throws IOException {
try ( JarOutputStream stream = new JarOutputStream(new FileOutputStream(Files.createFile(fileName).toFile()))) {
stream.putNextEntry(new ZipEntry("teste.class"));
stream.flush();
private static void createJarFile(Path fileName) throws IOException {
if (!Files.exists(fileName)) {
File file = Files.createFile(fileName).toFile();
try ( JarOutputStream stream = new JarOutputStream(new FileOutputStream(file))) {
stream.putNextEntry(new ZipEntry("test.class"));
stream.finish();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import static java.nio.file.Files.createFile;
import static java.nio.file.Files.createDirectory;
import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
*
Expand All @@ -34,19 +35,18 @@ public class TempDirTest {
@Test
public void testGetTempDir() {
Path tempDir = getTempDir();
Assertions.assertTrue(Files.exists(tempDir));
assertTrue(Files.exists(tempDir));
}

@Test
public void testRemoveTempDir() throws IOException {
Path tempDir = getTempDir();
createFile(tempDir.resolve("Test.class"));
createFile(createDirectory(tempDir
.resolve("folder"))
.resolve("OtheFile.class"));

Path testClass = tempDir.resolve("Test.class");
if(!Files.exists(testClass)){
createFile(testClass);
}
TempDir.removeTempDir();
Assertions.assertFalse(Files.exists(tempDir));
assertFalse(Files.exists(tempDir));
}

}

0 comments on commit 146ea81

Please sign in to comment.