Example usage for java.lang.reflect Method isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.zxy.commons.mybatis.SelectDataSourceAspect.java

/**
 * ?key// w ww  .  j  ava  2  s  .c  o  m
 * 
 * @param point JoinPoint
*/
@Before("aspect()")
public void before(JoinPoint point) {
    Object target = point.getTarget();
    String method = point.getSignature().getName();
    //        Class<?>[] classz = target.getClass().getInterfaces();
    Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
    try {
        Method mt = target.getClass().getMethod(method, parameterTypes);
        //            logger.info("mt.getName={}", mt.getName());
        if (mt != null && mt.isAnnotationPresent(SelectDataSource.class)) {
            SelectDataSource data = mt.getAnnotation(SelectDataSource.class);
            HandleDataSource.putDataSource(data.value());
        } else {
            HandleDataSource.clearCustomerType();
        }
        //logger.info("method.name={}, datasource.key={}", m.getName(), HandleDataSource.getDataSource());
    } catch (Exception e) {
        logger.error("before() error.", e);
        HandleDataSource.clearCustomerType();
    }
}

From source file:com.threewks.thundr.search.gae.meta.SearchMetadata.java

private void findMethodAccessors(Class<T> type) {
    for (Method method : ReflectUtil.getSupportedMethods(type)) {
        if (method.isAnnotationPresent(SearchId.class)) {
            String name = determineName(method);
            String encodedName = encodeFieldName(name);
            idAccessor = new MethodAccessor<T, K>(type, method, name, encodedName, IndexType.Identifier);
            encodedFieldNames.put(encodedName, name);
            accessors.put(name, idAccessor);
        }/*  w  w  w .j  a v  a2s .com*/
        if (method.isAnnotationPresent(SearchIndex.class)) {
            String name = determineName(method);
            String encodedName = encodeFieldName(name);
            IndexType indexType = method.getAnnotation(SearchIndex.class).as();
            if (indexType == IndexType.Automatic) {
                indexType = indexTypeLookup.inferIndexType(method.getReturnType());
            }
            accessors.put(name, new MethodAccessor<T, Object>(type, method, name, encodedName, indexType));
            encodedFieldNames.put(encodedName, name);
        }
    }
}

From source file:org.bigmouth.nvwa.zookeeper.concurrent.spring.SynchronousAdvisor.java

private String getPath(Method method, Object[] args) {
    String path = null;/*from   w w w .  j a va 2 s. com*/
    if (method.isAnnotationPresent(SynchronousSupport.class)) {
        SynchronousSupport annotation = method.getAnnotation(SynchronousSupport.class);
        String zkPath = annotation.zkPath();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        String primaryKey = getPrimaryKey(args, parameterAnnotations);
        if (StringUtils.isNotBlank(primaryKey)) {
            path = getPath(zkPath, primaryKey);
            PathUtils.validatePath(path);
        }
    }
    return path;
}

From source file:com.jayway.jaxrs.hateoas.DefaultHateoasContext.java

private String[] getConsumes(Method method) {
    if (method.isAnnotationPresent(Consumes.class)) {
        return method.getAnnotation(Consumes.class).value();
    }//  w w  w . j a  v a2s  .c o  m

    return DEFAULT_MEDIA_TYPE;
}

From source file:com.jayway.jaxrs.hateoas.DefaultHateoasContext.java

private String[] getProduces(Method method) {
    if (method.isAnnotationPresent(Produces.class)) {
        return method.getAnnotation(Produces.class).value();
    }//w w  w  .  java 2  s . com

    return DEFAULT_MEDIA_TYPE;
}

From source file:com.jayway.jaxrs.hateoas.DefaultHateoasContext.java

private String getPath(String rootPath, Method method) {
    if (method.isAnnotationPresent(Path.class)) {
        Path pathAnnotation = method.getAnnotation(Path.class);
        return rootPath + pathAnnotation.value();
    }/*from  w w w .j  av  a  2s  .  c  om*/

    return rootPath.isEmpty() ? "/" : rootPath;
}

