java.lang.reflect.Method.getAnnotation()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(219)

本文整理了Java中java.lang.reflect.Method.getAnnotation()方法的一些代码示例,展示了Method.getAnnotation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Method.getAnnotation()方法的具体详情如下:
包路径:java.lang.reflect.Method
类名称:Method
方法名:getAnnotation

Method.getAnnotation介绍

暂无

代码示例

代码示例来源:origin: google/guava

/**
 * Checks whether {@code method} is thread-safe, as indicated by the presence of the {@link
 * AllowConcurrentEvents} annotation.
 */
private static boolean isDeclaredThreadSafe(Method method) {
 return method.getAnnotation(AllowConcurrentEvents.class) != null;
}

代码示例来源:origin: spring-projects/spring-framework

protected <A extends Annotation> CacheMethodDetails<A> create(Class<A> annotationType,
    Class<?> targetType, String methodName,
    Class<?>... parameterTypes) {
  Method method = ReflectionUtils.findMethod(targetType, methodName, parameterTypes);
  Assert.notNull(method, "requested method '" + methodName + "'does not exist");
  A cacheAnnotation = method.getAnnotation(annotationType);
  return new DefaultCacheMethodDetails<>(method, cacheAnnotation, getCacheName(cacheAnnotation));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void supportsAllDefaultHandlerExceptionResolverExceptionTypes() throws Exception {
  Class<ResponseEntityExceptionHandler> clazz = ResponseEntityExceptionHandler.class;
  Method handleExceptionMethod = clazz.getMethod("handleException", Exception.class, WebRequest.class);
  ExceptionHandler annotation = handleExceptionMethod.getAnnotation(ExceptionHandler.class);
  List<Class<?>> exceptionTypes = Arrays.asList(annotation.value());
  for (Method method : DefaultHandlerExceptionResolver.class.getDeclaredMethods()) {
    Class<?>[] paramTypes = method.getParameterTypes();
    if (method.getName().startsWith("handle") && (paramTypes.length == 4)) {
      String name = paramTypes[0].getSimpleName();
      assertTrue("@ExceptionHandler is missing " + name, exceptionTypes.contains(paramTypes[0]));
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void localConfigWithDefaults() throws Exception {
  Method method = getClass().getMethod("localConfigMethodWithDefaults");
  SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
  MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
  assertDefaults(cfg);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getAnnotationAttributesWithAttributeAliasesWithDifferentValues() throws Exception {
  exception.expect(AnnotationConfigurationException.class);
  exception.expectMessage(containsString("attribute 'value' and its alias 'path'"));
  exception.expectMessage(containsString("values of [{/enigma}] and [{/test}]"));
  Method method = WebController.class.getMethod("handleMappedWithDifferentPathAndValueAttributes");
  WebMapping webMapping = method.getAnnotation(WebMapping.class);
  getAnnotationAttributes(webMapping);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationOnLeaf() throws Exception {
  Method m = Leaf.class.getMethod("annotatedOnLeaf");
  assertNotNull(m.getAnnotation(Order.class));
  assertNotNull(getAnnotation(m, Order.class));
  assertNotNull(findAnnotation(m, Order.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationWithMetaAnnotationOnLeaf() throws Exception {
  Method m = Leaf.class.getMethod("metaAnnotatedOnLeaf");
  assertNull(m.getAnnotation(Order.class));
  assertNotNull(getAnnotation(m, Order.class));
  assertNotNull(findAnnotation(m, Order.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationWithMetaMetaAnnotationOnLeaf() throws Exception {
  Method m = Leaf.class.getMethod("metaMetaAnnotatedOnLeaf");
  assertNull(m.getAnnotation(Component.class));
  assertNull(getAnnotation(m, Component.class));
  assertNotNull(findAnnotation(m, Component.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationWithMetaAnnotationOnRoot() throws Exception {
  Method m = Leaf.class.getMethod("metaAnnotatedOnRoot");
  assertNull(m.getAnnotation(Order.class));
  assertNotNull(getAnnotation(m, Order.class));
  assertNotNull(findAnnotation(m, Order.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationOnBridgedMethod() throws Exception {
  Method bridgedMethod = SimpleFoo.class.getMethod("something", String.class);
  assertFalse(bridgedMethod.isBridge());
  assertNull(bridgedMethod.getAnnotation(Order.class));
  assertNull(getAnnotation(bridgedMethod, Order.class));
  assertNotNull(findAnnotation(bridgedMethod, Order.class));
  assertNotNull(bridgedMethod.getAnnotation(Transactional.class));
  assertNotNull(getAnnotation(bridgedMethod, Transactional.class));
  assertNotNull(findAnnotation(bridgedMethod, Transactional.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void globalConfigWithDefaults() throws Exception {
  Method method = GlobalConfigWithDefaultsClass.class.getMethod("globalConfigMethod");
  SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
  MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, GlobalConfigWithDefaultsClass.class);
  assertDefaults(cfg);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void localConfigWithContinueOnError() throws Exception {
  Method method = getClass().getMethod("localConfigMethodWithContinueOnError");
  SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
  MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
  assertNotNull(cfg);
  assertEquals("errorMode", CONTINUE_ON_ERROR, cfg.getErrorMode());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationWithAnnotationOnMethodInInterface() throws Exception {
  Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot");
  // @Order is not @Inherited
  assertNull(m.getAnnotation(Order.class));
  // getAnnotation() does not search on interfaces
  assertNull(getAnnotation(m, Order.class));
  // findAnnotation() does search on interfaces
  assertNotNull(findAnnotation(m, Order.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationOnRoot() throws Exception {
  Method m = Leaf.class.getMethod("annotatedOnRoot");
  assertNotNull(m.getAnnotation(Order.class));
  assertNotNull(getAnnotation(m, Order.class));
  assertNotNull(findAnnotation(m, Order.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void findMethodAnnotationOnRootButOverridden() throws Exception {
  Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation");
  assertNull(m.getAnnotation(Order.class));
  assertNull(getAnnotation(m, Order.class));
  assertNotNull(findAnnotation(m, Order.class));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void localConfigWithIgnoreFailedDrops() throws Exception {
  Method method = getClass().getMethod("localConfigMethodWithIgnoreFailedDrops");
  SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
  MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
  assertNotNull(cfg);
  assertEquals("errorMode", IGNORE_FAILED_DROPS, cfg.getErrorMode());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void toStringForSynthesizedAnnotations() throws Exception {
  Method methodWithPath = WebController.class.getMethod("handleMappedWithPathAttribute");
  WebMapping webMappingWithAliases = methodWithPath.getAnnotation(WebMapping.class);
  assertNotNull(webMappingWithAliases);
  Method methodWithPathAndValue = WebController.class.getMethod("handleMappedWithSamePathAndValueAttributes");
  WebMapping webMappingWithPathAndValue = methodWithPathAndValue.getAnnotation(WebMapping.class);
  assertNotNull(webMappingWithPathAndValue);
  WebMapping synthesizedWebMapping1 = synthesizeAnnotation(webMappingWithAliases);
  assertNotNull(synthesizedWebMapping1);
  WebMapping synthesizedWebMapping2 = synthesizeAnnotation(webMappingWithAliases);
  assertNotNull(synthesizedWebMapping2);
  assertThat(webMappingWithAliases.toString(), is(not(synthesizedWebMapping1.toString())));
  assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping1);
  assertToStringForWebMappingWithPathAndValue(synthesizedWebMapping2);
}

代码示例来源:origin: spring-projects/spring-framework

protected DefaultCacheInvocationContext<?> createDummyContext() throws Exception {
  Method method = Sample.class.getMethod("get", String.class);
  CacheResult cacheAnnotation = method.getAnnotation(CacheResult.class);
  CacheMethodDetails<CacheResult> methodDetails =
      new DefaultCacheMethodDetails<>(method, cacheAnnotation, "test");
  CacheResultOperation operation = new CacheResultOperation(methodDetails,
      defaultCacheResolver, defaultKeyGenerator, defaultExceptionCacheResolver);
  return new DefaultCacheInvocationContext<>(operation, new Sample(), new Object[] {"id"});
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void synthesizeAlreadySynthesizedAnnotation() throws Exception {
  Method method = WebController.class.getMethod("handleMappedWithValueAttribute");
  WebMapping webMapping = method.getAnnotation(WebMapping.class);
  assertNotNull(webMapping);
  WebMapping synthesizedWebMapping = synthesizeAnnotation(webMapping);
  assertNotSame(webMapping, synthesizedWebMapping);
  WebMapping synthesizedAgainWebMapping = synthesizeAnnotation(synthesizedWebMapping);
  assertThat(synthesizedAgainWebMapping, instanceOf(SynthesizedAnnotation.class));
  assertSame(synthesizedWebMapping, synthesizedAgainWebMapping);
  assertEquals("name attribute: ", "foo", synthesizedAgainWebMapping.name());
  assertArrayEquals("aliased path attribute: ", asArray("/test"), synthesizedAgainWebMapping.path());
  assertArrayEquals("actual value attribute: ", asArray("/test"), synthesizedAgainWebMapping.value());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void localConfigWithCustomValues() throws Exception {
  Method method = getClass().getMethod("localConfigMethodWithCustomValues");
  SqlConfig localSqlConfig = method.getAnnotation(Sql.class).config();
  MergedSqlConfig cfg = new MergedSqlConfig(localSqlConfig, getClass());
  assertNotNull(cfg);
  assertEquals("dataSource", "ds", cfg.getDataSource());
  assertEquals("transactionManager", "txMgr", cfg.getTransactionManager());
  assertEquals("transactionMode", ISOLATED, cfg.getTransactionMode());
  assertEquals("encoding", "enigma", cfg.getEncoding());
  assertEquals("separator", "\n", cfg.getSeparator());
  assertEquals("commentPrefix", "`", cfg.getCommentPrefix());
  assertEquals("blockCommentStartDelimiter", "<<", cfg.getBlockCommentStartDelimiter());
  assertEquals("blockCommentEndDelimiter", ">>", cfg.getBlockCommentEndDelimiter());
  assertEquals("errorMode", IGNORE_FAILED_DROPS, cfg.getErrorMode());
}

相关文章

微信公众号

最新文章

更多