Java Code Examples for java.lang.annotation.Inherited

The following examples show how to use java.lang.annotation.Inherited. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: AndroidComponentPlugin   Source File: Class.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @throws NullPointerException {@inheritDoc}
 * @since 1.5
 */
@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
    Objects.requireNonNull(annotationClass);

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

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

    return null;
}
 
Example 2
Source Project: AndroidComponentPlugin   Source File: Class.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 * @since 1.5
 */
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
    if (annotationClass == null) {
        throw new NullPointerException("annotationClass == null");
    }

    if (isDeclaredAnnotationPresent(annotationClass)) {
        return true;
    }

    if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
        for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
            if (sup.isDeclaredAnnotationPresent(annotationClass)) {
                return true;
            }
        }
    }

    return false;
}
 
Example 3
Source Project: AndroidComponentPlugin   Source File: Class.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @throws NullPointerException {@inheritDoc}
 * @since 1.5
 */
@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
    Objects.requireNonNull(annotationClass);

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

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

    return null;
}
 
Example 4
Source Project: AndroidComponentPlugin   Source File: Class.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @throws NullPointerException {@inheritDoc}
 * @since 1.5
 */
@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
    if (annotationClass == null) {
        throw new NullPointerException("annotationClass == null");
    }

    if (isDeclaredAnnotationPresent(annotationClass)) {
        return true;
    }

    if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
        for (Class<?> sup = getSuperclass(); sup != null; sup = sup.getSuperclass()) {
            if (sup.isDeclaredAnnotationPresent(annotationClass)) {
                return true;
            }
        }
    }

    return false;
}
 
Example 5
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);
    }
}
 
Example 6
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);
    }
}
 
Example 7
Source Project: java-n-IDE-for-Android   Source File: JavacElements.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source Project: netbeans   Source File: AnnotationObjectProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<BindingQualifier> createObjects( TypeElement type ) {
    final List<BindingQualifier> result = new ArrayList<BindingQualifier>();
    Map<String, ? extends AnnotationMirror> annotationsByType = 
        getHelper().getAnnotationsByType(getHelper().getCompilationController().
            getElements().getAllAnnotationMirrors( type ));
    AnnotationMirror annotationMirror = annotationsByType.get( 
            getAnnotationName());
    if (annotationMirror != null ) {
        result.add( new BindingQualifier(getHelper(), type, getAnnotationName()));
    }
    if ( annotationMirror == null || !getHelper().hasAnnotation( annotationMirror.
            getAnnotationType().asElement().
            getAnnotationMirrors(), 
            Inherited.class.getCanonicalName()))
    {
        if ( checkSuper( type , getAnnotationName() , getHelper())!= null ){
            result.add( new BindingQualifier( getHelper(), type, getAnnotationName()) );
        }
    }
    return result;
}
 
Example 9
Source Project: javaide   Source File: JavacElements.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 10
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);
    }
}
 
Example 11
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);
    }
}
 
Example 12
Source Project: deadcode4j   Source File: AnnotationsAnalyzer.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 13
Source Project: spring-analysis-note   Source File: AnnotationTypeFilter.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@code AnnotationTypeFilter} for the given annotation type.
 * @param annotationType the annotation type to match
 * @param considerMetaAnnotations whether to also match on meta-annotations
 * @param considerInterfaces whether to also match interfaces
 */
public AnnotationTypeFilter(
		Class<? extends Annotation> annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) {

	super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces);
	this.annotationType = annotationType;
	this.considerMetaAnnotations = considerMetaAnnotations;
}
 
Example 14
Source Project: AndroidComponentPlugin   Source File: Class.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @throws NullPointerException {@inheritDoc}
 * @since 1.8
 */
@Override
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
  // Find any associated annotations [directly or repeatably (indirectly) present on this].
  A[] annotations = GenericDeclaration.super.getAnnotationsByType(annotationClass);

  if (annotations.length != 0) {
    return annotations;
  }

  // Nothing was found, attempt looking for associated annotations recursively up to the root
  // class if and only if:
  // * The annotation class was marked with @Inherited.
  //
  // Inherited annotations are not coalesced into a single set: the first declaration found is
  // returned.

  if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
    Class<?> superClass = getSuperclass();  // Returns null if klass's base is Object.

    if (superClass != null) {
      return superClass.getAnnotationsByType(annotationClass);
    }
  }

  // Annotated was not marked with @Inherited, or no superclass.
  return (A[]) Array.newInstance(annotationClass, 0);  // Safe by construction.
}
 
