All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.proofpoint.jmx.GuiceInjectorIterator Maven / Gradle / Ivy

There is a newer version: 3.23
Show newest version
/*
 * Copyright 2010 Proofpoint, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.proofpoint.jmx;

import com.google.common.collect.Sets;
import com.google.inject.Injector;
import com.google.inject.Key;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

class GuiceInjectorIterator implements Iterator>, Iterable>
{
    private final Set> visited = Sets.newHashSet();
    private final Iterator> keyIterator;
    private final Injector injector;

    private boolean needsReset = true;
    private Class currentClass = null;
    private GuiceDependencyIterator currentDependencyIterator = null;

    /**
     * @param injector the injector to iterate over
     */
    public GuiceInjectorIterator(Injector injector)
    {
        this.injector = injector;
        keyIterator = injector.getBindings().keySet().iterator();
    }

    @Override
    public boolean hasNext()
    {
        checkReset();
        return (currentClass != null);
    }

    @Override
    public Class next()
    {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        needsReset = true;
        return currentClass;
    }

    @Override
    public Iterator> iterator()
    {
        return new GuiceInjectorIterator(injector);
    }

    @Override
    public void remove()
    {
        throw new UnsupportedOperationException();
    }

    private void checkReset()
    {
        if (!needsReset) {
            return;
        }
        needsReset = false;

        currentClass = null;
        if (currentDependencyIterator != null) {
            if (currentDependencyIterator.hasNext()) {
                currentClass = currentDependencyIterator.next();
            }
            else {
                currentDependencyIterator = null;
            }
        }

        while ((currentClass == null) && keyIterator.hasNext()) {
            Key key = keyIterator.next();
            currentClass = parseKey(visited, key);
            if (currentClass == null) {
                continue;
            }

            currentDependencyIterator = new GuiceDependencyIterator(key.getTypeLiteral());
            currentDependencyIterator = currentDependencyIterator.substituteVisitedSet(visited);
        }
    }

    static Class parseKey(Set> visited, Key key)
    {
        if (visited.contains(key)) {
            return null;
        }
        visited.add(key);

        Class clazz;
        Type type = key.getTypeLiteral().getType();
        if (type instanceof GenericArrayType) {
            type = ((GenericArrayType) type).getGenericComponentType();
        }
        if (type instanceof Class) {
            clazz = (Class) type;
        }
        else {
            clazz = key.getTypeLiteral().getRawType();
        }

        return clazz;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy