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:org.echocat.redprecursor.annotations.utils.AccessAlsoProtectedMembersReflectivePropertyAccessor.java

@Override
protected Field findField(String name, Class<?> clazz, boolean mustBeStatic) {
    Field result = null;//from   w w w. j  av a  2s  . c  om
    Class<?> current = clazz;
    while (result == null && !Object.class.equals(current)) {
        try {
            final Field potentialField = current.getDeclaredField(name);
            if (!mustBeStatic || Modifier.isStatic(potentialField.getModifiers())) {
                if (!potentialField.isAccessible()) {
                    potentialField.setAccessible(true);
                }
                result = potentialField;
            }
        } catch (NoSuchFieldException ignored) {
        }
        current = current.getSuperclass();
    }
    return result;
}

From source file:com.link_intersystems.lang.reflect.SerializableField.java

@SuppressWarnings("unchecked")
@Override/*from   w  w w.  ja v  a  2 s.  c  o m*/
protected Field deserialize(Serializable restoreInfo) throws NoSuchFieldException {
    MemberSerialization<Field> memberSerializationInfo = (MemberSerialization<Field>) restoreInfo;
    Class<?> declaringClass = memberSerializationInfo.getDeclaringClass();
    String fieldName = memberSerializationInfo.getMemberName();
    int modifiers = memberSerializationInfo.getModifiers();
    Field field = declaringClass.getDeclaredField(fieldName);
    int currentModifiers = getModifier(field);
    if (modifiers != currentModifiers) {
        throw new SerializationException("Unable to restore field " + fieldName + " declared at "
                + declaringClass + ". Modifiers changed since serialization. Expected modifiers are "
                + Modifier.toString(modifiers) + ", but current modifiers are "
                + Modifier.toString(currentModifiers));
    }
    return field;
}

From source file:net.ljcomputing.gson.converter.impl.GsonConverterServiceImpl.java

/**
 * Find field./*from www.j av a2 s.c  o m*/
 *
 * @param clazz the clazz
 * @param fieldName the field name
 * @return the field
 */
private Field findField(final Class<?> clazz, final String fieldName) {
    Field field = null;
    Class<?> current = clazz;

    do {
        try {
            field = current.getDeclaredField(fieldName);
        } catch (NoSuchFieldException | SecurityException exception) {
            LOGGER.debug("exception ignored while getting declared field {} for class {}: {}", fieldName, clazz,
                    exception.getMessage());
        }
    } while ((current = current.getSuperclass()) != null);

    return field;
}

From source file:com.seer.datacruncher.services.ftp.FTPPollJobProcessor.java

