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

Commit

Permalink
Cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Sep 3, 2023
1 parent 6043951 commit ebd3c27
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 63 deletions.
2 changes: 2 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,38 @@
package net.raphimc.javadowngrader.standalone.transform;

import net.lenni0451.classtransform.utils.tree.IClassProvider;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Supplier;

public abstract class AbstractClassProvider implements IClassProvider {
@Nullable

private final IClassProvider parent;

protected AbstractClassProvider(@Nullable IClassProvider parent) {
protected AbstractClassProvider(final IClassProvider parent) {
this.parent = parent;
}

@Nullable
public IClassProvider getParent() {
return parent;
}

@Override
public byte[] getClass(String name) {
if (parent == null) {
if (this.parent == null) {
throw new NoSuchElementException("Unable to find class '" + name + "'");
}
return parent.getClass(name);
return this.parent.getClass(name);
}

@Override
public Map<String, Supplier<byte[]>> getAllClasses() {
if (parent == null) {
if (this.parent == null) {
return Collections.emptyMap();
}
return parent.getAllClasses();
return this.parent.getAllClasses();
}

public IClassProvider getParent() {
return this.parent;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
import java.io.IOException;
import java.nio.file.FileSystem;

public class ClosingFileSystemClassProvider extends PathClassProvider implements AutoCloseable {
private final FileSystem fs;
public class ClosingFileSystemClassProvider extends FileSystemClassProvider implements AutoCloseable {

public ClosingFileSystemClassProvider(FileSystem fs, IClassProvider parent) {
super(fs.getRootDirectories().iterator().next(), parent);
this.fs = fs;
public ClosingFileSystemClassProvider(final FileSystem fs, final IClassProvider parent) {
super(fs, parent);
}

@Override
public void close() throws IOException {
fs.close();
this.fs.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.javadowngrader.standalone.transform;

import net.lenni0451.classtransform.utils.tree.IClassProvider;

import java.nio.file.FileSystem;

public class FileSystemClassProvider extends PathClassProvider {

protected final FileSystem fs;

public FileSystemClassProvider(final FileSystem fs, final IClassProvider parent) {
super(fs.getRootDirectories().iterator().next(), parent);

this.fs = fs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,69 @@
import net.lenni0451.classtransform.utils.tree.IClassProvider;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystems;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;

public class LazyFileClassProvider extends AbstractClassProvider implements AutoCloseable {

private final Object[] path;

public LazyFileClassProvider(List<File> path, IClassProvider parent) {
public LazyFileClassProvider(final Collection<File> path, final IClassProvider parent) {
super(parent);

this.path = path.toArray();
}

@Override
public byte[] getClass(String name) {
for (int i = 0; i < path.length; i++) {
Object element = path[i];
for (int i = 0; i < this.path.length; i++) {
Object element = this.path[i];
if (element instanceof File) {
synchronized (path) {
if ((element = path[i]) instanceof File) {
path[i] = element = open((File)element);
synchronized (this.path) {
if ((element = this.path[i]) instanceof File) {
this.path[i] = element = open((File) element);
}
}
}
try {
return ((PathClassProvider)element).getClass(name);
return ((PathClassProvider) element).getClass(name);
} catch (NoSuchElementException ignored) {
}
}
return super.getClass(name);
}

private static PathClassProvider open(File file) {
private static PathClassProvider open(final File file) {
final URI uri;
try {
uri = new URI("jar:" + file.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}

try {
return new ClosingFileSystemClassProvider(FileSystems.newFileSystem(new URI("jar:" + file.toURI()), Collections.emptyMap()), null);
} catch (Exception e) {
throw e instanceof RuntimeException ? (RuntimeException)e : new RuntimeException(e);
return new ClosingFileSystemClassProvider(FileSystems.newFileSystem(uri, Collections.emptyMap()), null);
} catch (FileSystemAlreadyExistsException e) {
return new FileSystemClassProvider(FileSystems.getFileSystem(uri), null);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public void close() throws Exception {
for (final Object element : path) {
for (Object element : this.path) {
if (element instanceof AutoCloseable) {
((AutoCloseable)element).close();
((AutoCloseable) element).close();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand All @@ -33,14 +34,14 @@ public class PathClassProvider extends AbstractClassProvider {

private final Path root;

public PathClassProvider(Path root, IClassProvider parent) {
public PathClassProvider(final Path root, final IClassProvider parent) {
super(parent);
this.root = root;
}

@Override
public byte[] getClass(String name) {
final Path path = root.resolve(GeneralUtil.toClassFilename(name));
final Path path = this.root.resolve(GeneralUtil.toClassFilename(name));
if (Files.exists(path)) {
try {
return Files.readAllBytes(path);
Expand All @@ -54,26 +55,33 @@ public byte[] getClass(String name) {

@Override
public Map<String, Supplier<byte[]>> getAllClasses() {
try (Stream<Path> stream = Files.walk(root)) {
return GeneralUtil.merge(
(a, b) -> b,
super.getAllClasses(),
stream
.filter(Files::isRegularFile)
.filter(f -> f.getFileName().endsWith(".class"))
.collect(Collectors.toMap(
p -> GeneralUtil.toClassName(GeneralUtil.slashName(root.relativize(p))),
p -> () -> {
try {
return Files.readAllBytes(p);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
))
try (Stream<Path> stream = Files.walk(this.root)) {
return merge(
super.getAllClasses(),
stream
.filter(Files::isRegularFile)
.filter(f -> f.getFileName().endsWith(".class"))
.collect(Collectors.toMap(
p -> GeneralUtil.toClassName(GeneralUtil.slashName(this.root.relativize(p))),
p -> () -> {
try {
return Files.readAllBytes(p);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
))
);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@SafeVarargs
public static <K, V> Map<K, V> merge(final Map<K, V> map, final Map<K, V>... others) {
final Map<K, V> newMap = new HashMap<>(map);
for (Map<K, V> other : others) newMap.putAll(other);
return newMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
import java.nio.file.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GeneralUtil {
public static <T> List<T> flatten(List<List<T>> list) {
Expand All @@ -37,14 +33,6 @@ public static <T> List<T> flatten(List<List<T>> list) {
.collect(Collectors.toList());
}

@SafeVarargs
public static <K, V> Map<K, V> merge(BinaryOperator<V> merger, Map<K, V>... maps) {
return Stream.of(maps)
.map(Map::entrySet)
.flatMap(Set::stream)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, merger));
}

public static String toClassFilename(String className) {
return ASMUtils.slash(className).concat(".class");
}
Expand Down

0 comments on commit ebd3c27

Please sign in to comment.