Skip to content

Commit

Permalink
Remove most generics in services and candidates
Browse files Browse the repository at this point in the history
They actually create more problems than they solve resulting in a bunch of unsafe casts
  • Loading branch information
Yeregorix committed Sep 2, 2024
1 parent ff58d83 commit 41d93cf
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public String pluginLoader() {
}

@Override
public List<PluginCandidate<PluginResource>> createPluginCandidates(final Environment environment, final PluginResource resource) throws Exception {
public List<PluginCandidate> createPluginCandidates(final Environment environment, final PluginResource resource) throws Exception {
if (resource instanceof JVMPluginResource jvmResource && Files.exists(jvmResource.resourcesRoot().resolve("net/minecraft/server/MinecraftServer.class"))) {
this.logger.debug("Container in path '{}' has been detected as Minecraft.", resource.path());

final List<PluginCandidate<PluginResource>> candidates = new LinkedList<>();
final List<PluginCandidate> candidates = new LinkedList<>();
try (final InputStream stream = JavaPluginLanguageService.class.getClassLoader().getResourceAsStream("META-INF/minecraft_sponge_plugins.json")) {
for (final PluginMetadata metadata : loadMetadataContainer(environment, stream).metadata()) {
candidates.add(new StandardPluginCandidate<>(metadata, resource));
candidates.add(new StandardPluginCandidate(metadata, resource));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -53,11 +52,11 @@
public final class VanillaPluginPlatform implements PluginPlatform {

private final StandardEnvironment standardEnvironment;
private final Map<String, PluginResourceLocatorService<PluginResource>> locatorServices;
private final Map<String, PluginLanguageService<PluginResource>> languageServices;
private final Map<String, PluginResourceLocatorService<?>> locatorServices;
private final Map<String, PluginLanguageService> languageServices;

private final Map<String, Set<PluginResource>> locatorResources;
private final Map<PluginLanguageService<PluginResource>, List<PluginCandidate<PluginResource>>> pluginCandidates;
private final Map<String, Set<? extends PluginResource>> locatorResources;
private final Map<PluginLanguageService, List<PluginCandidate>> pluginCandidates;

public VanillaPluginPlatform(final StandardEnvironment standardEnvironment) {
this.standardEnvironment = standardEnvironment;
Expand Down Expand Up @@ -116,24 +115,24 @@ public StandardEnvironment getStandardEnvironment() {
return this.standardEnvironment;
}

public Map<String, PluginResourceLocatorService<PluginResource>> getLocatorServices() {
public Map<String, PluginResourceLocatorService<?>> getLocatorServices() {
return Collections.unmodifiableMap(this.locatorServices);
}

public Map<String, PluginLanguageService<PluginResource>> getLanguageServices() {
public Map<String, PluginLanguageService> getLanguageServices() {
return Collections.unmodifiableMap(this.languageServices);
}

public Map<String, Set<PluginResource>> getResources() {
public Map<String, Set<? extends PluginResource>> getResources() {
return Collections.unmodifiableMap(this.locatorResources);
}

public Map<PluginLanguageService<PluginResource>, List<PluginCandidate<PluginResource>>> getCandidates() {
public Map<PluginLanguageService, List<PluginCandidate>> getCandidates() {
return Collections.unmodifiableMap(this.pluginCandidates);
}

public void initializeLanguageServices() {
for (final Map.Entry<String, PluginLanguageService<PluginResource>> entry : this.languageServices.entrySet()) {
for (final Map.Entry<String, PluginLanguageService> entry : this.languageServices.entrySet()) {
entry.getValue().initialize(this.standardEnvironment);
}
}
Expand All @@ -144,10 +143,10 @@ public void discoverLocatorServices() {
blackboard.set(JVMKeys.JVM_PLUGIN_RESOURCE_FACTORY, SecureJarPluginResource::new);

final ModuleLayer serviceLayer = Launcher.INSTANCE.environment().findModuleLayerManager().flatMap(lm -> lm.getLayer(IModuleLayerManager.Layer.SERVICE)).orElseThrow();
final var serviceLoader = (ServiceLoader<PluginResourceLocatorService<PluginResource>>) (Object) ServiceLoader.load(serviceLayer, PluginResourceLocatorService.class);
final var serviceLoader = ServiceLoader.load(serviceLayer, PluginResourceLocatorService.class);

for (final Iterator<PluginResourceLocatorService<PluginResource>> iter = serviceLoader.iterator(); iter.hasNext(); ) {
final PluginResourceLocatorService<PluginResource> next;
for (final var iter = serviceLoader.iterator(); iter.hasNext(); ) {
final PluginResourceLocatorService<?> next;

try {
next = iter.next();
Expand All @@ -162,10 +161,10 @@ public void discoverLocatorServices() {

public void discoverLanguageServices() {
final ModuleLayer pluginLayer = Launcher.INSTANCE.environment().findModuleLayerManager().flatMap(lm -> lm.getLayer(IModuleLayerManager.Layer.PLUGIN)).orElseThrow();
final var serviceLoader = (ServiceLoader<PluginLanguageService<PluginResource>>) (Object) ServiceLoader.load(pluginLayer, PluginLanguageService.class);
final var serviceLoader = ServiceLoader.load(pluginLayer, PluginLanguageService.class);

for (final Iterator<PluginLanguageService<PluginResource>> iter = serviceLoader.iterator(); iter.hasNext(); ) {
final PluginLanguageService<PluginResource> next;
for (final var iter = serviceLoader.iterator(); iter.hasNext(); ) {
final PluginLanguageService next;

try {
next = iter.next();
Expand All @@ -179,32 +178,32 @@ public void discoverLanguageServices() {
}

public void locatePluginResources() {
for (final Map.Entry<String, PluginResourceLocatorService<PluginResource>> locatorEntry : this.locatorServices.entrySet()) {
final PluginResourceLocatorService<PluginResource> locatorService = locatorEntry.getValue();
final Set<PluginResource> resources = locatorService.locatePluginResources(this.standardEnvironment);
for (final Map.Entry<String, PluginResourceLocatorService<?>> locatorEntry : this.locatorServices.entrySet()) {
final PluginResourceLocatorService<?> locatorService = locatorEntry.getValue();
final Set<? extends PluginResource> resources = locatorService.locatePluginResources(this.standardEnvironment);
if (!resources.isEmpty()) {
this.locatorResources.put(locatorEntry.getKey(), resources);
}
}
}

public void createPluginCandidates() {
for (final PluginLanguageService<PluginResource> languageService : this.languageServices.values()) {
for (final Set<PluginResource> resources : this.locatorResources.values()) {
for (final PluginLanguageService languageService : this.languageServices.values()) {
for (final Set<? extends PluginResource> resources : this.locatorResources.values()) {
for (final PluginResource pluginResource : resources) {
if (ResourceType.of(pluginResource) != ResourceType.PLUGIN) {
continue;
}

try {
final List<PluginCandidate<PluginResource>> candidates = languageService.createPluginCandidates(this.standardEnvironment, pluginResource);
final List<PluginCandidate> candidates = languageService.createPluginCandidates(this.standardEnvironment, pluginResource);
if (candidates.isEmpty()) {
continue;
}
this.pluginCandidates.computeIfAbsent(languageService, k -> new LinkedList<>()).addAll(candidates);

if (pluginResource instanceof SecureJarPluginResource jarResource) {
jarResource.addCandidates((List) candidates);
jarResource.addCandidates(candidates);
}
} catch (final Exception ex) {
this.standardEnvironment.logger().error("Failed to create plugin candidates", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public List<Resource> beginScanning(final IEnvironment environment) {

final List<SecureJar> languageResources = new ArrayList<>();

for (final Set<PluginResource> resources : this.pluginPlatform.getResources().values()) {
for (final Set<? extends PluginResource> resources : this.pluginPlatform.getResources().values()) {
for (final PluginResource resource : resources) {
if (resource instanceof SecureJarPluginResource secureJarResource) {
if (ResourceType.of(resource) == ResourceType.LANGUAGE) {
Expand All @@ -136,7 +136,7 @@ public List<Resource> completeScan(IModuleLayerManager layerManager) {

final List<SecureJar> gameResources = new ArrayList<>();

for (final Set<PluginResource> resources : this.pluginPlatform.getResources().values()) {
for (final Set<? extends PluginResource> resources : this.pluginPlatform.getResources().values()) {
for (final PluginResource resource : resources) {
if (resource instanceof SecureJarPluginResource secureJarResource) {
// Build jar metadata from first candidate, or fallback to standard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class SecureJarPluginResource implements JVMPluginResource {
private final SecureJar jar;

private PluginJarMetadata pluginJarMetadata;
private List<PluginCandidate<SecureJarPluginResource>> candidates;
private List<PluginCandidate> candidates;

public SecureJarPluginResource(final String locator, final Path[] paths) {
Objects.requireNonNull(locator, "locator");
Expand Down Expand Up @@ -87,7 +87,7 @@ public Path resourcesRoot() {
return this.jar.getRootPath();
}

public void addCandidates(Collection<PluginCandidate<SecureJarPluginResource>> candidates) {
public void addCandidates(Collection<PluginCandidate> candidates) {
if (this.candidates != null) {
this.candidates.addAll(candidates);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@
import org.spongepowered.plugin.Environment;
import org.spongepowered.plugin.InvalidPluginException;
import org.spongepowered.plugin.PluginCandidate;
import org.spongepowered.plugin.builtin.jvm.JVMPluginLoader;
import org.spongepowered.plugin.builtin.jvm.JVMPluginResource;
import org.spongepowered.plugin.PluginLoader;

public final class JavaPluginLoader implements JVMPluginLoader<JVMPluginResource, VanillaJavaPluginContainer> {
public final class JavaPluginLoader implements PluginLoader<VanillaJavaPluginContainer> {

private final ArtifactVersion version = new DefaultArtifactVersion("1.0");

Expand All @@ -45,7 +44,7 @@ public ArtifactVersion version() {
}

@Override
public VanillaJavaPluginContainer loadPlugin(final Environment environment, final PluginCandidate<JVMPluginResource> candidate, final ClassLoader targetClassLoader)
public VanillaJavaPluginContainer loadPlugin(final Environment environment, final PluginCandidate candidate, final ClassLoader targetClassLoader)
throws InvalidPluginException {
final VanillaJavaPluginContainer container = new VanillaJavaPluginContainer(candidate);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
import org.apache.logging.log4j.Logger;
import org.spongepowered.common.applaunch.plugin.DummyPluginContainer;
import org.spongepowered.plugin.PluginCandidate;
import org.spongepowered.plugin.PluginResource;
import org.spongepowered.plugin.builtin.StandardPluginContainer;

public final class VanillaDummyPluginContainer extends StandardPluginContainer implements DummyPluginContainer {

public VanillaDummyPluginContainer(final PluginCandidate<? extends PluginResource> candidate, final Logger logger, final Object instance) {
public VanillaDummyPluginContainer(final PluginCandidate candidate, final Logger logger, final Object instance) {
super(candidate, logger);
this.initializeInstance(instance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@

import org.spongepowered.api.Sponge;
import org.spongepowered.plugin.PluginCandidate;
import org.spongepowered.plugin.PluginResource;
import org.spongepowered.plugin.builtin.StandardPluginContainer;

public final class VanillaJavaPluginContainer extends StandardPluginContainer {

public VanillaJavaPluginContainer(final PluginCandidate<? extends PluginResource> candidate) {
public VanillaJavaPluginContainer(final PluginCandidate candidate) {
super(candidate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ public Collection<PluginContainer> plugins() {
}

public void loadPlugins(final VanillaPluginPlatform platform) {
final Map<PluginCandidate<PluginResource>, PluginLanguageService<PluginResource>> pluginLanguageLookup = new HashMap<>();
final Map<PluginLanguageService<PluginResource>, PluginLoader<PluginResource, PluginContainer>> pluginLoaders = new HashMap<>();
final Map<PluginCandidate, PluginLanguageService> pluginLanguageLookup = new HashMap<>();
final Map<PluginLanguageService, PluginLoader<?>> pluginLoaders = new HashMap<>();

// Initialise the plugin language loaders.
for (final Map.Entry<PluginLanguageService<PluginResource>, List<PluginCandidate<PluginResource>>> candidate : platform.getCandidates().entrySet()) {
final PluginLanguageService<PluginResource> languageService = candidate.getKey();
for (final Map.Entry<PluginLanguageService, List<PluginCandidate>> candidate : platform.getCandidates().entrySet()) {
final PluginLanguageService languageService = candidate.getKey();
final String loaderClass = languageService.pluginLoader();
try {
pluginLoaders.put(languageService,
(PluginLoader<PluginResource, PluginContainer>) Class.forName(loaderClass).getConstructor().newInstance());
pluginLoaders.put(languageService, (PluginLoader<?>) Class.forName(loaderClass).getConstructor().newInstance());
} catch (final InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
Expand All @@ -105,15 +104,15 @@ public void loadPlugins(final VanillaPluginPlatform platform) {
// Priority to platform plugins that will already exist here -- meaning the resolver will act upon them first
// and if someone decides to give a plugin an ID that is the same as a platform plugin, the resolver will effectively
// reject it.
final Set<PluginCandidate<PluginResource>> resources = new LinkedHashSet<>();
final Set<PluginCandidate> resources = new LinkedHashSet<>();
pluginLanguageLookup.keySet().stream().filter(x -> this.plugins.containsKey(x.metadata().id())).forEach(resources::add);
resources.addAll(pluginLanguageLookup.keySet());

final ResolutionResult<PluginResource> resolutionResult = DependencyResolver.resolveAndSortCandidates(resources, platform.logger());
final Map<PluginCandidate<PluginResource>, String> failedInstances = new HashMap<>();
final Map<PluginCandidate<PluginResource>, String> consequentialFailedInstances = new HashMap<>();
final ResolutionResult resolutionResult = DependencyResolver.resolveAndSortCandidates(resources, platform.logger());
final Map<PluginCandidate, String> failedInstances = new HashMap<>();
final Map<PluginCandidate, String> consequentialFailedInstances = new HashMap<>();
final ClassLoader launchClassloader = VanillaLaunch.instance().getClass().getClassLoader();
for (final PluginCandidate<PluginResource> candidate : resolutionResult.sortedSuccesses()) {
for (final PluginCandidate candidate : resolutionResult.sortedSuccesses()) {
final String id = candidate.metadata().id();
if (id.indexOf('-') >= 0) {
platform.logger().warn("The dash character (-) is no longer supported in plugin ids.\n" +
Expand Down Expand Up @@ -149,8 +148,8 @@ public void loadPlugins(final VanillaPluginPlatform platform) {
// If a dependency failed to load, then we should bail on required dependencies too.
// This should work fine, we're sorted so all deps should be in place at this stage.
if (this.stillValid(candidate, consequentialFailedInstances)) {
final PluginLanguageService<PluginResource> languageService = pluginLanguageLookup.get(candidate);
final PluginLoader<PluginResource, PluginContainer> pluginLoader = pluginLoaders.get(languageService);
final PluginLanguageService languageService = pluginLanguageLookup.get(candidate);
final PluginLoader<?> pluginLoader = pluginLoaders.get(languageService);
try {
final PluginContainer container = pluginLoader.loadPlugin(platform.getStandardEnvironment(), candidate, launchClassloader);
this.addPlugin(container);
Expand Down Expand Up @@ -180,7 +179,7 @@ public PluginResource resource(final PluginContainer container) {
return this.containerToResource.get(container);
}

private boolean stillValid(final PluginCandidate<PluginResource> candidate, final Map<PluginCandidate<PluginResource>, String> consequential) {
private boolean stillValid(final PluginCandidate candidate, final Map<PluginCandidate, String> consequential) {
final Optional<PluginDependency> failedId =
candidate.metadata().dependencies().stream().filter(x -> !x.optional() && !this.plugins.containsKey(x.id())).findFirst();
if (failedId.isPresent()) {
Expand Down
Loading

0 comments on commit 41d93cf

Please sign in to comment.