private Object searchForAdaptor()

in tapestry-framework/src/org/apache/tapestry/util/AdaptorRegistry.java [191:267]


    private Object searchForAdaptor(Class subjectClass)
    {
        LinkedList queue = null;
        Object result = null;

        if (LOG.isDebugEnabled())
            LOG.debug("Searching for adaptor for class " + Tapestry.getClassName(subjectClass));

        // Step one: work up through the class inheritance.

        Class searchClass = subjectClass;

        // Primitive types have null, not Object, as their parent
        // class.

        while (searchClass != Object.class && searchClass != null)
        {
            result = registrations.get(searchClass);
            if (result != null)
                return result;

            // Not an exact match.  If the search class
            // implements any interfaces, add them to the queue.

            Class[] interfaces = searchClass.getInterfaces();
            int length = interfaces.length;

            if (queue == null && length > 0)
                queue = new LinkedList();

            for (int i = 0; i < length; i++)
                queue.addLast(interfaces[i]);

            // Advance up to the next superclass

            searchClass = getSuperclass(searchClass);

        }

        // Ok, the easy part failed, lets start searching
        // interfaces.

        if (queue != null)
        {
            while (!queue.isEmpty())
            {
                searchClass = (Class) queue.removeFirst();

                result = registrations.get(searchClass);
                if (result != null)
                    return result;

                // Interfaces can extend other interfaces; add them
                // to the queue.

                Class[] interfaces = searchClass.getInterfaces();
                int length = interfaces.length;

                for (int i = 0; i < length; i++)
                    queue.addLast(interfaces[i]);
            }
        }

        // Not a match on interface; our last gasp is to check
        // for a registration for java.lang.Object

        result = registrations.get(Object.class);
        if (result != null)
            return result;

        // No match?  That's rare ... and an error.

        throw new IllegalArgumentException(
            Tapestry.format(
                "AdaptorRegistry.adaptor-not-found",
                Tapestry.getClassName(subjectClass)));
    }