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:com.github.hateoas.forms.spring.AffordanceBuilderFactoryTest.java

@Test
public void testLinkToMethod() throws Exception {
    final Method getEventMethod = ReflectionUtils.findMethod(EventControllerSample.class, "getEvent",
            String.class);
    final Affordance affordance = factory.linkTo(getEventMethod, new Object[0]).rel("foo").build();
    assertEquals("http://example.com/events/{eventId}", affordance.getHref());
}

From source file:com.expedia.seiso.domain.repo.adapter.SimpleItemRepoAdapterTests.java

private void setUpTestData() {
    this.serviceKey = new SimpleItemKey(Service.class, SERVICE_KEY);
    this.nonExistentServiceKey = new SimpleItemKey(Service.class, NONEXISTENT_SERVICE_KEY);

    this.findByKeyMethod = ReflectionUtils.findMethod(ServiceRepo.class, "findByKey", String.class);

    when(serviceItemMeta.getRepositoryFindByKeyMethod()).thenReturn(findByKeyMethod);
    when(serviceInstancePortItemMeta.getRepositoryFindByKeyMethod()).thenReturn(null);
}

From source file:com.github.hateoas.forms.spring.sample.test.DummyEventController.java

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody Resources<Resource<Event>> getResourcesOfResourceOfEvent() {
    List<Resource<Event>> eventResourcesList = new ArrayList<Resource<Event>>();
    // each resource has links
    for (Event event : getEvents()) {
        Resource<Event> eventResource = new Resource<Event>(event);
        eventResource.add(linkTo(methodOn(this.getClass()).getEvent(event.id))
                .and(linkTo(methodOn(this.getClass()).updateEventWithRequestBody(event.id, event)))
                .and(linkTo(methodOn(this.getClass()).deleteEvent(event.id))).withSelfRel());
        event.getWorkPerformed().add(/*from ww  w  . java2  s . c  o  m*/
                linkTo(methodOn(ReviewController.class).addReview(event.id, new Review(null, new Rating(3))))
                        .withRel("review"));
        eventResourcesList.add(eventResource);
    }

    // the resources have templated links to methods
    // specify method by reflection
    final Method getEventMethod = ReflectionUtils.findMethod(this.getClass(), "findEventByName", String.class);
    final Affordance eventByNameAffordance = linkTo(getEventMethod, new Object[0]).withRel("hydra:search");
    final Affordance eventWithRegexAffordance = linkTo(
            methodOn(this.getClass()).getEventWithRegexPathVariableMapping(null)).withRel("ex:regex");
    final Affordance postEventAffordance = linkTo(methodOn(this.getClass()).addEvent(null)).withSelfRel();

    return new Resources<Resource<Event>>(eventResourcesList, eventByNameAffordance, eventWithRegexAffordance,
            postEventAffordance);
}

From source file:es.jffa.tsc.sip04.dao.hbn.AbstractHbnDao.java

/**
 *
 * @param t//w  ww . j av a  2  s .  c  om
 */
@Override
public void create(final T t) {
    Method method = ReflectionUtils.findMethod(_getDomainClass(), "setDateCreated",
            new Class[] { java.util.Date.class });

    if (method != null) {
        try {
            method.invoke(t, new Date());
        } catch (Exception e) {
        }
    }

    _getSession().save(t);
}

From source file:com.github.hateoas.forms.spring.AffordanceBuilderFactoryTest.java

@Test
public void testLinkToMethodInvocation() throws Exception {
    final Method getEventMethod = ReflectionUtils.findMethod(EventControllerSample.class, "getEvent",
            String.class);
    final Affordance affordance = factory
            .linkTo(AffordanceBuilder.methodOn(EventControllerSample.class).getEvent((String) null)).rel("foo")
            .build();/*from  w  w w . ja  v a2 s.co  m*/
    assertEquals("http://example.com/events/{eventId}", affordance.getHref());
}

From source file:org.pentaho.proxy.creators.userdatailsservice.ProxyUserDetailsServiceTest.java

@Test
public void testCreateProxyWrapper() {

    Object wrappedObject = new ProxyUserDetailsService(mockUserDetailsService);

    Assert.assertNotNull(wrappedObject);

    Method loadUserByUsernameMethod = ReflectionUtils.findMethod(wrappedObject.getClass(), "loadUserByUsername",
            String.class);

    try {/*from  ww  w.ja v  a2  s . c  o  m*/

        Object wrappedUserDetails = loadUserByUsernameMethod.invoke(wrappedObject, MOCK_USERNAME);

        Assert.assertNotNull(wrappedUserDetails);

        Method getUsernameMethod = ReflectionUtils.findMethod(wrappedUserDetails.getClass(), "getUsername");
        Method getPasswordMethod = ReflectionUtils.findMethod(wrappedUserDetails.getClass(), "getPassword");
        Method isEnabledMethod = ReflectionUtils.findMethod(wrappedUserDetails.getClass(), "isEnabled");

        Object wrappedUsername = getUsernameMethod.invoke(wrappedUserDetails);
        Object wrappedPassword = getPasswordMethod.invoke(wrappedUserDetails);
        Object wrappedIsEnabled = isEnabledMethod.invoke(wrappedUserDetails);

        Assert.assertNotNull(wrappedUsername);
        Assert.assertNotNull(wrappedPassword);
        Assert.assertNotNull(wrappedIsEnabled);

        Assert.assertTrue(wrappedUsername.toString().equals(MOCK_USERNAME));
        Assert.assertTrue(wrappedPassword.toString().equals(MOCK_PASSWORD));
        Assert.assertTrue(wrappedIsEnabled instanceof Boolean && (wrappedIsEnabled).equals(MOCK_IS_ENABLED));

    } catch (IllegalAccessException | InvocationTargetException e) {
        logger.error(e.getMessage(), e);
        Assert.fail();
    }
}

