java类java.lang.annotation.Inherited的实例源码

Symbol.java 文件源码 项目:infobip-open-jdk-8 阅读 26 收藏 0 点赞 0 评论 0
@Override
protected <A extends Annotation> Attribute.Compound getAttribute(final Class<A> annoType) {

    Attribute.Compound attrib = super.getAttribute(annoType);

    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    if (attrib != null || !inherited)
        return attrib;

    // Search supertypes
    ClassSymbol superType = getSuperClassToSearchForAnnotations();
    return superType == null ? null
                             : superType.getAttribute(annoType);
}
Class.java 文件源码 项目:In-the-Box-Fork 阅读 34 收藏 0 点赞 0 评论 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[] annos = getDeclaredAnnotations();

    for (int i = annos.length-1; i >= 0; --i)
        map.put(annos[i].annotationType(), annos[i]);

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

    /* convert annotation values from HashMap to array */
    Collection<Annotation> coll = map.values();
    return coll.toArray(new Annotation[coll.size()]);
}
ElementImpl.java 文件源码 项目:Eclipse-Postfix-Code-Completion 阅读 23 收藏 0 点赞 0 评论 0
@Override
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
    A annotation = _env.getFactory().getAnnotation(getPackedAnnotationBindings(), annotationClass);
    if (annotation != null || this.getKind() != ElementKind.CLASS || annotationClass.getAnnotation(Inherited.class) == null)
        return annotation;

    ElementImpl superClass = (ElementImpl) _env.getFactory().newElement(((ReferenceBinding) this._binding).superclass());
    return superClass == null ? null : superClass.getAnnotation(annotationClass);
}
ElementImpl.java 文件源码 项目:Eclipse-Postfix-Code-Completion 阅读 27 收藏 0 点赞 0 评论 0
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
    A [] annotations = _env.getFactory().getAnnotationsByType(Factory.getUnpackedAnnotationBindings(getPackedAnnotationBindings()), annotationType);
    if (annotations.length != 0 || this.getKind() != ElementKind.CLASS || annotationType.getAnnotation(Inherited.class) == null)
        return annotations;

    ElementImpl superClass =  (ElementImpl) _env.getFactory().newElement(((ReferenceBinding) this._binding).superclass());
    return superClass == null ? annotations : superClass.getAnnotationsByType(annotationType);
}
AnnotationType.java 文件源码 项目:gjpf-core 阅读 31 收藏 0 点赞 0 评论 0
private AnnotationType(final Class<?> annoCls) {
  if (!annoCls.isAnnotation()) {
    throw new IllegalArgumentException("Not an annotation type");
  }

  Method[] methods = annoCls.getDeclaredMethods();

  for (Method m : methods) {
    if (m.getParameterTypes().length == 0) {
      // cache name -> method assoc
      String mname = m.getName();
      members.put(mname, m);

      // cache member type
      Class<?> type = m.getReturnType();
      memberTypes.put(mname, invocationHandlerReturnType(type));

      // cache member default val (if any)
      Object val = m.getDefaultValue();
      if (val != null) {
        memberDefaults.put(mname, val);
      }
    } else {
      // probably an exception
    }
  }

  if ((annoCls != Retention.class) && (annoCls != Inherited.class)) { // don't get recursive
    inherited = annoCls.isAnnotationPresent(Inherited.class);

    Retention r = annoCls.getAnnotation(Retention.class);
    if (r == null) {
      retention = RetentionPolicy.CLASS;
    } else {
      retention = r.value();
    }
  }

  SharedSecrets.getJavaLangAccess().setAnnotationType(annoCls, this);
}
FeatureEnumTest.java 文件源码 项目:guava 阅读 28 收藏 0 点赞 0 评论 0
private static void assertGoodTesterAnnotation(Class<? extends Annotation> annotationClass) {
  assertNotNull(
      rootLocaleFormat("%s must be annotated with @TesterAnnotation.", annotationClass),
      annotationClass.getAnnotation(TesterAnnotation.class));
  final Retention retentionPolicy = annotationClass.getAnnotation(Retention.class);
  assertNotNull(
      rootLocaleFormat("%s must have a @Retention annotation.", annotationClass),
      retentionPolicy);
  assertEquals(
      rootLocaleFormat("%s must have RUNTIME RetentionPolicy.", annotationClass),
      RetentionPolicy.RUNTIME,
      retentionPolicy.value());
  assertNotNull(
      rootLocaleFormat("%s must be inherited.", annotationClass),
      annotationClass.getAnnotation(Inherited.class));

  for (String propertyName : new String[] {"value", "absent"}) {
    Method method = null;
    try {
      method = annotationClass.getMethod(propertyName);
    } catch (NoSuchMethodException e) {
      fail(
          rootLocaleFormat("%s must have a property named '%s'.", annotationClass, propertyName));
    }
    final Class<?> returnType = method.getReturnType();
    assertTrue(
        rootLocaleFormat("%s.%s() must return an array.", annotationClass, propertyName),
        returnType.isArray());
    assertSame(
        rootLocaleFormat(
            "%s.%s() must return an array of %s.",
            annotationClass, propertyName, annotationClass.getDeclaringClass()),
        annotationClass.getDeclaringClass(),
        returnType.getComponentType());
  }
}
FeatureEnumTest.java 文件源码 项目:guava 阅读 23 收藏 0 点赞 0 评论 0
private static void assertGoodTesterAnnotation(Class<? extends Annotation> annotationClass) {
  assertNotNull(
      rootLocaleFormat("%s must be annotated with @TesterAnnotation.", annotationClass),
      annotationClass.getAnnotation(TesterAnnotation.class));
  final Retention retentionPolicy = annotationClass.getAnnotation(Retention.class);
  assertNotNull(
      rootLocaleFormat("%s must have a @Retention annotation.", annotationClass),
      retentionPolicy);
  assertEquals(
      rootLocaleFormat("%s must have RUNTIME RetentionPolicy.", annotationClass),
      RetentionPolicy.RUNTIME,
      retentionPolicy.value());
  assertNotNull(
      rootLocaleFormat("%s must be inherited.", annotationClass),
      annotationClass.getAnnotation(Inherited.class));

  for (String propertyName : new String[] {"value", "absent"}) {
    Method method = null;
    try {
      method = annotationClass.getMethod(propertyName);
    } catch (NoSuchMethodException e) {
      fail(
          rootLocaleFormat("%s must have a property named '%s'.", annotationClass, propertyName));
    }
    final Class<?> returnType = method.getReturnType();
    assertTrue(
        rootLocaleFormat("%s.%s() must return an array.", annotationClass, propertyName),
        returnType.isArray());
    assertSame(
        rootLocaleFormat(
            "%s.%s() must return an array of %s.",
            annotationClass, propertyName, annotationClass.getDeclaringClass()),
        annotationClass.getDeclaringClass(),
        returnType.getComponentType());
  }
}
AnnotationType.java 文件源码 项目:jpf-core 阅读 35 收藏 0 点赞 0 评论 0
private AnnotationType(final Class<?> annoCls) {
  if (!annoCls.isAnnotation()) {
    throw new IllegalArgumentException("Not an annotation type");
  }

  Method[] methods = annoCls.getDeclaredMethods();

  for (Method m : methods) {
    if (m.getParameterTypes().length == 0) {
      // cache name -> method assoc
      String mname = m.getName();
      members.put(mname, m);

      // cache member type
      Class<?> type = m.getReturnType();
      memberTypes.put(mname, invocationHandlerReturnType(type));

      // cache member default val (if any)
      Object val = m.getDefaultValue();
      if (val != null) {
        memberDefaults.put(mname, val);
      }
    } else {
      // probably an exception
    }
  }

  if ((annoCls != Retention.class) && (annoCls != Inherited.class)) { // don't get recursive
    inherited = annoCls.isAnnotationPresent(Inherited.class);

    Retention r = annoCls.getAnnotation(Retention.class);
    if (r == null) {
      retention = RetentionPolicy.CLASS;
    } else {
      retention = r.value();
    }
  }

  SharedSecrets.getJavaLangAccess().setAnnotationType(annoCls, this);
}
ClassDeclarationImpl.java 文件源码 项目:openjdk-source-code-learn 阅读 21 收藏 0 点赞 0 评论 0
/**
 * {@inheritDoc}
 * Overridden here to handle @Inherited.
 */