From source file:mitm.common.hibernate.AutoCommitProxyFactory.java

private void searchForStartTransactionAnnotations(Class<T> clazz) {
    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        if (method.isAnnotationPresent(StartTransaction.class)) {
            addMethod(method);//from   w ww .  j a v a 2  s .c om
        }
    }
}

From source file:permissionhelper.PermissionHelper.java

/**
 * Invoke a method with annotation PermissionGranted.
 *
 * @param obj/*from  w  w  w. jav  a 2  s. c  o  m*/
 * @param permission
 */
private void invokeGrantedMethod(Object obj, String permission) {
    Class clazz = obj.getClass();
    PermissionGranted permissionGranted;

    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(PermissionGranted.class)) {
            permissionGranted = method.getAnnotation(PermissionGranted.class);
            if (permissionGranted.permission().equals(permission)) {
                if (method.getModifiers() != Modifier.PUBLIC) {
                    throw new IllegalArgumentException(
                            String.format("Annotation method %s must be public.", method));
                }

                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException(String.format("Cannot execute non-void method %s.", method));
                }

                try {
                    method.invoke(obj);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:permissionhelper.PermissionHelper.java

/**
 * Invoke a method with annotation PermissionDenied.
 *
 * @param obj/*from  w  w w.  j  a v  a 2s  .c  o m*/
 * @param permission
 */
private void invokeDeniedMethod(Object obj, String permission) {
    Class clazz = obj.getClass();
    PermissionDenied permissionDenied;

    for (Method method : clazz.getMethods()) {
        if (method.isAnnotationPresent(PermissionDenied.class)) {
            permissionDenied = method.getAnnotation(PermissionDenied.class);
            if (permissionDenied.permission().equals(permission)) {
                if (method.getModifiers() != Modifier.PUBLIC) {
                    throw new IllegalArgumentException(
                            String.format("Annotation method %s must be public.", method));
                }

                if (method.getParameterTypes().length > 0) {
                    throw new RuntimeException(String.format("Cannot execute non-void method %s.", method));
                }

                try {
                    method.invoke(obj);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:org.archone.ad.rpc.RpcServiceImpl.java

public void init() throws ClassNotFoundException, InstantiationException, SecurityViolationException {
    for (String className : actionClasses) {
        Object object = applicationContext.getBean(Class.forName(className));
        for (Method method : Class.forName(className).getMethods()) {
            if (method.isAnnotationPresent(RPCAction.class)) {

                //Setting up method invocation vars
                HashMap<String, Object> rpcCall = new HashMap<String, Object>();
                rpcCall.put("method", method);
                rpcCall.put("object", object);
                rpcCall.put("required", method.getAnnotation(RPCAction.class).required());
                rpcCall.put("optional", method.getAnnotation(RPCAction.class).optional());
                rpcCall.put("role", method.getAnnotation(RPCAction.class).role());

                callMap.put(method.getAnnotation(RPCAction.class).name(), rpcCall);

                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Registered rpc call with name {0}",
                        method.getAnnotation(RPCAction.class).name());
            }//from  w w w . j  ava2  s.c  o m
        }
    }

    for (String className : getSecurityConstraintClasses()) {
        Object object = applicationContext.getBean(Class.forName(className));
        for (Method method : Class.forName(className).getMethods()) {
            if (method.isAnnotationPresent(SecurityConstraint.class)) {
                HashMap<String, Object> scCheck = new HashMap<String, Object>();
                scCheck.put("method", method);
                scCheck.put("object", object);

                String name = method.getAnnotation(SecurityConstraint.class).name();
                scMap.put(name, scCheck);

                Logger.getLogger(this.getClass().getName()).log(Level.INFO,
                        "Registered Security Constraint with name {0}", name);
            }
        }
    }

}