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
This commit is contained in:
stevenh 2011-05-13 01:57:00 +01:00
parent 4188ba6215
commit e0c9b44db9
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.world.*;
import org.bukkit.event.weather.*; import org.bukkit.event.weather.*;
import org.bukkit.plugin.*; import org.bukkit.plugin.*;
import org.bukkit.plugin.java.annotations.DontExport;
import org.yaml.snakeyaml.error.YAMLException; 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) { 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); classes.put(name, clazz);
} }
} }

View File

@ -25,27 +25,35 @@ public class PluginClassLoader extends URLClassLoader {
} }
protected Class<?> findClass(String name, boolean checkGlobal) throws ClassNotFoundException { 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); Class<?> result = classes.get(name);
if ( null == result ) {
if (result == null) { try {
result = super.findClass(name);
classes.put(name, result);
loader.setClass(name, result);
} catch ( ClassNotFoundException e ) {
if ( checkGlobal ) { if ( checkGlobal ) {
result = loader.getClassByName(name); result = loader.getClassByName(name);
if ( null == result ) {
// We really couldnt find it
throw new ClassNotFoundException(name);
}
} else {
throw e; // no more options just rethrow
}
}
} }
if (result == null) { // NOTE: setClass already does a not exists check
result = super.findClass(name);
if (result != null) {
loader.setClass(name, result); loader.setClass(name, result);
}
}
classes.put(name, result);
}
return result; return result;
} }
public Set<String> getClasses() { public Set<String> getClasses() {
return classes.keySet(); 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 {
}