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

The following are Jave code examples for showing how to use getDeclaredAnnotations() 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: weex-3d-map   File: TypeModuleFactory.java   View Source Code Vote up 6 votes
private void generateMethodMap() {
  if(WXEnvironment.isApkDebugable()) {
    WXLogUtils.d(TAG, "extractMethodNames:" + mClazz.getSimpleName());
  }
  ArrayList<String> methods = new ArrayList<>();
  HashMap<String, Invoker> methodMap = new HashMap<>();
  try {
    for (Method method : mClazz.getMethods()) {
      // iterates all the annotations available in the method
      for (Annotation anno : method.getDeclaredAnnotations()) {
        if (anno != null && anno instanceof WXModuleAnno) {
          methods.add(method.getName());
          methodMap.put(method.getName(), new MethodInvoker(method));
          break;
        }
      }
    }
  } catch (Throwable e) {
    WXLogUtils.e("[WXModuleManager] extractMethodNames:", e);
  }
  mMethods = methods;
  mMethodMap = methodMap;
}
 
Example 2
Project: crnk-framework   File: JacksonResourceFieldInformationProvider.java   View Source Code Vote up 6 votes
protected Optional<String> getName(Method method) {
	ObjectMapper objectMapper = context.getObjectMapper();
	SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
	if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
		String name = ClassUtils.getGetterFieldName(method);
		Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
		AnnotationMap annotationMap = buildAnnotationMap(declaredAnnotations);

		int paramsLength = method.getParameterAnnotations().length;
		AnnotationMap[] paramAnnotations = new AnnotationMap[paramsLength];
		for (int i = 0; i < paramsLength; i++) {
			AnnotationMap parameterAnnotationMap = buildAnnotationMap(method.getParameterAnnotations()[i]);
			paramAnnotations[i] = parameterAnnotationMap;
		}

		AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(method.getDeclaringClass(), serializationConfig);
		AnnotatedMethod annotatedField = AnnotatedMethodBuilder.build(annotatedClass, method, annotationMap, paramAnnotations);
		return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForGetterMethod(serializationConfig, annotatedField, name));
	}
	return Optional.empty();
}
 
Example 3
Project: vertx-zero   File: MethodResolver.java   View Source Code Vote up 6 votes
public static HttpMethod resolve(final Method method) {
    // 1. Method checking.
    Fn.flingUp(null == method, LOGGER,
            MethodNullException.class, MethodResolver.class);
    final Annotation[] annotations = method.getDeclaredAnnotations();
    // 2. Method ignore
    HttpMethod result = null;
    for (final Annotation annotation : annotations) {
        final Class<?> key = annotation.annotationType();
        if (METHODS.containsKey(key)) {
            result = METHODS.get(key);
            break;
        }
    }
    // 2. Ignore this method.
    if (null == result) {
        LOGGER.info(Info.METHOD_IGNORE, method.getName());
    }
    return result;
}
 
Example 4
Project: FoodCraft-Reloaded   File: LoaderManager.java   View Source Code Vote up 6 votes
public void addLoader(Class<?> loaderClass) {
    try {
        for (Method method : loaderClass.getMethods())
            for (Annotation annotation : method.getDeclaredAnnotations())
                if (annotation.annotationType().equals(Load.class))
                    if (method.getParameterCount() == 0 || (FMLStateEvent.class.isAssignableFrom(method.getParameterTypes()[0]) && method.getParameterTypes()[0].equals(((Load) annotation).value().getEvent().getClass()))) {
                        Collection<Method> methods = stateLoaderMap.getOrDefault(((Load) annotation).value(), new ArrayList<>());
                        if (!methods.contains(method))
                            methods.add(method);
                        stateLoaderMap.put(((Load) annotation).value(), methods);
                    }
        loaderInstanceMap.put(loaderClass, loaderClass.newInstance());
    } catch (Exception e) {
        FoodCraftReloaded.getLogger().warn("Un-able to register loader " + loaderClass.getName(), e);
    }
}
 
Example 5
Project: Reer   File: PropertyAccessorExtractionContext.java   View Source Code Vote up 5 votes
private Map<Class<? extends Annotation>, Annotation> collectAnnotations(Iterable<Method> methods) {
    Map<Class<? extends Annotation>, Annotation> annotations = Maps.newLinkedHashMap();
    for (Method method : methods) {
        for (Annotation annotation : method.getDeclaredAnnotations()) {
            // Make sure more specific annotation doesn't get overwritten with less specific one
            if (!annotations.containsKey(annotation.annotationType())) {
                annotations.put(annotation.annotationType(), annotation);
            }
        }
    }
    return Collections.unmodifiableMap(annotations);
}
 
