Example usage for java.lang.reflect Field getLong

List of usage examples for java.lang.reflect Field getLong

Introduction

In this page you can find the example usage for java.lang.reflect Field getLong.

Prototype

@CallerSensitive
@ForceInline 
public long getLong(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Gets the value of a static or instance field of type long or of another primitive type convertible to type long via a widening conversion.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getLong(x));

    f.setLong(x, 9L);//from  w  ww.  j  a  va2 s  . c  om
    System.out.println(f.getLong(x));

}

From source file:Tweedle.java

public static void main(String... args) {
    Book book = new Book();
    String fmt = "%6S:  %-12s = %s%n";

    try {//from   w ww. j a v a 2s .c o  m
        Class<?> c = book.getClass();

        Field chap = c.getDeclaredField("chapters");
        out.format(fmt, "before", "chapters", book.chapters);
        chap.setLong(book, 12);
        out.format(fmt, "after", "chapters", chap.getLong(book));

        Field chars = c.getDeclaredField("characters");
        out.format(fmt, "before", "characters", Arrays.asList(book.characters));
        String[] newChars = { "Queen", "King" };
        chars.set(book, newChars);
        out.format(fmt, "after", "characters", Arrays.asList(book.characters));

        Field t = c.getDeclaredField("twin");
        out.format(fmt, "before", "twin", book.twin);
        t.set(book, Tweedle.DUM);
        out.format(fmt, "after", "twin", t.get(book));

        // production code should handle these exceptions more gracefully
    } catch (NoSuchFieldException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:com.alibaba.wasp.ipc.WaspRPC.java

static long getProtocolVersion(Class<? extends VersionedProtocol> protocol)
        throws NoSuchFieldException, IllegalAccessException {
    Field versionField = protocol.getField("VERSION");
    versionField.setAccessible(true);/*  w  ww  .j  a v a 2 s . com*/
    return versionField.getLong(protocol);
}

From source file:Main.java

public static Object getStaticField(Class clz, String fieldName, int type) {
    if (null != clz) {
        try {/*  w w  w . ja  v  a  2s  .com*/
            Field field = clz.getField(fieldName);
            switch (type) {
            case TYPE_OBJECT:
                return field.get(clz);
            case TYPE_INT:
                return field.getInt(clz);
            case TYPE_SHORT:
                return field.getShort(clz);
            case TYPE_BYTE:
                return field.getByte(clz);
            case TYPE_BOOLEAN:
                return field.getBoolean(clz);
            case TYPE_FLOAT:
                return field.getFloat(clz);
            case TYPE_LONG:
                return field.getLong(clz);
            case TYPE_DOUBLE:
                return field.getDouble(clz);
            default:
                return field.get(clz);
            }
        } catch (Exception e) {
        }
        return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type));
    }
    return getDefault(type);
}

From source file:Main.java

public static ContentValues objectToContentValues(Object object) {
    if (object == null) {
        throw new IllegalArgumentException("please check your argument");
    }//from   w ww. j  a  v  a  2  s .c om
    ContentValues contentValues = new ContentValues();
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            Type type = field.getType();
            field.setAccessible(true);
            if (type.equals(String.class)) {
                contentValues.put(fieldName, field.get(object).toString());
            } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
                contentValues.put(fieldName, field.getInt(object));
            } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
                contentValues.put(fieldName, field.getFloat(object));
            } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
                contentValues.put(fieldName, field.getLong(object));
            } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
                contentValues.put(fieldName, field.getDouble(object));
            } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
                contentValues.put(fieldName, field.getBoolean(object));
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        e.printStackTrace();
    }
    return contentValues;
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static String get(Object obj, Field field) {

    try {/*  w  w w .j a  va 2 s  .  co  m*/
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }

        if (field.getType() == int.class) {
            return String.valueOf(field.getInt(obj));

        } else if (field.getType() == long.class) {
            return String.valueOf(field.getLong(obj));

        } else if (field.getType() == double.class) {
            return String.valueOf(field.getDouble(obj));
        } else if (field.getType() == byte.class) {
            return String.valueOf(field.getByte(obj));
        } else if (field.getType() == boolean.class) {
            return String.valueOf(field.getBoolean(obj));
        } else if (field.getType() == String.class) {
            if (field.get(obj) == null) {
                return "";
            }
            return String.valueOf(field.get(obj));
        } else if (field.getType() == Date.class) {
            if (field.get(obj) == null) {
                return "";
            }
            return MiscDateUtils.getDateTime((Date) field.get(obj));
        }

    } catch (Exception e) {

    }
    return "";
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Return the value of the given field in the given object.
 *//*from  www  . ja v  a 2 s  . c o m*/
public static long getLong(Object target, Field field) {
    if (target == null || field == null)
        return 0L;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getLong(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("get-field", target, field));
    }
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Return the value of the given field in the given object.
 *///w  w  w .  j  a  va2s  . c om
public static long getLong(Object target, Field field) {
    if (target == null || field == null)
        return 0L;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getLong(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:hivemall.dataset.LogisticRegressionDataGeneratorUDTFWrapper.java

@Override
public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
    try {//from   w w w  . ja  va 2  s .c  o  m
        // Extract a collector for LogisticRegressionDataGeneratorUDTF
        Field collector = GenericUDTF.class.getDeclaredField("collector");
        collector.setAccessible(true);
        udtf.setCollector((Collector) collector.get(this));

        // To avoid HadoopUtils#getTaskId()
        Class<?> clazz = udtf.getClass();
        Field rnd1 = clazz.getDeclaredField("rnd1");
        Field rnd2 = clazz.getDeclaredField("rnd2");
        Field r_seed = clazz.getDeclaredField("r_seed");
        r_seed.setAccessible(true);
        final long seed = r_seed.getLong(udtf) + (int) Thread.currentThread().getId();
        rnd1.setAccessible(true);
        rnd2.setAccessible(true);
        rnd1.set(udtf, new Random(seed));
        rnd2.set(udtf, new Random(seed + 1));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return udtf.initialize(argOIs);
}

From source file:org.powertac.samplebroker.core.MessageDispatcher.java

private boolean validateId(Object thing) {
    try {/*from w  ww  .  j av a2s .c o  m*/
        Field idField = thing.getClass().getDeclaredField("id");
        idField.setAccessible(true);
        long value = idField.getLong(thing);
        if (IdGenerator.getPrefix() != IdGenerator.extractPrefix(value)) {
            log.error("Invalid id value " + value + " in message " + thing.toString());
            return false;
        }
    } catch (NoSuchFieldException e) {
        // no id field, OK to send
        return true;
    } catch (SecurityException e) {
        // Should not happen
        log.error("Exception accessing id field: " + e.toString());
    } catch (IllegalArgumentException e) {
        // Should not happen
        log.error("Exception reading id field: " + e.toString());
    } catch (IllegalAccessException e) {
        // Should not happen
        log.error("Exception reading id field: " + e.toString());
    }
    return true;
}