Example usage for org.springframework.util ReflectionUtils invokeMethod

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

Introduction

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

Prototype

@Nullable
public static Object invokeMethod(Method method, @Nullable Object target) 

Source Link

Document

Invoke the specified Method against the supplied target object with no arguments.

Usage

From source file:de.tolina.common.validation.AnnotationValidation.java

private void checkForUndefinedMethodsInAnnotation(@Nonnull final SoftAssertions softly,
        @Nonnull final Annotation annotation, @Nonnull final List<String> validatedMethods) {
    final Method[] allMethods = annotation.getClass().getDeclaredMethods();

    for (final Method declaredMethod : allMethods) {
        // we do not want these to be checked
        final boolean isBlacklisted = paramBlacklist.contains(declaredMethod.getName());
        // skip already validated methods
        final boolean isAlreadyValidated = validatedMethods.contains(declaredMethod.getName());

        if (isBlacklisted || isAlreadyValidated) {
            continue;
        }// ww  w. j  a  va  2 s .c om

        // all methods in current annotation which are not defined in annotation definition or blacklist are to be reported as error
        final Object methodResult = ReflectionUtils.invokeMethod(declaredMethod, annotation);
        if (Object[].class.isInstance(methodResult)) {
            softly.assertThat((Object[]) methodResult)
                    .as("Unexpected values for %s found.", declaredMethod.getName()).isNullOrEmpty();
        } else {
            final String description = "Unexpected value for Method '%s' found.";
            if (methodResult instanceof String) {
                softly.assertThat((String) methodResult).as(description, declaredMethod.getName())
                        .isNullOrEmpty();
            } else {
                softly.assertThat(methodResult).as(description, declaredMethod.getName()).isNull();
            }
        }
    }
}

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

public static <T> Object getValue(T example, Attribute<? super T, ?> attr) {
    try {/*from   w  w w .  jav a2 s .c o  m*/
        if (attr.getJavaMember() instanceof Method) {
            return ReflectionUtils.invokeMethod((Method) attr.getJavaMember(), example);
        } else {
            return ReflectionUtils.getField((Field) attr.getJavaMember(), example);
        }
    } catch (Exception e) {
        throw propagate(e);
    }
}

From source file:ch.qos.logback.ext.spring.web.WebLogbackConfigurer.java

/**
 * Shut down Logback, properly releasing all file locks
 * and resetting the web app root system property.
 *
 * @param servletContext the current ServletContext
 * @see WebUtils#removeWebAppRootSystemProperty
 *///from   w  w w.j  a v  a2 s.  com
public static void shutdownLogging(ServletContext servletContext) {
    //Uninstall the SLF4J java.util.logging bridge *before* shutting down the Logback framework.
    try {
        Class<?> julBridge = ClassUtils.forName("org.slf4j.bridge.SLF4JBridgeHandler",
                ClassUtils.getDefaultClassLoader());
        Method uninstall = ReflectionUtils.findMethod(julBridge, "uninstall");
        if (uninstall != null) {
            servletContext.log("Uninstalling JUL to SLF4J bridge");
            ReflectionUtils.invokeMethod(uninstall, null);
        }
    } catch (ClassNotFoundException ignored) {
        //No need to shutdown the java.util.logging bridge. If it's not on the classpath, it wasn't started either.
    }

    try {
        servletContext.log("Shutting down Logback");
        LogbackConfigurer.shutdownLogging();
    } finally {
        // Remove the web app root system property.
        if (exposeWebAppRoot(servletContext)) {
            WebUtils.removeWebAppRootSystemProperty(servletContext);
        }
    }
}

From source file:ch.digitalfondue.npjt.QueryType.java

protected Object buildOptional(List<Object> res) {
    if (res.size() > 1) {
        throw new IncorrectResultSizeDataAccessException(1, res.size());
    }/*from w  w w  . j a v a2  s.com*/

    try {
        Class<?> clazz = Class.forName("java.util.Optional");
        if (res.isEmpty()) {
            return ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(clazz, "empty"), null);
        } else {
            return ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(clazz, "ofNullable", Object.class),
                    null, res.iterator().next());
        }
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.javaetmoi.core.spring.vfs.Vfs2Utils.java

