Example usage for java.lang Class getMethods

List of usage examples for java.lang Class getMethods

Introduction

In this page you can find the example usage for java.lang Class getMethods.

Prototype

@CallerSensitive
public Method[] getMethods() throws SecurityException 

Source Link

Document

Returns an array containing Method objects reflecting all the public methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

Usage

From source file:org.querybyexample.jpa.JpaUniqueUtil.java

private List<String> validateSimpleUniqueConstraintsDefinedOnMethods(Identifiable<?> entity) {
    Class<?> entityClass = getClassWithoutInitializingProxy(entity);
    List<String> errors = newArrayList();
    for (Method method : entityClass.getMethods()) {
        Column column = findAnnotation(method, Column.class);
        if (column != null && column.unique()) {
            Map<String, Object> values = newHashMap();
            String property = methodToProperty(method);
            values.put(property, invokeMethod(method, entity));
            if (existsInDatabaseOnAllObjects(entity, values)) {
                errors.add(simpleUniqueConstraintError(entity, property));
            }/*from  w w  w .  ja  v a 2s  .  c  om*/
        }
    }
    return errors;
}

From source file:com.dbs.sdwt.jpa.JpaUniqueUtil.java

private List<String> validateSimpleUniqueConstraintsDefinedOnMethods(Identifiable<?> entity) {
    Class<?> entityClass = getClassWithoutInitializingProxy(entity);
    List<String> errors = newArrayList();
    for (Method method : entityClass.getMethods()) {
        Column column = entityClass.getAnnotation(Column.class);
        if (column != null && column.unique()) {
            Map<String, Object> values = newHashMap();
            String property = jpaUtil.methodToProperty(method);
            values.put(property, invokeMethod(method, entity));
            if (existsInDatabaseOnAllObjects(entity, values)) {
                errors.add(simpleUniqueConstraintError(entity, property));
            }// w w w .  ja  v a 2 s  . com
        }
    }
    return errors;
}

From source file:grails.plugin.springsecurity.acl.AclAutoProxyCreator.java

protected boolean beanIsAnnotated(final Class<?> c) {
    for (Class<? extends Annotation> annotation : ANNOTATIONS) {
        if (c.isAnnotationPresent(annotation)) {
            return true;
        }//from  w ww .j  a va  2 s.  c  om

        for (Method method : c.getMethods()) {
            if (method.isAnnotationPresent(annotation)) {
                return true;
            }
        }
    }
    return false;
}

From source file:gda.jython.accesscontrol.ProtectedMethodComponent.java

/**
 * Analyse the given class and add any methods annotated to be protected to the protectedMethods array.
 * //  w w w.j  a  v  a2  s .  c o  m
 * @param clazz
 */
@SuppressWarnings("rawtypes")
private void analyseClass(Class clazz) {
    // loop over this classes methods
    Method[] newMethods = clazz.getMethods();
    for (Method thisMethod : newMethods) {
        if (thisMethod.isAnnotationPresent(gda.jython.accesscontrol.MethodAccessProtected.class)
                && thisMethod.getAnnotation(MethodAccessProtected.class).isProtected()) {
            addMethod(thisMethod);
        }
    }

    // loop over the interfaces the class implements
    Class[] newInterfaces = clazz.getInterfaces();
    for (Class thisInterface : newInterfaces) {
        // if its a new interface (for performance, ensure each interface only looked at once)
        if (!ArrayUtils.contains(analysedInterfaces, thisInterface)) {
            analysedInterfaces = (Class[]) ArrayUtils.add(analysedInterfaces, thisInterface);

            // check if any method in that interface is annotated that it should be protected
            for (Method method : thisInterface.getMethods()) {
                if (method.isAnnotationPresent(gda.jython.accesscontrol.MethodAccessProtected.class)
                        && method.getAnnotation(MethodAccessProtected.class).isProtected()) {
                    addMethod(method);
                }
            }
        }
    }
}

From source file:com.bstek.dorado.common.service.ExposedServiceAnnotationBeanPostProcessor.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType,
        String beanName) {//from www .ja  va  2 s. c  o m
    boolean defaultExposed = (beanType.getAnnotation(Expose.class) != null)
            && (beanType.getAnnotation(Unexpose.class) == null);

    for (Method method : beanType.getMethods()) {
        Expose annotation = method.getAnnotation(Expose.class);
        boolean exposed = defaultExposed
                || ((annotation != null) && (beanType.getAnnotation(Unexpose.class) == null));
        if (!exposed) {
            continue;
        }
        pendingDataObjects.add(new PendingObject(annotation, beanName, method.getName()));
    }
}

From source file:com.haulmont.cuba.gui.xml.DeclarativeColumnGenerator.java

protected Method findGeneratorMethod(Class cls, String methodName) {
    Method exactMethod = MethodUtils.getAccessibleMethod(cls, methodName, Entity.class);
    if (exactMethod != null) {
        return exactMethod;
    }// w w  w .  j a va 2 s  .c  o m

    // search through all methods
    Method[] methods = cls.getMethods();
    for (Method availableMethod : methods) {
        if (availableMethod.getName().equals(methodName)) {
            if (availableMethod.getParameterCount() == 1
                    && Component.class.isAssignableFrom(availableMethod.getReturnType())) {
                if (Entity.class.isAssignableFrom(availableMethod.getParameterTypes()[0])) {
                    // get accessible version of method
                    return MethodUtils.getAccessibleMethod(availableMethod);
                }
            }
        }
    }
    return null;
}

