Skip to content

Commit 11fe229

Browse files
moacirrfmoacirrf
authored andcommitted
Fix de path problems on windows.
1 parent 8733f83 commit 11fe229

File tree

7 files changed

+74
-68
lines changed

7 files changed

+74
-68
lines changed

pom.xml

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
<licenseName>GNU GENERAL PUBLIC LICENSE 3.0</licenseName>
1818
<licenseFile>LICENSE</licenseFile>
1919
<!-- Path of Netbeans instalation.
20-
<netbeansInstallation>${netbeansInstalationPath}</netbeansInstallation>
21-
-->
20+
<netbeansInstallation>${netbeansInstalationPath}</netbeansInstallation> -->
2221
</configuration>
2322
</plugin>
2423
<plugin>
@@ -64,26 +63,12 @@
6463

6564
<!-- Junit -->
6665
<dependency>
67-
<groupId>org.junit.jupiter</groupId>
68-
<artifactId>junit-jupiter-api</artifactId>
69-
<version>5.6.0</version>
66+
<groupId>junit</groupId>
67+
<artifactId>junit</artifactId>
68+
<version>4.12</version>
7069
<scope>test</scope>
7170
</dependency>
72-
73-
<dependency>
74-
<groupId>org.junit.jupiter</groupId>
75-
<artifactId>junit-jupiter-params</artifactId>
76-
<version>5.6.0</version>
77-
<scope>test</scope>
78-
</dependency>
79-
80-
<dependency>
81-
<groupId>org.junit.jupiter</groupId>
82-
<artifactId>junit-jupiter-engine</artifactId>
83-
<version>5.6.0</version>
84-
<scope>test</scope>
85-
</dependency>
86-
71+
8772
<!-- Netbeans modules -->
8873
<dependency>
8974
<groupId>org.netbeans.api</groupId>

src/main/java/com/mrf/javadecompiler/decompiler/cfr/PluginDumperFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.benf.cfr.reader.util.output.ProgressDumperNop;
3232
import org.benf.cfr.reader.util.output.StringStreamDumper;
3333
import org.benf.cfr.reader.util.output.SummaryDumper;
34+
import static org.openide.util.Exceptions.printStackTrace;
3435

3536
/*
3637
* Class entirely copied from cfr.jar
@@ -76,7 +77,12 @@ public ProgressDumper getProgressDumper() {
7677

7778
@Override
7879
public ExceptionDumper getExceptionDumper() {
79-
return null;
80+
return new ExceptionDumper() {
81+
@Override
82+
public void noteException(String path, String comment, Exception e) {
83+
printStackTrace(e);
84+
}
85+
};
8086
}
8187

8288
@Override

src/main/java/com/mrf/javadecompiler/filesystems/FileSystemHelper.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import com.machinezoo.noexception.Exceptions;
2020
import com.mrf.javadecompiler.exception.ExceptionHandler;
21-
import static java.io.File.separatorChar;
2221
import java.net.URL;
2322
import static java.util.Objects.isNull;
2423
import static java.util.Objects.nonNull;
2524
import org.netbeans.api.java.classpath.ClassPath;
26-
import org.netbeans.api.java.source.ClasspathInfo;
2725
import org.openide.filesystems.FileObject;
2826
import org.openide.filesystems.FileStateInvalidException;
2927
import org.openide.filesystems.FileUtil;
@@ -57,11 +55,7 @@ public static String extractName(FileObject file) {
5755
if (nonNull(fileName)) {
5856
return String.valueOf(fileName);
5957
}
60-
//when class is other places
61-
if (isNull(file.getParent().getPath()) || file.getParent().getPath().isEmpty()) {
62-
return file.getName();
63-
}
64-
return file.getParent().getPath() + separatorChar + file.getName();
58+
return file.getPath();
6559
}
6660

6761
private FileObject file;
@@ -96,6 +90,7 @@ public FileObject findResource(String internalName) {
9690
}
9791

9892
private FileObject getClassIfOpenEditor(String internalName) throws FileStateInvalidException {
93+
// find jar path from attribute
9994
URL url = (URL) file.getAttribute(CLASSFILE_ROOT);
10095
if (nonNull(url)) {
10196
FileObject jarFile = FileUtil.toFileObject(FileUtil.archiveOrDirForURL(url));

src/main/java/com/mrf/javadecompiler/filesystems/TempDir.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,37 @@
2727
public final class TempDir {
2828

2929
public static final String TEMP_DIR_PLUGIN = getProperty("java.io.tmpdir") + "/nb_java_decompiler";
30-
30+
3131
/**
3232
* Return directory where decompiled classes will be created.
33-
*
34-
* @return
33+
*
34+
* @return
3535
*/
3636
public static Path getTempDir() {
3737
Path path = Paths.get(TEMP_DIR_PLUGIN);
3838
if (!Files.exists(path)) {
3939
wrap(ExceptionHandler::handleException)
40-
.run(() -> Files.createDirectory(path));
40+
.run(() -> {
41+
if (!Files.exists(path)) {
42+
Files.createDirectory(path);
43+
}
44+
});
4145
}
4246
return path;
4347
}
4448

