Example usage for org.apache.commons.lang3.reflect FieldUtils getField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils getField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils getField.

Prototype

public static Field getField(final Class<?> cls, final String fieldName) 

Source Link

Document

Gets an accessible Field by name respecting scope.

Usage

From source file:com.github.daddo.validation.validator.Util.java

public static Object accessStaticFieldByName(Class<?> clasz, String fieldName) {
    try {/*from  w  w w  .ja va2 s  .  c o  m*/
        Field field = FieldUtils.getField(clasz, fieldName);
        return FieldUtils.readStaticField(field);
    } catch (IllegalAccessException ex) {
        throw new StaticFieldAccessException("Unable to access field " + clasz.getName() + "#" + fieldName, ex);
    } catch (IllegalArgumentException ex) {
        throw new StaticFieldAccessException("Unable to access field " + clasz.getName() + "#" + fieldName, ex);
    }
}

From source file:com.boozallen.cognition.ingest.storm.topology.ConfigurableIngestTopology.java

protected void configureStorm(Configuration conf, Config stormConf) throws IllegalAccessException {
    stormConf.registerSerialization(LogRecord.class);
    //stormConf.registerSerialization(Entity.class);
    stormConf.registerMetricsConsumer(LoggingMetricsConsumer.class);

    for (Iterator<String> iter = conf.getKeys(); iter.hasNext();) {
        String key = iter.next();

        String keyString = key.toString();
        String cleanedKey = keyString.replaceAll("\\.\\.", ".");

        String schemaFieldName = cleanedKey.replaceAll("\\.", "_").toUpperCase() + "_SCHEMA";
        Field field = FieldUtils.getField(Config.class, schemaFieldName);
        Object fieldObject = field.get(null);

        if (fieldObject == Boolean.class)
            stormConf.put(cleanedKey, conf.getBoolean(keyString));
        else if (fieldObject == String.class)
            stormConf.put(cleanedKey, conf.getString(keyString));
        else if (fieldObject == ConfigValidation.DoubleValidator)
            stormConf.put(cleanedKey, conf.getDouble(keyString));
        else if (fieldObject == ConfigValidation.IntegerValidator)
            stormConf.put(cleanedKey, conf.getInt(keyString));
        else if (fieldObject == ConfigValidation.PowerOf2Validator)
            stormConf.put(cleanedKey, conf.getLong(keyString));
        else if (fieldObject == ConfigValidation.StringOrStringListValidator)
            stormConf.put(cleanedKey, Arrays.asList(conf.getStringArray(keyString)));
        else if (fieldObject == ConfigValidation.StringsValidator)
            stormConf.put(cleanedKey, Arrays.asList(conf.getStringArray(keyString)));
        else {/*from w w  w .j  ava 2 s.c om*/
            logger.error(
                    "{} cannot be configured from XML. Consider configuring in navie storm configuration.");
            throw new UnsupportedOperationException(cleanedKey + " cannot be configured from XML");
        }
    }
}

From source file:lineage2.gameserver.scripts.Scripts.java

/**
 * Method callScripts.//from  ww  w .j a va2 s .c  om
 * @param caller Player
 * @param className String
 * @param methodName String
 * @param args Object[]
 * @param variables Map<String,Object>
 * @return Object
 */
