Java Code Examples for java.lang.reflect.Method.getDeclaredAnnotation()

The following are Jave code examples for showing how to use getDeclaredAnnotation() of the java.lang.reflect.Method class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: vertx-zero   File: ReceiptExtractor.java   View Source Code Vote up 6 votes
private Receipt extract(final Method method) {
    // 1. Scan whole Endpoints
    final Class<?> clazz = method.getDeclaringClass();
    final Annotation annotation = method.getDeclaredAnnotation(Address.class);
    final String address = Instance.invoke(annotation, "value");

    // 2. Ensure address incoming.
    Fn.flingUp(!ADDRESS.contains(address), LOGGER,
            AddressWrongException.class,
            this.getClass(), address, clazz, method);

    final Receipt receipt = new Receipt();
    receipt.setMethod(method);
    receipt.setAddress(address);

    // Fix: Instance class for proxy
    final Object proxy = Instance.singleton(clazz);
    receipt.setProxy(proxy);
    return receipt;
}
 
Example 2
Project: microprofile-fault-tolerance   File: PureJ2SERetryVisibilityTest.java   View Source Code Vote up 6 votes
@Test
public void checkRetryVisibilityOnRetryServiceMethodSuppressLevel() throws Exception {
    Retry foundAnnotation;
    Method m = RetryOnClassServiceNoAnnotationOnOveriddenMethod.class.getDeclaredMethod("service");

    foundAnnotation = m.getDeclaredAnnotation(Retry.class);
    Assert.assertNull(foundAnnotation,
            "no Retry annotation should be found on RetryOnClassServiceNoAnnotationOnOveriddenMethod#service() via getDeclaredAnnotation()");

    foundAnnotation = m.getAnnotation(Retry.class);
    Assert.assertNull(foundAnnotation,
            "no Retry annotation should be found on RetryOnClassServiceNoAnnotationOnOveriddenMethod#service() via getAnnotation()");

    foundAnnotation = RetryOnClassServiceNoAnnotationOnOveriddenMethod.class.getDeclaredAnnotation(Retry.class);
    Assert.assertNull(foundAnnotation,
            "no Retry annotation should be found on RetryOnClassServiceNoAnnotationOnOveriddenMethod class via getDeclaredAnnotation()");

    foundAnnotation = RetryOnClassServiceNoAnnotationOnOveriddenMethod.class.getAnnotation(Retry.class);
    Assert.assertNotNull(foundAnnotation,
            "a Retry annotation should have been found because of inheritance on RetryOnClassServiceNoAnnotationOnOveriddenMethod " +
                    "class via getAnnotation()");
}
 
Example 3
Project: eclectic-orc   File: GenInfo.java   View Source Code Vote up 5 votes
default <A extends Annotation> A getAnnotation(Class<? extends A> clz) {
    Method theMethod = null;
    if (Enum.class.isAssignableFrom(getColumnClassType())) {
        // For enums, we need to get the annotation from the enum annotated method.
        theMethod = GeneratorUtil.getAnnotatedMethodInEnum((Class<? extends Enum<?>>) getColumnClassType()) //
                .orElse(null);
    } else {
        theMethod = getLastAccessorMethod();
    }
    return theMethod == null ? null : theMethod.isAnnotationPresent(clz) ? theMethod.getDeclaredAnnotation(clz) : null;
}
 
Example 4
Project: RestyPass   File: RestyCommandContext.java   View Source Code Vote up 5 votes
/**
 * 为service创建Resty配置
 *
 * @param serviceClz the service clz
 */
public void initContextForService(Class serviceClz) {
    RestyService restyService = (RestyService) serviceClz.getDeclaredAnnotation(RestyService.class);
    if (restyService == null) {
        return;
    }
    String serviceName = restyService.serviceName();

    // class->@RestyService
    this.storeRestyService(serviceClz, restyService);
    AsyncHttpClientConfig httpClientConfig = AsyncHttpConfigFactory.createConfig(restyService.connectTimeout(), restyService.requestTimeout());
    HttpClientWrapper clientHolder = new HttpClientWrapper(httpClientConfig);
    httpClientPool.putIfAbsent(serviceName, clientHolder);


    SpringAnnotationWrapper wrapper = new SpringAnnotationWrapper();

    RestyCommandConfig commandProperties = new RestyCommandConfig.DefaultRestyCommandConfig();
    processRestyServiceAnnotation(restyService, commandProperties);


    for (Method method : serviceClz.getMethods()) {
        //存储 httpMethod 和 restyCommandConfig
        RestyMethod restyMethod = method.getDeclaredAnnotation(RestyMethod.class);
        if (restyMethod != null) {
            storeRestyMethod(method, restyMethod);
            processRestyMethodAnnotation(restyMethod, commandProperties);
        }
        commandPropertiesMap.putIfAbsent(method, commandProperties);

        // 存储 httpMethod 和 requestTemplate
        RestyRequestTemplate restyRequestTemplate = wrapper.processAnnotation(serviceClz, method);
        requestTemplateMap.putIfAbsent(method, restyRequestTemplate);

        serviceMethodTable.put(serviceName, restyRequestTemplate.getPath(), method);
    }
    log.info("RestyCommandContext初始化成功!");
}
 
