From 237dbe3e2b9c02b9419418dca07d28c51dde795d Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 31 Jan 2011 12:59:07 -0800 Subject: [PATCH] Other languages on JVM may not be able to provide the type information. --- .../java/hudson/DescriptorExtensionList.java | 12 ++++++------ .../src/main/java/hudson/model/Descriptor.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/hudson/DescriptorExtensionList.java b/core/src/main/java/hudson/DescriptorExtensionList.java index 7e72fcafd3..dc136e67c6 100644 --- a/core/src/main/java/hudson/DescriptorExtensionList.java +++ b/core/src/main/java/hudson/DescriptorExtensionList.java @@ -38,6 +38,7 @@ import hudson.tasks.Publisher.DescriptorExtensionListImpl; import java.util.List; import java.util.ArrayList; import java.util.Iterator; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.concurrent.CopyOnWriteArrayList; import java.lang.reflect.Type; @@ -156,13 +157,12 @@ public class DescriptorExtensionList, D extends Descrip List> r = new ArrayList>(); for( ExtensionComponent c : hudson.getExtensionList(Descriptor.class).getComponents() ) { Descriptor d = c.getInstance(); - Type subTyping = Types.getBaseClass(d.getClass(), Descriptor.class); - if (!(subTyping instanceof ParameterizedType)) { - LOGGER.severe(d.getClass()+" doesn't extend Descriptor with a type parameter"); - continue; // skip this one + try { + if(d.getT()==describableType) + r.add((ExtensionComponent)c); + } catch (IllegalStateException e) { + LOGGER.log(Level.SEVERE, d.getClass() + " doesn't extend Descriptor with a type parameter", e); // skip this one } - if(Types.erasure(Types.getTypeArgument(subTyping,0))==(Class)describableType) - r.add((ExtensionComponent)c); } return r; } diff --git a/core/src/main/java/hudson/model/Descriptor.java b/core/src/main/java/hudson/model/Descriptor.java index 279b0ffb92..480fad649d 100644 --- a/core/src/main/java/hudson/model/Descriptor.java +++ b/core/src/main/java/hudson/model/Descriptor.java @@ -23,6 +23,8 @@ */ package hudson.model; +import hudson.DescriptorExtensionList; +import hudson.ExtensionComponent; import hudson.RelativePath; import hudson.XmlFile; import hudson.BulkChange; @@ -267,6 +269,22 @@ public abstract class Descriptor> implements Saveable { return clazz.getName(); } + /** + * Unlike {@link #clazz}, return the parameter type 'T', which determines + * the {@link DescriptorExtensionList} that this goes to. + * + *

+ * In those situations where subtypes cannot provide the type parameter, + * this method can be overridden to provide it. + */ + public Class getT() { + Type subTyping = Types.getBaseClass(getClass(), Descriptor.class); + if (!(subTyping instanceof ParameterizedType)) { + throw new IllegalStateException(getClass()+" doesn't extend Descriptor with a type parameter."); + } + return Types.erasure(Types.getTypeArgument(subTyping, 0)); + } + /** * Gets the URL that this Descriptor is bound to, relative to the nearest {@link DescriptorByNameOwner}. * Since {@link Hudson} is a {@link DescriptorByNameOwner}, there's always one such ancestor to any request.