public Object callScripts(Player caller, String className, String methodName, Object[] args,
        Map<String, Object> variables) {
    Object o;
    Class<?> clazz;

    clazz = _classes.get(className);
    if (clazz == null) {
        _log.error("Script class " + className + " not found!");
        return null;
    }

    try {
        o = clazz.newInstance();
    } catch (Exception e) {
        _log.error("Scripts: Failed creating instance of " + clazz.getName(), e);
        return null;
    }

    if ((variables != null) && !variables.isEmpty()) {
        for (Map.Entry<String, Object> param : variables.entrySet()) {
            try {
                FieldUtils.writeField(o, param.getKey(), param.getValue());
            } catch (Exception e) {
                _log.error("Scripts: Failed setting fields for " + clazz.getName(), e);
            }
        }
    }

    if (caller != null) {
        try {
            Field field = null;
            if ((field = FieldUtils.getField(clazz, "self")) != null) {
                FieldUtils.writeField(field, o, caller.getRef());
            }
        } catch (Exception e) {
            _log.error("Scripts: Failed setting field for " + clazz.getName(), e);
        }
    }

    Object ret = null;
    try {
        Class<?>[] parameterTypes = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++) {
            parameterTypes[i] = args[i] != null ? args[i].getClass() : null;
        }

        ret = MethodUtils.invokeMethod(o, methodName, args, parameterTypes);
    } catch (NoSuchMethodException nsme) {
        _log.error("Scripts: No such method " + clazz.getName() + "." + methodName + "()!");
    } catch (InvocationTargetException ite) {
        _log.error("Scripts: Error while calling " + clazz.getName() + "." + methodName + "()",
                ite.getTargetException());
    } catch (Exception e) {
        _log.error("Scripts: Failed calling " + clazz.getName() + "." + methodName + "()", e);
    }

    return ret;
}

From source file:lineage2.gameserver.Config.java

/**
 * Method getField./*from  www.  ja  va  2 s. c  o  m*/
 * @param fieldName String
 * @return String
 */
public static String getField(String fieldName) {
    Field field = FieldUtils.getField(Config.class, fieldName);
    if (field == null) {
        return null;
    }
    try {
        return String.valueOf(field.get(null));
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    }
    return null;
}

From source file:lineage2.gameserver.Config.java

/**
 * Method setField.// w w w .j  ava  2 s  . c o  m
 * @param fieldName String
 * @param value String
 * @return boolean
 */
public static boolean setField(String fieldName, String value) {
    Field field = FieldUtils.getField(Config.class, fieldName);
    if (field == null) {
        return false;
    }
    try {
        if (field.getType() == boolean.class) {
            field.setBoolean(null, BooleanUtils.toBoolean(value));
        } else if (field.getType() == int.class) {
            field.setInt(null, NumberUtils.toInt(value));
        } else if (field.getType() == long.class) {
            field.setLong(null, NumberUtils.toLong(value));
        } else if (field.getType() == double.class) {
            field.setDouble(null, NumberUtils.toDouble(value));
        } else if (field.getType() == String.class) {
            field.set(null, value);
        } else {
            return false;
        }
    } catch (IllegalArgumentException e) {
        return false;
    } catch (IllegalAccessException e) {
        return false;
    }
    return true;
}

From source file:org.evosuite.setup.TestAccessField.java

@Test
public void testPublicField() {
    Properties.CLASS_PREFIX = "some.package";
    Properties.TARGET_CLASS = "some.package.Foo";
    Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "publicField");
    boolean result = TestUsageChecker.canUse(f);
    Assert.assertTrue(result);/*from  w  w w  .  ja v a2s .co  m*/
}

From source file:org.evosuite.setup.TestAccessField.java

@Test
public void testPublicFieldTargetPackage() {
    Properties.CLASS_PREFIX = "com.examples.with.different.packagename";
    Properties.TARGET_CLASS = "com.examples.with.different.packagename.Foo";
    Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "publicField");
    boolean result = TestUsageChecker.canUse(f);
    Assert.assertTrue(result);//from   w  w  w .  ja  v  a 2 s . c om
}

From source file:org.lambdamatic.analyzer.ast.node.FieldAccess.java

/**
 * Returns the underlying Java field.//from  ww  w. j av a  2s  .c  o  m
 */
@Override
public Object getValue() {
    try {
        return FieldUtils.getField(this.source.getJavaType(), getFieldName());
    } catch (IllegalArgumentException e) {
        throw new AnalyzeException(
                "Cannot retrieve field named '" + this.fieldName + "' on class " + this.source.getJavaType(),
                e);
    }
}