Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit ebd3c27

Browse files
committed
Cleaned up code
1 parent 6043951 commit ebd3c27

7 files changed

Lines changed: 109 additions & 63 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
46
zipStoreBase=GRADLE_USER_HOME
57
zipStorePath=wrapper/dists

standalone/src/main/java/net/raphimc/javadowngrader/standalone/transform/AbstractClassProvider.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,38 @@
1818
package net.raphimc.javadowngrader.standalone.transform;
1919

2020
import net.lenni0451.classtransform.utils.tree.IClassProvider;
21-
import org.jetbrains.annotations.Nullable;
2221

2322
import java.util.Collections;
2423
import java.util.Map;
2524
import java.util.NoSuchElementException;
2625
import java.util.function.Supplier;
2726

2827
public abstract class AbstractClassProvider implements IClassProvider {
29-
@Nullable
28+
3029
private final IClassProvider parent;
3130

32-
protected AbstractClassProvider(@Nullable IClassProvider parent) {
31+
protected AbstractClassProvider(final IClassProvider parent) {
3332
this.parent = parent;
3433
}
3534

36-
@Nullable
37-
public IClassProvider getParent() {
38-
return parent;
39-
}
40-
4135
@Override
4236
public byte[] getClass(String name) {
43-
if (parent == null) {
37+
if (this.parent == null) {
4438
throw new NoSuchElementException("Unable to find class '" + name + "'");
4539
}
46-
return parent.getClass(name);
40+
return this.parent.getClass(name);
4741
}
4842

4943
@Override
5044
public Map<String, Supplier<byte[]>> getAllClasses() {
51-
if (parent == null) {
45+
if (this.parent == null) {
5246
return Collections.emptyMap();
5347
}
54-
return parent.getAllClasses();
48+
return this.parent.getAllClasses();
5549
}
50+
51+
public IClassProvider getParent() {
52+
return this.parent;
53+
}
54+
5655
}

standalone/src/main/java/net/raphimc/javadowngrader/standalone/transform/ClosingFileSystemClassProvider.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
2222
import java.io.IOException;
2323
import java.nio.file.FileSystem;
2424

25-
public class ClosingFileSystemClassProvider extends PathClassProvider implements AutoCloseable {
26-
private final FileSystem fs;
25+
public class ClosingFileSystemClassProvider extends FileSystemClassProvider implements AutoCloseable {
2726

28-
public ClosingFileSystemClassProvider(FileSystem fs, IClassProvider parent) {
29-
super(fs.getRootDirectories().iterator().next(), parent);
30-
this.fs = fs;
27+
public ClosingFileSystemClassProvider(final FileSystem fs, final IClassProvider parent) {
28+
super(fs, parent);
3129
}
3230

3331
@Override
3432
public void close() throws IOException {
35-
fs.close();
33+
this.fs.close();
3634
}
35+
3736
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader
3+
* Copyright (C) 2023 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.javadowngrader.standalone.transform;
19+
20+
import net.lenni0451.classtransform.utils.tree.IClassProvider;
21+
22+
import java.nio.file.FileSystem;
23+
24+
public class FileSystemClassProvider extends PathClassProvider {
25+
26+
protected final FileSystem fs;
27+
28+
public FileSystemClassProvider(final FileSystem fs, final IClassProvider parent) {
29+
super(fs.getRootDirectories().iterator().next(), parent);
30+
31+
this.fs = fs;
32+
}
33+
34+
}

standalone/src/main/java/net/raphimc/javadowngrader/standalone/transform/LazyFileClassProvider.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,69 @@
2020
import net.lenni0451.classtransform.utils.tree.IClassProvider;
2121

2222
import java.io.File;
23+
import java.io.IOException;
24+
import java.io.UncheckedIOException;
2325
import java.net.URI;
26+
import java.net.URISyntaxException;
27+
import java.nio.file.FileSystemAlreadyExistsException;
2428
import java.nio.file.FileSystems;
29+
import java.util.Collection;
2530
import java.util.Collections;
26-
import java.util.List;
2731
import java.util.NoSuchElementException;
2832

2933
public class LazyFileClassProvider extends AbstractClassProvider implements AutoCloseable {
34+
3035
private final Object[] path;
3136

32-
public LazyFileClassProvider(List<File> path, IClassProvider parent) {
37+
public LazyFileClassProvider(final Collection<File> path, final IClassProvider parent) {
3338
super(parent);
39+
3440
this.path = path.toArray();
3541
}
3642

3743
@Override
3844
public byte[] getClass(String name) {
39-
for (int i = 0; i < path.length; i++) {
40-
Object element = path[i];
45+
for (int i = 0; i < this.path.length; i++) {
46+
Object element = this.path[i];
4147
if (element instanceof File) {
42-
synchronized (path) {
43-
if ((element = path[i]) instanceof File) {
44-
path[i] = element = open((File)element);
48+
synchronized (this.path) {
49+
if ((element = this.path[i]) instanceof File) {
50+
this.path[i] = element = open((File) element);
4551
}
4652
}
4753
}
4854
try {
49-
return ((PathClassProvider)element).getClass(name);
55+
return ((PathClassProvider) element).getClass(name);
5056
} catch (NoSuchElementException ignored) {
5157
}
5258
}
5359
return super.getClass(name);
5460
}
5561

56-
private static PathClassProvider open(File file) {
62+
private static PathClassProvider open(final File file) {
63+
final URI uri;
64+
try {
65+
uri = new URI("jar:" + file.toURI());
66+
} catch (URISyntaxException e) {
67+
throw new RuntimeException(e);
68+
}
69+
5770
try {
58-
return new ClosingFileSystemClassProvider(FileSystems.newFileSystem(new URI("jar:" + file.toURI()), Collections.emptyMap()), null);
59-
} catch (Exception e) {
60-
throw e instanceof RuntimeException ? (RuntimeException)e : new RuntimeException(e);
71+
return new ClosingFileSystemClassProvider(FileSystems.newFileSystem(uri, Collections.emptyMap()), null);
72+
} catch (FileSystemAlreadyExistsException e) {
73+
return new FileSystemClassProvider(FileSystems.getFileSystem(uri), null);
74+
} catch (IOException e) {
75+
throw new UncheckedIOException(e);
6176
}
6277
}
6378

6479
@Override
6580
public void close() throws Exception {
66-
for (final Object element : path) {
81+
for (Object element : this.path) {
6782
if (element instanceof AutoCloseable) {
68-
((AutoCloseable)element).close();
83+
((AutoCloseable) element).close();
6984
}
7085
}
7186
}
87+
7288
}

standalone/src/main/java/net/raphimc/javadowngrader/standalone/transform/PathClassProvider.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.UncheckedIOException;
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
27+
import java.util.HashMap;
2728
import java.util.Map;
2829
import java.util.function.Supplier;
2930
import java.util.stream.Collectors;
@@ -33,14 +34,14 @@ public class PathClassProvider extends AbstractClassProvider {
3334

3435
private final Path root;
3536

36-
public PathClassProvider(Path root, IClassProvider parent) {
37+
public PathClassProvider(final Path root, final IClassProvider parent) {
3738
super(parent);
3839
this.root = root;
3940
}
4041

4142
@Override
4243
public byte[] getClass(String name) {
43-
final Path path = root.resolve(GeneralUtil.toClassFilename(name));
44+
final Path path = this.root.resolve(GeneralUtil.toClassFilename(name));
4445
if (Files.exists(path)) {
4546
try {
4647
return Files.readAllBytes(path);
@@ -54,26 +55,33 @@ public byte[] getClass(String name) {
5455

5556
@Override
5657
public Map<String, Supplier<byte[]>> getAllClasses() {
57-
try (Stream<Path> stream = Files.walk(root)) {
58-
return GeneralUtil.merge(
59-
(a, b) -> b,
60-
super.getAllClasses(),
61-
stream
62-
.filter(Files::isRegularFile)
63-
.filter(f -> f.getFileName().endsWith(".class"))
64-
.collect(Collectors.toMap(
65-
p -> GeneralUtil.toClassName(GeneralUtil.slashName(root.relativize(p))),
66-
p -> () -> {
67-
try {
68-
return Files.readAllBytes(p);
69-
} catch (IOException e) {
70-
throw new UncheckedIOException(e);
71-
}
72-
}
73-
))
58+
try (Stream<Path> stream = Files.walk(this.root)) {
59+
return merge(
60+
super.getAllClasses(),
61+
stream
62+
.filter(Files::isRegularFile)
63+
.filter(f -> f.getFileName().endsWith(".class"))
64+
.collect(Collectors.toMap(
65+
p -> GeneralUtil.toClassName(GeneralUtil.slashName(this.root.relativize(p))),
66+
p -> () -> {
67+
try {
68+
return Files.readAllBytes(p);
69+
} catch (IOException e) {
70+
throw new UncheckedIOException(e);
71+
}
72+
}
73+
))
7474
);
7575
} catch (IOException e) {
7676
throw new UncheckedIOException(e);
7777
}
7878
}
79+
80+
@SafeVarargs
81+
public static <K, V> Map<K, V> merge(final Map<K, V> map, final Map<K, V>... others) {
82+
final Map<K, V> newMap = new HashMap<>(map);
83+
for (Map<K, V> other : others) newMap.putAll(other);
84+
return newMap;
85+
}
86+
7987
}

standalone/src/main/java/net/raphimc/javadowngrader/standalone/util/GeneralUtil.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
import java.nio.file.*;
2525
import java.util.Collections;
2626
import java.util.List;
27-
import java.util.Map;
28-
import java.util.Set;
29-
import java.util.function.BinaryOperator;
3027
import java.util.stream.Collectors;
31-
import java.util.stream.Stream;
3228

3329
public class GeneralUtil {
3430
public static <T> List<T> flatten(List<List<T>> list) {
@@ -37,14 +33,6 @@ public static <T> List<T> flatten(List<List<T>> list) {
3733
.collect(Collectors.toList());
3834
}
3935

40-
@SafeVarargs
41-
public static <K, V> Map<K, V> merge(BinaryOperator<V> merger, Map<K, V>... maps) {
42-
return Stream.of(maps)
43-
.map(Map::entrySet)
44-
.flatMap(Set::stream)
45-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, merger));
46-
}
47-
4836
public static String toClassFilename(String className) {
4937
return ASMUtils.slash(className).concat(".class");
5038
}

0 commit comments

Comments
 (0)