public <A extends Annotation> A getAnnotation(Class<A> annoType) {

    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    for (Type t = sym.type;
         t.tsym != env.symtab.objectType.tsym && !t.isErroneous();
         t = env.jctypes.supertype(t)) {

        A result = getAnnotation(annoType, t.tsym);
        if (result != null || !inherited) {
            return result;
        }
    }
    return null;
}
AnnotationUtil.java 文件源码 项目:jesterj 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Execute the supplied runnable once (in same thread) if the supplied method has the supplied annotation.
 * This method supports inheritance of method annotations. If the supplied method overrides a superclass or
 * implements an interface with the annotation the runnable will be executed. Even if the annotation is available
 * from multiple levels of the class hierarchy the runnable will only execute once.
 *
 * @param meth         The method to test
 * @param r            The runnable that will run depending on the annotation
 * @param runIfPresent true to run when the annotation is found, false to run when it's not found.
 * @param annotation   The annotation we are looking for
 */
public void runIfMethodAnnotated(Method meth, Runnable r, boolean runIfPresent,
                                 Class<? extends Annotation> annotation) {
  if (!annotation.isAnnotationPresent(Inherited.class)) {
    boolean annotationPresent = meth.isAnnotationPresent(annotation);
    if (runIfPresent && annotationPresent || !runIfPresent && !annotationPresent) {
      r.run();
    }
  } else {
    Set<Class> classes = new HashSet<>();
    Class<?> clazz = meth.getDeclaringClass();
    collectInterfaces(classes, clazz);
    while (clazz != Object.class) {
      classes.add(clazz);
      clazz = clazz.getSuperclass();
    }

    // now iterate all superclasses and interfaces looking for a method with identical signature that has 
    // the annotation in question.
    boolean found = false;
    for (Class<?> c : classes) {
      try {
        Method m = c.getMethod(meth.getName(), meth.getParameterTypes());
        Annotation[] declaredAnnotations = m.getDeclaredAnnotations();
        for (Annotation a : declaredAnnotations) {
          found |= annotation == a.annotationType();
          if (runIfPresent && found) {
            r.run();
            return;
          }
        }
      } catch (NoSuchMethodException ignored) {
      }
    }
    if (!runIfPresent && !found) {
      r.run();
    }
  }
}


问题


面经


文章

微信
公众号

扫码关注公众号