fi.luontola.buildtest.StubClasses.java Source code

Java tutorial

Introduction

Here is the source code for fi.luontola.buildtest.StubClasses.java

Source

// Copyright  2011-2013 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package fi.luontola.buildtest;

import com.google.common.collect.AbstractIterator;
import com.google.common.io.Closeables;
import org.objectweb.asm.tree.ClassNode;

import java.io.*;
import java.util.*;

public class StubClasses implements Iterable<ClassNode> {

    private final List<Class<?>> classes;

    public StubClasses(Class<?>... classes) {
        this.classes = Arrays.asList(classes);
    }

    @Override
    public Iterator<ClassNode> iterator() {
        final Iterator<Class<?>> it = classes.iterator();
        return new AbstractIterator<ClassNode>() {
            @Override
            protected ClassNode computeNext() {
                if (!it.hasNext()) {
                    return endOfData();
                }
                Class<?> clazz = it.next();
                InputStream in = getClass().getResourceAsStream("/" + clazz.getName().replace('.', '/') + ".class");
                try {
                    return AsmUtils.readClass(in);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                } finally {
                    Closeables.closeQuietly(in);
                }
            }
        };
    }
}