Example usage for java.lang NoSuchFieldException NoSuchFieldException

List of usage examples for java.lang NoSuchFieldException NoSuchFieldException

Introduction

In this page you can find the example usage for java.lang NoSuchFieldException NoSuchFieldException.

Prototype

public NoSuchFieldException(String s) 

Source Link

Document

Constructor with a detail message.

Usage

From source file:org.talend.commons.utils.BeanUtils.java

public static Field getDeclaredField(Class clazz, String propertyName) throws NoSuchFieldException {
    for (Class superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {
        try {//w ww  . j a  v a  2  s  .  com
            return superClass.getDeclaredField(propertyName);
        } catch (NoSuchFieldException e) {
            log.error(e);
        }
    }
    throw new NoSuchFieldException("No such field: " + clazz.getName() + '.' + propertyName);
}

From source file:com.dianping.lion.util.BeanUtils.java

public static Field getDeclaredField(Class<?> clazz, String propertyName) throws NoSuchFieldException {
    Assert.notNull(clazz);/*from  w  ww  .j a v a 2 s.  co  m*/
    Assert.hasText(propertyName);
    for (Class<?> superClass = clazz; superClass != Object.class; superClass = superClass.getSuperclass()) {

        try {
            return superClass.getDeclaredField(propertyName);
        } catch (NoSuchFieldException e) {
            // Field??,?
            logger.debug("no such method !");
        }
    }
    throw new NoSuchFieldException("No such field: " + clazz.getName() + '.' + propertyName);
}

From source file:ca.uhn.fhir.util.reflection.JavaReflectBeanUtil.java

@Override
public Method findAccessor(Class<?> theClassToIntrospect, Class<?> theTargetReturnType, String thePropertyName)
        throws NoSuchFieldException {
    String methodName = "get" + WordUtils.capitalize(thePropertyName);
    try {/*from ww  w . ja  v  a 2 s . c  om*/
        Method method = theClassToIntrospect.getMethod(methodName);
        if (theTargetReturnType.isAssignableFrom(method.getReturnType())) {
            return method;
        }
    } catch (NoSuchMethodException e) {
        // fall through
    } catch (SecurityException e) {
        throw new ConfigurationException(
                "Failed to scan class '" + theClassToIntrospect + "' because of a security exception", e);
    }
    throw new NoSuchFieldException(theClassToIntrospect + " has no accessor for field " + thePropertyName);
}

From source file:com.cassius.spring.assembly.test.common.reflect.FieldReader.java

/**
 * New instance.//from   w w w  .j av  a 2s.  c  o m
 *
 * @param object the object
 * @param fieldName the field name
 * @return the field reader
 * @throws NoSuchFieldException the no such field exception
 */
public static FieldReader newInstance(Object object, String fieldName) throws NoSuchFieldException {
    Class<?> clazz = object.getClass();
    while (clazz != Object.class) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            if (field != null) {
                return newInstance(object, field);
            }
        } catch (NoSuchFieldException ignore) {
        }
        clazz = clazz.getSuperclass();
    }
    throw new NoSuchFieldException(fieldName + "not exist");
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

/**
 * Searches for a field in the given class and all of its super classes.
 * @param clazz Class to start the search for the field
 * @param name Name of the field//from   w w w  .j av  a 2  s .  c  om
 * @return The field that was found
 * @throws NoSuchFieldException
 */
public static Field getField(Class<?> clazz, String name) throws NoSuchFieldException {
    Class<?> searchClass = clazz;
    Field field = null;
    while (field == null && searchClass != null) {
        try {
            field = searchClass.getDeclaredField(name);
        } catch (NoSuchFieldException e) {
            searchClass = searchClass.getSuperclass();
        }
    }
    if (field == null) {
        throw new NoSuchFieldException(clazz.getSimpleName() + "." + name); //$NON-NLS-1$
    }
    return field;
}

From source file:ca.uhn.fhir.util.reflection.JavaReflectBeanUtil.java

@Override
public Method findMutator(Class<?> theClassToIntrospect, Class<?> theTargetArgumentType, String thePropertyName)
        throws NoSuchFieldException {
    String methodName = "set" + WordUtils.capitalize(thePropertyName);
    try {/*  w w  w . ja v a  2 s.  co  m*/
        return theClassToIntrospect.getMethod(methodName, theTargetArgumentType);
    } catch (NoSuchMethodException e) {
        //fall through
    } catch (SecurityException e) {
        throw new ConfigurationException(
                "Failed to scan class '" + theClassToIntrospect + "' because of a security exception", e);
    }
    throw new NoSuchFieldException(theClassToIntrospect + " has an mutator for field " + thePropertyName
            + " but it does not return type " + theTargetArgumentType);

}

From source file:com.cassius.spring.assembly.test.common.reflect.FieldWriter.java

/**
 * New instance./*  w  w w  . ja  va 2 s.c  o m*/
 *
 * @param object the object
 * @param fieldName the field name
 * @param value the value
 * @return the field writer
 * @throws NoSuchFieldException the no such field exception
 */
public static FieldWriter newInstance(Object object, String fieldName, Object value)
        throws NoSuchFieldException {
    Class<?> clazz = object.getClass();
    while (clazz != Object.class) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            if (field != null) {
                return new FieldWriter(object, field, value);
            }
        } catch (NoSuchFieldException ignore) {
        }
        clazz = clazz.getSuperclass();
    }
    throw new NoSuchFieldException(fieldName + "not exist");
}

From source file:app.commons.ReflectionUtils.java

/**
 * We cannot use <code>clazz.getField(String fieldName)</code> because it only returns public fields.
 *///w  ww  . ja v  a 2  s. c o  m
public static Field getField(Class clazz, String fieldName) {
    Class currentClass = clazz;
    while (currentClass != null) {
        try {
            Field field = currentClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {
            currentClass = currentClass.getSuperclass();
        }
    }
    throw new RuntimeException(new NoSuchFieldException(fieldName));
}

From source file:org.jkcsoft.java.util.Beans.java

public static Object get(Object bean, String propName) throws Exception {
    PropertyDescriptor pd = getPropertyDescriptor(bean, propName);

    if (pd == null) {
        throw new NoSuchFieldException("Unknown property: " + propName);
    }//from ww  w  .  j  a va 2 s. c  o  m

    Method getter = pd.getReadMethod();
    if (getter == null) {
        throw new NoSuchMethodException("No read method for: " + propName);
    }

    return getter.invoke(bean, new Object[] {});
}

From source file:com.manydesigns.elements.reflection.CommonsConfigurationAccessor.java

public PropertyAccessor getProperty(String propertyName) throws NoSuchFieldException {
    for (CommonsConfigurationEntryAccessor current : accessors) {
        if (current.getName().equals(propertyName)) {
            return current;
        }/*from  w w w  .ja  va  2s.  c  om*/
    }
    throw new NoSuchFieldException(propertyName);
}