Example 5
Project: drinkwater-java   File: RestHelper.java   View Source Code Vote up 5 votes
public static HttpMethod httpMethodFor(Method method) {

        HttpMethod defaultHttpMethod = HttpMethod.GET;

        drinkwater.rest.HttpMethod methodAsAnnotation = method.getDeclaredAnnotation(drinkwater.rest.HttpMethod.class);

        if (methodAsAnnotation != null) {
            return mapToUnirestHttpMethod(methodAsAnnotation);
        }

        return List.ofAll(prefixesMap.entrySet())
                .filter(prefix -> startsWithOneOf(method.getName(), prefix.getValue()))
                .map(entryset -> entryset.getKey())
                .getOrElse(defaultHttpMethod);
    }
 
Example 6
Project: zeratul   File: AnnotationUtils.java   View Source Code Vote up 5 votes
/**
 * 判断测试方法上是否有@Ignore注解
 * @param method 测试方法
 * @return       是否有
 */
public static boolean isIgnoreAnnotation(Method method) {
    Ignore ignore = method.getDeclaredAnnotation(Ignore.class);
    if (ignore == null) {
        return false;
    }
    return true;
}
 
Example 7
Project: spring-boot-gae   File: MethodSearchFieldMetadata.java   View Source Code Vote up 5 votes
/**
 * Create a new instance.
 *
 * @param entityType        Entity class.
 * @param method            For this method.
 * @param indexTypeRegistry Determine the index type with this lookup.
 */
public MethodSearchFieldMetadata(Class<?> entityType, Method method, IndexTypeRegistry indexTypeRegistry) {
    this.entityType = entityType;
    this.method = method;
    this.indexName = NAME_CALCULATOR.apply(method);
    this.encodedName = NAME_ENCODER.apply(indexName);

    SearchIndex annotation = method.getDeclaredAnnotation(SearchIndex.class);
    if (annotation == null || annotation.type() == IndexType.AUTO) {
        this.indexType = indexTypeRegistry.apply(method.getGenericReturnType());
    } else {
        this.indexType = annotation.type();
    }
}
 
Example 8
Project: wolf   File: DefaultMenuScanStrategy.java   View Source Code Vote up 5 votes
@Override
public Menu process(Object target) throws ScanException {
    Method method = (Method) target;
    com.carl.wolf.core.annotation.Menu menu = method.getDeclaredAnnotation(com.carl.wolf.core.annotation.Menu.class);
    Menu menuVo = new Menu();
    menuVo.setIcon(menu.icon())
            .setOrder(menu.order())
            .setPath(menu.path())
            .setTitle(menu.title())
            .setTarget(target)
            .setPros(propertiesResolve(menu.pros()));
    return menuVo;
}
 
Example 9
Project: NekoooGuilds   File: Commands.java   View Source Code Vote up 5 votes
public void registerCommandClass(Class clazz) {
    for (Method method : clazz.getDeclaredMethods()) {
        method.setAccessible(true);

        Annotation annotation = method.getDeclaredAnnotation(CommandInfo.class);
        if (annotation != null) {
            this.registerCommandMethod(method, null, (CommandInfo) annotation);
        }
    }
}
 
Example 10
Project: NekoooGuilds   File: Commands.java   View Source Code Vote up 5 votes
public void registerCommandObject(Object object) {
    for (Method method : object.getClass().getDeclaredMethods()) {
        method.setAccessible(true);

        Annotation annotation = method.getDeclaredAnnotation(CommandInfo.class);
        if (annotation != null) {
            this.registerCommandMethod(method, object, (CommandInfo) annotation);
        }
    }
}
 
