Beispiel #1
0
 protected RunListener() {
   Type type = Types.getBaseClass(getClass(), RunListener.class);
   if (type instanceof ParameterizedType)
     targetType = Types.erasure(Types.getTypeArgument(type, 0));
   else
     throw new IllegalStateException(getClass() + " uses the raw type for extending RunListener");
 }
Beispiel #2
0
 /**
  * Given {@code c=MyList (extends ArrayList<Foo>), base=List}, compute the parameterization of
  * 'base' that's assignable from 'c' (in this case {@code List<Foo>}), and return its n-th type
  * parameter (n=0 would return {@code Foo}).
  *
  * <p>This method is useful for doing type arithmetic.
  *
  * @throws AssertionError if c' is not parameterized.
  */
 public static <B> Class getTypeParameter(Class<? extends B> c, Class<B> base, int n) {
   Type parameterization = Types.getBaseClass(c, base);
   if (parameterization instanceof ParameterizedType) {
     ParameterizedType pt = (ParameterizedType) parameterization;
     return Types.erasure(Types.getTypeArgument(pt, n));
   } else {
     throw new AssertionError(c + " doesn't properly parameterize " + base);
   }
 }
Beispiel #3
0
    private Class computeItemType() {
      if (clazz.isArray()) {
        return clazz.getComponentType();
      }
      if (Collection.class.isAssignableFrom(clazz)) {
        Type col = Types.getBaseClass(type, Collection.class);

        if (col instanceof ParameterizedType) return Types.erasure(Types.getTypeArgument(col, 0));
        else return Object.class;
      }
      return null;
    }
Beispiel #4
0
 /** Auto-discovers {@link OptionHandler}s and add them to the given command line parser. */
 protected void registerOptionHandlers() {
   try {
     for (Class c :
         Index.list(
             OptionHandlerExtension.class,
             Jenkins.getInstance().pluginManager.uberClassLoader,
             Class.class)) {
       Type t = Types.getBaseClass(c, OptionHandler.class);
       CmdLineParser.registerHandler(Types.erasure(Types.getTypeArgument(t, 0)), c);
     }
   } catch (IOException e) {
     throw new Error(e);
   }
 }
Beispiel #5
0
  static {
    // register option handlers that are defined
    ClassLoaders cls = new ClassLoaders();
    Jenkins j = Jenkins.getInstance();
    if (j != null) { // only when running on the master
      cls.put(j.getPluginManager().uberClassLoader);

      ResourceNameIterator servicesIter =
          new DiscoverServiceNames(cls).findResourceNames(OptionHandler.class.getName());
      final ResourceClassIterator itr = new DiscoverClasses(cls).findResourceClasses(servicesIter);

      while (itr.hasNext()) {
        Class h = itr.nextResourceClass().loadClass();
        Class c =
            Types.erasure(Types.getTypeArgument(Types.getBaseClass(h, OptionHandler.class), 0));
        CmdLineParser.registerHandler(c, h);
      }
    }
  }
Beispiel #6
0
  /**
   * Infers the type of the corresponding {@link Describable} from the outer class. This version
   * works when you follow the common convention, where a descriptor is written as the static nested
   * class of the describable class.
   *
   * @since 1.278
   */
  protected Descriptor() {
    this.clazz = (Class<T>) getClass().getEnclosingClass();
    if (clazz == null)
      throw new AssertionError(
          getClass()
              + " doesn't have an outer class. Use the constructor that takes the Class object explicitly.");

    // detect an type error
    Type bt = Types.getBaseClass(getClass(), Descriptor.class);
    if (bt instanceof ParameterizedType) {
      ParameterizedType pt = (ParameterizedType) bt;
      // this 't' is the closest approximation of T of Descriptor<T>.
      Class t = Types.erasure(pt.getActualTypeArguments()[0]);
      if (!t.isAssignableFrom(clazz))
        throw new AssertionError(
            "Outer class "
                + clazz
                + " of "
                + getClass()
                + " is not assignable to "
                + t
                + ". Perhaps wrong outer class?");
    }

    // detect a type error. this Descriptor is supposed to be returned from getDescriptor(), so make
    // sure its type match up.
    // this prevents a bug like
    // http://www.nabble.com/Creating-a-new-parameter-Type-%3A-Masked-Parameter-td24786554.html
    try {
      Method getd = clazz.getMethod("getDescriptor");
      if (!getd.getReturnType().isAssignableFrom(getClass())) {
        throw new AssertionError(getClass() + " must be assignable to " + getd.getReturnType());
      }
    } catch (NoSuchMethodException e) {
      throw new AssertionError(getClass() + " is missing getDescriptor method.");
    }
  }