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:com.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 *  Field,?? Class?.//from   ww w .  j a v  a  2 s .  com
 *
 * @param clz
 *            clz
 * @param fieldName
 *            ??
 * @return  Field ,?? Class ?
 * @see java.lang.Class#getDeclaredField(String)
 * @see org.apache.commons.lang3.reflect.FieldUtils#getDeclaredField(Class, String)
 * @see org.apache.commons.lang3.reflect.FieldUtils#getDeclaredField(Class, String, boolean)
 */
public static Field getDeclaredField(Class<?> clz, String fieldName) {
    try {
        return clz.getDeclaredField(fieldName);
    } catch (Exception e) {
        LOGGER.error(e.getClass().getName(), e);
        throw new ReflectException(e);
    }
}

From source file:nc.noumea.mairie.appock.core.utility.AppockUtil.java

private static int getMaxLengthGeneric(Class<?> clazz, String property) throws Exception {
    return clazz.getDeclaredField(property).getAnnotation(Column.class).length();
}

From source file:ru.appsm.inapphelp.IAHHelpDesk.java

private static int getIconValue(String className, String iconName) {
    try {/* w  w  w  .  j  av a  2 s.com*/
        Class<?> clazz = Class.forName(className + ".R$drawable");
        return (Integer) clazz.getDeclaredField(iconName).get(Integer.class);
    } catch (Exception ignore) {
    }
    return -1;
}

From source file:com.linkedin.pinot.common.utils.MmapUtils.java

private static void clearSynchronizedMapEntrySetCache() {
    // For some bizarre reason, Collections.synchronizedMap's implementation (at least on JDK 1.8.0.25) caches the
    // entry set, and will thus return stale (and incorrect) values if queried multiple times, as well as cause those
    // entries to not be garbage-collectable. This clears its cache.
    try {// w w w.j  a va  2  s.co  m
        Class<?> clazz = BUFFER_TO_CONTEXT_MAP.getClass();
        Field field = clazz.getDeclaredField("entrySet");
        field.setAccessible(true);
        field.set(BUFFER_TO_CONTEXT_MAP, null);
    } catch (Exception e) {
        // Well, that didn't work.
    }
}

From source file:lt.emasina.resthub.server.factory.MetadataFactory.java

public static void injectPrivateField(Object o, Class<?> fieldHolderClass, String fieldName, Object value)
        throws Exception {
    Field fResourceMd = fieldHolderClass.getDeclaredField(fieldName);
    fResourceMd.setAccessible(true);//from w w  w .  j av a2s. c o m
    fResourceMd.set(o, value);
}

From source file:it.sample.parser.util.CommonsUtil.java

/**
 * //from  ww w.  ja  va  2  s  .c  o m
 * @param propertyName
 * @param type
 * @return
 * @throws SecurityException
 * @throws NoSuchFieldException
 */
private static Field getDeclaredField(String propertyName, Class<?> type) throws SecurityException {
    Field declaredField = null;

    if (!propertyName.equals("class")) {
        try {
            declaredField = type.getDeclaredField(propertyName);
        } catch (NoSuchFieldException e) {
            if (type.getSuperclass() != null) {
                declaredField = getDeclaredField(propertyName, type.getSuperclass());
            }
        }
    }
    return declaredField;
}

From source file:de.tuberlin.uebb.jbop.access.ClassAccessor.java

private static Field getField(final Object object, final String fieldName) {
    final Class<? extends Object> clazz = object.getClass();
    try {/*from  www.ja  v a 2  s .c  o m*/
        return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException | SecurityException e) {
        return null;
    }
}

From source file:fi.foyt.foursquare.api.JSONFieldParser.java

/**
 * Returns field of class/*  ww  w .  java  2s .com*/
 * 
 * @param entityClass class
 * @param fieldName field
 * @return Field
 */
private static Field getField(Class<?> entityClass, String fieldName) {
    try {
        Field field = entityClass.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field;
    } catch (SecurityException e) {
        return null;
    } catch (NoSuchFieldException e) {
        Class<?> superClass = entityClass.getSuperclass();
        if (superClass.equals(Object.class)) {
            return null;
        } else {
            return getField(superClass, fieldName);
        }
    }
}

From source file:com.cat.external.util.OtherUtils.java

/**
 * @param context if null, use the default format
 *                (Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 %sSafari/534.30).
 * @return/*www.ja  v  a  2s  .c o m*/
 */
@SuppressLint("DefaultLocale")
public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
        try {
            @SuppressWarnings("rawtypes")
            Class sysResCls = Class.forName("com.android.internal.R$string");
            Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
            Integer resId = (Integer) webUserAgentField.get(null);
            webUserAgent = context.getString(resId);
        } catch (Throwable ignored) {
        }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
        webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        buffer.append(version);
    } else {
        // default to "1.0"
        buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
        buffer.append(language.toLowerCase());
        final String country = locale.getCountry();
        if (country != null) {
            buffer.append("-");
            buffer.append(country.toLowerCase());
        }
    } else {
        // default to "en"
        buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        final String model = Build.MODEL;
        if (model.length() > 0) {
            buffer.append("; ");
            buffer.append(model);
        }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        buffer.append(" Build/");
        buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
}

From source file:com.rockagen.commons.util.ClassUtil.java

/**
 * obtain field If recursively is true, obtain fields from all class
 * hierarchy//  w  ww.  ja  va  2 s  . c  o  m
 * 
 * @param clazz class
 *            where fields are searching
 * @param fieldName
 *            field name
 * @param recursively
 *            param
 * @return list of fields
 */
public static Field getDeclaredField(Class<?> clazz, String fieldName, boolean recursively) {
    try {
        return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && recursively) {
            return getDeclaredField(superClass, fieldName, true);
        }
    } catch (SecurityException e) {
        log.error("{}", e.getMessage(), e);
    }
    return null;

}