Example usage for java.lang.reflect Field getInt

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public int getInt(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

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

Usage

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

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

    try {/*from w  w w  .j  a va 2s.c  o 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.pentaho.platform.engine.security.acls.PentahoAclEntry.java

private static void initializePermissionsArray() {
    if (null == PentahoAclEntry.validPermissions) {
        int maxPower = -1;
        Field[] fields = IPentahoAclEntry.class.getDeclaredFields();
        for (Field field : fields) {
            // if field is public static final int
            if (int.class == field.getType() && Modifier.isPublic(field.getModifiers())
                    && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())
                    && field.getName().startsWith(PERMISSION_PREFIX)) {
                if (PentahoAclEntry.logger.isDebugEnabled()) {
                    PentahoAclEntry.logger.debug("Candidate field: " + field.getName()); //$NON-NLS-1$
                }// w ww.  j  a  va  2 s  . co m

                // power of two (0-based)
                double powerOfTwo = -1;
                try {
                    powerOfTwo = Math.log(field.getInt(null)) / Math.log(2);
                } catch (IllegalArgumentException e) {
                    //ignore
                } catch (IllegalAccessException e) {
                    //ignore
                }
                // if log calculation results in an integer
                if (powerOfTwo == (int) powerOfTwo) {
                    if (powerOfTwo > maxPower) {
                        if (PentahoAclEntry.logger.isDebugEnabled()) {
                            PentahoAclEntry.logger.debug("Found new power of two."); //$NON-NLS-1$
                        }
                        maxPower = (int) powerOfTwo;
                    }
                }
            }
        }
        if (PentahoAclEntry.logger.isDebugEnabled()) {
            PentahoAclEntry.logger.debug("Max power of two: " + maxPower); //$NON-NLS-1$
        }

        int numberOfPermutations = (int) Math.pow(2, maxPower + 1);

        PentahoAclEntry.validPermissions = new int[numberOfPermutations + 1];
        for (int i = 0; i < numberOfPermutations; i++) {
            PentahoAclEntry.validPermissions[i] = i;
        }
        PentahoAclEntry.validPermissions[PentahoAclEntry.validPermissions.length
                - 1] = PentahoAclEntry.PERM_FULL_CONTROL;
    }
}

From source file:FindField.java

int intFieldValue(Object o, String name) throws NoSuchFieldException, IllegalAccessException {
    Class c = o.getClass();//ww  w  .  j a  va  2  s. c  om
    Field fld = c.getField(name);
    int value = fld.getInt(o);
    return value;
}

From source file:com.mobicage.rogerthat.util.ui.UIUtils.java

private static void colorEditText(EditText view, String fieldNameRes, String fieldNameDrawable, int color,
        boolean isArray) {
    try {/*from w w w  .  j  av  a 2  s . co  m*/
        // Get the cursor resource id
        final Field field = TextView.class.getDeclaredField(fieldNameRes);
        field.setAccessible(true);
        int drawableCursorResId = field.getInt(view);

        // Get the editor
        final Field editor = TextView.class.getDeclaredField("mEditor");
        editor.setAccessible(true);
        Object cursorEditor = editor.get(view);

        // Get the drawable and set a color filter
        Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableCursorResId);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);

        // Set the drawables
        final Field drawableField = cursorEditor.getClass().getDeclaredField(fieldNameDrawable);
        drawableField.setAccessible(true);
        if (isArray) {
            Drawable[] drawables = { drawable, drawable };
            drawableField.set(cursorEditor, drawables);
        } else {
            drawableField.set(cursorEditor, drawable);
        }
    } catch (IllegalAccessException e) {
        L.bug(e);
    } catch (NoSuchFieldException e) {
        L.e("NoSuchFieldException for " + fieldNameRes, e);
    }
}

From source file:org.apache.lens.cube.parse.HQLParser.java

public static Map<Integer, String> getHiveTokenMapping() throws Exception {
    Map<Integer, String> mapping = new HashMap<>();

    for (Field f : HiveParser.class.getFields()) {
        if (f.getType() == int.class) {
            Integer tokenId = f.getInt(null);
            String token = f.getName();
            mapping.put(tokenId, token);
        }/*from   w  ww  .  j  av a2  s.  co  m*/
    }

    return mapping;
}

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

