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

JavaReflectionUtil.java 文件源码 项目:Reer 阅读 28 收藏 0 点赞 0 评论 0
private static <A extends Annotation> A getAnnotation(Class<?> type, Class<A> annotationType, boolean checkType) {
    A annotation;
    if (checkType) {
        annotation = type.getAnnotation(annotationType);
        if (annotation != null) {
            return annotation;
        }
    }

    if (annotationType.getAnnotation(Inherited.class) != null) {
        for (Class<?> anInterface : type.getInterfaces()) {
            annotation = getAnnotation(anInterface, annotationType, true);
            if (annotation != null) {
                return annotation;
            }
        }
    }

    if (type.isInterface() || type.equals(Object.class)) {
        return null;
    } else {
        return getAnnotation(type.getSuperclass(), annotationType, false);
    }
}
Utils.java 文件源码 项目:reflect 阅读 30 收藏 0 点赞 0 评论 0
public static Predicate<Class<?>> predicateClassAnnotatedWith(
    final Class<? extends Annotation> annotation) {
  if (!annotation.isAnnotationPresent(Inherited.class)) {
    return predicateAnnotatedWith(annotation);
  }

  return new Predicate<Class<?>>() {
    @Override
    public boolean apply(Class<?> c) {
      while (c != null) {
        if (c.isAnnotationPresent(annotation)) {
          return true;
        }
        c = c.getSuperclass();
      }
      return false;
    }
  };
}
JavacElements.java 文件源码 项目:javaide 阅读 26 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
ReflectAllInOneCreator.java 文件源码 项目:gwt-backbone 阅读 34 收藏 0 点赞 0 评论 0
private void getAllReflectionClasses() throws NotFoundException{

        //System annotations
        addClassIfNotExists(typeOracle.getType(Retention.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
        addClassIfNotExists(typeOracle.getType(Documented.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
        addClassIfNotExists(typeOracle.getType(Inherited.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
        addClassIfNotExists(typeOracle.getType(Target.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
        addClassIfNotExists(typeOracle.getType(Deprecated.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
        //typeOracle.getType("org.lirazs.gbackbone.client.test.reflection.TestReflectionGenerics.TestReflection1");

        //=====GWT0.7
        for (JClassType classType : typeOracle.getTypes()) {
            Reflectable reflectable = GenUtils.getClassTypeAnnotationWithMataAnnotation(classType, Reflectable.class);
            if (reflectable != null){
                processClass(classType, reflectable);

                if (reflectable.assignableClasses()){
                    for (JClassType type : classType.getSubtypes()){
                        processClass(type, reflectable);
                    }
                }
            }
        }
        //======end of gwt0.7
    }
JavaReflectionUtil.java 文件源码 项目:Pushjet-Android 阅读 24 收藏 0 点赞 0 评论 0
private static <A extends Annotation> A getAnnotation(Class<?> type, Class<A> annotationType, boolean checkType) {
    A annotation;
    if (checkType) {
        annotation = type.getAnnotation(annotationType);
        if (annotation != null) {
            return annotation;
        }
    }

    if (annotationType.getAnnotation(Inherited.class) != null) {
        for (Class<?> anInterface : type.getInterfaces()) {
            annotation = getAnnotation(anInterface, annotationType, true);
            if (annotation != null) {
                return annotation;
            }
        }
    }

    if (type.isInterface() || type.equals(Object.class)) {
        return null;
    } else {
        return getAnnotation(type.getSuperclass(), annotationType, false);
    }
}
JavaReflectionUtil.java 文件源码 项目:Pushjet-Android 阅读 31 收藏 0 点赞 0 评论 0
private static <A extends Annotation> A getAnnotation(Class<?> type, Class<A> annotationType, boolean checkType) {
    A annotation;
    if (checkType) {
        annotation = type.getAnnotation(annotationType);
        if (annotation != null) {
            return annotation;
        }
    }

    if (annotationType.getAnnotation(Inherited.class) != null) {
        for (Class<?> anInterface : type.getInterfaces()) {
            annotation = getAnnotation(anInterface, annotationType, true);
            if (annotation != null) {
                return annotation;
            }
        }
    }

    if (type.isInterface() || type.equals(Object.class)) {
        return null;
    } else {
        return getAnnotation(type.getSuperclass(), annotationType, false);
    }
}
JavacElements.java 文件源码 项目:form-follows-function 阅读 34 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
ClassIndexProcessor.java 文件源码 项目:soklet 阅读 31 收藏 0 点赞 0 评论 0
/**
 * Index super types for {@link IndexSubclasses} and any {@link IndexAnnotated}
 * additionally accompanied by {@link Inherited}.
 */
private void indexSupertypes(TypeElement rootElement, TypeElement element) throws IOException {

    for (TypeMirror mirror : types.directSupertypes(element.asType())) {
        if (mirror.getKind() != TypeKind.DECLARED) {
            continue;
        }

        DeclaredType superType = (DeclaredType) mirror;
        TypeElement superTypeElement = (TypeElement) superType.asElement();
        storeSubclass(superTypeElement, rootElement);

        for (AnnotationMirror annotationMirror : superTypeElement.getAnnotationMirrors()) {
            TypeElement annotationElement = (TypeElement) annotationMirror.getAnnotationType()
                    .asElement();

            if (hasAnnotation(annotationElement, Inherited.class)) {
                storeAnnotation(annotationElement, rootElement);
            }
        }

        indexSupertypes(rootElement, superTypeElement);
    }
}
JavacElements.java 文件源码 项目:openjdk-source-code-learn 阅读 38 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
JavacElements.java 文件源码 项目:s4j 阅读 29 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
JavacElements.java 文件源码 项目:jdk7-langtools 阅读 47 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
JavacElements.java 文件源码 项目:javap 阅读 32 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
JavacElements.java 文件源码 项目:openjdk-icedtea7 阅读 26 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
JavacElements.java 文件源码 项目:metricgenerator-jdk-compiler 阅读 39 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
AnnotationsAnalyzer.java 文件源码 项目:deadcode4j 阅读 27 收藏 0 点赞 0 评论 0
@Nonnull
@Override
public List<String> apply(@Nonnull AnalysisContext analysisContext) {
    List<String> inheritedAnnotations = newArrayList();
    ClassPool classPool = classPoolAccessorFor(analysisContext).getClassPool();
    for (String annotation : getAnnotationsFoundInClassPath(analysisContext)) {
        CtClass annotationClazz = classPool.getOrNull(annotation);
        if (annotationClazz == null) {
            logger.debug("Annotation [{}] cannot be found on the class path; skipping detection", annotation);
            continue;
        }
        try {
            if (annotationClazz.getAnnotation(Inherited.class) != null) {
                inheritedAnnotations.add(annotation);
            }
        } catch (ClassNotFoundException e) {
            logger.debug("@Inherited is not available; this is quite disturbing.");
        }
    }
    logger.debug("Found those inheritable annotations: {}", inheritedAnnotations);
    return inheritedAnnotations;
}
AnnotationUtils.java 文件源码 项目:kie-wb-common 阅读 27 收藏 0 点赞 0 评论 0
private static Set<org.kie.soup.project.datamodel.oracle.Annotation> getAnnotations( final java.lang.annotation.Annotation[] annotations,
                                                                                            boolean checkInheritance ) {
    final Set<org.kie.soup.project.datamodel.oracle.Annotation> fieldAnnotations = new LinkedHashSet<>();
    for ( java.lang.annotation.Annotation a : annotations ) {

        if ( checkInheritance ) {
            if ( !a.annotationType().isAnnotationPresent( Inherited.class ) ) {
                continue;
            }
        }

        final org.kie.soup.project.datamodel.oracle.Annotation fieldAnnotation = new org.kie.soup.project.datamodel.oracle.Annotation( a.annotationType().getName() );
        for ( Method m : a.annotationType().getDeclaredMethods() ) {
            final String methodName = m.getName();
            fieldAnnotation.addParameter( methodName, getAnnotationAttributeValue( a, methodName ) );
        }
        fieldAnnotations.add( fieldAnnotation );
    }
    return fieldAnnotations;
}
JavacElements.java 文件源码 项目:INF5000-StaticProxy 阅读 26 收藏 0 点赞 0 评论 0
/**
 * An internal-use utility that creates a reified annotation.
 * This overloaded version take annotation inheritance into account.
 */
public static <A extends Annotation> A getAnnotation(ClassSymbol annotated,
                                                     Class<A> annoType) {
    boolean inherited = annoType.isAnnotationPresent(Inherited.class);
    A result = null;
    while (annotated.name != annotated.name.table.names.java_lang_Object) {
        result = getAnnotation((Symbol)annotated, annoType);
        if (result != null || !inherited)
            break;
        Type sup = annotated.getSuperclass();
        if (sup.tag != TypeTags.CLASS || sup.isErroneous())
            break;
        annotated = (ClassSymbol) sup.tsym;
    }
    return result;
}
AsmBackedClassGenerator.java 文件源码 项目:Reer 阅读 40 收藏 0 点赞 0 评论 0
public void addConstructor(Constructor<?> constructor) throws Exception {
    List<Type> paramTypes = new ArrayList<Type>();
    for (Class<?> paramType : constructor.getParameterTypes()) {
        paramTypes.add(Type.getType(paramType));
    }
    String methodDescriptor = Type.getMethodDescriptor(VOID_TYPE, paramTypes.toArray(EMPTY_TYPES));

    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), EMPTY_STRINGS);

    for (Annotation annotation : constructor.getDeclaredAnnotations()) {
        if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
            continue;
        }
        Retention retention = annotation.annotationType().getAnnotation(Retention.class);
        AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME);
        annotationVisitor.visitEnd();
    }

    methodVisitor.visitCode();

    // this.super(p0 .. pn)
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    for (int i = 0; i < constructor.getParameterTypes().length; i++) {
        methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1);
    }
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor, false);

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();
}
AsmBackedClassGenerator.java 文件源码 项目:Reer 阅读 40 收藏 0 点赞 0 评论 0
private void includeNotInheritedAnnotations() {
    for (Annotation annotation : type.getDeclaredAnnotations()) {
        if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
            continue;
        }
        Retention retention = annotation.annotationType().getAnnotation(Retention.class);
        boolean visible = retention != null && retention.value() == RetentionPolicy.RUNTIME;
        AnnotationVisitor annotationVisitor = visitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), visible);
        visitAnnotationValues(annotation, annotationVisitor);
        annotationVisitor.visitEnd();
    }
}
FeatureEnumTest.java 文件源码 项目:guava-mock 阅读 32 收藏 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());
  }
}
Symbol.java 文件源码 项目:OpenJSharp 阅读 59 收藏 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);
}
Symbol.java 文件源码 项目:openjdk-jdk10 阅读 37 收藏 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);
}
FeatureEnumTest.java 文件源码 项目:googles-monorepo-demo 阅读 31 收藏 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());
  }
}
Symbol.java 文件源码 项目:openjdk9 阅读 38 收藏 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);
}
Symbol.java 文件源码 项目:lookaside_java-1.8.0-openjdk 阅读 36 收藏 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);
}
Symbol.java 文件源码 项目:jsr308-langtools 阅读 33 收藏 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);
}
Annotations.java 文件源码 项目:gwt-backbone 阅读 38 收藏 0 点赞 0 评论 0
private void initializeAnnotations() {
    if (lazyAnnotations != null) {
        return;
    }

    if (parent != null) {
        lazyAnnotations = new HashMap<Class<?>, Annotation>();
        // ((Annotations)parent).initializeAnnotations();
        // for (Entry<Class<?>, Annotation> entry :
        // ((Annotations)parent).lazyAnnotations.entrySet()) {
        // if
        // (entry.getValue().annotationType().isAnnotationPresent(Inherited.class))
        // {
        // lazyAnnotations.put(entry.getKey(), entry.getValue());
        // }
        // }

        for (Annotation a : parent.getAnnotations()) {
            if (ClassHelper.AsClass(a.annotationType())
                    .isAnnotationPresent(Inherited.class)) {
                lazyAnnotations.put(a.annotationType(), a);
            }
        }

        lazyAnnotations.putAll(declaredAnnotations);
    } else {
        lazyAnnotations = declaredAnnotations;
    }
}
FeatureEnumTest.java 文件源码 项目:guava-libraries 阅读 28 收藏 0 点赞 0 评论 0
private static void assertGoodTesterAnnotation(
    Class<? extends Annotation> annotationClass) {
  assertNotNull(
      String.format("%s must be annotated with @TesterAnnotation.",
          annotationClass),
      annotationClass.getAnnotation(TesterAnnotation.class));
  final Retention retentionPolicy =
      annotationClass.getAnnotation(Retention.class);
  assertNotNull(
      String.format("%s must have a @Retention annotation.", annotationClass),
      retentionPolicy);
  assertEquals(
      String.format("%s must have RUNTIME RetentionPolicy.", annotationClass),
      RetentionPolicy.RUNTIME, retentionPolicy.value());
  assertNotNull(
      String.format("%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(String.format("%s must have a property named '%s'.",
          annotationClass, propertyName));
    }
    final Class<?> returnType = method.getReturnType();
    assertTrue(String.format("%s.%s() must return an array.",
        annotationClass, propertyName),
        returnType.isArray());
    assertSame(String.format("%s.%s() must return an array of %s.",
        annotationClass, propertyName, annotationClass.getDeclaringClass()),
        annotationClass.getDeclaringClass(), returnType.getComponentType());
  }
}
AsmBackedClassGenerator.java 文件源码 项目:Pushjet-Android 阅读 51 收藏 0 点赞 0 评论 0
public void addConstructor(Constructor<?> constructor) throws Exception {
    List<Type> paramTypes = new ArrayList<Type>();
    for (Class<?> paramType : constructor.getParameterTypes()) {
        paramTypes.add(Type.getType(paramType));
    }
    String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, paramTypes.toArray(
            new Type[paramTypes.size()]));

    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), new String[0]);

    for (Annotation annotation : constructor.getDeclaredAnnotations()) {
        if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
            continue;
        }
        Retention retention = annotation.annotationType().getAnnotation(Retention.class);
        AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME);
        annotationVisitor.visitEnd();
    }

    methodVisitor.visitCode();

    // this.super(p0 .. pn)
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    for (int i = 0; i < constructor.getParameterTypes().length; i++) {
        methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1);
    }
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>",
            methodDescriptor);

    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();
}
ClassDeclarationImpl.java 文件源码 项目:form-follows-function 阅读 30 收藏 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;
}


问题


面经


文章

微信
公众号

扫码关注公众号