protected static String doGetPath(Object resource) {
    return (String) ReflectionUtils.invokeMethod(VIRTUAL_FILE_METHOD_GET_PATH_NAME, resource);
}

From source file:de.tolina.common.validation.AnnotationValidation.java

private List<String> validateAllMethodsOfAnnotationDefinition(@Nonnull final SoftAssertions softly,
        @Nonnull final AnnotationDefinition annotationDefinition, @Nonnull final Annotation annotation) {
    final List<String> validatedMethods = Lists.newArrayList();

    // check all methods defined in annotation definition
    for (final AnnotationDefinition.AnnotationMethodDefinition annotationMethodDefinition : annotationDefinition
            .getAnnotationMethodDefinitions()) {
        final String methodName = annotationMethodDefinition.getMethod();
        final Object[] values = annotationMethodDefinition.getValues();

        Method method = null;/*w  w  w .  ja  v a  2 s . c o m*/
        try {
            method = annotation.annotationType().getMethod(methodName);
        } catch (final NoSuchMethodException e) {
            // noop
        }

        // check that defined method is present in annotation
        softly.assertThat(method).as("Method %s not found.", methodName).isNotNull();

        if (method == null) {
            continue;
        }

        // check that actual method in annotation has defined return types
        final Object methodResult = ReflectionUtils.invokeMethod(method, annotation);
        if (Object[].class.isInstance(methodResult)) {
            // this produces readable descriptions on its own
            // all and only defined values must be returned in defined order
            softly.assertThat((Object[]) methodResult).containsExactlyElementsOf(Arrays.asList(values));
        } else {
            // this produces readable descriptions on its own
            softly.assertThat(methodResult).isEqualTo(values[0]);
        }
        validatedMethods.add(method.getName());
        // check if this annotation's method is an alias
        final AliasFor alias = AnnotationUtils.findAnnotation(method, AliasFor.class);
        if (alias != null) {
            // mark the aliased method as validated
            validatedMethods.add(alias.value());
        }

    }
    return validatedMethods;
}

From source file:org.solq.dht.db.redis.service.JedisConnectionFactory.java

private int getTimeoutFrom(JedisShardInfo shardInfo) {
    return (Integer) ReflectionUtils.invokeMethod(GET_TIMEOUT_METHOD, shardInfo);
}

From source file:org.hdiv.web.servlet.tags.UrlTagTests.java

private String invokeCreateUrl(UrlTagHDIV tag) {
    Method createUrl = ReflectionUtils.findMethod(tag.getClass(), "createUrl");
    ReflectionUtils.makeAccessible(createUrl);
    return (String) ReflectionUtils.invokeMethod(createUrl, tag);
}

From source file:nivance.jpa.cassandra.prepare.convert.MappingCassandraConverter.java

public List<Clause> getKeyPart(final CassandraPersistentEntity<?> entity, final Object valueBean) {
    final List<Clause> result = new LinkedList<Clause>();
    entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
        public void doWithPersistentProperty(CassandraPersistentProperty prop) {
            //TODO
            if (prop.getKeyPart() != null) {
                //keypart?
                Method method = ReflectionUtils.findMethod(entity.getType(),
                        "get" + StringUtils.capitalize(prop.getColumnName()));
                Object id = ReflectionUtils.invokeMethod(method, valueBean);
                result.add(QueryBuilder.eq(prop.getColumnName(), id));
            }//from  ww  w .j  a v  a2  s.c  om
        }
    });
    if (result.isEmpty()) {
        throw new MappingException(
                "Could not form a where clause for the primary key for an entity " + entity.getName());
    }
    return result;
}

From source file:com.glaf.core.util.ReflectUtils.java

public static Object invoke(Object object, String methodName) {
    try {//from   ww w . j a v  a  2s .c o  m
        Method method = ReflectionUtils.findMethod(object.getClass(), methodName);
        if (!Modifier.isPublic(method.getModifiers())) {
            method.setAccessible(true);
        }
        return ReflectionUtils.invokeMethod(method, object);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}