From source file:at.ac.tuwien.infosys.jcloudscale.test.unit.TestReflectionUtil.java

@Test
public void testVarArgsArrayMethod() throws ClassNotFoundException {
    Method method = findMethod(JCloudScaleServerRunner.class, "main", String[].class);
    assertEquals(ReflectionUtils.findMethod(JCloudScaleServerRunner.class, "main", null), method);
}

From source file:org.pentaho.proxy.creators.userdetailsservice.S4UserDetailsServiceProxyCreatorTest.java

@Test
public void testCreateProxyWrapper() {

    Object wrappedObject = new S4UserDetailsServiceProxyCreatorForTest().create(mockUserDetailsService);

    Assert.assertNotNull(wrappedObject);

    Method loadUserByUsernameMethod = ReflectionUtils.findMethod(wrappedObject.getClass(), "loadUserByUsername",
            String.class);

    try {//from   www.ja  v  a  2s. c  o  m

        Object wrappedUserDetails = loadUserByUsernameMethod.invoke(wrappedObject, MOCK_USERNAME);

        Assert.assertNotNull(wrappedUserDetails);

        Method getUsernameMethod = ReflectionUtils.findMethod(wrappedUserDetails.getClass(), "getUsername");
        Method getPasswordMethod = ReflectionUtils.findMethod(wrappedUserDetails.getClass(), "getPassword");
        Method isEnabledMethod = ReflectionUtils.findMethod(wrappedUserDetails.getClass(), "isEnabled");

        Object wrappedUsername = getUsernameMethod.invoke(wrappedUserDetails);
        Object wrappedPassword = getPasswordMethod.invoke(wrappedUserDetails);
        Object wrappedIsEnabled = isEnabledMethod.invoke(wrappedUserDetails);

        Assert.assertNotNull(wrappedUsername);
        Assert.assertNotNull(wrappedPassword);
        Assert.assertNotNull(wrappedIsEnabled);

        Assert.assertTrue(wrappedUsername.toString().equals(MOCK_USERNAME));
        Assert.assertTrue(wrappedPassword.toString().equals(MOCK_PASSWORD));
        Assert.assertTrue(wrappedIsEnabled instanceof Boolean && (wrappedIsEnabled).equals(MOCK_IS_ENABLED));

    } catch (IllegalAccessException | InvocationTargetException e) {
        logger.error(e.getMessage(), e);
        Assert.fail();
    }
}

From source file:org.grails.plugin.platform.events.registry.DefaultEventsRegistry.java

private String registerHandler(@SuppressWarnings("rawtypes") Closure callback, String namespace, String topic) {
    if (log.isDebugEnabled()) {
        log.debug("Registering event handler [" + callback.getClass() + "] for topic [" + topic + "]");
    }//from   www. j  ava2 s  . co  m

    ListenerId listener = ListenerId.build(namespace, topic, callback);
    ListenerHandler handler = new ListenerHandler(callback,
            ReflectionUtils.findMethod(callback.getClass(), "call", Object.class), listener);

    synchronized (listeners) {
        listeners.add(handler);
    }

    return listener.toString();
}

From source file:com.expedia.seiso.web.controller.v2.RepoSearchControllerV2Tests.java

private void setUpTestData() {
    when(nonPagingItemMeta.isPagingRepo()).thenReturn(false);
    when(pagingItemMeta.isPagingRepo()).thenReturn(true);

    val searchMethodWithListResult = ReflectionUtils.findMethod(StatusTypeRepo.class, "findBySourceKey",
            String.class);
    assert (searchMethodWithListResult != null);
    when(nonPagingItemMeta.getRepositorySearchMethod(SEARCH)).thenReturn(searchMethodWithListResult);

    val searchMethodWithPageResult = ReflectionUtils.findMethod(PersonRepo.class, "findByLastName",
            String.class, Pageable.class);
    assert (searchMethodWithPageResult != null);
    when(pagingItemMeta.getRepositorySearchMethod(SEARCH)).thenReturn(searchMethodWithPageResult);

    val searchMethodWithUniqueResult = ReflectionUtils.findMethod(ServiceRepo.class, "findByName",
            String.class);
    assert (searchMethodWithUniqueResult != null);
    when(pagingItemMeta.getRepositorySearchMethod(SEARCH_WITH_UNIQUE_RESULT))
            .thenReturn(searchMethodWithUniqueResult);
}