From source file:com.frameworkx.mvc.ControllerRouteHandler.java

/**
 * Determine what method from the controller class to call
 *
 * @param methodName/*from   w w  w. ja  v a  2  s  .  c om*/
 * @param clss
 * @return
 */
private Method chooseMethod(final String methodName, final Class<? extends Controller> clss,
        final HttpServletRequest request) {
    for (Method func : clss.getMethods()) {
        if (func.getName().equals(methodName)) {
            // check HTTP method
            HttpMethod annotation = func.getAnnotation(HttpMethod.class);
            if (annotation != null) {
                if (annotation.value() != null && !annotation.value().isEmpty()) {
                    final String httpMethod = request.getMethod();
                    if (!annotation.value().contains(httpMethod)) {
                        continue;
                    }
                }
            }

            return func;
        }
    }
    return null;
}

From source file:com.gargoylesoftware.htmlunit.javascript.configuration.JavaScriptConfigurationTest.java

/**
 * Tests that anything annotated with {@link JsxGetter} does not start with "set" and vice versa.
 *//*from  www  .  jav a 2 s  .com*/
@Test
public void methodPrefix() {
    for (final Class<?> klass : JavaScriptConfiguration.CLASSES_) {
        for (final Method method : klass.getMethods()) {
            final String methodName = method.getName();
            if (method.getAnnotation(JsxGetter.class) != null && methodName.startsWith("set")) {
                fail("Method " + methodName + " in " + klass.getSimpleName()
                        + " should not start with \"set\"");
            }
            if (method.getAnnotation(JsxSetter.class) != null && methodName.startsWith("get")) {
                fail("Method " + methodName + " in " + klass.getSimpleName()
                        + " should not start with \"get\"");
            }
        }
    }
}

From source file:com.flipkart.flux.deploymentunit.DeploymentUnit.java

/**
 * Given a class loader, retrieves workflow classes names from config file, and returns methods
 * which are annotated with {@link com.flipkart.flux.client.model.Task} annotation in those classes.
 *//*from   w  ww.ja v  a2s .  co m*/
private void populateTaskMethods() {

    List<String> classNames = (List<String>) configuration.getProperty(WORKFLOW_CLASSES);

    try {
        //loading this class separately in this class loader as the following isAnnotationPresent check returns false, if
        //we use default class loader's Task, as both class loaders don't have any relation between them.
        Class taskAnnotationClass = deploymentUnitClassLoader.loadClass(Task.class.getCanonicalName());

        for (String name : classNames) {
            Class clazz = deploymentUnitClassLoader.loadClass(name);

            for (Method method : clazz.getMethods()) {
                if (method.isAnnotationPresent(taskAnnotationClass)) {
                    Annotation taskAnnotation = method.getAnnotationsByType(taskAnnotationClass)[0];
                    long version = 0;

                    for (Method annotationMethod : taskAnnotationClass.getDeclaredMethods()) {
                        if (annotationMethod.getName().equals("version")) {
                            version = (Long) annotationMethod.invoke(taskAnnotation);
                        }
                    }

                    MethodId methodId = new MethodId(method);
                    String taskIdentifier = methodId.toString() + _VERSION + version;

                    taskMethods.put(taskIdentifier, method);
                }
            }
        }
    } catch (Exception e) {
        throw new FluxError(FluxError.ErrorType.runtime,
                "Error while getting task methods for deploymentUnit: " + name + "/" + version, e);
    }
}

From source file:org.seasar.dao.spring.autoregister.AbstractBeanAutoRegister.java

protected void register(final String packageName, final String shortClassName) {

    final String className = ClassUtil.concatName(packageName, shortClassName);

    final Class targetClass = ClassUtil.forName(className);
    Class enhancedClass;//from  www.j av a2s.c o  m

    final AspectWeaver weaver = new AspectWeaver(targetClass, null);

    final Method[] methods = targetClass.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method method = methods[i];
        if (isBridgeMethod(method)) {
            continue;
        }

        final List interceptorList = new ArrayList();
        for (int j = 0; j < interceptorNames.length; j++) {
            final MethodInterceptor interceptor = (MethodInterceptor) getBeanFactory()
                    .getBean(interceptorNames[j]);
            interceptorList.add(interceptor);
        }
        if (!isApplicableAspect(method)) {
            continue;
        }
        if (interceptorList.size() == 0) {
            weaver.setInterceptors(method, new MethodInterceptor[0]);
        } else {
            weaver.setInterceptors(method, (MethodInterceptor[]) interceptorList
                    .toArray(new MethodInterceptor[interceptorList.size()]));
        }
    }
    enhancedClass = weaver.generateClass();

    final BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(
            new XmlReaderContext(null, null, null, null, null, null));
    final int autowireMode = delegate.getAutowireMode(autowire);

    final RootBeanDefinition bd = new RootBeanDefinition();
    bd.setBeanClass(enhancedClass);
    bd.setAutowireMode(autowireMode);
    bd.setScope(scope);

    String beanName;
    if (autoNaming != null) {
        beanName = autoNaming.defineName(packageName, shortClassName);
    } else {
        beanName = className;
    }
    getBeanFactory().registerBeanDefinition(beanName, bd);

}