Example usage for java.lang Class getField

List of usage examples for java.lang Class getField

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:Main.java

public static int getResId(String resName, Class<?> c) {
    try {//  w w w.  ja  va 2  s  .  co  m
        Field idField = c.getField(resName);
        return idField.getInt(null);
    } catch (Exception e) {
        return -1;
    }
}

From source file:Main.java

public static int getRIdFromName(Class<?> cls, String filedName) {
    try {/*  w  w w . ja v a2  s. co m*/
        Field f = cls.getField(filedName);
        return Integer.parseInt(f.get(null).toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return 0;
}

From source file:Main.java

public static Type getFieldType(Class<?> clazz, String fieldName) {
    try {/*from w  w  w.  j  a  va 2 s  . com*/
        Field field = clazz.getField(fieldName);

        return field.getGenericType();
    } catch (Exception ex) {
        return null;
    }
}

From source file:Main.java

public static Type getFieldType(Class<?> paramClass, String paramString) {
    try {//from w  w  w .  ja  va2s .  c  o m
        Type localType = paramClass.getField(paramString).getGenericType();
        return localType;
    } catch (Exception localException) {
    }
    return null;
}

From source file:Main.java

public static int getScreenOrieLockedValue(Class<ActivityInfo> object) {
    int value = -1;
    try {// ww  w . j av  a 2s.  c  o m
        Field field = object.getField("SCREEN_ORIENTATION_LOCKED");
        value = field.getInt(object);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

From source file:Main.java

public static String value(Class<? extends Enum> aClass, String name) {
    try {//from   w  ww . jav a2  s  .  c  o m
        Field field = aClass.getField(name);

        XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);

        return annotation.value();
    } catch (NoSuchFieldException e) {
        throw new IllegalStateException();
    }
}

From source file:Main.java

/**
 * Get the API access token request uri/*from  w  ww .ja v a2  s .c o  m*/
 *
 * @param cloudApi type of cloud account
 * @return Uri
 */
public static Uri getAccessTokenUri(String cloudApi) throws MalformedURLException {
    // use reflection for flexibility
    try {
        Class clazz = Class.forName(cloudApi);
        Field tokenUrl = clazz.getField("TOKEN_URL");
        return Uri.parse((String) tokenUrl.get(null));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    throw new MalformedURLException("No url or malformed url for request!");
}

From source file:Main.java

public static void init(Context context)
        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    Class<?> buildConfigClass = Class.forName(context.getPackageName() + ".BuildConfig");
    BUILD_CONFIG_DEBUG = buildConfigClass.getField("DEBUG").getBoolean(null);
}

From source file:Main.java

/**
 * Get the API redirect uri/*from   w w  w.  j  a  v a2 s. co  m*/
 *
 * @param cloudApi type of cloud account
 * @return Uri
 */
public static Uri getRedirectUri(String cloudApi) {
    // use reflection for flexibility
    try {
        Class clazz = Class.forName(cloudApi);
        Field redirectUrl = clazz.getField("REDIRECT_URL");
        return Uri.parse((String) redirectUrl.get(null));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.liferay.tool.datamanipulator.util.GetterUtil.java

public static Object getFieldValue(String className, String fieldName) throws ClassNotFoundException,
        SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

    Class<?> clazz = getClass(className);

    Field field = clazz.getField(fieldName);

    return field.get(null);
}