/**
 * Return the value of the given field in the given object.
 *///from ww w. ja  va 2s.c om
public static int getInt(Object target, Field field) {
    if (target == null || field == null)
        return 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getInt(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("get-field", target, field));
    }
}

From source file:org.evosuite.regression.ObjectFields.java

private static Object getFieldValue(Field field, Object p) {
    try {//from ww w.  j a v a2  s .c  o  m
        /*Class objClass = p.getClass();
        if(p instanceof java.lang.String){
           ((String) p).hashCode();
        }*/
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (fieldType.isPrimitive()) {
            if (fieldType.equals(Boolean.TYPE)) {
                return field.getBoolean(p);
            }
            if (fieldType.equals(Integer.TYPE)) {
                return field.getInt(p);
            }
            if (fieldType.equals(Byte.TYPE)) {
                return field.getByte(p);
            }
            if (fieldType.equals(Short.TYPE)) {
                return field.getShort(p);
            }
            if (fieldType.equals(Long.TYPE)) {
                return field.getLong(p);
            }
            if (fieldType.equals(Double.TYPE)) {
                return field.getDouble(p);
            }
            if (fieldType.equals(Float.TYPE)) {
                return field.getFloat(p);
            }
            if (fieldType.equals(Character.TYPE)) {
                return field.getChar(p);
            }
            throw new UnsupportedOperationException("Primitive type " + fieldType + " not implemented!");
        }
        return field.get(p);
    } catch (IllegalAccessException exc) {
        throw new RuntimeException(exc);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (MAX_RECURSION != 0)
            MAX_RECURSION = 0;
        else
            throw new RuntimeErrorException(e);
        return getFieldValue(field, p);
    }
}

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

/**
 * Return the value of the given field in the given object.
 *//*  w w  w .  j  av a  2 s .com*/
public static int getInt(Object target, Field field) {
    if (target == null || field == null)
        return 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getInt(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:uk.ac.horizon.artcodes.fragment.FeatureListFragment.java

@Nullable
@Override/*w  w  w.j a  v  a  2 s  .  co m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ListBinding binding = ListBinding.inflate(inflater, container, false);
    final FeatureAdapter adapter = new FeatureAdapter(getActivity());
    binding.setAdapter(adapter);

    final R.bool features = new R.bool();
    final Field[] fields = features.getClass().getDeclaredFields();
    for (Field field : fields) {
        try {
            addFeature(adapter, field.getInt(features));
        } catch (Exception e) {
            GoogleAnalytics.trackException(e);
        }
    }

    final uk.ac.horizon.artcodes.scanner.R.bool scannerFeatures = new uk.ac.horizon.artcodes.scanner.R.bool();
    final Field[] scannerFields = scannerFeatures.getClass().getDeclaredFields();
    for (Field field : scannerFields) {
        try {
            addFeature(adapter, field.getInt(features));
        } catch (Exception e) {
            GoogleAnalytics.trackException(e);
        }
    }

    return binding.getRoot();
}

From source file:darwin.geometrie.io.json.JsonModelReader.java

private Model convert(ModelBean bean) throws IOException {
    int primitiv_typ;
    try {/*from  ww w . j  ava2 s .  c o m*/
        Field field = GL2.class.getField(bean.type);
        primitiv_typ = field.getInt(null);
    } catch (Throwable ex) {
        throw new IOException("No known geometry type: " + bean.type);
    }
    Element[] elements = new Element[bean.data.length];
    for (int i = 0; i < elements.length; i++) {
        try {
            elements[i] = convertElement(bean.data[i]);
        } catch (IllegalArgumentException ex) {
            throw new IOException(
                    "One data element of the model could not get converted!" + bean.data[i].element.tag);
        }
    }
    DataLayout layout = new DataLayout(elements);
    VertexBuffer vb = new VertexBuffer(layout, bean.getVertexCount());
    vb.fullyInitialize();

    for (int i = 0; i < elements.length; i++) {
        vb.copyInto(0, new VertexBuffer(elements[i], bean.data[i].values));
    }

    Mesh m = new Mesh(bean.indices, vb, primitiv_typ);
    return new Model(m, null);
}