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

kendal.model.builders.ForestBuilder Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
package kendal.model.builders;

import java.util.HashSet;
import java.util.Set;

import javax.lang.model.element.Element;

import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import kendal.model.Node;

public class ForestBuilder {

    private final Trees trees;

    public ForestBuilder(Trees trees) {
        this.trees = trees;
    }

    public Set buildForest(Set elements) {
        final Set compilationUnits = toCompilationUnits(elements);
        Set forest = new HashSet<>();
        compilationUnits.forEach(compilationUnit -> forest.add(TreeBuilder.buildTree(compilationUnit)));
        return forest;
    }

    private Set toCompilationUnits(Set elements) {
        final Set compilationUnits = new HashSet<>();
        for (Element element : elements) {
            JCCompilationUnit unit = toUnit(element);
            if (unit == null) continue;
            compilationUnits.add(unit);
        }
        return compilationUnits;
    }

    private JCCompilationUnit toUnit(Element element) {
        TreePath path = trees == null ? null : trees.getPath(element);
        return path != null ? (JCCompilationUnit) path.getCompilationUnit() : null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy