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:org.apache.directory.fortress.core.impl.TestUtils.java

/**
 *
 * @param inClass//from  w ww. ja v  a 2 s  .com
 * @param fieldLabel
 * @return
 */
public static String getDataLabel(Class inClass, String fieldLabel) {
    String labelValue = null;
    try {
        Field field = inClass.getField(fieldLabel);
        Annotation annotation = field.getAnnotation(MyAnnotation.class);
        if (annotation != null) {
            MyAnnotation myAnnotation = (MyAnnotation) annotation;
            labelValue = myAnnotation.value();
        }
    } catch (NoSuchFieldException e) {
        System.out.println("annotation excep=" + e);
    }

    return labelValue;
}

From source file:Main.java

private static Field scanField(Class<?> clazz, String... names) {
    for (String name : names) {
        try {/*from w w w  .  j a  va 2 s. c  o  m*/
            final Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {
        }
        try {
            return clazz.getField(name);
        } catch (NoSuchFieldException e) {
        }
    }
    return null;
}

From source file:Main.java

public static void jsonToAndroidLayoutMapper(Class<?> classObj, JSONObject json, String prefixStr,
        Activity view) throws JSONException {

    for (int i = 0; i < json.names().length(); i++) {
        String keyStr = (String) json.names().get(i);
        Field fieldObj = null;// w  w w  .  j  ava  2  s  .co m
        try {
            fieldObj = classObj.getField(prefixStr + "_" + keyStr.toLowerCase());
        } 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) json.get(keyStr));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }
    }

}

From source file:com.alibaba.wasp.ipc.WaspRPC.java

static long getProtocolVersion(Class<? extends VersionedProtocol> protocol)
        throws NoSuchFieldException, IllegalAccessException {
    Field versionField = protocol.getField("VERSION");
    versionField.setAccessible(true);/*from www . ja  v  a2 s  . c  o  m*/
    return versionField.getLong(protocol);
}

From source file:com.l2jfree.network.mmocore.packethandlers.PacketDefinition.java

@SuppressWarnings("unchecked")
public static <S> S[] findStates(Class<?> clazz, S... defaultStates) throws Exception {
    // easier than checking all the modifiers, visibility, type, etc :)
    final Field field;
    try {/*from  w w  w  .  ja  v  a 2 s.  co  m*/
        field = clazz.getField("STATES");
    } catch (NoSuchFieldException e) {
        return defaultStates;
    }

    return (S[]) field.get(null);
}

From source file:Main.java

public static void intentToAndroidLayoutMapper(Class<?> classObj, Intent intent, String prefixStr,
        Activity view) {/*w w w .j a v  a2  s  .com*/

    Bundle map = intent.getExtras();

    for (Object keyObj : map.keySet().toArray()) {
        String keyStr = keyObj.toString();
        Field fieldObj = null;
        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) map.get(keyStr));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }

    }

}

From source file:org.exem.flamingo.shared.util.el.ELServiceImpl.java

public static Object findConstant(String className, String constantName) throws ServiceException {
    try {//from   w w w .j a v  a  2s .c o m
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        Field field = clazz.getField(constantName);
        if ((field.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC)) != (Modifier.PUBLIC
                | Modifier.STATIC)) {
            // throw new ServiceException(ErrorCode.E0114, className, constantName);
        }
        return field.get(null);
    } catch (Exception ex) {
        throw new ServiceException(ex);
    }
}

From source file:org.wahtod.wififixer.ui.ConnectFragment.java

private static WifiConfiguration addHiddenFields(WifiConfiguration w) {
    try {/*from w  w  w  .j a va 2s. c o  m*/
        Field f = w.getClass().getField(IP_ASSIGNMENT);
        Field f2 = w.getClass().getField(PROXY_SETTINGS);
        Class<?> ipc = Class.forName(IPASSIGNMENT_CLASS);
        Class<?> proxy = Class.forName(PROXY_CLASS);
        Field dhcp = ipc.getField(DHCP_CONSTANT);
        Field none = proxy.getField(NONE_CONSTANT);
        Object v = dhcp.get(null);
        Object v2 = none.get(null);
        f.set(w, v);
        f2.set(w, v2);
    } catch (Exception e) {
        /*
         * Log
        */
        e.printStackTrace();
    }
    return w;
}

From source file:org.seasar.mayaa.impl.cycle.script.rhino.RhinoUtil.java

/**
 * public??????/*w w  w  . jav a  2 s. com*/
 *
 * @param bean 
 * @param propertyName ??
 * @return ?????null
 */
protected static Object getFromPublicField(Object bean, String propertyName) {
    try {
        // TODO Field
        Class beanClass = bean.getClass();
        Field field = beanClass.getField(propertyName);
        if (Modifier.isPublic(field.getModifiers())) {
            if (field.isAccessible() == false) {
                field.setAccessible(true);
            }
            return field.get(bean);
        }
    } catch (SecurityException ignore) {
        // no-op
    } catch (NoSuchFieldException ignore) {
        // no-op
    } catch (IllegalArgumentException ignore) {
        // no-op
    } catch (IllegalAccessException eignore) {
        // no-op
    }

    return null;
}

From source file:org.openflamingo.uploader.el.ELService.java

public static Object findConstant(String className, String constantName) throws SystemException {
    try {/*from   ww  w.  java 2s.com*/
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        Field field = clazz.getField(constantName);
        if ((field.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC)) != (Modifier.PUBLIC
                | Modifier.STATIC)) {
            // throw new SystemException(ErrorCode.E0114, className, constantName);
        }
        return field.get(null);
    } catch (IllegalAccessException ex) {
        throw new SystemException(ex);
    } catch (NoSuchFieldException ex) {
        throw new SystemException(ex);
    } catch (ClassNotFoundException ex) {
        throw new SystemException(ex);
    }
}