Example usage for org.springframework.util ReflectionUtils findMethod

List of usage examples for org.springframework.util ReflectionUtils findMethod

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils findMethod.

Prototype

@Nullable
public static Method findMethod(Class<?> clazz, String name, @Nullable Class<?>... paramTypes) 

Source Link

Document

Attempt to find a Method on the supplied class with the supplied name and parameter types.

Usage

From source file:org.codehaus.groovy.grails.commons.AbstractGrailsClass.java

public boolean isActionMethod(String methodName) {
    Method m = ReflectionUtils.findMethod(getClazz(), methodName, new Class[0]);
    if (m != null) {
        ReflectionUtils.makeAccessible(m);
    }//from   ww w .  j  a  v a2  s . c  o m
    return m != null && m.getAnnotation(Action.class) != null;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriterionAdapter.java

protected void addCriterionAdaptor(final Class<?> clazz, final String constraintName,
        final Class<?> valueClass) {
    criterionAdaptors.put(clazz, new CriterionAdaptor() {
        @Override/*  w ww.ja v a  2s .  com*/
        public org.hibernate.criterion.Criterion toHibernateCriterion(AbstractHibernateQuery hibernateQuery,
                Query.Criterion criterion, String alias) {
            Query.PropertyCriterion propertyCriterion = (Query.PropertyCriterion) criterion;
            Object value = propertyCriterion.getValue();
            String propertyName = getPropertyName(criterion, alias);
            if (value instanceof DetachedCriteria) {
                Method propertyMethod = ReflectionUtils.findMethod(Property.class, constraintName,
                        new Class[] { DetachedCriteria.class });
                if (propertyMethod != null) { // if supports subqueries
                    return (Criterion) ReflectionUtils.invokeMethod(propertyMethod,
                            Property.forName(propertyName), new Object[] { value });
                }
            }
            return callRestrictionsMethod(constraintName, new Class[] { String.class, valueClass },
                    new Object[] { propertyName, value });
        }
    });
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateCriterionAdapter.java

/**
 * utility method that generically returns a criterion using methods in Restrictions
 *
 * @param constraintName - the criteria//w  ww. jav a  2s  . c o m
 */
protected Criterion callRestrictionsMethod(String constraintName, Class<?>[] paramTypes, Object[] params) {
    final Method restrictionsMethod = ReflectionUtils.findMethod(Restrictions.class, constraintName,
            paramTypes);
    Assert.notNull(restrictionsMethod,
            "Could not find method: " + constraintName + " in class Restrictions for parameters: "
                    + ArrayUtils.toString(params) + " with types: " + ArrayUtils.toString(paramTypes));
    return (Criterion) ReflectionUtils.invokeMethod(restrictionsMethod, null, params);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.HibernateCriterionAdapter.java

private static void addCriterionAdaptor(final Class<?> clazz, final String constraintName,
        final Class<?> valueClass) {
    criterionAdaptors.put(clazz, new CriterionAdaptor() {
        @Override/* www.j ava2 s . c  om*/
        public org.hibernate.criterion.Criterion toHibernateCriterion(HibernateQuery hibernateQuery,
                Query.Criterion criterion, String alias) {
            Query.PropertyCriterion propertyCriterion = (Query.PropertyCriterion) criterion;
            Object value = propertyCriterion.getValue();
            String propertyName = getPropertyName(criterion, alias);
            if (value instanceof DetachedCriteria) {
                Method propertyMethod = ReflectionUtils.findMethod(Property.class, constraintName,
                        new Class[] { DetachedCriteria.class });
                if (propertyMethod != null) { // if supports subqueries
                    return (Criterion) ReflectionUtils.invokeMethod(propertyMethod,
                            Property.forName(propertyName), new Object[] { value });
                }
            }
            return callRestrictionsMethod(constraintName, new Class[] { String.class, valueClass },
                    new Object[] { propertyName, value });
        }
    });
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.HibernateCriterionAdapter.java

/**
 * utility method that generically returns a criterion using methods in Restrictions
 *
 * @param constraintName - the criteria/*from w ww  . ja  v  a2s .c om*/
 */
private static Criterion callRestrictionsMethod(String constraintName, Class<?>[] paramTypes, Object[] params) {
    final Method restrictionsMethod = ReflectionUtils.findMethod(Restrictions.class, constraintName,
            paramTypes);
    Assert.notNull(restrictionsMethod,
            "Could not find method: " + constraintName + " in class Restrictions for parameters: "
                    + ArrayUtils.toString(params) + " with types: " + ArrayUtils.toString(paramTypes));
    return (Criterion) ReflectionUtils.invokeMethod(restrictionsMethod, null, params);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor.java

public ClosureEventTriggeringInterceptor() {
    try {/*from  w w w  . j a  v a  2  s  . c  o m*/
        markInterceptorDirtyMethod = ReflectionUtils.findMethod(AbstractSaveEventListener.class,
                "markInterceptorDirty", new Class[] { Object.class, EntityPersister.class, EventSource.class });
        ReflectionUtils.makeAccessible(markInterceptorDirtyMethod);
    } catch (Exception e) {
        // ignore
    }
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateRenderer.java

public void afterPropertiesSet() throws Exception {
    if (scaffoldingTemplateGenerator != null) {
        // use reflection to locate method (would cause cyclic dependency otherwise)
        generateViewMethod = ReflectionUtils.findMethod(scaffoldingTemplateGenerator.getClass(), "generateView",
                new Class<?>[] { GrailsDomainClass.class, String.class, Writer.class });
    }//from   ww w  .j ava2s  . c om
    reloadEnabled = groovyPagesTemplateEngine.isReloadEnabled();
}

From source file:org.openmrs.module.webservices.docs.swagger.SwaggerSpecificationCreator.java

private boolean testOperationImplemented(OperationEnum operation,
        DelegatingResourceHandler<?> resourceHandler) {
    Method method;// w  w w.  j  a  v  a  2 s . co  m
    try {
        switch (operation) {
        case get:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "getAll", RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, new RequestContext());
            }

            break;
        case getSubresource:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "getAll", String.class,
                    RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        new RequestContext());
            }

            break;
        case getWithUUID:
        case getSubresourceWithUUID:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "getByUniqueId", String.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID);
            }

            break;
        case getWithDoSearch:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "search", RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, new RequestContext());
            }

            break;
        case postCreate:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "create", SimpleObject.class,
                    RequestContext.class);

            if (method == null) {
                return false;
            } else {
                try {
                    // to avoid saving data to the database, we pass a null SimpleObject
                    method.invoke(resourceHandler, null, new RequestContext());
                } catch (ResourceDoesNotSupportOperationException re) {
                    return false;
                } catch (Exception ee) {
                    // if the resource doesn't immediate throw ResourceDoesNotSupportOperationException
                    // then we need to check if it's thrown in the save() method
                    resourceHandler.save(null);
                }
            }

            break;
        case postSubresource:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "create", String.class,
                    SimpleObject.class, RequestContext.class);

            if (method == null) {
                return false;
            } else {
                try {
                    // to avoid saving data to the database, we pass a null SimpleObject
                    method.invoke(resourceHandler, null, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                            new RequestContext());
                } catch (ResourceDoesNotSupportOperationException re) {
                    return false;
                } catch (Exception ee) {
                    // if the resource doesn't immediate throw ResourceDoesNotSupportOperationException
                    // then we need to check if it's thrown in the save() method
                    resourceHandler.save(null);
                }
            }

            break;
        case postUpdate:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "update", String.class,
                    SimpleObject.class, RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        buildPOSTUpdateSimpleObject(resourceHandler), new RequestContext());
            }

            break;
        case postUpdateSubresouce:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "update", String.class,
                    String.class, SimpleObject.class, RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        buildPOSTUpdateSimpleObject(resourceHandler), new RequestContext());
            }

            break;
        case delete:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "delete", String.class,
                    String.class, RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new String(),
                        new RequestContext());
            }

            break;
        case deleteSubresource:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "delete", String.class,
                    String.class, String.class, RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new String(), new RequestContext());
            }
            break;
        case purge:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "purge", String.class,
                    RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        new RequestContext());
            }

            break;
        case purgeSubresource:
            method = ReflectionUtils.findMethod(resourceHandler.getClass(), "purge", String.class, String.class,
                    RequestContext.class);

            if (method == null) {
                return false;
            } else {
                method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
                        RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
            }
        }
        return true;
    } catch (Exception e) {
        if (e instanceof ResourceDoesNotSupportOperationException
                || e.getCause() instanceof ResourceDoesNotSupportOperationException) {
            return false;
        } else {
            return true;
        }
    }
}

