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.springframework.yarn.configuration.EnvironmentFactoryBean.java

/**
 * Utility method reading static String values from a class.
 * @param clazzName full class name/*from  ww  w  . ja  v  a  2 s  .  co  m*/
 * @param fieldName field name
 * @param classLoader classloader
 * @return the value or empty in any other case
 */
private static String readStaticField(String clazzName, String fieldName, ClassLoader classLoader) {
    try {
        Class<?> clazz = ClassUtils.forName(clazzName, classLoader);
        Field field = clazz.getField(fieldName);
        return (String) field.get(null);
    } catch (Error e) {
        log.warn("Unable to read static " + fieldName + " from " + clazzName, e);
    } catch (Exception e) {
        log.warn("Unable to read static " + fieldName + " from " + clazzName, e);
    }
    return "";
}

From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java

/**
 * ???.//ww  w.  java 2 s  .  c o m
 * 
 * <pre>
 * {@code
 * example1 :
 *  IOConstants GB??
 * FieldUtil.getStaticProperty("com.feilong.commons.core.io.IOConstants", "GB")
 *  :1073741824
 * }
 * </pre>
 * 
 * @param <T>
 *            the generic type
 * @param className
 *            ??
 * @param fieldName
 *            ??
 * @return 
 * @throws ReflectException
 *             the reflect exception
 * @see com.feilong.commons.core.lang.ClassUtil#loadClass(String)
 * @see java.lang.Class#getField(String)
 * @see java.lang.reflect.Field#get(Object)
 */
@SuppressWarnings("unchecked")
public static <T> T getStaticProperty(String className, String fieldName) throws ReflectException {
    try {
        Class<?> ownerClass = ClassUtil.loadClass(className);
        Field field = ownerClass.getField(fieldName);
        Object property = field.get(ownerClass);
        return (T) property;
    } catch (Exception e) {
        log.error(e.getClass().getName(), e);
        throw new ReflectException(e);
    }
}

From source file:net.servicefixture.converter.ObjectConverter.java

/**
 * Converts types that are supported by <code>ConvertUtils</code>.
 * //from  w ww.java 2  s. co  m
 * @param data
 * @param destType
 * @return
 */
public static Object specialConvert(String data, Class destType) {
    try {
        //If it is enumeration class.
        if (ReflectionUtils.isEnumarationPatternClass(destType)) {
            try {
                Field field = destType.getField(data);
                return field.get(null);
            } catch (NoSuchFieldException e) {
                //No such field, meaning the variable name and the data
                //doesn't match, call fromString method.
                return destType.getMethod("fromString", new Class[] { String.class }).invoke(null,
                        new Object[] { data });
            }
        }
        //JDK5.0 enum type
        if (destType.isEnum()) {
            return destType.getMethod("valueOf", String.class).invoke(null, data);
        }
    } catch (Exception ignoreIt) {
        ignoreIt.printStackTrace();
    }

    throw new ServiceFixtureException("Unable to convert data:" + data + " to class:" + destType.getName());
}

From source file:org.apache.directory.fortress.core.impl.TestUtils.java

/**
 *
 * @param inClass/*w ww  . ja  v  a 2  s .  co  m*/
 * @param fieldLabel
 * @return
 */
public static String getTestDataLabels(Class inClass, String fieldLabel) {
    String fieldName = null;
    try {
        //Field field = inClass.getField(fieldLabel);
        Field field = inClass.getField("POLICIES_TP1");

        Annotation annotation = field.getAnnotation(MyAnnotation.class);
        //Annotation[] annotations = field.getDeclaredAnnotations();
        if (annotation != null) {
            MyAnnotation myAnnotation = (MyAnnotation) annotation;

            //System.out.println("name: " + "dd");
            System.out.println("*************** name: " + myAnnotation.name());
            System.out.println("*************** value: " + myAnnotation.value());
            fieldName = myAnnotation.name();
        }
    } catch (NoSuchFieldException e) {
        System.out.println("annotation excep=" + e);
    }

    return fieldName;
}

From source file:szjy.advtech.BuildInfo.java

/**
 * Get field from Class/*from w w w.ja va 2  s .co m*/
 * @param c
 * @param fieldName
  * @return
  */
private static Field getClassField(Class c, String fieldName) {
    Field field = null;

    try {
        field = c.getField(fieldName);
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    }

    return field;
}

From source file:Main.java

/**
 * /*from w  ww .  j ava2 s .c o m*/
 * @param node
 * @param classname
 * @return
 */
public static Object decode(Element node, String classname) {
    try {
        Class classObject = Class.forName(classname);
        Object object = classObject.newInstance();
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node child = attributes.item(i);
            String nodeName = child.getNodeName();
            Field field = classObject.getField(nodeName);
            field.setAccessible(true);
            field.set(object, child.getNodeValue());
        }
        return object;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (SecurityException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

private static int getSmartBarHeight(Activity activity) {
    ActionBar actionbar = activity.getActionBar();
    if (actionbar != null)
        try {//w ww .j  a  va2  s .com
            Class c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("mz_action_button_min_height");
            int height = Integer.parseInt(field.get(obj).toString());
            return activity.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
            actionbar.getHeight();
        }
    return 0;
}

From source file:com.microsoft.tfs.client.common.ui.framework.helper.SWTUtil.java

public static int addGridLayoutVerticalIndent(final Control[] controls, final int verticalIndent) {
    Check.notNull(controls, "controls"); //$NON-NLS-1$

    if (SWT.getVersion() >= 3100) {
        for (int i = 0; i < controls.length; i++) {
            GridData gridData = (GridData) controls[i].getLayoutData();
            if (gridData == null) {
                gridData = new GridData();
                controls[i].setLayoutData(gridData);
            }//  w ww .ja va 2 s .c  om

            try {
                final Class gridDataClass = gridData.getClass();
                final Field viField = gridDataClass.getField("verticalIndent"); //$NON-NLS-1$

                viField.setInt(gridData, verticalIndent);
            } catch (final Exception e) {
                break;
            }
        }
        return 0;
    }

    final GridLayout layout = (GridLayout) controls[0].getParent().getLayout();
    final int spacing = Math.max(0, verticalIndent - layout.verticalSpacing);
    final Control spacer = createGridLayoutSpacer(controls[0].getParent(), SWT.DEFAULT, spacing,
            layout.numColumns, 1);
    spacer.moveAbove(controls[0]);
    return 1;
}

From source file:net.sourceforge.jwbf.mediawiki.MediaWiki.java

static Field getFieldUnchecked(Class<?> clazz, String fieldName) {
    try {// ww  w  .  ja va2 s .  c om
        return clazz.getField(fieldName);
    } catch (NoSuchFieldException nsfe) {
        throw new IllegalArgumentException(nsfe);
    }
}

From source file:org.jiemamy.utils.reflect.ClassUtil.java

/**
 * {@link Field}??????????{@code null}?//w  ww . j  a  va  2  s.co  m
 * 
 * @param clazz 
 * @param fieldName ??
 * @return {@link Field}
 * @see Class#getField(String)
 * @throws IllegalArgumentException {@code clazz}?{@code null}???
 */
public static Field getFieldNoException(Class<?> clazz, String fieldName) {
    Validate.notNull(clazz);
    try {
        return clazz.getField(fieldName);
    } catch (SecurityException e) {
        return null;
    } catch (NoSuchFieldException e) {
        return null;
    }
}