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:com.ejisto.modules.web.util.FieldSerializationUtil.java

private static Object safeGet(Field f, Object container) {
    try {/*  w ww.j  a  v  a 2 s .c  o m*/
        f.setAccessible(true);
        return f.get(container);
    } catch (IllegalAccessException e) {
        log.log(Level.SEVERE, "unexpected exception", e);
        return null;
    }
}

From source file:Main.java

public static <C> C cloneObject(C original) {
    try {/*from w  w w  .j  av  a  2  s.  c  o m*/
        C clone = (C) original.getClass().newInstance();
        for (Field field : getAllFieldsValues(original.getClass())) {
            field.setAccessible(true);
            if (field.get(original) == null || Modifier.isFinal(field.getModifiers())) {
                continue;
            }
            if (field.getType().isPrimitive() || field.getType().equals(String.class)
                    || field.getType().getSuperclass().equals(Number.class)
                    || field.getType().equals(Boolean.class)) {
                field.set(clone, field.get(original));
            } else {
                Object childObj = field.get(original);
                if (childObj == original) {
                    field.set(clone, clone);
                } else {
                    field.set(clone, cloneObject(field.get(original)));
                }
            }
        }
        return clone;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.eyeq.pivot4j.sort.SortMode.java

/**
 * @param name/*from  w  w  w . j  av  a2s  .c o  m*/
 * @return
 */
public static SortMode fromName(String name) {
    Field[] fields = SortMode.class.getFields();

    for (Field field : fields) {
        Object value;

        try {
            value = field.get(null);
        } catch (Exception e) {
            throw new PivotException(e);
        }

        if (value instanceof SortMode) {
            SortMode mode = (SortMode) value;

            if (name.equals(mode.getName())) {
                return mode;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns a reference to the thread's threadLocals field
 * //from   ww  w. j  a  v a2  s.com
 * @param t
 * @return
 * @throws NoSuchFieldException 
 * @throws SecurityException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 */
private static Object getMap(Thread t)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field threadLocalField = Thread.class.getDeclaredField("threadLocals");
    threadLocalField.setAccessible(true);
    return threadLocalField.get(t);
}

From source file:com.netflix.spinnaker.kork.jedis.JedisHealthIndicatorFactory.java

public static HealthIndicator build(JedisClientDelegate client) {
    try {//from ww  w. jav  a  2  s  . c  o  m
        final JedisClientDelegate src = client;
        final Field clientAccess = JedisClientDelegate.class.getDeclaredField("jedisPool");
        clientAccess.setAccessible(true);

        return build((Pool<Jedis>) clientAccess.get(src));
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}

From source file:Main.java

/**
 * Dynamically load R.styleable.name//from   w ww  .  j  a  v  a 2  s  . co  m
 * @param context
 * @param name
 * @return
 */
public static int getStyleableResourceInt(Context context, String name) {
    if (context == null)
        return 0;
    try {
        //use reflection to access the resource class
        Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();

        //browse all fields
        for (Field f : fields2) {
            //pick matching field
            if (f.getName().equals(name)) {
                //return as int array
                int ret = (Integer) f.get(null);
                return ret;
            }
        }
    } catch (Throwable t) { /*no op*/
    }
    return 0;
}

From source file:Main.java

/*********************************************************************************
 *   Returns the resource-IDs for all attributes specified in the
 *   given <declare-styleable>-resource tag as an int array.
 *
 *   @param  context     The current application context.
 *   @param  name        The name of the <declare-styleable>-resource-tag to pick.
 *   @return             All resource-IDs of the child-attributes for the given
 *                       <declare-styleable>-resource or <code>null</code> if
 *                       this tag could not be found or an error occured.
 *********************************************************************************/
public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) {
    try {/* w  w w  .  j  a v a2s  .c o  m*/
        //use reflection to access the resource class
        Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields();

        //browse all fields
        for (Field f : fields2) {
            //pick matching field
            if (f.getName().equals(name)) {
                //return as int array
                int[] ret = (int[]) f.get(null);
                return ret;
            }
        }
    } catch (Throwable t) {
    }

    return null;
}

From source file:Main.java

private static int getExifOrientation(String src) throws IOException {
    int orientation = 1;

    try {/*from   w  w w  .j a va  2s.c om*/
        /**
         * if your are targeting only api level >= 5
         * ExifInterface exif = new ExifInterface(src);
         * orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
         */
        if (Build.VERSION.SDK_INT >= 5) {
            Class<?> exifClass = Class.forName("android.media.ExifInterface");
            Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class });
            Object exifInstance = exifConstructor.newInstance(new Object[] { src });
            Method getAttributeInt = exifClass.getMethod("getAttributeInt",
                    new Class[] { String.class, int.class });
            Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
            String tagOrientation = (String) tagOrientationField.get(null);
            orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1 });
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }

    return orientation;
}

From source file:Main.java

public static Drawable showUninstallAPKIcon(Context context, String apkPath) {
    String PATH_PackageParser = "android.content.pm.PackageParser";
    String PATH_AssetManager = "android.content.res.AssetManager";
    try {/*from   www .  j  a  v a2  s .co m*/
        Class<?> pkgParserCls = Class.forName(PATH_PackageParser);
        Class<?>[] typeArgs = new Class[1];
        typeArgs[0] = String.class;
        Constructor<?> pkgParserCt = pkgParserCls.getConstructor(typeArgs);
        Object[] valueArgs = new Object[1];
        valueArgs[0] = apkPath;
        Object pkgParser = pkgParserCt.newInstance(valueArgs);
        DisplayMetrics metrics = new DisplayMetrics();
        metrics.setToDefaults();
        typeArgs = new Class[4];
        typeArgs[0] = File.class;
        typeArgs[1] = String.class;
        typeArgs[2] = DisplayMetrics.class;
        typeArgs[3] = Integer.TYPE;
        Method pkgParser_parsePackageMtd = pkgParserCls.getDeclaredMethod("parsePackage", typeArgs);
        valueArgs = new Object[4];
        valueArgs[0] = new File(apkPath);
        valueArgs[1] = apkPath;
        valueArgs[2] = metrics;
        valueArgs[3] = 0;
        Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(pkgParser, valueArgs);
        Field appInfoFld = pkgParserPkg.getClass().getDeclaredField("applicationInfo");
        ApplicationInfo info = (ApplicationInfo) appInfoFld.get(pkgParserPkg);
        Class<?> assetMagCls = Class.forName(PATH_AssetManager);
        Constructor<?> assetMagCt = assetMagCls.getConstructor((Class[]) null);
        Object assetMag = assetMagCt.newInstance((Object[]) null);
        typeArgs = new Class[1];
        typeArgs[0] = String.class;
        Method assetMag_addAssetPathMtd = assetMagCls.getDeclaredMethod("addAssetPath", typeArgs);
        valueArgs = new Object[1];
        valueArgs[0] = apkPath;
        assetMag_addAssetPathMtd.invoke(assetMag, valueArgs);
        Resources res = context.getResources();
        typeArgs = new Class[3];
        typeArgs[0] = assetMag.getClass();
        typeArgs[1] = res.getDisplayMetrics().getClass();
        typeArgs[2] = res.getConfiguration().getClass();
        Constructor<?> resCt = Resources.class.getConstructor(typeArgs);
        valueArgs = new Object[3];
        valueArgs[0] = assetMag;
        valueArgs[1] = res.getDisplayMetrics();
        valueArgs[2] = res.getConfiguration();
        res = (Resources) resCt.newInstance(valueArgs);
        if (info.icon != 0) {
            Drawable icon = res.getDrawable(info.icon);
            return icon;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Object getStaticProperty(String paramString1, String paramString2) {
    setClass(paramString1);/*from   w  ww . ja v  a 2 s  . c  o  m*/
    Field localField = getField(paramString2);
    Object localObject1 = null;
    if (localField != null) {
        ;
    }

    try {
        Object localIllegalArgumentException = localField.get((Object) null);
        return localIllegalArgumentException;
    } catch (IllegalArgumentException var5) {
        return null;
    } catch (IllegalAccessException var6) {
        return null;
    }
}