Example 15
Source Project: AndroidComponentPlugin   Source File: Class.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @throws NullPointerException {@inheritDoc}
 * @since 1.8
 */
@Override
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
  // Find any associated annotations [directly or repeatably (indirectly) present on this].
  A[] annotations = GenericDeclaration.super.getAnnotationsByType(annotationClass);

  if (annotations.length != 0) {
    return annotations;
  }

  // Nothing was found, attempt looking for associated annotations recursively up to the root
  // class if and only if:
  // * The annotation class was marked with @Inherited.
  //
  // Inherited annotations are not coalesced into a single set: the first declaration found is
  // returned.

  if (annotationClass.isDeclaredAnnotationPresent(Inherited.class)) {
    Class<?> superClass = getSuperclass();  // Returns null if klass's base is Object.

    if (superClass != null) {
      return superClass.getAnnotationsByType(annotationClass);
    }
  }

  // Annotated was not marked with @Inherited, or no superclass.
  return (A[]) Array.newInstance(annotationClass, 0);  // Safe by construction.
}
 
Example 16
Source Project: TencentKona-8   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 17
Source Project: java-technology-stack   Source File: AnnotationTypeFilter.java    License: MIT License 5 votes vote down vote up
/**
 * Create a new {@link AnnotationTypeFilter} for the given annotation type.
 * @param annotationType the annotation type to match
 * @param considerMetaAnnotations whether to also match on meta-annotations
 * @param considerInterfaces whether to also match interfaces
 */
public AnnotationTypeFilter(
		Class<? extends Annotation> annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) {

	super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces);
	this.annotationType = annotationType;
	this.considerMetaAnnotations = considerMetaAnnotations;
}
 
Example 18
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();
}
 
Example 19
Source Project: jdk8u60   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 20
Source Project: openjdk-jdk8u   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 21
@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);
}
 
Example 22
Source Project: netbeans   Source File: AnnotationObjectProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<BindingQualifier> createInitialObjects() throws InterruptedException {
    final List<BindingQualifier> result = new LinkedList<BindingQualifier>();
    final Set<TypeElement> set = new HashSet<TypeElement>(); 
    getHelper().getAnnotationScanner().findAnnotations(getAnnotationName(), 
            AnnotationScanner.TYPE_KINDS, 
            new AnnotationHandler() {
                @Override
                public void handleAnnotation(TypeElement type, 
                        Element element, AnnotationMirror annotation) 
                {
                    if ( !set.contains( type )){
                        result.add( new BindingQualifier( getHelper(), type , 
                            getAnnotationName()));
                    }
                    set.add( type );
                    if ( !getHelper().hasAnnotation( annotation.
                            getAnnotationType().asElement().
                            getAnnotationMirrors(), 
                            Inherited.class.getCanonicalName()))
                    {
                        /*
                         *  if annotation is inherited then method 
                         *  findAnnotations()
                         *  method will return types with this annotation.
                         *  Otherwise there could be implementors which 
                         *  specialize this type.
                         */
                        collectSpecializedImplementors( type , set, result );
                    }
                }

    } );
    return new ArrayList<BindingQualifier>( result );
}
 
Example 23
Source Project: openjdk-jdk8u-backup   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 24
Source Project: openjdk-jdk9   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 25
private static boolean isInheritedAnnotationType(ClassStructure classStructure) {
    if (!classStructure.getAccess().isAnnotation()) {
        return false;
    }
    for (final ClassStructure annotationTypeClassStructure : classStructure.getAnnotationTypeClassStructures()) {
        if (Inherited.class.getName().equals(annotationTypeClassStructure.getJavaClassName())) {
            return true;
        }
    }
    return false;
}
 
Example 26
Source Project: hottub   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 27
Source Project: openjdk-8-source   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 28
Source Project: openjdk-8   Source File: Symbol.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example 29
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();
}
 
Example 30
@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);
}