Пример #1
0
 private static boolean classExtendsClass(java.lang.Class<?> klass, String className) {
   if (klass == null) return false;
   if (klass.getName().equals(className)) return true;
   if ((className.equals("ceylon.language.Basic"))
       && klass != java.lang.Object.class
       && !isSubclassOfString(klass)
       // && klass!=java.lang.String.class
       && !klass.isAnnotationPresent(Class.class)
       && (!klass.isInterface() || !klass.isAnnotationPresent(Ceylon.class))) {
     // TODO: this is broken for a Java class that
     //      extends a Ceylon class
     return true;
   }
   Class classAnnotation = klass.getAnnotation(Class.class);
   if (classAnnotation != null) {
     String superclassName = declClassName(classAnnotation.extendsType());
     int i = superclassName.indexOf('<');
     if (i > 0) {
       superclassName = superclassName.substring(0, i);
     }
     if (superclassName.isEmpty()) {
       return false;
     }
     try {
       return classExtendsClass(
           java.lang.Class.forName(superclassName, true, klass.getClassLoader()), className);
     } catch (ClassNotFoundException e) {
       throw new RuntimeException(e);
     }
   }
   return classExtendsClass(klass.getSuperclass(), className);
 }
Пример #2
0
  /**
   * Returns all the annotations of this class. If there are no annotations then an empty array is
   * returned.
   *
   * @return a copy of the array containing this class' annotations.
   * @see #getDeclaredAnnotations()
   */
  public Annotation[] getAnnotations() {
    /*
     * We need to get the annotations declared on this class, plus the
     * annotations from superclasses that have the "@Inherited" annotation
     * set.  We create a temporary map to use while we accumulate the
     * annotations and convert it to an array at the end.
     *
     * It's possible to have duplicates when annotations are inherited.
     * We use a Map to filter those out.
     *
     * HashMap might be overkill here.
     */
    HashMap<Class, Annotation> map = new HashMap<Class, Annotation>();
    Annotation[] declaredAnnotations = getDeclaredAnnotations();

    for (int i = declaredAnnotations.length - 1; i >= 0; --i) {
      map.put(declaredAnnotations[i].annotationType(), declaredAnnotations[i]);
    }
    for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
      declaredAnnotations = sup.getDeclaredAnnotations();
      for (int i = declaredAnnotations.length - 1; i >= 0; --i) {
        Class<?> clazz = declaredAnnotations[i].annotationType();
        if (!map.containsKey(clazz) && clazz.isAnnotationPresent(Inherited.class)) {
          map.put(clazz, declaredAnnotations[i]);
        }
      }
    }

    /* convert annotation values from HashMap to array */
    Collection<Annotation> coll = map.values();
    return coll.toArray(new Annotation[coll.size()]);
  }
Пример #3
0
  private static boolean classSatisfiesInterface(
      java.lang.Class<?> klass, String className, Set<java.lang.Class<?>> alreadyVisited) {
    if (klass == null || klass == ceylon.language.Anything.class) return false;
    if ((className.equals("ceylon.language.Identifiable"))
        && klass != java.lang.Object.class
        // && klass!=java.lang.String.class
        && !klass.isAnnotationPresent(Ceylon.class)) {
      // TODO: this is broken for a Java class that
      //      extends a Ceylon class
      return true;
    }
    if (className.equals("ceylon.language.Identifiable") && isSubclassOfString(klass)) {
      return false;
    }
    // try the interfaces
    if (lookForInterface(klass, className, alreadyVisited)) return true;
    // try its superclass
    Class classAnnotation = klass.getAnnotation(Class.class);
    String superclassName;
    if (classAnnotation != null) {
      superclassName = declClassName(classAnnotation.extendsType());
      int i = superclassName.indexOf('<');
      if (i > 0) {
        superclassName = superclassName.substring(0, i);
      }

    } else {
      // Maybe the class didn't have an extends, so implictly Basic
      superclassName = "ceylon.language.Basic";
    }
    if (!superclassName.isEmpty()) {
      try {
        return classSatisfiesInterface(
            java.lang.Class.forName(superclassName, true, klass.getClassLoader()),
            className,
            alreadyVisited);
      } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
      }
    }
    return classSatisfiesInterface(klass.getSuperclass(), className, alreadyVisited);
  }
Пример #4
0
  @Override
  public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
    if (annotationType == null) {
      throw new NullPointerException("annotationType == null");
    }

    A annotation = getDeclaredAnnotation(annotationType);
    if (annotation != null) {
      return annotation;
    }

    if (annotationType.isAnnotationPresent(Inherited.class)) {
      for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
        annotation = sup.getDeclaredAnnotation(annotationType);
        if (annotation != null) {
          return annotation;
        }
      }
    }

    return null;
  }