Example 6
Project: PetBlocks   File: ReflectionUtils.java   View Source Code Vote up 5 votes
/**
 * Invokes the annotation of the given method
 *
 * @param method          method
 * @param annotationClazz annotation
 * @param <T>             returnType
 * @return returnValue
 */
public static <T extends Annotation> T invokeAnnotationByMethod(Method method, Class<T> annotationClazz) {
    if (method == null)
        throw new IllegalArgumentException("Method cannot be null!");
    if (annotationClazz == null)
        throw new IllegalArgumentException("AnnotationClass cannot be null!");
    for (final Annotation annotation : method.getDeclaredAnnotations()) {
        if (annotation.annotationType() == annotationClazz)
            return (T) annotation;
    }
    return null;
}
 
Example 7
Project: MooProject   File: EventEar.java   View Source Code Vote up 5 votes
public EventEar(Object declaringInstance, Class<?> eventType, Class<?> eventClass, Method method) {
    this.declaringInstance = declaringInstance;
    this.eventType = eventType;
    this.eventClass = eventClass;
    this.method = method;

    for(Annotation a : method.getDeclaredAnnotations()) {
        for(Method m : a.getClass().getMethods()) {
            if(m.getName().equals(PRIORITY_METHOD)) {
                priority = (EventPriority) ReflectionUtil.invokeMethod(m, a);
            }
        }
    }
}
 
Example 8
Project: Core-Java   File: JavaCompilerUtils.java   View Source Code Vote up 5 votes
public JavaCompilerUtils(String name) throws ClassNotFoundException {
    Class clazz = this.getClass().getClassLoader().loadClass(name);

    Method[] methods = clazz.getDeclaredMethods();
    List<String> methodList = new ArrayList<>();
    for (Method method : methods){
        String param = new String();
        for (Class paramClass : method.getParameterTypes()){
            param += paramClass.getName()+", ";
        }
        String temp = "";
        if(param.length()>0){
            temp = method.getName()+" ("+param.substring(0, param.length()-2)+")<br>";
        }else{
            temp = method.getName()+" ()<br>";
        }
        if(method.getDeclaredAnnotations().length>0){
            Annotation[] t = method.getDeclaredAnnotations();
            for (Annotation tt : t){
                if (tt.annotationType().equals(java.lang.Deprecated.class)){
                    temp = "@Deprecated "+temp;
                }
            }
        }
        methodList.add(temp);
    }
    methodList = methodList.stream().sorted().collect(Collectors.toList());
    methodList.forEach(item ->{
        System.out.println(item);
    });

}
 
Example 9
Project: BlockBall   File: ReflectionUtils.java   View Source Code Vote up 5 votes
/**
 * Invokes the annotation of the given method
 *
 * @param method          method
 * @param annotationClazz annotation
 * @param <T>             returnType
 * @return returnValue
 */
public static <T extends Annotation> T invokeAnnotationByMethod(Method method, Class<T> annotationClazz) {
    if (method == null)
        throw new IllegalArgumentException("Method cannot be null!");
    if (annotationClazz == null)
        throw new IllegalArgumentException("AnnotationClass cannot be null!");
    for (final Annotation annotation : method.getDeclaredAnnotations()) {
        if (annotation.annotationType() == annotationClazz)
            return (T) annotation;
    }
    return null;
}
 
Example 10
Project: phonk   File: APIManager.java   View Source Code Vote up 5 votes
public void listAPIs() {
    Iterator it = apis.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry) it.next();

        API p = (API) pairs.getValue();
        Method[] methods = p.methods;

        for (Method m : methods) {
            // get class and method
            MLog.d(TAG, pairs.getKey() + " = " + m.getName());

            Annotation[] annotations = m.getDeclaredAnnotations();

            for (Annotation annotation2 : annotations) {

                MLog.d(TAG, annotation2.toString() + " " + annotation2.annotationType().getSimpleName() + " "
                        + ProtoMethod.class.getSimpleName());

                if (annotation2.annotationType().getSimpleName().equals(ProtoMethod.class.getSimpleName())) {
                    String desc = ((ProtoMethod) annotation2).description();
                    String example = ((ProtoMethod) annotation2).example();
                    MLog.d(TAG, desc);
                }
            }

        }
        it.remove(); // avoids mContext ConcurrentModificationException
    }

}
 
Example 11
Project: Yoghurt   File: SelectPageFilter.java   View Source Code Vote up 5 votes
/**
 * 如果切点方法有使用 @AutoPage 注解,则依照注解说明去取参数
 * */
private void searchPageAnnotation(Object[] args, Integer[] params, Method targetMethod) {
    Annotation[] annotations = targetMethod.getDeclaredAnnotations();
    for (Annotation annotation : annotations){
        if (annotation instanceof AutoPage){
            AutoPage page = (AutoPage) annotation;
            if (page.value()){
                params[0] = (Integer) args[page.pageNum()];
                params[1] = (Integer) args[page.pageSize()];
            }
            return;
        }
    }
}
 
Example 12
Project: Summer   File: ProtocolEventBus.java   View Source Code Vote up 5 votes
private List<Method> getOnEventMethod(Object object) {
    Class clazz = object.getClass();
    if (cacheMethodMap.containsKey(clazz)) {
        return cacheMethodMap.get(clazz);
    }
    Method[] declaredMethods = clazz.getDeclaredMethods();
    if (declaredMethods != null) {
        for (Method method : declaredMethods) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            if (annotations != null) {
                for (Annotation annotation : annotations) {
                    if (annotation instanceof OnEvent) {
                        //cacheMethodMap.put(clazz, method);
                        List<Method> methodList = cacheMethodMap.get(clazz);
                        if (methodList == null) {
                            methodList = new ArrayList<>();
                            List<Method> tmp = cacheMethodMap.putIfAbsent(clazz, methodList);
                            if (tmp != null) {
                                methodList = tmp;
                            }
                        }
                        methodList.add(method);
                    }
                }
            }
        }
    }
    return cacheMethodMap.get(clazz);
}
 
Example 13
Project: AstralEdit   File: ReflectionUtils.java   View Source Code Vote up 5 votes
/**
 * Invokes the annotation of the given method
 *
 * @param method          method
 * @param annotationClazz annotation
 * @param <T>             returnType
 * @return returnValue
 */
public static <T extends Annotation> T invokeAnnotationByMethod(Method method, Class<T> annotationClazz) {
    if (method == null)
        throw new IllegalArgumentException("Method cannot be null!");
    if (annotationClazz == null)
        throw new IllegalArgumentException("AnnotationClass cannot be null!");
    for (final Annotation annotation : method.getDeclaredAnnotations()) {
        if (annotation.annotationType() == annotationClazz)
            return (T) annotation;
    }
    return null;
}
 
Example 14
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 15
Project: phonk   File: APIManager.java   View Source Code Vote up 4 votes
/**
 * add mContext new class to extract the methods
 *
 * @param c
 */
public void addClass(Class c, boolean b) {

    try {
        // api docs
        APIManagerClass apiClass = new APIManagerClass();
        apiClass.name = c.getSimpleName().substring(1).toLowerCase();
        apiClass.isMainObject = b;
        MLog.d(TAG, "" + c.getName());

        // getting all the methods
        Method m[] = c.getDeclaredMethods();
        for (Method element : m) {

            // get method
            APIManagerMethod apiMethod = new APIManagerMethod();
            apiMethod.id = methodCount++;
            apiMethod.parent = apiClass.name;
            apiMethod.name = element.getName();

            // get parameter types
            Class<?>[] param = element.getParameterTypes();
            String[] paramsType = new String[param.length];

            for (int j = 0; j < param.length; j++) {
                String p = param[j].getSimpleName().toString();
                paramsType[j] = p;
            }
            apiMethod.paramsType = paramsType;

            // return type
            apiMethod.returnType = element.getReturnType().getSimpleName().toString();

            // get method information
            if (apiMethod.name.contains("$") == false) {
                Annotation[] annotations = element.getDeclaredAnnotations();
                // check if annotation exists and add apidocs
                for (Annotation annotation2 : annotations) {

                    // description and example
                    if (annotation2.annotationType().getSimpleName().equals(ProtoMethod.class.getSimpleName())) {
                        apiMethod.description = ((ProtoMethod) annotation2).description();
                        apiMethod.example = ((ProtoMethod) annotation2).example();

                    }

                    // get parameters names
                    if (annotation2.annotationType().getSimpleName().equals(ProtoMethodParam.class.getSimpleName())) {
                        apiMethod.parametersName = ((ProtoMethodParam) annotation2).params();
                        MLog.d(TAG, "getting names " + apiMethod.parametersName);
                    }

                }

                apiClass.apiMethods.add(apiMethod);
            }
        }

        doc.apiClasses.add(apiClass);

        // classes and methods
        // apis.put(c.getSimpleName(), new API(c, m));

    } catch (Throwable e) {
        System.err.println(e);
    }

}