Example usage for org.springframework.util ReflectionUtils findField

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

Introduction

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

Prototype

@Nullable
public static Field findField(Class<?> clazz, String name) 

Source Link

Document

Attempt to find a Field field on the supplied Class with the supplied name .

Usage

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

@SuppressWarnings("rawtypes")
public GroovyPageMetaInfo(Class<?> pageClass) {
    this();/*from  w  ww.  jav  a  2  s.  com*/
    precompiledMode = true;
    this.pageClass = pageClass;
    contentType = (String) ReflectionUtils
            .getField(ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_CONTENT_TYPE), null);
    jspTags = (Map) ReflectionUtils
            .getField(ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_JSP_TAGS), null);
    lastModified = (Long) ReflectionUtils
            .getField(ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_LAST_MODIFIED), null);
    expressionCodecName = (String) ReflectionUtils.getField(
            ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_EXPRESSION_CODEC), null);
    staticCodecName = (String) ReflectionUtils
            .getField(ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_STATIC_CODEC), null);
    outCodecName = (String) ReflectionUtils
            .getField(ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_OUT_CODEC), null);
    taglibCodecName = (String) ReflectionUtils
            .getField(ReflectionUtils.findField(pageClass, GroovyPageParser.CONSTANT_NAME_TAGLIB_CODEC), null);

    try {
        readHtmlData();
    } catch (IOException e) {
        throw new RuntimeException("Problem reading html data for page class " + pageClass, e);
    }
}

From source file:org.grails.orm.hibernate.support.ClosureEventListener.java

public ClosureEventListener(Class<?> domainClazz, boolean failOnError, List failOnErrorPackages,
        TimestampProvider timestampProvider) {
    this.timestampProvider = timestampProvider;
    domainMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(domainClazz);
    applyAutotimestampSettings(domainClazz, timestampProvider);

    saveOrUpdateCaller = buildCaller(ClosureEventTriggeringInterceptor.ONLOAD_SAVE, domainClazz);
    beforeInsertCaller = buildCaller(ClosureEventTriggeringInterceptor.BEFORE_INSERT_EVENT, domainClazz);
    preLoadEventCaller = buildCaller(ClosureEventTriggeringInterceptor.ONLOAD_EVENT, domainClazz);
    if (preLoadEventCaller == null) {
        preLoadEventCaller = buildCaller(ClosureEventTriggeringInterceptor.BEFORE_LOAD_EVENT, domainClazz);
    }//from w  w  w .  jav  a 2s.c  o m
    postLoadEventListener = buildCaller(ClosureEventTriggeringInterceptor.AFTER_LOAD_EVENT, domainClazz);
    postInsertEventListener = buildCaller(ClosureEventTriggeringInterceptor.AFTER_INSERT_EVENT, domainClazz);
    postUpdateEventListener = buildCaller(ClosureEventTriggeringInterceptor.AFTER_UPDATE_EVENT, domainClazz);
    postDeleteEventListener = buildCaller(ClosureEventTriggeringInterceptor.AFTER_DELETE_EVENT, domainClazz);
    preDeleteEventListener = buildCaller(ClosureEventTriggeringInterceptor.BEFORE_DELETE_EVENT, domainClazz);
    preUpdateEventListener = buildCaller(ClosureEventTriggeringInterceptor.BEFORE_UPDATE_EVENT, domainClazz);

    beforeValidateEventListener = new BeforeValidateEventTriggerCaller(domainClazz, domainMetaClass);

    if (failOnErrorPackages.size() > 0) {
        failOnErrorEnabled = GrailsClassUtils.isClassBelowPackage(domainClazz, failOnErrorPackages);
    } else {
        failOnErrorEnabled = failOnError;
    }

    validateParams = new HashMap();
    validateParams.put(AbstractHibernateGormValidationApi.ARGUMENT_DEEP_VALIDATE, Boolean.FALSE);

    errorsProperty = domainMetaClass.getMetaProperty(GrailsDomainClassProperty.ERRORS);

    validateMethod = domainMetaClass.getMetaMethod("validate", new Object[] { Map.class });

    try {
        actionQueueUpdatesField = ReflectionUtils.findField(ActionQueue.class, "updates");
        actionQueueUpdatesField.setAccessible(true);
        entityUpdateActionStateField = ReflectionUtils.findField(EntityUpdateAction.class, "state");
        entityUpdateActionStateField.setAccessible(true);
    } catch (Exception e) {
        // ignore
    }
}

From source file:org.jtalks.jcommune.web.exception.PrettyLogExceptionResolverTest.java

