Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:info.papdt.blacklight.support.Utility.java

public static void setActionBarTranslation(Activity activity, float y) {
    ViewGroup vg = (ViewGroup) activity.findViewById(android.R.id.content).getParent();
    int count = vg.getChildCount();

    if (DEBUG) {//from   w ww. j av a2 s. co m
        Log.d(TAG, "==========================");
    }

    // Get the class of action bar
    Class<?> actionBarContainer = null;
    Field isSplit = null;

    try {
        actionBarContainer = Class.forName("com.android.internal.widget.ActionBarContainer");
        isSplit = actionBarContainer.getDeclaredField("mIsSplit");
        isSplit.setAccessible(true);
    } catch (Exception e) {
        if (DEBUG) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    }

    for (int i = 0; i < count; i++) {
        View v = vg.getChildAt(i);

        if (v.getId() != android.R.id.content) {
            if (DEBUG) {
                Log.d(TAG, "Found View: " + v.getClass().getName());
            }

            try {
                if (actionBarContainer.isInstance(v)) {
                    if (DEBUG) {
                        Log.d(TAG, "Found ActionBarContainer");
                    }

                    if (isSplit.getBoolean(v)) {
                        if (DEBUG) {
                            Log.d(TAG, "Found Split Action Bar");
                        }

                        continue;
                    }
                }
            } catch (Exception e) {
                if (DEBUG) {
                    Log.e(TAG, Log.getStackTraceString(e));
                }
            }

            v.setTranslationY(y);
        }
    }

    if (DEBUG) {
        Log.d(TAG, "==========================");
    }
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Get the value of a declared field on an object
 *
 * @param obj/*w  w  w .jav a 2s . co m*/
 * @param name
 * @return The object value or null if there is no such field or access problems
 */
public static Object getFieldValue(Object obj, String name) {
    Class<?> clazz = obj.getClass();
    try {
        Field f = clazz.getDeclaredField(name);
        return f.get(obj);
    } catch (Exception e) {
        return null;
    }
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Work out if the specified object has a public field with the name supplied.
 *
 * @param obj//ww w .j  ava 2 s  .  com
 * @param name
 * @return true if a public field with the name exists
 */
public static boolean isPublicField(Object obj, String name) {
    Class<?> clazz = obj.getClass();
    try {
        Field f = clazz.getDeclaredField(name);
        return Modifier.isPublic(f.getModifiers());
    } catch (NoSuchFieldException e) {
        return false;
    }
}

From source file:forge.quest.io.QuestDataIO.java

private static <T> void setFinalField(final Class<T> clasz, final String fieldName, final T instance,
        final Object newValue) throws IllegalAccessException, NoSuchFieldException {
    final Field field = clasz.getDeclaredField(fieldName);
    field.setAccessible(true);/*from   ww  w.j a  v  a  2 s.  c  o m*/
    field.set(instance, newValue); // no difference here (used only to set
                                   // initial lives)
}

From source file:com.puppycrawl.tools.checkstyle.internal.XdocsPagesTest.java

private static Field getField(Class<?> clss, String propertyName) {
    Field result = null;/* ww w  .j ava  2  s .com*/

    if (clss != null) {
        try {
            result = clss.getDeclaredField(propertyName);
            result.setAccessible(true);
        } catch (NoSuchFieldException ex) {
            result = getField(clss.getSuperclass(), propertyName);
        }
    }

    return result;
}

From source file:Main.java

private static boolean setProxyPreICS(WebView webView, String host, final int port) {
    HttpHost proxyServer;//from   w w w .  ja  v a 2s.  co m
    if (null == host) {
        proxyServer = null;
    } else {
        proxyServer = new HttpHost(host, port);
    }
    Class networkClass = null;
    Object network = null;
    try {
        networkClass = Class.forName("android.webkit.Network");
        if (networkClass == null) {
            Log.e(LOG_TAG, "failed to get class for android.webkit.Network");
            return false;
        }
        Method getInstanceMethod = networkClass.getMethod("getInstance", Context.class);
        if (getInstanceMethod == null) {
            Log.e(LOG_TAG, "failed to get getInstance method");
        }
        network = getInstanceMethod.invoke(networkClass, new Object[] { webView.getContext() });
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting network: " + ex);
        return false;
    }
    if (network == null) {
        Log.e(LOG_TAG, "error getting network: network is null");
        return false;
    }
    Object requestQueue = null;
    try {
        Field requestQueueField = networkClass.getDeclaredField("mRequestQueue");
        requestQueue = getFieldValueSafely(requestQueueField, network);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting field value");
        return false;
    }
    if (requestQueue == null) {
        Log.e(LOG_TAG, "Request queue is null");
        return false;
    }
    Field proxyHostField = null;
    try {
        Class requestQueueClass = Class.forName("android.net.http.RequestQueue");
        proxyHostField = requestQueueClass.getDeclaredField("mProxyHost");
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error getting proxy host field");
        return false;
    }

    boolean temp = proxyHostField.isAccessible();
    try {
        proxyHostField.setAccessible(true);
        proxyHostField.set(requestQueue, proxyServer);
    } catch (Exception ex) {
        Log.e(LOG_TAG, "error setting proxy host");
    } finally {
        proxyHostField.setAccessible(temp);
    }
    Log.d(LOG_TAG, "Setting proxy with <= 3.2 API successful!");
    return true;
}

From source file:com.eharmony.matching.seeking.translator.solr.SolrjPropertyResolver.java

@Override
public String resolve(String fieldName, Class<?> entityClass) {
    try {/*  www  .j av  a2s.com*/
        java.lang.reflect.Field field = entityClass.getDeclaredField(fieldName);
        if (field.isAnnotationPresent(Field.class)) {
            return field.getAnnotation(Field.class).value();
        }
    } catch (Exception e) {
        log.warn("Unabled to resolve name for field: " + fieldName, e);
    }
    return fieldName;
}

From source file:com.seer.datacruncher.streams.ZipStreamTest.java

private boolean isStreamClose(ZipInputStream inStream) {
    try {//  ww w. j  ava2 s. c  o  m
        Class<?> c = inStream.getClass();
        Field in;
        in = c.getDeclaredField("closed");
        in.setAccessible(true);
        Boolean inReader = (Boolean) in.get(inStream);
        return inReader;
    } catch (Exception e) {
        logger.error(e);
    }
    return false;
}

From source file:org.jsonschema2pojo.integration.JacksonViewIT.java

private Annotation jsonViewTest(String annotationStyle, Class<? extends Annotation> annotationType)
        throws ClassNotFoundException, NoSuchFieldException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/views/views.json", "com.example",
            config("annotationStyle", annotationStyle));

    Class<?> generatedType = resultsClassLoader.loadClass("com.example.Views");
    Field fieldInView = generatedType.getDeclaredField("inView");
    assertThat(fieldInView.getAnnotation(annotationType), notNullValue());

    Field fieldNotInView = generatedType.getDeclaredField("notInView");
    assertThat(fieldNotInView.getAnnotation(annotationType), nullValue());

    return fieldInView.getAnnotation(annotationType);
}

From source file:hivemall.dataset.LogisticRegressionDataGeneratorUDTFWrapper.java

@Override
public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
    try {/*from w  w w .jav  a2  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);
}