Fixed java plugin class loader so it works with plugins that contain classes also present in other plugins.

This also removes the changes from commit 1c4bde50bc12d130f6c8 which was added in order to fix this issue but wasn't ideal as it required plugins to be updated which isnt required with this fix

By: stevenh <steven.hartland@multiplay.co.uk>
This commit is contained in:
Bukkit/Spigot 2011-05-13 01:57:00 +01:00
parent 11804b78a5
commit 4af56e3d16
3 changed files with 21 additions and 30 deletions

View File

@ -27,7 +27,6 @@ import org.bukkit.event.vehicle.*;
import org.bukkit.event.world.*;
import org.bukkit.event.weather.*;
import org.bukkit.plugin.*;
import org.bukkit.plugin.java.annotations.DontExport;
import org.yaml.snakeyaml.error.YAMLException;
/**
@ -218,7 +217,7 @@ public final class JavaPluginLoader implements PluginLoader {
}
public void setClass(final String name, final Class<?> clazz) {
if ((!classes.containsKey(name)) && (clazz.getAnnotation(DontExport.class) != null)) {
if (!classes.containsKey(name)) {
classes.put(name, clazz);
}
}

View File

@ -25,27 +25,35 @@ public class PluginClassLoader extends URLClassLoader {
}
protected Class<?> findClass(String name, boolean checkGlobal) throws ClassNotFoundException {
// We use the following load order:
// 1. Local first, this avoids IllegalAccessError exceptions for duplicate classes
// 2. Global cache second which prevents ClassCastException's apparently
Class<?> result = classes.get(name);
if (result == null) {
if (checkGlobal) {
result = loader.getClassByName(name);
}
if (result == null) {
if ( null == result ) {
try {
result = super.findClass(name);
if (result != null) {
loader.setClass(name, result);
classes.put(name, result);
loader.setClass(name, result);
} catch ( ClassNotFoundException e ) {
if ( checkGlobal ) {
result = loader.getClassByName(name);
if ( null == result ) {
// We really couldnt find it
throw new ClassNotFoundException(name);
}
} else {
throw e; // no more options just rethrow
}
}
classes.put(name, result);
}
// NOTE: setClass already does a not exists check
loader.setClass(name, result);
return result;
}
public Set<String> getClasses() {
return classes.keySet();
}

View File

@ -1,16 +0,0 @@
package org.bukkit.plugin.java.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Flags a class as private and not to be exported with other plugins
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DontExport {
}