Example usage for java.lang.reflect Field get

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

Introduction

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

Prototype

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

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

From source file:Main.java

public static Object getFieldValue(Object object, String fieldName) {
    try {/* w w w. j  a v  a 2 s. c  om*/
        final Field field = getField(object.getClass(), fieldName);
        if (field == null) {
            throw new IllegalArgumentException("No field '" + fieldName + "' found in "
                    + object.getClass().getName() + " or its super classes");
        }
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        return field.get(object);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.keystore_explorer.crypto.jcepolicy.JcePolicyUtil.java

/**
 * Hack to disable crypto restrictions until Java 9 is out.
 *
 * See http://stackoverflow.com/a/22492582/2672392
 *//*from  www  . ja v  a 2  s  . c  o m*/
public static void removeRestrictions() {
    try {
        Class<?> jceSecurityClass = Class.forName("javax.crypto.JceSecurity");
        Class<?> cryptoPermissionsClass = Class.forName("javax.crypto.CryptoPermissions");
        Class<?> cryptoAllPermissionClass = Class.forName("javax.crypto.CryptoAllPermission");

        Field isRestrictedField = jceSecurityClass.getDeclaredField("isRestricted");
        isRestrictedField.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(isRestrictedField, isRestrictedField.getModifiers() & ~Modifier.FINAL);
        isRestrictedField.set(null, false);

        Field defaultPolicyField = jceSecurityClass.getDeclaredField("defaultPolicy");
        defaultPolicyField.setAccessible(true);
        PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null);

        Field permsField = cryptoPermissionsClass.getDeclaredField("perms");
        permsField.setAccessible(true);
        ((Map<?, ?>) permsField.get(defaultPolicy)).clear();

        Field cryptoAllPermissionInstanceField = cryptoAllPermissionClass.getDeclaredField("INSTANCE");
        cryptoAllPermissionInstanceField.setAccessible(true);
        defaultPolicy.add((Permission) cryptoAllPermissionInstanceField.get(null));
    } catch (Exception e) {
        // ignore
    }
}

From source file:in.mycp.utils.Commons.java

public static List getAllJbpmProcDefNames() {
    // public static final String JBPM_PROC_DEF_NAME_FVC_BILL = "fvc bill";
    Field[] allFields = Commons.class.getFields();
    List<String> fieldList = new ArrayList<String>();
    for (int i = 0; i < allFields.length; i++) {
        Field eachField = allFields[i];
        if (eachField.getName().startsWith("JBPM_PROC_DEF_NAME")) {
            try {
                fieldList.add((String) eachField.get(Commons.class));
            } catch (Exception e) {
            }//from www . jav  a 2 s  .  c  o  m
        }
    }
    return fieldList;
}

From source file:com.searchbox.core.ref.ReflectionUtils.java

public static void copyAllFields(Object from, Object to) {
    for (Field fromField : findAllFields(from.getClass())) {
        try {/*w w w .j a va 2 s  .c om*/
            Field toField = findUnderlying(to.getClass(), fromField.getName());
            if (toField != null) {
                toField.setAccessible(true);
                fromField.setAccessible(true);
                toField.set(to, fromField.get(from));
            }
        } catch (Exception e) {
            LOGGER.warn("Could not copye Fields.", e);
        }
    }
}

From source file:com.harshad.linconnectclient.NotificationUtilities.java

@SuppressLint("DefaultLocale")
public static ArrayList<String> getNotificationText(Notification notification) {
    RemoteViews views = notification.contentView;
    Class<?> secretClass = views.getClass();

    try {//from   w  ww  .j ava2  s  .co m
        ArrayList<String> notificationData = new ArrayList<String>();

        Field outerFields[] = secretClass.getDeclaredFields();
        for (int i = 0; i < outerFields.length; i++) {
            if (!outerFields[i].getName().equals("mActions"))
                continue;

            outerFields[i].setAccessible(true);

            @SuppressWarnings("unchecked")
            ArrayList<Object> actions = (ArrayList<Object>) outerFields[i].get(views);
            for (Object action : actions) {
                Field innerFields[] = action.getClass().getDeclaredFields();

                Object value = null;
                for (Field field : innerFields) {
                    field.setAccessible(true);
                    // Value field could possibly contain text
                    if (field.getName().equals("value")) {
                        value = field.get(action);
                    }
                }

                // Check if value is a String
                if (value != null && value.getClass().getName().toUpperCase().contains("STRING")) {

                    notificationData.add(value.toString());
                }
            }

            return notificationData;
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:com.ms.commons.test.common.ReflectUtil.java

public static Object getValueFromBean(Object bean, String field) {
    Class<?> clazz = bean.getClass();
    try {/*from   w  w w  .jav  a 2 s.c  om*/
        Field f = getDeclaredField(clazz, NamingUtil.dbNameToJavaName(field));
        f.setAccessible(true);
        try {
            return f.get(bean);
        } catch (IllegalArgumentException e) {
            throw new UnknowException(e);
        } catch (IllegalAccessException e) {
            throw new UnknowException(e);
        }
    } catch (SecurityException e) {
        throw new UnknowException(e);
    } catch (NoSuchFieldException e) {
        throw new JavaFieldNotFoundException(clazz, field);
    }
}

From source file:net.ljcomputing.gson.converter.impl.GsonConverterServiceImpl.java

/**
 * Sets the field values./*from ww w .  j av  a2 s .c  o m*/
 *
 * @param to the to
 * @param fieldTo the field to
 * @param from the from
 * @param fieldFrom the field from
 * @throws IllegalArgumentException the illegal argument exception
 * @throws IllegalAccessException the illegal access exception
 */
private static void setFieldValues(final Object to, final Field fieldTo, final Object from,
        final Field fieldFrom) throws IllegalArgumentException, IllegalAccessException {
    if (null != fieldTo && null != fieldFrom) {
        fieldTo.setAccessible(true);
        fieldFrom.setAccessible(true);
        fieldTo.set(to, fieldFrom.get(from));
    }
}

From source file:com.aw.support.reflection.MethodInvoker.java

public static List getAttributes(Object target) {
    logger.info("searching attributes " + target.getClass().getName());
    List attributes = new ArrayList();
    List<Field> forms = new ArrayList();
    Class cls = target.getClass();
    Field[] fields = cls.getFields();
    for (int i = 0; i < fields.length; i++) {
        if ((fields[i].getName().startsWith("txt") || fields[i].getName().startsWith("chk"))
                && !fields[i].getName().startsWith("chkSel")) {
            attributes.add(fields[i]);/*from  w  ww.  j av  a  2 s.  c  o  m*/
        }
        if ((fields[i].getType().getSimpleName().startsWith("Frm"))) {
            forms.add(fields[i]);
        }
    }
    if (forms.size() > 0) {
        for (Field field : forms) {
            try {
                Object formToBeChecked = field.get(target);
                if (formToBeChecked != null) {
                    List formAttributes = getAttributes(formToBeChecked);
                    if (formAttributes.size() > 0) {
                        attributes.addAll(formAttributes);
                    }
                } else {
                    logger.warn("FRM NULL:" + field.getName());
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new AWSystemException("Problems getting value for:<" + field.getName() + ">", e);
            }
        }
    }
    return attributes;
}

From source file:com.cons.gps2exif.exif.ReadMetaData.java

private static void printTagInfos(List<Field> tagfields, JpegImageMetadata jpegMetadata)
        throws IllegalAccessException, IllegalArgumentException, ImageReadException {
    for (Field field : tagfields) {
        printTagValue(jpegMetadata, (TagInfo) field.get(null));
    }// w  w  w  .  ja  v a 2  s  .  c  o  m
    System.out.println();
}

From source file:com.bjwg.back.util.ReflectionUtils.java

/**
 *//*from www.  j a  va2 s  . c  om*/
public static Object getFieldValue(final Object obj, final String fieldName) {
    Field field = getAccessibleField(obj, fieldName);

    if (field == null) {
        throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
    }

    Object result = null;
    try {
        result = field.get(obj);
    } catch (IllegalAccessException e) {
        //
    }
    return result;
}