From source file:org.shept.org.springframework.web.servlet.mvc.support.ModelUtils.java

/**
 * //w  ww .  jav  a2 s.  co m
 * @return the initialize(sourceObject) a target object from a source
 */
public static void initialize(Object target, Object sourceModel) {
    if (sourceModel == null) {
        return;
    }
    Assert.notNull(target, "The object being initialized with '" + sourceModel + "' (of class '"
            + sourceModel.getClass() + "') may not be null");
    Method mth = ReflectionUtils.findMethod(target.getClass(), initMethod, sourceModel.getClass());
    if (mth == null) {
        String message = "The object of class '" + target.getClass() + "' cannot be initialized from model '"
                + sourceModel.getClass() + "' Method '" + initMethod + "(" + sourceModel.getClass()
                + ")' is missing";
        if (sourceModel instanceof String) {
            logger.info(message);
        } else {
            logger.warn(message);
        }
    } else {
        // void initialize(sourceObject)
        ReflectionUtils.invokeMethod(mth, target, sourceModel);
    }
}

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void restOperationsAreAvailable() throws Exception {
    RestTemplate delegate = mock(RestTemplate.class);
    given(delegate.getUriTemplateHandler()).willReturn(new DefaultUriBuilderFactory());
    final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
    ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {

        @Override//from  w w w  .jav a  2s .co  m
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class, method.getName(),
                    method.getParameterTypes());
            assertThat(equivalent).as("Method %s not found", method).isNotNull();
            assertThat(Modifier.isPublic(equivalent.getModifiers()))
                    .as("Method %s should have been public", equivalent).isTrue();
            try {
                equivalent.invoke(restTemplate, mockArguments(method.getParameterTypes()));
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

        private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                arguments[i] = mockArgument(parameterTypes[i]);
            }
            return arguments;
        }

        @SuppressWarnings("rawtypes")
        private Object mockArgument(Class<?> type) throws Exception {
            if (String.class.equals(type)) {
                return "String";
            }
            if (Object[].class.equals(type)) {
                return new Object[0];
            }
            if (URI.class.equals(type)) {
                return new URI("http://localhost");
            }
            if (HttpMethod.class.equals(type)) {
                return HttpMethod.GET;
            }
            if (Class.class.equals(type)) {
                return Object.class;
            }
            if (RequestEntity.class.equals(type)) {
                return new RequestEntity(HttpMethod.GET, new URI("http://localhost"));
            }
            return mock(type);
        }

    }, (method) -> Modifier.isPublic(method.getModifiers()));

}