4549
public static void removeTempDir() {
4650
clearTempFolder(getTempDir());
4751
}
48-
52+
4953
private static void clearTempFolder(Path path) {
5054
Exceptions.wrap(ExceptionHandler::handleException).run(() -> {
5155
if (Files.isDirectory(path) && Files.list(path).count() > 0) {
5256
Files.list(path).forEach(it -> clearTempFolder(it));
5357
}
58+
path.toFile().setWritable(true);
5459
Files.deleteIfExists(path);
5560
});
5661
}
57-
62+
5863
}

src/main/java/com/mrf/javadecompiler/openide/action/DecompileAction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private void writeToNewClass(FileObject file, String decompiled) {
7373
wrap(ExceptionHandler::handleException).run(() -> {
7474
Path newFile = Path.of(decompilerDir.toString(), file.getName().concat(".java"));
7575
if (Files.exists(newFile)) {
76+
newFile.toFile().setWritable(true);
7677
Files.delete(newFile);
7778
}
7879
Files.write(newFile, decompiled.getBytes());

src/test/java/com/mrf/javadecompiler/filesystems/FileSystemHelperTest.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,70 +24,84 @@
2424
import java.nio.file.Path;
2525
import java.util.jar.JarOutputStream;
2626
import java.util.zip.ZipEntry;
27-
import org.junit.jupiter.api.Test;
28-
import static org.junit.jupiter.api.Assertions.*;
29-
import org.junit.jupiter.api.io.TempDir;
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertNotNull;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.TemporaryFolder;
3032
import org.openide.filesystems.FileObject;
3133
import org.openide.filesystems.FileUtil;
32-
import org.openide.filesystems.JarFileSystem;
3334

3435
/**
3536
*
3637
* @author moacirrf
3738
*/
3839
public class FileSystemHelperTest {
3940

40-
@TempDir
41-
Path tempDir;
41+
@Rule
42+
public TemporaryFolder tempFolder = new TemporaryFolder();
4243

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

48-
String expResult = "teste";
48+
FileObject file = FileUtil.toFileObject(Files.createFile(tempDir.resolve("test.java")).toFile());
49+
file.setAttribute(CLASSFILE_BINARY_NAME, "test");
50+
51+
String expResult = "test";
4952
String result = FileSystemHelper.extractName(file);
5053

5154
assertEquals(expResult, result);
5255
}
5356

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

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

6165
assertEquals(expResult, result);
6266
}
6367

6468
@Test
6569
public void test_extractName_when_class_file_is_inside_jar() throws IOException {
66-
Path jar = tempDir.resolve("teste.jar");
70+
Path tempDir = tempFolder.newFolder("temp").toPath();
71+
Path jar = tempDir.resolve("test.jar");
6772
createJarFile(jar);
68-
JarFileSystem jarFileSystem = new JarFileSystem(jar.toFile());
69-
FileObject file = jarFileSystem.findResource("teste.class");
7073

71-
String expResult = "teste";
72-
String result = FileSystemHelper.extractName(file);
74+
FileObject fileObject = FileUtil.getArchiveRoot(FileUtil.toFileObject(jar.toFile()));
75+
FileObject file = fileObject.getFileSystem().findResource("test.class");
7376

77+
String expResult = "test.class";
78+
String result = FileSystemHelper.extractName(file);
7479
assertEquals(expResult, result);
80+
7581
}
7682

7783
@Test
7884
public void test_findResource_inside_jar() throws IOException {
79-
Path jar = tempDir.resolve("teste.jar");
80-
createJarFile(jar);
81-
FileSystemHelper instance = FileSystemHelper.of(new JarFileSystem(jar.toFile()).findResource("teste.class"));
82-
83-
FileObject result = instance.findResource("teste.class");
85+
Path tempDir = tempFolder.newFolder("temp").toPath();
86+
Path jar = tempDir.resolve("test.jar");
87+
createJarFile(jar);
88+
89+
FileObject fileObject = FileUtil.getArchiveRoot(FileUtil.toFileObject(jar.toFile()));
90+
FileObject file = fileObject.getFileSystem().findResource("test.class");
91+
92+
FileSystemHelper instance = FileSystemHelper.of(file);
93+
94+
FileObject result = instance.findResource("test.class");
8495
assertNotNull(result);
8596
}
8697

87-
private void createJarFile(Path fileName) throws IOException {
88-
try ( JarOutputStream stream = new JarOutputStream(new FileOutputStream(Files.createFile(fileName).toFile()))) {
89-
stream.putNextEntry(new ZipEntry("teste.class"));
90-
stream.flush();
98+
private static void createJarFile(Path fileName) throws IOException {
99+
if (!Files.exists(fileName)) {
100+
File file = Files.createFile(fileName).toFile();
101+
try ( JarOutputStream stream = new JarOutputStream(new FileOutputStream(file))) {
102+
stream.putNextEntry(new ZipEntry("test.class"));
103+
stream.finish();
104+
}
91105
}
92106
}
93107
}

src/test/java/com/mrf/javadecompiler/filesystems/TempDirTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import static java.nio.file.Files.createFile;
2323
import static java.nio.file.Files.createDirectory;
2424
import java.nio.file.Path;
25-
import org.junit.jupiter.api.Assertions;
26-
import org.junit.jupiter.api.Test;
25+
import static org.junit.Assert.assertFalse;
26+
import static org.junit.Assert.assertTrue;
27+
import org.junit.Test;
2728

2829
/**
2930
*
@@ -34,19 +35,18 @@ public class TempDirTest {
3435
@Test
3536
public void testGetTempDir() {
3637
Path tempDir = getTempDir();
37-
Assertions.assertTrue(Files.exists(tempDir));
38+
assertTrue(Files.exists(tempDir));
3839
}
3940

4041
@Test
4142
public void testRemoveTempDir() throws IOException {
4243
Path tempDir = getTempDir();
43-
createFile(tempDir.resolve("Test.class"));
44-
createFile(createDirectory(tempDir
45-
.resolve("folder"))
46-
.resolve("OtheFile.class"));
47-
44+
Path testClass = tempDir.resolve("Test.class");
45+
if(!Files.exists(testClass)){
46+
createFile(testClass);
47+
}
4848
TempDir.removeTempDir();
49-
Assertions.assertFalse(Files.exists(tempDir));
49+
assertFalse(Files.exists(tempDir));
5050
}
5151

5252
}

0 commit comments

Comments
 (0)