Example 11
Project: microprofile-fault-tolerance   File: PureJ2SERetryVisibilityTest.java   View Source Code Vote up 5 votes
@Test
public void check_BASE_ROM_RETRY_MISSING_ON_METHOD() throws Exception {
    Retry foundAnnotation;
    Method m = RetryOnMethodServiceNoAnnotationOnOverridedMethod.class.getDeclaredMethod("service");
    
    foundAnnotation = m.getDeclaredAnnotation(Retry.class);
    Assert.assertNull(foundAnnotation,
            "no Retry annotation should be found on RetryOnMethodServiceNoAnnotationOnOverridedMethod#service() " +
                    "via getDeclaredAnnotation()");

    foundAnnotation = m.getAnnotation(Retry.class);
    Assert.assertNull(foundAnnotation,
            "no Retry annotation should be found on RetryOnMethodServiceNoAnnotationOnOverridedMethod#service() via getAnnotation()");
}
 
Example 12
Project: sshd-shell-spring-boot   File: SshdShellAutoConfiguration.java   View Source Code Vote up 5 votes
private void loadMethodLevelCommandSupplier(Class<?> clazz, Map<String, CommandExecutableDetails> map, Object obj)
        throws NoSuchMethodException, SecurityException, InterruptedException {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.isAnnotationPresent(SshdShellCommand.class)) {
            log.debug("{}.#{} is marked with annotation {}", clazz.getName(), method.getName(),
                    SshdShellCommand.class.getName());
            SshdShellCommand command = method.getDeclaredAnnotation(SshdShellCommand.class);
            map.put(command.value(), getMethodSupplier(command, method, obj));
        }
    }
}
 
Example 13
Project: artdeco   File: DecorationMethodHandler.java   View Source Code Vote up 5 votes
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        Object targetDecoratingObject = null;
        Method targetDecoratingMethod = null;

        for (Object decoratingMethodObject : decoratingMethodsObjects){
            Method[] methods = decoratingMethodObject.getClass().getMethods();
            for (Method candidateDecoratingMethod : methods){
                if (MethodUtils.equals(candidateDecoratingMethod,method)
                        && candidateDecoratingMethod.getDeclaredAnnotations()!=null
                        && candidateDecoratingMethod.getDeclaredAnnotation(Decorates.class)!=null){
                    targetDecoratingMethod  = candidateDecoratingMethod;
                    targetDecoratingObject = decoratingMethodObject;
                }
            }
        }

        if (targetDecoratingMethod != null && targetDecoratingObject !=null){
            boolean assesible = targetDecoratingMethod.isAccessible();
            targetDecoratingMethod.setAccessible(true);
            Object value = targetDecoratingMethod.invoke(targetDecoratingObject,args);
            targetDecoratingMethod.setAccessible(assesible);
            return value;
        } else {
            return method.invoke(delegateObject,args);
        }
    }
 
Example 14
Project: ETF-Java   File: ReflectionUtils.java   View Source Code Vote up 4 votes
public PropertyManager(Object instance, Field field) {
	this.instance = instance;
	field.setAccessible(true);
	IPropertyAccessor accessor = null;
	boolean isFinal = Modifier.isFinal(field.getModifiers());
	IPropertyMutator mutator = isFinal ? NOPAccessorAndMutator.INSTANCE : null;
	for (Method m : getAllMethods(instance.getClass())) {
		if (mutator != null && accessor != null)
			break;
		
		m.setAccessible(true);
		
		if (m.getName().equals(String.format("get%s", capitalize(field.getName())))) {
			accessor = new MethodAccessorAndMutator(instance, m);
			continue;
		} else if (!isFinal
				&& m.getName().equals(String.format("set%s", capitalize(field.getName())))
				&& m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(field.getType())) {
			mutator = new MethodAccessorAndMutator(instance, m);
			continue;
		} else if (m.getDeclaredAnnotation(GetterMethod.class) != null) {
			if (m.getDeclaredAnnotation(GetterMethod.class).value().equals(field.getName())) {
				accessor = new MethodAccessorAndMutator(instance, m);
				continue;
			}
		} else if (!isFinal && m.getDeclaredAnnotation(SetterMethod.class) != null
				&& m.getParameterCount() == 1) {
			if (m.getDeclaredAnnotation(SetterMethod.class).value().equals(field.getName())) {
				mutator = new MethodAccessorAndMutator(instance, m);
				continue;
			}
		}
	}
	
	if (accessor == null)
		accessor = new FieldAccessorAndMutator(instance, field);
	
	if (mutator == null)
		mutator = new FieldAccessorAndMutator(instance, field);
	
	this.accessor = accessor;
	this.mutator = mutator;
	
	this.name = field.getName();
}
 
Example 15
Project: vertx-zero   File: BaseAim.java   View Source Code Vote up 2 votes
/**
 * Get event bus address.
 *
 * @param event
 * @return
 */
protected String address(final Event event) {
    final Method method = event.getAction();
    final Annotation annotation = method.getDeclaredAnnotation(Address.class);
    return Instance.invoke(annotation, "value");
}