private Log replaceLoggerWithMock(PrettyLogExceptionResolver resolver) throws Exception {
    Log mockLog = mock(Log.class);
    Field loggerField = ReflectionUtils.findField(PrettyLogExceptionResolver.class, "logger");
    assert loggerField != null;
    loggerField.setAccessible(true);/*from  w  ww  .j  a  v a  2 s .c o m*/
    loggerField.set(resolver, mockLog);
    return mockLog;
}

From source file:org.lexevs.cache.AbstractMethodCachingBean.java

protected String getArgumentKey(Object argument, ParameterKey key) {
    if (key != null) {
        StringBuffer sb = new StringBuffer();
        String[] fields = key.field();
        for (String fieldName : fields) {
            Field field = ReflectionUtils.findField(argument.getClass(), fieldName);
            field.setAccessible(true);// w w w.j ava 2  s.c o  m

            Object fieldArg = ReflectionUtils.getField(field, argument);
            sb.append(getArgumentKey(fieldArg));
        }
        return sb.toString();
    } else {
        return getArgumentKey(argument);
    }
}

From source file:org.opentestsystem.delivery.testreg.domain.constraintvalidators.AtLeastOneAccommodationValidator.java

@Override
public boolean isValid(final Accommodation accommodation, final ConstraintValidatorContext context) {

    // at least one of the accommodations within the Accommodation object must be non-null

    Object value = null;//from   w  w  w  .  j a v a  2  s  . co  m

    for (String property : properties) {
        try {
            /*
             * Uses Field access to avoid using getter which throws IllegalArgumentException for unknown values for
             * Enum lookups.
             */
            Field field = ReflectionUtils.findField(Accommodation.class, property);
            if (field != null) {
                field.setAccessible(true);
                value = field.get(accommodation);
            }

        } catch (IllegalArgumentException | IllegalAccessException e) {
        } finally {
            if (value != null && isNotEmpty(value.toString().trim())) {
                return true;
            }
        }
    }
    /*
     * This validation is a cross-field( @ClassLevel) validation. We need to pass a field for the constraint
     * processor to lookup an invalid value. So lets add a node to the message.
     */
    addConstraintViolation(context);
    return false;
}

From source file:org.springframework.batch.integration.chunk.RemoteChunkHandlerFactoryBean.java

private static Object getField(Object target, String name) {
    Assert.notNull(target, "Target object must not be null");
    Field field = ReflectionUtils.findField(target.getClass(), name);
    if (field == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Could not find field [" + name + "] on target [" + target + "]");
        }/*  ww w .j  a  v  a2 s  . com*/
        return null;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Getting field [" + name + "] from target [" + target + "]");
    }
    ReflectionUtils.makeAccessible(field);
    return ReflectionUtils.getField(field, target);
}

From source file:org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer.java

@SuppressWarnings("unchecked")
private List<BoundChannel> extractChannels() {
    Field channelsField = ReflectionUtils.findField(Undertow.class, "channels");
    ReflectionUtils.makeAccessible(channelsField);
    return (List<BoundChannel>) ReflectionUtils.getField(channelsField, this.undertow);
}

From source file:org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainer.java

private Port getPortFromChannel(BoundChannel channel) {
    String protocol = ReflectionUtils.findField(channel.getClass(), "ssl") != null ? "https" : "http";
    SocketAddress socketAddress = channel.getLocalAddress();
    if (socketAddress instanceof InetSocketAddress) {
        return new Port(((InetSocketAddress) socketAddress).getPort(), protocol);
    }// w w w . j a v a 2s .  c  o  m
    return null;
}

From source file:org.springframework.boot.context.embedded.undertow.UndertowWebServer.java

private UndertowWebServer.Port getPortFromChannel(BoundChannel channel) {
    SocketAddress socketAddress = channel.getLocalAddress();
    if (socketAddress instanceof InetSocketAddress) {
        String protocol = ReflectionUtils.findField(channel.getClass(), "ssl") != null ? "https" : "http";
        return new UndertowWebServer.Port(((InetSocketAddress) socketAddress).getPort(), protocol);
    }/*  w ww. j a v a  2  s. co m*/
    return null;
}

From source file:org.springframework.boot.context.embedded.undertow.UndertowWebServer.java

@SuppressWarnings("unchecked")
private List<Object> extractListeners() {
    Field listenersField = ReflectionUtils.findField(Undertow.class, "listeners");
    ReflectionUtils.makeAccessible(listenersField);
    return (List<Object>) ReflectionUtils.getField(listenersField, this.undertow);
}