java.lang.annotation.Inherited Java Examples
The following examples show how to use
java.lang.annotation.Inherited.
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 File: JavaReflectionUtil.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
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 #2
Source File: AnnotationObjectProvider.java From netbeans with Apache License 2.0 | 6 votes |
@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 #3
Source File: JavacElements.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * 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 #4
Source File: JavaReflectionUtil.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
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 #5
Source File: JavacElements.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * 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 #6
Source File: JavaReflectionUtil.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
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 File: JavaReflectionUtil.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
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 #8
Source File: Class.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
/** * @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 #9
Source File: AnnotationsAnalyzer.java From deadcode4j with Apache License 2.0 | 6 votes |
@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 #10
Source File: Class.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
/** * {@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 #11
Source File: Class.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
/** * {@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 #12
Source File: Class.java From AndroidComponentPlugin with Apache License 2.0 | 6 votes |
/** * @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 #13
Source File: AnnotationTypeFilter.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 File: Symbol.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@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 #15
Source File: Symbol.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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 #16
Source File: FamilyClassStructure.java From jvm-sandbox with GNU Lesser General Public License v3.0 | 5 votes |
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 #17
Source File: Symbol.java From hottub with GNU General Public License v2.0 | 5 votes |
@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 #18
Source File: Symbol.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@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 #19
Source File: Symbol.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@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 File: AnnotationUtil.java From jesterj with Apache License 2.0 | 5 votes |
/** * 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(); } } }
Example #21
Source File: AsmBackedClassGenerator.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
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 #22
Source File: AutoValueOrOneOfProcessor.java From auto with Apache License 2.0 | 5 votes |
private static Set<String> getAnnotationsMarkedWithInherited(Element element) { return element .getAnnotationMirrors() .stream() .filter(a -> isAnnotationPresent(a.getAnnotationType().asElement(), Inherited.class)) .map(a -> getAnnotationFqName(a)) .collect(toSet()); }
Example #23
Source File: ElementImpl.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@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); }
Example #24
Source File: ElementImpl.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
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); }
Example #25
Source File: ReflectedInvokerHelper.java From actframework with Apache License 2.0 | 5 votes |
public static <T extends Annotation> T getAnnotation(Class<T> annotationClass, Method method) { T anno = method.getAnnotation(annotationClass); if (null != anno) { return anno; } if (!annotationClass.isAnnotationPresent(Inherited.class)) { return null; } Method overridenMethod = getOverridenMethod(method); return null == overridenMethod ? null : getAnnotation(annotationClass, overridenMethod); }
Example #26
Source File: Symbol.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@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 File: Class.java From AndroidComponentPlugin with Apache License 2.0 | 5 votes |
/** * @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 #28
Source File: Class.java From AndroidComponentPlugin with Apache License 2.0 | 5 votes |
/** * @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 #29
Source File: Symbol.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@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 #30
Source File: AnnotationTypeFilter.java From java-technology-stack with MIT License | 5 votes |
/** * 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; }