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 getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;//w ww. ja  v  a2s .  co  m
    java.lang.reflect.Field field = null;
    int x = 0;
    int statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
        return statusBarHeight;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusBarHeight;
}

From source file:org.apache.bval.util.PropertyAccess.java

private static Field getField(String propertyName, Class<?> beanClass) {
    try { // try public field
        return beanClass.getField(propertyName);
    } catch (NoSuchFieldException ex2) {
        // search for private/protected field up the hierarchy
        Class<?> theClass = beanClass;
        while (theClass != null) {
            try {
                return theClass.getDeclaredField(propertyName);
            } catch (NoSuchFieldException ex3) {
                // do nothing
            }//from ww  w  . j  a v  a2  s. c o m
            theClass = theClass.getSuperclass();
        }
    }
    return null;
}

From source file:Debug.java

public static Debug getDebugLevel(Class cls) throws NoSuchFieldException {
    try {/*from  w w  w . j a  va  2 s  . co  m*/
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        return (Debug) fld.get(null);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:Debug.java

public static void setDebugLevel(Class cls, Debug level) throws NoSuchFieldException {
    try {//from   w w w  .  jav a 2 s.c  om
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        fld.set(null, level);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:fit.Binding.java

private static TypeAdapter makeAdapterForField(String name, Fixture fixture) {
    Field field = null;//from   w  w  w .ja  v a2  s . c  o  m
    if (GracefulNamer.isGracefulName(name)) {
        String simpleName = GracefulNamer.disgrace(name).toLowerCase();
        field = findField(fixture, simpleName);
    } else {
        Matcher matcher = fieldPattern.matcher(name);
        matcher.find();
        String fieldName = matcher.group(1);
        Class<?> clazz = fixture.getTargetClass();
        try {
            field = clazz.getField(fieldName);
        } catch (NoSuchFieldException e) {
            try {
                field = getField(clazz, fieldName);
            } catch (NoSuchFieldException e2) {
            }
        }
    }

    if (field == null)
        throw new NoSuchFieldFitFailureException(name);
    return TypeAdapter.on(fixture, field);
}

From source file:io.apiman.gateway.engine.es.ESClientFactory.java

/**
 * Creates a cache by looking it up in a static field.  Typically used for
 * testing.//from  w ww  .ja  v a 2 s .  c o  m
 * @param className the class name
 * @param fieldName the field name
 * @param indexName the name of the ES index
 * @return the ES client
 */
public static JestClient createLocalClient(String className, String fieldName, String indexName) {
    String clientKey = "local:" + className + '/' + fieldName; //$NON-NLS-1$
    synchronized (clients) {
        if (clients.containsKey(clientKey)) {
            return clients.get(clientKey);
        } else {
            try {
                Class<?> clientLocClass = Class.forName(className);
                Field field = clientLocClass.getField(fieldName);
                JestClient client = (JestClient) field.get(null);
                clients.put(clientKey, client);
                initializeClient(client, indexName);
                return client;
            } catch (ClassNotFoundException | NoSuchFieldException | SecurityException
                    | IllegalArgumentException | IllegalAccessException e) {
                throw new RuntimeException("Error using local elasticsearch client.", e); //$NON-NLS-1$
            }
        }
    }
}

From source file:Main.java

public static void mapToAndroidLayoutMapper(Class classObj, Map map, String prefixStr, View view) {

    for (Object keyObj : map.keySet().toArray()) {
        String keyStr = keyObj.toString();
        Field fieldObj = null;//w  w  w  .j a  v a2  s  . c  o m
        try {
            fieldObj = classObj.getField(prefixStr + "_" + keyStr);
        } catch (NoSuchFieldException e) {
            continue;
        }
        Object layoutObj = null;
        try {
            layoutObj = fieldObj.get(fieldObj);
        } catch (IllegalAccessException e) {
            continue;
        }
        Integer layoutIntvalue = (Integer) layoutObj;

        View selectedView = view.findViewById(layoutIntvalue);

        if (selectedView instanceof TextView) {
            TextView textView = (TextView) selectedView;
            textView.setText(String.valueOf(map.get(keyStr)));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }

    }

}

From source file:com.vmware.photon.controller.common.dcp.BasicServiceHost.java

private static String buildPath(Class<? extends Service> type) {
    try {/*from w w w. j a  va 2  s.  com*/
        Field f = type.getField(FIELD_NAME_SELF_LINK);
        return (String) f.get(null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Utils.log(Utils.class, Utils.class.getSimpleName(), Level.SEVERE, "%s field not found in class %s: %s",
                FIELD_NAME_SELF_LINK, type.getSimpleName(), Utils.toString(e));
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {

    Class<?> c = null;

    Object obj = null;/*  w ww . j a  v  a  2 s. c o  m*/

    Field field = null;

    int

    x = 0, statusBarHeight = 0;

    try

    {

        c = Class.forName("com.android.internal.R$dimen");

        obj = c.newInstance();

        field = c.getField("status_bar_height");

        x = Integer.parseInt(field.get(obj).toString());

        statusBarHeight = context.getResources().getDimensionPixelSize(x);

    } catch

    (Exception e1) {

        e1.printStackTrace();

    }

    return

    statusBarHeight;

}

From source file:ryerson.daspub.Config.java

/**
 * Parse arguments and assign variables//from www .  ja  v a2 s . c om
 * @param Args
 */
private static void setValues(HashMap<String, String> Args) {
    Set<String> keys = Args.keySet();
    Iterator<String> it = keys.iterator();
    String name = "";
    String val = "";
    Field field = null;
    while (it.hasNext()) {
        name = it.next(); // all local fields have upper case names
        Class aClass = Config.class;
        try {
            field = aClass.getField(name.toUpperCase());
            val = Args.get(name);
            if (field.getType() == int.class) {
                field.setInt(Config.class, Integer.valueOf(val));
            } else {
                field.set(Config.class, val);
            }
            logger.log(Level.INFO, "Set \"{0}\" as \"{1}\"", new Object[] { field.getName(), val.toString() });
        } catch (Exception ex) {
            String stack = ExceptionUtils.getStackTrace(ex);
            logger.log(Level.SEVERE, "Could not find or set field \"{0}\"\n\n{1}",
                    new Object[] { name, stack });
        }
    }
    // create derived objects
    if (ARCHIVE_PATH != null && ARCHIVE_PATH.contains(";")) {
        String[] items = ARCHIVE_PATH.split(";");
        for (int i = 0; i < items.length; i++) {
            String path = items[i].trim();
            ARCHIVE_PATHS.add(path);
        }
    } else {
        ARCHIVE_PATHS.add(ARCHIVE_PATH);
    }
}