public <T extends Item> T getItem(Class<T> c) {
    for (int i = 0; i < items.length; i++) {
      if (c.isInstance(items[i])) return c.cast(items[i]);
    }

    return null;
  }
 private static <T> T getParamXPath(
     final Class<T> pClass, final String pXpath, final CompactFragment pBody) throws XmlException {
   // TODO Avoid JAXB where possible, use XMLDeserializer instead
   final boolean string = CharSequence.class.isAssignableFrom(pClass);
   Node match;
   DocumentFragment fragment =
       DomUtil.childrenToDocumentFragment(XMLFragmentStreamReader.from(pBody));
   for (Node n = fragment.getFirstChild(); n != null; n = n.getNextSibling()) {
     match = xpathMatch(n, pXpath);
     if (match != null) {
       if (!string) {
         XmlDeserializer deserializer = pClass.getAnnotation(XmlDeserializer.class);
         if (deserializer != null) {
           try {
             XmlDeserializerFactory<?> factory = deserializer.value().newInstance();
             factory.deserialize(XmlStreaming.newReader(new DOMSource(n)));
           } catch (InstantiationException | IllegalAccessException e) {
             throw new RuntimeException(e);
           }
         } else {
           return JAXB.unmarshal(new DOMSource(match), pClass);
         }
       } else {
         return pClass.cast(nodeToString(match));
       }
     }
   }
   return null;
 }
示例#3
0
文件: Job.java 项目: neo2buha/jenkins
 /**
  * Removes the property of the given type.
  *
  * @return The property that was just removed.
  * @since 1.279
  */
 public <T extends JobProperty> T removeProperty(Class<T> clazz) throws IOException {
   for (JobProperty<? super JobT> p : properties) {
     if (clazz.isInstance(p)) {
       removeProperty(p);
       return clazz.cast(p);
     }
   }
   return null;
 }
 public static <E extends Exception> VirtualFileVisitor.Result visitChildrenRecursively(
     @NotNull VirtualFile file, @NotNull VirtualFileVisitor visitor, @NotNull Class<E> eClass)
     throws E {
   try {
     return visitChildrenRecursively(file, visitor);
   } catch (VisitorException e) {
     final Throwable cause = e.getCause();
     if (eClass.isInstance(cause)) {
       throw eClass.cast(cause);
     }
     throw e;
   }
 }
  /**
   * Return the first fetch response item of the given class for the given message number.
   *
   * @param r the responses
   * @param msgno the message number
   * @param c the class
   * @param	<T> the type of fetch item
   * @return the fetch item
   */
  public static <T extends Item> T getItem(Response[] r, int msgno, Class<T> c) {
    if (r == null) return null;

    for (int i = 0; i < r.length; i++) {

      if (r[i] == null
          || !(r[i] instanceof FetchResponse)
          || ((FetchResponse) r[i]).getNumber() != msgno) continue;

      FetchResponse f = (FetchResponse) r[i];
      for (int j = 0; j < f.items.length; j++) {
        if (c.isInstance(f.items[j])) return c.cast(f.items[j]);
      }
    }

    return null;
  }
  /**
   * Return all fetch response items of the given class for the given message number.
   *
   * @param r the responses
   * @param msgno the message number
   * @param c the class
   * @param	<T> the type of fetch items
   * @return the list of fetch items
   * @since JavaMail 1.5.2
   */
  public static <T extends Item> List<T> getItems(Response[] r, int msgno, Class<T> c) {
    List<T> items = new ArrayList<T>();

    if (r == null) return items;

    for (int i = 0; i < r.length; i++) {

      if (r[i] == null
          || !(r[i] instanceof FetchResponse)
          || ((FetchResponse) r[i]).getNumber() != msgno) continue;

      FetchResponse f = (FetchResponse) r[i];
      for (int j = 0; j < f.items.length; j++) {
        if (c.isInstance(f.items[j])) items.add(c.cast(f.items[j]));
      }
    }

    return items;
  }
示例#7
0
文件: Job.java 项目: neo2buha/jenkins
 /** Gets the specific property, or null if the propert is not configured for this job. */
 public <T extends JobProperty> T getProperty(Class<T> clazz) {
   for (JobProperty p : properties) {
     if (clazz.isInstance(p)) return clazz.cast(p);
   }
   return null;
 }