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:fit.Binding.java

private static Field getField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
    try {/*from  w ww  .j  a va2  s .c om*/
        return clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass == null) {
            throw e;
        } else {
            return getField(superClass, fieldName);
        }
    }
}

From source file:de.Keyle.MyPet.util.Util.java

public static Field getField(Class<?> clazz, String field) {
    try {/*ww  w . j  av a2  s . c  o m*/
        Field f = clazz.getDeclaredField(field);
        f.setAccessible(true);
        return f;
    } catch (Exception ignored) {
        return null;
    }
}

From source file:Main.java

/**
 * Gets the value of a static field./*from w ww . j a  va2s .c om*/
 *
 * @param clazz from which to get the field value
 * @param name the name of the field
 * @return the value of the field.
 * @throws PrivilegedActionException
 */
static <T> T getStaticFieldValue(final Class<?> clazz, final String name) throws PrivilegedActionException {
    final PrivilegedExceptionAction<T> action = new PrivilegedExceptionAction<T>() {
        @SuppressWarnings("unchecked")
        public T run() throws Exception {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return (T) field.get(null);
        }
    };

    return AccessController.doPrivileged(action);
}

From source file:ReflectionUtils.java

public static Field getPropertyField(Class<?> beanClass, String property) throws NoSuchFieldException {
    if (beanClass == null)
        throw new IllegalArgumentException("beanClass cannot be null");

    Field field = null;/*from  w  w w.  j a  va  2 s .  co m*/
    try {
        field = beanClass.getDeclaredField(property);
    } catch (NoSuchFieldException e) {
        if (beanClass.getSuperclass() == null)
            throw e;
        // look for the field in the superClass
        field = getPropertyField(beanClass.getSuperclass(), property);
    }
    return field;
}

From source file:com.agimatec.validation.jsr303.util.SecureActions.java

public static Field getDeclaredField(final Class clazz, final String fieldName) {
    return run(new PrivilegedAction<Field>() {
        public Field run() {
            try {
                Field f = clazz.getDeclaredField(fieldName);
                setAccessibility(f);/*from  w ww .  j a  v a 2  s. com*/
                return f;
            } catch (NoSuchFieldException e) {
                return null;
            }
        }
    });
}

From source file:com.junly.common.util.ReflectUtils.java

/**
 * <p class="detail">/*from  w  w  w .java 2 s  . c  o m*/
 * 
 * </p>
 * @author wan.Dong
 * @date 20161112 
 * @param obj 
 * @param name    ??
 * @param value  (?)??false
 * @return
 */
public static boolean setProperty(Object obj, String name, Object value) {
    if (obj != null) {
        Class<?> clazz = obj.getClass();
        while (clazz != null) {
            Field field = null;
            try {
                field = clazz.getDeclaredField(name);
            } catch (Exception e) {
                clazz = clazz.getSuperclass();
                continue;
            }
            try {
                Class<?> type = field.getType();
                if (type.isPrimitive() == true && value != null) {
                    if (value instanceof String) {
                        if (type.equals(int.class) == true) {
                            value = Integer.parseInt((String) value);
                        } else if (type.equals(double.class) == true) {
                            value = Double.parseDouble((String) value);
                        } else if (type.equals(boolean.class) == true) {
                            value = Boolean.parseBoolean((String) value);
                        } else if (type.equals(long.class) == true) {
                            value = Long.parseLong((String) value);
                        } else if (type.equals(byte.class) == true) {
                            value = Byte.parseByte((String) value);
                        } else if (type.equals(char.class) == true) {
                            value = Character.valueOf(((String) value).charAt(0));
                        } else if (type.equals(float.class) == true) {
                            value = Float.parseFloat((String) value);
                        } else if (type.equals(short.class) == true) {
                            value = Short.parseShort((String) value);
                        }
                    }
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                if (value == null || type.equals(value.getClass()) == true) {
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }
    return false;
}

From source file:com.atlassian.connector.eclipse.internal.crucible.ui.operations.CrucibleFileInfoCompareEditorInput.java

private static Viewer findContentViewer(Viewer contentViewer, ICompareInput input, Composite parent,
        CrucibleCompareAnnotationModel annotationModel) {

    // FIXME: hack
    if (contentViewer instanceof TextMergeViewer) {
        TextMergeViewer textMergeViewer = (TextMergeViewer) contentViewer;
        try {//from  ww w.  j  a va 2s.  co  m
            Class<TextMergeViewer> clazz = TextMergeViewer.class;
            Field declaredField = clazz.getDeclaredField("fLeft");
            declaredField.setAccessible(true);
            final MergeSourceViewer fLeft = (MergeSourceViewer) declaredField.get(textMergeViewer);

            declaredField = clazz.getDeclaredField("fRight");
            declaredField.setAccessible(true);
            final MergeSourceViewer fRight = (MergeSourceViewer) declaredField.get(textMergeViewer);

            annotationModel.attachToViewer(textMergeViewer, fLeft, fRight);
            annotationModel.focusOnComment();
            annotationModel.registerContextMenu();

            Method setActiveViewer = clazz.getDeclaredMethod("setActiveViewer", MergeSourceViewer.class,
                    boolean.class);
            setActiveViewer.setAccessible(true);
            setActiveViewer.invoke(textMergeViewer, fRight, true);

            hackGalileo(contentViewer, textMergeViewer, fLeft, fRight);
        } catch (Throwable t) {
            StatusHandler.log(new Status(IStatus.WARNING, AtlassianTeamUiPlugin.PLUGIN_ID,
                    "Could not initialize annotation model for " + input.getName(), t));
        }
    }
    return contentViewer;
}

From source file:com.app.test.BaseTestCase.java

protected static void _initializeVelocityTemplate(Class clazz, Object classInstance) throws Exception {

    Field velocityEngine = clazz.getDeclaredField("_velocityEngine");

    velocityEngine.setAccessible(true);/*from  w w  w .j  av  a  2  s .  co m*/

    VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();

    Map<String, Object> velocityPropertiesMap = new HashMap<>();

    velocityPropertiesMap.put("resource.loader", "class");
    velocityPropertiesMap.put("class.resource.loader.class",
            "org.apache.velocity.runtime.resource.loader." + "ClasspathResourceLoader");

    velocityEngineFactoryBean.setVelocityPropertiesMap(velocityPropertiesMap);

    velocityEngine.set(classInstance, velocityEngineFactoryBean.createVelocityEngine());
}

From source file:fi.vm.kapa.identification.metadata.background.MetadataFilesGenerator.java

License:asdf

@SuppressWarnings("unchecked")
private static <T> T createSAMLObject(final Class<T> object) throws Exception {
    XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
    QName name = (QName) object.getDeclaredField("DEFAULT_ELEMENT_NAME").get(null);

    return (T) builderFactory.getBuilder(name).buildObject(name);
}

From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java

public static Field findField(Class<?> clazz, String... fieldNames) {
    Exception failed = null;//from  ww  w.ja va2 s .  c  om
    for (String fieldName : fieldNames) {
        try {
            Field f = clazz.getDeclaredField(fieldName);
            f.setAccessible(true);
            return f;
        } catch (Exception e) {
            failed = e;
        }
    }
    throw new UnableToFindFieldException(fieldNames, failed);
}