@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) { T[] annotationsByType = getJavaClass().getAnnotationsByType(annotationType); return new LinkedHashSet<>(asList(annotationsByType)); } }
private List<SystemResourceConsideration> getSystemResourceConsiderations(final ConfigurableComponent component) { SystemResourceConsideration[] systemResourceConsiderations = component.getClass().getAnnotationsByType(SystemResourceConsideration.class); if (systemResourceConsiderations == null) { return Collections.emptyList(); } return Arrays.asList(systemResourceConsiderations); }
public static List<SqlWindowFunction> parseFunctionDefinition(Class<? extends WindowFunction> clazz) { WindowFunctionSignature[] signatures = clazz.getAnnotationsByType(WindowFunctionSignature.class); checkArgument(signatures.length > 0, "Class is not annotated with @WindowFunctionSignature: %s", clazz.getName()); return Stream.of(signatures) .map(signature -> parse(clazz, signature)) .collect(toImmutableList()); }
public static <A extends Annotation> A[] getRepeatableAnnotationsArray(Class<?> cls, Class<A> annotationClass) { A[] annotations = cls.getAnnotationsByType(annotationClass); if (annotations == null || annotations.length == 0) { for (Annotation metaAnnotation : cls.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return annotations; } } Class<?> superClass = cls.getSuperclass(); if (superClass != null && !(superClass.equals(Object.class))) { annotations = getRepeatableAnnotationsArray(superClass, annotationClass); } } if (annotations == null || annotations.length == 0) { for (Class<?> anInterface : cls.getInterfaces()) { for (Annotation metaAnnotation : anInterface.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return annotations; } } annotations = getRepeatableAnnotationsArray(anInterface, annotationClass); if (annotations != null) { return annotations; } } } return annotations; }
/** * Returns a List of repeatable annotations by type from a method. * * @param method is the method to find * @param annotationClass is the type of annotation * @param <A> is the type of annotation * @return List of repeatable annotations if it is found */ public static <A extends Annotation> List<A> getRepeatableAnnotations(Method method, Class<A> annotationClass) { A[] annotations = method.getAnnotationsByType(annotationClass); if (annotations == null || annotations.length == 0) { for (Annotation metaAnnotation : method.getAnnotations()) { annotations = metaAnnotation.annotationType().getAnnotationsByType(annotationClass); if (annotations != null && annotations.length > 0) { return Arrays.asList(annotations); } } Method superclassMethod = getOverriddenMethod(method); if (superclassMethod != null) { return getRepeatableAnnotations(superclassMethod, annotationClass); } } if (annotations == null) { return null; } return Arrays.asList(annotations); }
private List<Input> fetchDefaultBindings(Class<?> event, BindsConfiguration config) { List<Input> defaultInputs = Lists.newArrayList(); Collection<Input> boundInputs = config.getBoundInputs(); for (Annotation annotation : event.getAnnotationsByType(DefaultBinding.class)) { DefaultBinding defaultBinding = (DefaultBinding) annotation; Input defaultInput = defaultBinding.type().getInput(defaultBinding.id()); if (!boundInputs.contains(defaultInput)) { defaultInputs.add(defaultInput); } else { logger.warn("Input {} is already registered, can not use it as default for event {}", defaultInput, event); } } return defaultInputs; }
private static long getTimeoutInSeconds(Class<?> clazz) { Category[] categories = clazz.getAnnotationsByType(Category.class); for (Class<?> c : categories[0].value()) { if (c == SmallTests.class || c == MediumTests.class || c == LargeTests.class) { // All tests have a 10minute timeout. return TimeUnit.MINUTES.toSeconds(13); } if (c == IntegrationTests.class) { return TimeUnit.MINUTES.toSeconds(Long.MAX_VALUE); } } throw new IllegalArgumentException( clazz.getName() + " does not have SmallTests/MediumTests/LargeTests in @Category"); }
private static boolean hasConditionalIgnoreAnnotationOnClass(Description aDescription) { return aDescription.getTestClass().getAnnotationsByType(ConditionalRun.class).length > 0; }
objectResolvers, beanFactoryId.type.getAnnotationsByType(RequestConverter.class));
@Test public void getRepeatableAnnotationsDeclaredOnClass() { final List<String> expectedValuesJava = asList("A", "B", "C"); final List<String> expectedValuesSpring = asList("A", "B", "C", "meta1"); // Java 8 MyRepeatable[] array = MyRepeatableClass.class.getAnnotationsByType(MyRepeatable.class); assertNotNull(array); List<String> values = stream(array).map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesJava)); // Spring Set<MyRepeatable> set = getRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class, MyRepeatableContainer.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); // When container type is omitted and therefore inferred from @Repeatable set = getRepeatableAnnotations(MyRepeatableClass.class, MyRepeatable.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); }
@Test public void getRepeatableAnnotationsDeclaredOnSuperclass() { final Class<?> clazz = SubMyRepeatableClass.class; final List<String> expectedValuesJava = asList("A", "B", "C"); final List<String> expectedValuesSpring = asList("A", "B", "C", "meta1"); // Java 8 MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class); assertNotNull(array); List<String> values = stream(array).map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesJava)); // Spring Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); // When container type is omitted and therefore inferred from @Repeatable set = getRepeatableAnnotations(clazz, MyRepeatable.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); }
@Test public void getRepeatableAnnotationsDeclaredOnMultipleSuperclasses() { final Class<?> clazz = SubSubMyRepeatableWithAdditionalLocalDeclarationsClass.class; final List<String> expectedValuesJava = asList("X", "Y", "Z"); final List<String> expectedValuesSpring = asList("X", "Y", "Z", "meta2"); // Java 8 MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class); assertNotNull(array); List<String> values = stream(array).map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesJava)); // Spring Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); // When container type is omitted and therefore inferred from @Repeatable set = getRepeatableAnnotations(clazz, MyRepeatable.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); }
@Test public void getRepeatableAnnotationsDeclaredOnClassAndSuperclass() { final Class<?> clazz = SubMyRepeatableWithAdditionalLocalDeclarationsClass.class; final List<String> expectedValuesJava = asList("X", "Y", "Z"); final List<String> expectedValuesSpring = asList("X", "Y", "Z", "meta2"); // Java 8 MyRepeatable[] array = clazz.getAnnotationsByType(MyRepeatable.class); assertNotNull(array); List<String> values = stream(array).map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesJava)); // Spring Set<MyRepeatable> set = getRepeatableAnnotations(clazz, MyRepeatable.class, MyRepeatableContainer.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); // When container type is omitted and therefore inferred from @Repeatable set = getRepeatableAnnotations(clazz, MyRepeatable.class); assertNotNull(set); values = set.stream().map(MyRepeatable::value).collect(toList()); assertThat(values, is(expectedValuesSpring)); }
final Repeatable[] repeatableAnnotations = annotationType.getAnnotationsByType(Repeatable.class); final Class<? extends Annotation> containerType = repeatableAnnotations.length > 0 ? repeatableAnnotations[0].value() : null;
private Plugin instantiate(List<Plugin> scannedPlugins, Class<Plugin> clazz) throws PluginInstantiationException PluginDependency[] pluginDependencies = clazz.getAnnotationsByType(PluginDependency.class); List<Plugin> deps = new ArrayList<>(); for (PluginDependency pluginDependency : pluginDependencies)
/** * Writes all the system resource considerations for this component * * @param configurableComponent the component to describe * @param xmlStreamWriter the xml stream writer to use * @throws XMLStreamException thrown if there was a problem writing the XML */ private void writeSystemResourceConsiderationInfo(ConfigurableComponent configurableComponent, XMLStreamWriter xmlStreamWriter) throws XMLStreamException { SystemResourceConsideration[] systemResourceConsiderations = configurableComponent.getClass().getAnnotationsByType(SystemResourceConsideration.class); writeSimpleElement(xmlStreamWriter, "h3", "System Resource Considerations:"); if (systemResourceConsiderations.length > 0) { xmlStreamWriter.writeStartElement("table"); xmlStreamWriter.writeAttribute("id", "system-resource-considerations"); xmlStreamWriter.writeStartElement("tr"); writeSimpleElement(xmlStreamWriter, "th", "Resource"); writeSimpleElement(xmlStreamWriter, "th", "Description"); xmlStreamWriter.writeEndElement(); for (SystemResourceConsideration systemResourceConsideration : systemResourceConsiderations) { xmlStreamWriter.writeStartElement("tr"); writeSimpleElement(xmlStreamWriter, "td", systemResourceConsideration.resource().name()); writeSimpleElement(xmlStreamWriter, "td", systemResourceConsideration.description().trim().isEmpty() ? "Not Specified" : systemResourceConsideration.description()); xmlStreamWriter.writeEndElement(); } xmlStreamWriter.writeEndElement(); } else { xmlStreamWriter.writeCharacters("None specified."); } }
PluginDependency[] pluginDependencies = pluginClazz.getAnnotationsByType(PluginDependency.class);
@Override public void testStarted(Description description) throws Exception { Category[] categories = description.getTestClass().getAnnotationsByType(Category.class); for (Class<?> c : categories[0].value()) { if (c == IntegrationTests.class) { return; } } for (Field field : description.getTestClass().getFields()) { if (Modifier.isStatic(field.getModifiers()) && field.getType() == HBaseClassTestRule.class && field.isAnnotationPresent(ClassRule.class)) { HBaseClassTestRule timeout = (HBaseClassTestRule) field.get(null); assertEquals( "The HBaseClassTestRule ClassRule in " + description.getTestClass().getName() + " is for " + timeout.getClazz().getName(), description.getTestClass(), timeout.getClazz()); return; } } fail("No HBaseClassTestRule ClassRule for " + description.getTestClass().getName()); } }
private static RunCondition getIgnoreConditionOnClass(Description aDescription) { ConditionalRun[] annotations = aDescription.getTestClass().getAnnotationsByType(ConditionalRun.class); return new IgnoreConditionCreator(aDescription.getTestClass(), annotations).create(); }
private static List<Graph.OptOut> getAllOptOuts(final Class<?> clazz) { // we typically get a null class if this is called recursively and the original clazz was an interface if (clazz == Object.class || null == clazz) return Collections.emptyList(); return Stream.concat(getAllOptOuts(clazz.getSuperclass()).stream(), Stream.of(clazz.getAnnotationsByType(Graph.OptOut.class))).distinct().collect(Collectors.toList()); }