com.facebook.buck.jvm.java.Classpaths.java Source code

Java tutorial

Introduction

Here is the source code for com.facebook.buck.jvm.java.Classpaths.java

Source

/*
 * Copyright 2013-present Facebook, 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.facebook.buck.jvm.java;

import com.facebook.buck.rules.BuildRule;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;

import java.nio.file.Path;
import java.util.Set;

public class Classpaths {
    private Classpaths() {
        // Utility class
    }

    /**
     * Include the classpath entries from all JavaLibraryRules that have a direct line of lineage
     * to this rule through other JavaLibraryRules. For example, in the following dependency graph:
     *
     *        A
     *      /   \
     *     B     C
     *    / \   / \
     *    D E   F G
     *
     * If all of the nodes correspond to BuildRules that implement JavaLibraryRule except for
     * B (suppose B is a Genrule), then A's classpath will include C, F, and G, but not D and E.
     * This is because D and E are used to generate B, but do not contribute .class files to things
     * that depend on B. However, if C depended on E as well as F and G, then E would be included in
     * A's classpath.
     */
    public static ImmutableSetMultimap<JavaLibrary, Path> getClasspathEntries(Set<BuildRule> deps) {
        final ImmutableSetMultimap.Builder<JavaLibrary, Path> classpathEntries = ImmutableSetMultimap.builder();
        for (BuildRule dep : deps) {
            JavaLibrary library = null;
            if (dep instanceof JavaLibrary) {
                library = (JavaLibrary) dep;
            }

            if (library != null) {
                classpathEntries.putAll(library.getTransitiveClasspathEntries());
            }
        }
        return classpathEntries.build();
    }

    public static ImmutableSet<JavaLibrary> getClasspathDeps(Iterable<BuildRule> deps) {
        ImmutableSet.Builder<JavaLibrary> classpathDeps = ImmutableSet.builder();
        for (BuildRule dep : deps) {
            if (dep instanceof JavaLibrary) {
                classpathDeps.addAll(((JavaLibrary) dep).getTransitiveClasspathDeps());
            }
        }
        return classpathDeps.build();
    }

}