提交 be5ab549 编写于 作者: K kohsuke

made the generated classes visible to the classloader.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@36008 71c3de6d-444a-0410-be80-ed276b4c234a
上级 cfd817d2
......@@ -59,6 +59,7 @@ import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
......@@ -70,6 +71,8 @@ import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -580,13 +583,30 @@ public abstract class PluginManager extends AbstractModelObject {
/**
* {@link ClassLoader} that can see all plugins.
*/
private final class UberClassLoader extends ClassLoader {
public final class UberClassLoader extends ClassLoader {
/**
* Make generated types visible.
* Keyed by the generated class name.
*/
private ConcurrentMap<String, WeakReference<Class>> generatedClasses = new ConcurrentHashMap<String, WeakReference<Class>>();
public UberClassLoader() {
super(PluginManager.class.getClassLoader());
}
public void addNamedClass(String className, Class c) {
generatedClasses.put(className,new WeakReference<Class>(c));
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
WeakReference<Class> wc = generatedClasses.get(name);
if (wc!=null) {
Class c = wc.get();
if (c!=null) return c;
else generatedClasses.remove(name,wc);
}
// first, use the context classloader so that plugins that are loading
// can use its own classloader first.
ClassLoader cl = Thread.currentThread().getContextClassLoader();
......
......@@ -23,6 +23,8 @@
*/
package hudson.util;
import hudson.PluginManager.UberClassLoader;
import hudson.model.Hudson;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;
......@@ -43,7 +45,7 @@ public class SubClassGenerator extends ClassLoader {
public <T> Class<? extends T> generate(Class<T> base, String name) {
ClassWriter cw = new ClassWriter(false,false);//?
cw.visit(49, ACC_PUBLIC, name, null,
cw.visit(49, ACC_PUBLIC, name.replace('.', '/'), null,
Type.getInternalName(base),null);
for (Constructor c : base.getDeclaredConstructors()) {
......@@ -72,7 +74,13 @@ public class SubClassGenerator extends ClassLoader {
cw.visitEnd();
byte[] image = cw.toByteArray();
return defineClass(name, image, 0, image.length).asSubclass(base);
Class<? extends T> c = defineClass(name, image, 0, image.length).asSubclass(base);
Hudson h = Hudson.getInstance();
if (h!=null) // null only during tests.
((UberClassLoader)h.pluginManager.uberClassLoader).addNamedClass(name,c); // can't change the field type as it breaks binary compatibility
return c;
}
private String getMethodDescriptor(Constructor c) {
......
......@@ -365,6 +365,10 @@ public final class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,Ma
return new File(getModulesDir(),child.getModuleName().toFileSystemName());
}
public void onRenamed(MavenModule item, String oldName, String newName) throws IOException {
throw new UnsupportedOperationException();
}
public Collection<Job> getAllJobs() {
Set<Job> jobs = new HashSet<Job>(getItems());
jobs.add(this);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册