Example usage for java.lang.reflect Method getAnnotation

List of usage examples for java.lang.reflect Method getAnnotation

Introduction

In this page you can find the example usage for java.lang.reflect Method getAnnotation.

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:de.zib.gndms.infra.system.PluggableTaskFlowProvider.java

@PreDestroy
public void destroyPlugins() {

    final Map<String, TaskFlowFactory> factories = getFactories();

    for (TaskFlowFactory factory : factories.values()) {
        for (Method method : factory.getClass().getDeclaredMethods()) {
            if (method.getAnnotation(PreDestroy.class) != null) {
                ReflectionUtils.makeAccessible(method);
                try {
                    method.invoke(factory, (Object[]) null);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(
                            "THIS IS NOT HAPPENING!!! Method had been made accessible but is not accessible anyway",
                            e);/* ww w .j  av  a 2s. c om*/
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(
                            "Could not call PreDestroy method (" + method.toGenericString() + ")", e);
                }
            }
        }
    }
}

From source file:koper.MessageListenerBeanPostProcessor.java

/**
 * ??//from   www  . java  2 s.  c o m
 *
 * @param listener
 */
protected void registerMsgBeanListener(MsgBeanListener listener) {

    Class<?> clazz = listener.getClass();
    Method method;
    String topic = null;
    Listen listen;
    method = getMethod(clazz, "onMsgBean");
    listen = method == null ? null : method.getAnnotation(Listen.class);

    if (listen == null) {
        //  Listen , ?Listen
        final Listen clazzAnnotation = ReflectUtil.getListenAnnotation(clazz);
        if (clazzAnnotation == null) {
            method = getMethod(clazz, "onMessage");
            listen = method == null ? null : method.getAnnotation(Listen.class);
        } else {
            listen = clazzAnnotation;
        }
    }

    if (listen == null) {
        log.info(
                "Tip>>> Topic not specified! @Listen annotation not declared on event method 'onMessage or onMsgBean' of bean "
                        + listener);
    } else {
        topic = listen.topic();
    }

    registerListener(listener, topic);
}

From source file:net.firejack.platform.core.validation.LessThanProcessor.java

@Override
public List<ValidationMessage> validate(Method readMethod, String property, Object value, ValidationMode mode)
        throws RuleValidationException {
    LessThan greaterThanAnnotation = readMethod.getAnnotation(LessThan.class);
    List<ValidationMessage> messages = null;
    if (greaterThanAnnotation != null) {
        messages = new ArrayList<ValidationMessage>();
        Class<?> returnType = readMethod.getReturnType();
        String parameterName = StringUtils.isBlank(greaterThanAnnotation.parameterName()) ? property
                : greaterThanAnnotation.parameterName();
        if (value != null) {
            if (returnType.getSuperclass() == Number.class || returnType.isPrimitive()) {
                boolean checkEquality = greaterThanAnnotation.checkEquality();
                Number val = null;
                if (returnType == Float.class || returnType == float.class) {
                    Float f = (Float) value;
                    if (checkEquality && f > greaterThanAnnotation.floatVal()
                            || !checkEquality && f >= greaterThanAnnotation.floatVal()) {
                        val = greaterThanAnnotation.floatVal();
                    }/*from www  . j a  va 2  s.c  o m*/
                } else if (returnType == Double.class || returnType == double.class) {
                    Double d = (Double) value;
                    if (checkEquality && d > greaterThanAnnotation.doubleVal()
                            || !checkEquality && d >= greaterThanAnnotation.doubleVal()) {
                        val = greaterThanAnnotation.doubleVal();
                    }
                } else {
                    Long longValue = ((Number) value).longValue();
                    Long rangeValue = ((Integer) greaterThanAnnotation.intVal()).longValue();
                    if (checkEquality && longValue > rangeValue || !checkEquality && longValue >= rangeValue) {
                        val = greaterThanAnnotation.intVal();
                    }
                }
                if (val != null) {
                    messages.add(new ValidationMessage(property,
                            checkEquality ? greaterThanAnnotation.equalityMsgKey()
                                    : greaterThanAnnotation.msgKey(),
                            parameterName, val));
                }
            }
        }
    }
    return messages;
}

From source file:nl.edia.xapi.proxy.StatementMethodInterceptor.java

/**
 * Runs the {@link Statement} enhancement.
 * @param invocation the invocation of the method call.
 * @param returnValue the return value of the method call.
 * @throws IllegalAccessException// w  w  w  .j a v a  2  s.  co m
 * @throws InvocationTargetException
 */
protected void doAnalysis(MethodInvocation invocation, Object returnValue)
        throws IllegalAccessException, InvocationTargetException {
    // Get the called method
    Method method = invocation.getMethod();

    Statement statementAnnotation = method.getAnnotation(Statement.class);
    if (statementAnnotation != null) {
        processStatement(invocation, returnValue, statementAnnotation);
    }
}

From source file:com.sf.ddao.crud.param.CRUDBeanPropsParameter.java

private synchronized void init(Context ctx) {
    if (descriptors != null) {
        return;/*w  ww .  j  a  va2  s . co m*/
    }
    Class<?> beanClass = CRUDParameterService.getCRUDDaoBean(ctx, argNum);
    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    List<PropertyDescriptor> res = new ArrayList<PropertyDescriptor>(descriptors.length);
    for (PropertyDescriptor descriptor : descriptors) {
        if (CRUDDao.IGNORED_PROPS.contains(descriptor.getName())) {
            continue;
        }
        final Method readMethod = descriptor.getReadMethod();
        if (readMethod == null) {
            continue;
        }
        if (readMethod.getAnnotation(CRUDIgnore.class) != null) {
            continue;
        }
        res.add(descriptor);
    }
    this.descriptors = res;
}

From source file:moe.encode.airblock.commands.core.components.ComponentBag.java

/**
 * Invokes the function in the way described by the function that has been annotated.
 * @param method       The method that should be called.
 * @param instance     The instance that should be implemented.
 * @param parameters   The parameters/*w  w  w .  ja  v a 2  s.  c om*/
 * @return The result of the method or the callable.
 * @throws Throwable If the method throws an exception.
 */
<T> T invoke(Method method, Environment environment, Object instance, Object... parameters) throws Throwable {
    Component component = method.getAnnotation(Component.class);
    Component.ExecutionThread syncstate = Component.ExecutionThread.SAME_THREAD;
    if (component != null) {
        syncstate = (component != null) ? component.thread() : Component.ExecutionThread.SAME_THREAD;
    }
    return syncstate.invoke(environment, method, instance, parameters);
}

From source file:com.clican.pluto.cms.core.service.impl.DataModelServiceImpl.java

public void convertToDataModel(Map<String, Object> map, IDataModel obj) {
    Method[] methods = obj.getClass().getMethods();
    for (Method method : methods) {
        if (method.isAnnotationPresent(DynamicProperty.class)) {
            DynamicProperty dp = method.getAnnotation(DynamicProperty.class);
            String name = dp.name();
            String firstCharLowerName = name.substring(0, 1).toLowerCase() + name.substring(1);
            Object value = map.get(firstCharLowerName);
            try {
                com.clican.pluto.common.util.BeanUtils.setProperty(obj, firstCharLowerName, value);
            } catch (Exception e) {
                log.error("", e);
            }/* w w w. j  a  va 2  s.c  om*/
        }
    }
}

From source file:com.example.jumpnote.web.jsonrpc.server.JsonRpcServlet.java

public JsonRpcServlet() {
    for (Method method : getClass().getMethods()) {
        JsonRpcMethod rpcMethodAnnotation = method.getAnnotation(JsonRpcMethod.class);
        if (rpcMethodAnnotation != null) {
            mMethods.put(rpcMethodAnnotation.method(), method);
        }//from ww w .  j a  va  2 s  .co  m
    }
}

From source file:net.mindengine.oculus.frontend.web.controllers.api.ApiController.java

public ApiController() {
    Collection<Method> allMethods = ClassUtils.getAllMethods(getClass());
    for (Method method : allMethods) {

        Path path = method.getAnnotation(Path.class);
        if (path != null) {
            ApiMethod apiMethod = new ApiMethod();
            apiMethod.setMethod(method);
            apiMethod.setPattern(Pattern.compile(".*" + path.value()));

            apiMethod.setType(findRequestMethod(method));
            apiMethods.add(apiMethod);/*w  w w .  j a va 2 s .c  o m*/
        }
    }
}

From source file:org.jboss.spring.support.SpringInjectionSupport.java

protected Object inject(Object target) throws Exception {
    log.debug("Invoking Spring injection: " + target.getClass().getName());

    Method[] methods = getAllMethods(target);
    for (Method m : methods) {
        Spring spring = m.getAnnotation(Spring.class);
        if (spring != null) {
            if (isSetterMethod(m)) {
                injectToMethod(target, m, spring);
            } else {
                log.warn("Spring annotation only allowed on setter methods.");
            }//from  w  w  w . j a  va 2s.co m
        }
    }

    Field[] fields = getAllFields(target);
    for (Field f : fields) {
        Spring spring = f.getAnnotation(Spring.class);
        if (spring != null) {
            injectToField(target, f, spring);
        }
    }

    return target;
}