public static void main(String[] args) throws Exception { CreateVm[] createVms = VmManager.class.getMethod("manage").getAnnotationsByType(CreateVm.class); Stream.of(createVms).map(CreateVm::name).forEach(System.out::println); } }
@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) { T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType); return new LinkedHashSet<>(asList(annotationsByType)); } }
public static List<LongVariableConstraint> parseLongVariableConstraints(Method inputFunction) { return Stream.of(inputFunction.getAnnotationsByType(Constraint.class)) .map(annotation -> new LongVariableConstraint(annotation.variable(), annotation.expression())) .collect(toImmutableList()); }
public static Map<String, Class<?>> getDeclaredSpecializedTypeParameters(Method method, Set<TypeParameter> typeParameters) { Map<String, Class<?>> specializedTypeParameters = new HashMap<>(); TypeParameterSpecialization[] typeParameterSpecializations = method.getAnnotationsByType(TypeParameterSpecialization.class); ImmutableSet<String> typeParameterNames = typeParameters.stream() .map(TypeParameter::value) .collect(toImmutableSet()); for (TypeParameterSpecialization specialization : typeParameterSpecializations) { checkArgument(typeParameterNames.contains(specialization.name()), "%s does not match any declared type parameters (%s) [%s]", specialization.name(), typeParameters, method); Class<?> existingSpecialization = specializedTypeParameters.get(specialization.name()); checkArgument(existingSpecialization == null || existingSpecialization.equals(specialization.nativeContainerType()), "%s has conflicting specializations %s and %s [%s]", specialization.name(), existingSpecialization, specialization.nativeContainerType(), method); specializedTypeParameters.put(specialization.name(), specialization.nativeContainerType()); } return specializedTypeParameters; } }
/** * 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); }
checkArgument(nullable || !containsLegacyNullable(method.getAnnotations()), "Method [%s] is annotated with @Nullable but not @SqlNullable", method); typeParameters.addAll(Arrays.asList(method.getAnnotationsByType(TypeParameter.class)));
final Set<Method> methods = getAllMethods(beanFactoryId.type); for (final Method method : methods) { final RequestConverter[] converters = method.getAnnotationsByType(RequestConverter.class); try { final List<AnnotatedValueResolver> resolvers =
typeParameters = Arrays.asList(inputFunction.getAnnotationsByType(TypeParameter.class));
private static boolean hasConditionalIgnoreAnnotationOnMethod(Description aDescription) { if (aDescription.getMethodName() == null) { // if @ClassRule is used return false; } try { // this is possible because test methods must not have any argument Method testMethod = aDescription.getTestClass().getMethod(aDescription.getMethodName()); return testMethod.getAnnotationsByType(ConditionalRun.class).length > 0; } catch (NoSuchMethodException | SecurityException ex) { throw new RuntimeException(ex); } }
private static RunCondition getIgnoreConditionOnMethod(Description aDescription) { try { // this is possible because test methods must not have any argument ConditionalRun[] annotations = aDescription.getTestClass().getMethod(aDescription.getMethodName()).getAnnotationsByType(ConditionalRun.class); return new IgnoreConditionCreator(aDescription.getTestClass(), annotations).create(); } catch (NoSuchMethodException | SecurityException ex) { throw new RuntimeException(ex); } }
private static Set<FeatureRequirement> getFeatureRequirementsForTest(final Method testMethod, final LoadGraphWith[] loadGraphWiths) { // get feature requirements on the test method and add them to the list of ones to check final FeatureRequirement[] featureRequirement = testMethod.getAnnotationsByType(FeatureRequirement.class); final List<FeatureRequirement> frs = new ArrayList<>(Arrays.asList(featureRequirement)); // if the graph is loading data then it will come with it's own requirements if (loadGraphWiths.length > 0) frs.addAll(loadGraphWiths[0].value().featuresRequired()); // if the graph has a set of feature requirements bundled together then add those final FeatureRequirementSet[] featureRequirementSets = testMethod.getAnnotationsByType(FeatureRequirementSet.class); if (featureRequirementSets.length > 0) frs.addAll(Arrays.stream(featureRequirementSets) .flatMap(f -> f.value().featuresRequired().stream()).collect(Collectors.toList())); // process the unique set of feature requirements return new HashSet<>(frs); }
private Method resolveInjectableMethod(Object target, Method method) { Method candidate = null; for (Method serviceImplMethod : getImplementationDeclaredMethods(target)) { if (isPublic(serviceImplMethod.getModifiers()) && serviceImplMethod.getName().equals(method.getName()) && serviceImplMethod.getAnnotationsByType(Inject.class).length > 0 && equivalentParams(method.getParameters(), serviceImplMethod.getParameters())) { if (candidate != null && !(candidate.getName().equals(serviceImplMethod.getName()) && deepEquals(candidate.getParameterTypes(), serviceImplMethod.getParameterTypes()))) { throw new IllegalDependencyInjectionException(format(MANY_CANDIDATES_ERROR_MSG_TEMPLATE, method.getName(), target.toString())); } candidate = serviceImplMethod; } } return candidate; }
private Method resolveInjectableMethod(Method method) { Method candidate = null; for (Method serviceImplMethod : getImplementationDeclaredMethods()) { if (isPublic(serviceImplMethod.getModifiers()) && serviceImplMethod.getName().equals(method.getName()) && serviceImplMethod.getAnnotationsByType(Inject.class).length > 0 && equivalentParams(method.getParameters(), serviceImplMethod.getParameters())) { if (candidate != null && !(candidate.getName().equals(serviceImplMethod.getName()) && deepEquals(candidate.getParameterTypes(), serviceImplMethod.getParameterTypes()))) { throw new IllegalDependencyInjectionException(format(MANY_CANDIDATES_ERROR_MSG_TEMPLATE, method.getName(), getProxiedObject().getName())); } candidate = serviceImplMethod; } } return candidate; }
@Before public void setup() throws Exception { final Method testMethod = this.getClass().getMethod(cleanMethodName(name.getMethodName())); final LoadGraphWith[] loadGraphWiths = testMethod.getAnnotationsByType(LoadGraphWith.class); final LoadGraphWith loadGraphWith = loadGraphWiths.length == 0 ? null : loadGraphWiths[0]; final LoadGraphWith.GraphData loadGraphWithData = null == loadGraphWith ? null : loadGraphWith.value(); final Set<FeatureRequirement> featureRequirementSet = getFeatureRequirementsForTest(testMethod, loadGraphWiths); graphProvider = GraphManager.getGraphProvider(); // pre-check if available from graph provider to avoid graph creation final Optional<Graph.Features> staticFeatures = graphProvider.getStaticFeatures(); if (staticFeatures.isPresent()) { assumeRequirementsAreMetForTest(featureRequirementSet, staticFeatures.get(), true); } graphProvider.getTestListener().ifPresent(l -> l.onTestStart(this.getClass(), name.getMethodName())); config = graphProvider.standardGraphConfiguration(this.getClass(), name.getMethodName(), loadGraphWithData); // this should clear state from a previously unfinished test. since the graph does not yet exist, // persisted graphs will likely just have their directories removed graphProvider.clear(config); graph = graphProvider.openTestGraph(config); g = graphProvider.traversal(graph); // even if we checked static features earlier it's of little cost to recheck again with the real graph // once it is instantiated. the real cost savings is preventing graph creation in the first place so // let's double check that all is legit. assumeRequirementsAreMetForTest(featureRequirementSet, graph.features(), false); beforeLoadGraphWith(graph); // load a graph with sample data if the annotation is present on the test graphProvider.loadGraphData(graph, loadGraphWith, this.getClass(), name.getMethodName()); afterLoadGraphWith(graph); }
Stream[] streams = method.getMethod().getAnnotationsByType(Stream.class); if (streams.length > 0)
@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) { T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType); return new LinkedHashSet<>(asList(annotationsByType)); } }
@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) { T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType); return new LinkedHashSet<>(asList(annotationsByType)); } }
@Override default <T extends Annotation> Set<T> getAnnotations(Class<T> annotationType) { T[] annotationsByType = getJavaMember().getAnnotationsByType(annotationType); return new LinkedHashSet<>(asList(annotationsByType)); } }
@Around(value = "anyPublicServiceMethod() && annotationOnClass(targetDataSource))", argNames = "proceedingJoinPoint,targetDataSource") public Object annotationOnClassInvoke(ProceedingJoinPoint proceedingJoinPoint, TargetDataSource targetDataSource) throws Throwable { MethodSignature ms = (MethodSignature) proceedingJoinPoint.getSignature(); Method method = ms.getMethod(); // 方法是否标注了注解 TargetDataSource if(method.isAnnotationPresent(TargetDataSource.class)){ targetDataSource = method.getAnnotationsByType(TargetDataSource.class)[0]; } return doInvoke(proceedingJoinPoint, targetDataSource); }
private static void checkForInvalidClientHeaderParams(Class<?> userType, Method[] methods) { ClientHeaderParam[] interfaceAnnotations = userType.getAnnotationsByType(ClientHeaderParam.class); checkClientHeaderParamAnnotation(interfaceAnnotations, userType, methods); for (Method method : methods) { ClientHeaderParam[] methodAnnotations = method.getAnnotationsByType(ClientHeaderParam.class); checkClientHeaderParamAnnotation(methodAnnotations, userType, methods); } }