Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AutoService #365

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ dependencies {
include implementation("rip.hippo:Doctor:1.0.1")
include implementation("javax.inject:javax.inject:1")

include implementation("org.reflections:reflections:0.10.2")
include implementation("org.javassist:javassist:3.30.2-GA")

// Json
include implementation("javax.json:javax.json-api:1.1.4")
include "org.glassfish:javax.json:1.1.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import net.minecraft.util.Identifier;

import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import rip.hippo.inject.Doctor;
import rip.hippo.inject.Injector;
import tools.redstone.redstonetools.features.arguments.serializers.GenericArgumentType;
import tools.redstone.redstonetools.utils.DependencyLookup;
import tools.redstone.redstonetools.utils.ReflectionUtils;

Expand All @@ -27,7 +23,6 @@ public class RedstoneToolsClient implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final Path CONFIG_DIR = FabricLoader.getInstance().getConfigDir().resolve("redstonetools");
public static final Injector INJECTOR = Doctor.createInjector(ReflectionUtils.getModules());
public static final Reflections REFLECTIONS = new Reflections("tools.redstone.redstonetools");

@Override
public void onInitializeClient() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

import java.math.BigInteger;
import java.util.Optional;

@AutoService(GenericArgumentType.class)
public class BigIntegerArgumentType extends IntLikeArgumentType<BigInteger> {
private static final BigIntegerArgumentType INSTANCE = new BigIntegerArgumentType(null, null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.BlockColor;

@AutoService(GenericArgumentType.class)
public class BlockColorSerializer extends EnumSerializer<BlockColor> {
private static final BlockColorSerializer INSTANCE = new BlockColorSerializer();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.BlockStateArgument;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import net.minecraft.registry.Registries;

@AutoService(GenericArgumentType.class)
public class BlockStateArgumentType extends BrigadierArgumentType<BlockStateArgument, String> {

private static BlockStateArgumentType INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.BoolArgumentType;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class BoolSerializer extends StringBrigadierSerializer<Boolean> {

private static final BoolSerializer INSTANCE = new BoolSerializer(BoolArgumentType.bool());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand All @@ -20,6 +21,7 @@
* @param <E> The element type.
* @param <C> The collection type.
*/
@AutoService(GenericArgumentType.class)
public class CollectionSerializer<E, C extends Collection<E>>
extends GenericArgumentType<C, List<Object>> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.ColoredBlockType;

@AutoService(GenericArgumentType.class)
public class ColoredBlockTypeSerializer extends EnumSerializer<ColoredBlockType> {
private static final ColoredBlockTypeSerializer INSTANCE = new ColoredBlockTypeSerializer();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.DirectionArgument;

@AutoService(GenericArgumentType.class)
public class DirectionSerializer extends EnumSerializer<DirectionArgument> {
private static final DirectionSerializer INSTANCE = new DirectionSerializer();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class DoubleSerializer extends StringBrigadierSerializer<Double> {

private static final DoubleSerializer INSTANCE = new DoubleSerializer(DoubleArgumentType.doubleArg());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class FloatSerializer extends StringBrigadierSerializer<Float> {

private static final FloatSerializer INSTANCE = new FloatSerializer(FloatArgumentType.floatArg());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.Optional;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class IntegerSerializer extends IntLikeArgumentType<Integer> {
private static final IntegerSerializer INSTANCE = new IntegerSerializer(Integer.MIN_VALUE, Integer.MAX_VALUE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.Optional;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class LongSerializer extends IntLikeArgumentType<Long> {
private static final LongSerializer INSTANCE = new LongSerializer(Long.MIN_VALUE, Long.MAX_VALUE);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import tools.redstone.redstonetools.macros.Macro;
import tools.redstone.redstonetools.macros.MacroManager;
import com.google.auto.service.AutoService;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.macros.MacroManager;

import java.util.concurrent.CompletableFuture;

import static tools.redstone.redstonetools.RedstoneToolsClient.INJECTOR;

@AutoService(GenericArgumentType.class)
public class MacroNameSerializer extends StringSerializer {
private static final MacroNameSerializer INSTANCE = new MacroNameSerializer();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand All @@ -14,6 +15,7 @@
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

@AutoService(GenericArgumentType.class)
public class NumberBaseArgumentType extends GenericArgumentType<Integer, String> {
private static final IntegerSerializer INT_SERIALIZER = IntegerSerializer.integer(2, 36);
private static final NumberBaseArgumentType INSTANCE = new NumberBaseArgumentType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import tools.redstone.redstonetools.utils.NumberArg;

import java.util.Optional;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class NumberSerializer extends IntLikeArgumentType<NumberArg> {
private static final NumberSerializer INSTANCE = new NumberSerializer(null, null);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;
import tools.redstone.redstonetools.utils.SignalBlock;

@AutoService(GenericArgumentType.class)
public class SignalBlockSerializer extends EnumSerializer<SignalBlock> {
private static final SignalBlockSerializer INSTANCE = new SignalBlockSerializer();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tools.redstone.redstonetools.features.arguments.serializers;

import com.google.auto.service.AutoService;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;

import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.argument.serialize.ArgumentSerializer;

@AutoService(GenericArgumentType.class)
public class StringSerializer extends StringBrigadierSerializer<String> {

private static final StringSerializer INSTANCE_WORD = new StringSerializer(StringArgumentType.word());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package tools.redstone.redstonetools.utils;

import static tools.redstone.redstonetools.RedstoneToolsClient.REFLECTIONS;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import rip.hippo.inject.DoctorModule;
import sun.misc.Unsafe;
import tools.redstone.redstonetools.features.AbstractFeature;
import tools.redstone.redstonetools.features.Feature;
import tools.redstone.redstonetools.features.arguments.Argument;
import tools.redstone.redstonetools.features.arguments.serializers.GenericArgumentType;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

public class ReflectionUtils {
private static final Logger LOGGER = LogManager.getLogger();
private static DoctorModule[] modules;
Expand Down Expand Up @@ -81,8 +73,14 @@ public static DoctorModule[] getModules() {

public static Set<Class<? extends GenericArgumentType>> getAllArguments() {
if (arguments == null) {
arguments = REFLECTIONS.getSubTypesOf(GenericArgumentType.class).stream()
.filter(argument -> !Modifier.isAbstract(argument.getModifiers())).collect(Collectors.toSet());
try {
arguments = findClasses(GenericArgumentType.class)
.stream()
.filter(argument -> !Modifier.isAbstract(argument.getModifiers()))
.collect(Collectors.toSet());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

return arguments;
Expand All @@ -99,7 +97,7 @@ public static Set<? extends AbstractFeature> getFeatures() {
return features;
}

private static <T> Set<? extends T> serviceLoad(Class<T> clazz) throws IOException {
public static <T> Set<Class<? extends T>> findClasses(Class<T> clazz) throws IOException {
ClassLoader cl = ReflectionUtils.class.getClassLoader();
Enumeration<URL> serviceFiles = cl.getResources("META-INF/services/" + clazz.getName());
Set<String> classNames = new HashSet<>();
Expand All @@ -109,11 +107,19 @@ private static <T> Set<? extends T> serviceLoad(Class<T> clazz) throws IOExcepti
classNames.addAll(IOUtils.readLines(reader, "UTF-8"));
}
}
return classNames.stream()
//noinspection unchecked
return (Set<Class<? extends T>>) classNames.stream()
.filter(it -> !it.isEmpty() && !it.isBlank())
.map(ReflectionUtils::loadClass)
.filter(Objects::nonNull)
.filter(clazz::isAssignableFrom)
.map(clazz::cast)
.collect(Collectors.toSet());
}

private static <T> Set<? extends T> serviceLoad(Class<T> clazz) throws IOException {
return findClasses(clazz)
.stream()
.map(it -> {
try {
return it.getDeclaredConstructor().newInstance();
Expand Down
Loading