Friday, March 13, 2015

NoSuchFieldException

java.lang.NoSuchFieldException

NoSuchFieldException is described in the javadoc comments as:

Signals that the class doesn't have a field of a specified name.
author: unascribed version: 1.14, 12/19/03 since: JDK1.1

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

com.sun.corba.se.impl.orbutil.ObjectStreamClass_1_3_1.translateFields(Object)

private static Object[] translateFields(Object objs[]) throws NoSuchFieldException {
    try {
        java.io.ObjectStreamField fields[] = (java.io.ObjectStreamField[]) objs;
        Object translation[] = null;
        if (translatedFields == null) translatedFields = new Hashtable();
        translation = (Object[]) translatedFields.get(fields);
        if (translation != null) return translation; else {
            Class osfClass = com.sun.corba.se.impl.orbutil.ObjectStreamField.class;
            translation = (Object[]) java.lang.reflect.Array.newInstance(osfClass, objs.length);
            Object arg[] = new Object[2];
            Class types[] = { String.class, Class.class };
            Constructor constructor = osfClass.getDeclaredConstructor(types);
            for (int i = fields.length - 1; i >= 0; i--) {
                arg[0] = fields[i].getName();
                arg[1] = fields[i].getType();
                translation[i] = constructor.newInstance(arg);
            }
            translatedFields.put(fields, translation);
        }
        return (Object[]) translation;
    } catch (Throwable t) {
        throw new NoSuchFieldException();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Class.getDeclaredField(String)

/**
     * Returns a <code>Field</code> object that reflects the specified declared
     * field of the class or interface represented by this <code>Class</code>
     * object. The <code>name</code> parameter is a <code>String</code> that
     * specifies the simple name of the desired field.  Note that this method
     * will not reflect the <code>length</code> field of an array class.
     * @param name the name of the field
     * @return the <code>Field</code> object for the specified field in this
     * class
     * @exception NoSuchFieldException if a field with the specified name is
     *              not found.
     * @exception NullPointerException if <code>name</code> is <code>null</code>
     * @exception  SecurityException
     *             If a security manager, <i>s</i>, is present and any of the
     *             following conditions is met:
     *             <ul>
     *             <li> invocation of 
     *             <tt>{@link SecurityManager#checkMemberAccess
     *             s.checkMemberAccess(this, Member.DECLARED)}</tt> denies
     *             access to the declared field
     *             <li> the caller's class loader is not the same as or an
     *             ancestor of the class loader for the current class and
     *             invocation of <tt>{@link SecurityManager#checkPackageAccess
     *             s.checkPackageAccess()}</tt> denies access to the package
     *             of this class
     *             </ul>
     * @since JDK1.1
     */
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException {
    checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
    Field field = searchFields(privateGetDeclaredFields(false), name);
    if (field == null) {
        throw new NoSuchFieldException(name);
    }
    return field;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.lang.Class.getField(String)

/**
     * Returns a <code>Field</code> object that reflects the specified public
     * member field of the class or interface represented by this
     * <code>Class</code> object. The <code>name</code> parameter is a
     * <code>String</code> specifying the simple name of the desired field.
     *  The field to be reflected is determined by the algorithm that
     * follows.  Let C be the class represented by this object:
     * <OL>
     * <LI> If C declares a public field with the name specified, that is the
     *      field to be reflected.</LI>
     * <LI> If no field was found in step 1 above, this algorithm is applied
     *      recursively to each direct superinterface of C. The direct
     *      superinterfaces are searched in the order they were declared.</LI>
     * <LI> If no field was found in steps 1 and 2 above, and C has a
     *      superclass S, then this algorithm is invoked recursively upon S.
     *      If C has no superclass, then a <code>NoSuchFieldException</code>
     *      is thrown.</LI>
     * </OL>
     *  See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
     * @param name the field name
     * @return  the <code>Field</code> object of this class specified by 
     * <code>name</code>
     * @exception NoSuchFieldException if a field with the specified name is
     *              not found.
     * @exception NullPointerException if <code>name</code> is <code>null</code>
     * @exception  SecurityException
     *             If a security manager, <i>s</i>, is present and any of the
     *             following conditions is met:
     *             <ul>
     *             <li> invocation of 
     *             <tt>{@link SecurityManager#checkMemberAccess
     *             s.checkMemberAccess(this, Member.PUBLIC)}</tt> denies
     *             access to the field
     *             <li> the caller's class loader is not the same as or an
     *             ancestor of the class loader for the current class and
     *             invocation of <tt>{@link SecurityManager#checkPackageAccess
     *             s.checkPackageAccess()}</tt> denies access to the package
     *             of this class
     *             </ul>
     * @since JDK1.1
     */
public Field getField(String name) throws NoSuchFieldException, SecurityException {
    checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
    Field field = getField0(name);
    if (field == null) {
        throw new NoSuchFieldException(name);
    }
    return field;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment