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:Main.java

public static Field getField(Class<?> klass, String member) throws NoSuchFieldException {
    try {/*from   w w w.  java  2  s.c om*/
        return klass.getDeclaredField(member);
    } catch (NoSuchFieldException e) {
        if (klass.getSuperclass() != null)
            return getField(klass.getSuperclass(), member);
        else
            throw new NoSuchFieldException(String.format("Class does not contain member %s!", member));
    }
}

From source file:Main.java

private static Field findField(Object instance, String name) throws NoSuchFieldException {
    for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {/*from   ww  w.j  av  a  2  s. com*/
            Field f = clazz.getDeclaredField(name);
            f.setAccessible(true);
            return f;
        } catch (NoSuchFieldException e) {
        }
    }
    throw new NoSuchFieldException("Unable to find field " + name + " in " + instance.getClass());
}

From source file:Main.java

private static Field getField(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Field field = null;// ww w.j  a va  2 s  .  com
    while (objectClass != null && field == null) {
        try {
            field = objectClass.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (field != null) {
        field.setAccessible(true);
    } else {
        throw new NoSuchFieldException(fieldName);
    }
    return field;
}

From source file:Main.java

public static Object getDeclaredField(Object obj, Class<?> cls, String fieldName) throws NoSuchFieldException {
    if (obj == null || cls == null || fieldName == null) {
        throw new IllegalArgumentException("parameter can not be null!");
    }/*from w w  w  . j av a2  s  . co m*/
    try {
        Field declaredField = cls.getDeclaredField(fieldName);
        declaredField.setAccessible(true);
        return declaredField.get(obj);
    } catch (Exception e) {
        throw new NoSuchFieldException(fieldName);
    }
}

From source file:Main.java

private static Field getField(Class thisClass, String fieldName) throws NoSuchFieldException {

    if (fieldName == null) {
        throw new NoSuchFieldException("Error field !");
    }/*from w  ww .ja  va2 s . c om*/

    Field field = thisClass.getDeclaredField(fieldName);
    return field;
}

From source file:Main.java

/**
 * Locates a given field anywhere in the class inheritance hierarchy.
 *
 * @param instance an object to search the field into.
 * @param name field name//from  w w w .j a  v a 2  s  .  com
 * @return a field object
 * @throws NoSuchFieldException if the field cannot be located
 */
static Field findField(Object instance, String name) throws NoSuchFieldException {
    for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {
            Field field = clazz.getDeclaredField(name);

            if (!field.isAccessible()) {
                field.setAccessible(true);
            }

            return field;
        } catch (NoSuchFieldException e) {
            // ignore and search next
        }
    }

    throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}

From source file:Main.java

static Method getMethod(String fieldName, Class<?> objectClass) throws NoSuchFieldException {
    Method finalMethod = null;//from  ww  w. j av a  2s .c  om
    while (objectClass != null && finalMethod == null) {
        for (Method method : objectClass.getDeclaredMethods()) {
            if (method.getName().equals(fieldName)) {
                Class<?>[] paramsType = method.getParameterTypes();
                if (paramsType.length == 0) {
                    finalMethod = method;
                    break;
                } else if (paramsType.length == 1) {
                    if (paramsType[0].equals(Context.class) || View.class.isAssignableFrom(paramsType[0])) {
                        finalMethod = method;
                        break;
                    }
                }

            }
        }
        if (finalMethod == null) {
            objectClass = objectClass.getSuperclass();
        }
    }
    if (finalMethod == null) {
        throw new NoSuchFieldException(fieldName);
    }
    return finalMethod;
}

From source file:IntrospectionUtil.java

public static Field findField(Class clazz, String targetName, Class targetType, boolean checkInheritance,
        boolean strictType) throws NoSuchFieldException {
    if (clazz == null)
        throw new NoSuchFieldException("No class");
    if (targetName == null)
        throw new NoSuchFieldException("No field name");

    try {//  w  w w. jav a  2 s .c om
        Field field = clazz.getDeclaredField(targetName);
        if (strictType) {
            if (field.getType().equals(targetType))
                return field;
        } else {
            if (field.getType().isAssignableFrom(targetType))
                return field;
        }
        if (checkInheritance) {
            return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType,
                    strictType);
        } else
            throw new NoSuchFieldException("No field with name " + targetName + " in class " + clazz.getName()
                    + " of type " + targetType);
    } catch (NoSuchFieldException e) {
        return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType,
                strictType);
    }
}

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

@SuppressWarnings("unchecked")
public static <T> T getDeclaredFieldValue(Object object, String propertyName) throws NoSuchFieldException {
    Assert.notNull(object);//  w  w w .j av a  2  s . co  m
    Assert.hasText(propertyName);

    Field field = getDeclaredField(object.getClass(), propertyName);

    boolean accessible = field.isAccessible();
    Object result = null;
    synchronized (field) {
        field.setAccessible(true);
        try {
            result = field.get(object);
        } catch (IllegalAccessException e) {
            throw new NoSuchFieldException("No such field: " + object.getClass() + '.' + propertyName);
        } finally {
            field.setAccessible(accessible);
        }
    }
    return (T) result;
}

From source file:Main.java

private static Object getSupperField(Object obj, Class<?> cls, String fieldName) throws NoSuchFieldException {
    Class superclass = cls.getSuperclass();
    while (superclass != null) {
        try {//w w w  .  j av a  2  s. co  m
            return getDeclaredField(obj, superclass, fieldName);
        } catch (NoSuchFieldException e) {
            try {
                superclass = superclass.getSuperclass();
            } catch (Exception e2) {
                superclass = null;
            }
        }
    }
    throw new NoSuchFieldException(fieldName);
}