private boolean isStreamClose(ZipInputStream inStream) {
    try {// w ww . j  av  a  2  s. com
        @SuppressWarnings("rawtypes")
        Class c = inStream.getClass();
        Field in;
        in = c.getDeclaredField("closed");
        in.setAccessible(true);
        Boolean inReader = (Boolean) in.get(inStream);
        return inReader;
    } catch (Exception e) {
        log.error(e);
    }
    return false;
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Try find a field with given name. Goes throw class hierarchie.
 *
 * @param cls the cls/*from   w  w w .j av  a 2  s  .co m*/
 * @param fieldName the field name
 * @return the field
 */
public static Field findField(Class<?> cls, String fieldName) {
    Field f = null;
    try {
        f = cls.getDeclaredField(fieldName);
    } catch (SecurityException ex) {
    } catch (NoSuchFieldException ex) {
    }
    if (f != null) {
        return f;
    }
    if (cls == Object.class || cls.getSuperclass() == null) {
        return null;
    }
    return findField(cls.getSuperclass(), fieldName);
}

From source file:com.basho.riak.client.convert.RiakBeanSerializerModifier.java

/**
 * Checks if the property has any of the Riak annotations on it or the 
 * Jackson JsonProperty annotation. //  w  w  w  .  ja v  a 2s.c o m
 * 
 * If a Riak annotation is present without the Jackson JsonProperty
 * annotation, this will return false.
 * 
 * If a property has been annotated with both the Jackson JsonProperty
 * annotation and a Riak annotation, the Jackson annotation takes precedent 
 * and this will return true.
 * 
 * @param beanPropertyWriter
 *            a {@link BeanPropertyWriter} to check for Riak* annotations
 * @return true if the property is not Riak annotated or is Jackson
* JsonProperty annotated, false otherwise
 */
private boolean keepProperty(BeanPropertyWriter beanPropertyWriter) {
    RiakKey key = null;
    RiakUsermeta usermeta = null;
    RiakLinks links = null;
    RiakIndex index = null;
    RiakVClock vclock = null;
    JsonProperty jacksonJsonProperty = null;
    RiakTombstone tombstone = null;

    AnnotatedMember member = beanPropertyWriter.getMember();
    if (member instanceof AnnotatedField) {
        AnnotatedElement element = member.getAnnotated();
        key = element.getAnnotation(RiakKey.class);
        usermeta = element.getAnnotation(RiakUsermeta.class);
        links = element.getAnnotation(RiakLinks.class);
        index = element.getAnnotation(RiakIndex.class);
        vclock = element.getAnnotation(RiakVClock.class);
        tombstone = element.getAnnotation(RiakTombstone.class);
        jacksonJsonProperty = element.getAnnotation(JsonProperty.class);
    } else {
        @SuppressWarnings("rawtypes")
        Class clazz = member.getDeclaringClass();
        Field field;
        try {
            field = clazz.getDeclaredField(beanPropertyWriter.getName());
            key = field.getAnnotation(RiakKey.class);
            usermeta = field.getAnnotation(RiakUsermeta.class);
            links = field.getAnnotation(RiakLinks.class);
            index = field.getAnnotation(RiakIndex.class);
            vclock = field.getAnnotation(RiakVClock.class);
            tombstone = field.getAnnotation(RiakTombstone.class);
            jacksonJsonProperty = field.getAnnotation(JsonProperty.class);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            // ignore, not a field means not a Riak annotated field.
        }
    }

    if (jacksonJsonProperty != null) {
        return true;
    } else {
        return key == null && usermeta == null && links == null && vclock == null && index == null
                && tombstone == null;
    }
}

From source file:nz.co.senanque.validationengine.Binder.java

private Field getField(Class<?> clazz, String fieldName) throws Exception {
    try {/* w  w  w  .  j  a  va 2s  . c  o m*/
        java.lang.reflect.Field propertyField = clazz.getDeclaredField(fieldName);
        return propertyField;
    } catch (Exception e) {
        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null) {
            return getField(superClass, fieldName);
        } else {
            throw e;
        }
    }
}

From source file:com.wavemaker.json.type.reflect.WMPropertyUtilsBean.java

@Override
public PropertyDescriptor[] getPropertyDescriptors(Class klass) {
    PropertyDescriptor[] pds = super.getPropertyDescriptors(klass);

    for (PropertyDescriptor pd : pds) {
        String name = pd.getName();
        Field fld = null;//from w w  w.ja va  2 s .co m
        try {
            fld = klass.getDeclaredField(name);
        } catch (NoSuchFieldException ex) {
        }

        if (fld != null) {
            continue;
        }

        String shifted = name.substring(0, 1).toUpperCase();

        String newName = shifted + name.substring(1);

        fld = null;
        try {
            fld = klass.getDeclaredField(newName);
        } catch (NoSuchFieldException ex) {
        }

        if (fld != null) {
            pd.setName(newName);
        }
    }

    return pds;
}

From source file:eu.vital.vitalcep.cep.CepProcess.java

private int getPid(Process process) {
    try {/*from  w w w. j  ava  2s .co  m*/

        Class<?> cProcessImpl = process.getClass();
        java.lang.reflect.Field fPid = cProcessImpl.getDeclaredField("pid");

        if (!fPid.isAccessible()) {
            fPid.setAccessible(true);
        } else {
            return -1;
        }

        return fPid.getInt(process);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        return -2;
    }
}

From source file:org.testng.spring.test.AbstractDependencyInjectionSpringContextTests.java

private Field findField(Class clazz, String name) throws NoSuchFieldException {
    try {/*from  w w w.j a  v  a  2s  .  com*/
        return clazz.getDeclaredField(name);
    } catch (NoSuchFieldException ex) {
        Class superclass = clazz.getSuperclass();
        if (superclass != AbstractSpringContextTests.class) {
            return findField(superclass, name);
        } else {
            throw ex;
        }
    }
}