Skip to content

Commit

Permalink
Improve readability, reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
guw committed Nov 26, 2024
1 parent db8b966 commit f786a36
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,21 @@
*/
public class BazelClasspathContainer implements IClasspathContainer, Serializable {

private static final long serialVersionUID = 390898179243551622L;
private static final long serialVersionUID = 1010694752435706159L;

private final IPath path;
private final IClasspathEntry[] classpath;
private final IClasspathEntry[] transitiveClasspath;
private final IClasspathEntry[] additionalRuntimeClasspath;

public BazelClasspathContainer(IPath path, IClasspathEntry[] classpath, IClasspathEntry[] transitiveClasspath) {
public BazelClasspathContainer(IPath path, IClasspathEntry[] classpath,
IClasspathEntry[] additionalRuntimeClasspath) {
this.path = path;
this.classpath = requireNonNull(classpath);
this.transitiveClasspath = requireNonNull(transitiveClasspath);
this.additionalRuntimeClasspath = requireNonNull(additionalRuntimeClasspath);
}

public IClasspathEntry[] getAdditionalRuntimeClasspathEntries() {
return additionalRuntimeClasspath;
}

@Override
Expand All @@ -75,11 +80,16 @@ public String getDescription() {
}

public IClasspathEntry[] getFullClasspath() {
if (transitiveClasspath.length == 0) {
if (additionalRuntimeClasspath.length == 0) {
return classpath;
}
var fullClasspath = Arrays.copyOf(classpath, classpath.length + transitiveClasspath.length);
System.arraycopy(transitiveClasspath, 0, fullClasspath, classpath.length, transitiveClasspath.length);
var fullClasspath = Arrays.copyOf(classpath, classpath.length + additionalRuntimeClasspath.length);
System.arraycopy(
additionalRuntimeClasspath,
0,
fullClasspath,
classpath.length,
additionalRuntimeClasspath.length);
return fullClasspath;
}

Expand All @@ -92,13 +102,4 @@ public int getKind() {
public IPath getPath() {
return path;
}

/**
* A array of transitive classpath entries.
*
* @return
*/
public IClasspathEntry[] getTransitiveEntries() {
return transitiveClasspath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ TargetProvisioningStrategy getTargetProvisioningStrategy(BazelWorkspace bazelWor
* @param monitor
* @throws CoreException
*/
public void patchClasspathContainer(BazelProject bazelProject, ClasspathHolder classpath, IProgressMonitor progress)
public void patchClasspathContainer(BazelProject bazelProject, CompileAndRuntimeClasspath classpath, IProgressMonitor progress)
throws CoreException {
var monitor = SubMonitor.convert(progress);
try {
Expand Down Expand Up @@ -301,7 +301,7 @@ public void persistAttachedSourcesAndJavadoc(IJavaProject project, IClasspathCon
DEFAULT_CLASSPATH,
monitor.split(1, SUPPRESS_ALL_LABELS));
entries = configureClasspathWithSourceAttachments(
classpaths.get(bazelProject).compile(),
classpaths.get(bazelProject).compileEntries(),
null /* no props */,
monitor);
for (IClasspathEntry entry : entries) {
Expand Down Expand Up @@ -340,19 +340,19 @@ public void persistAttachedSourcesAndJavadoc(IJavaProject project, IClasspathCon
}
}

void saveAndSetContainer(IJavaProject javaProject, ClasspathHolder classpath, IProgressMonitor monitor)
void saveAndSetContainer(IJavaProject javaProject, CompileAndRuntimeClasspath classpath, IProgressMonitor monitor)
throws CoreException, JavaModelException {
var containerEntry = getBazelContainerEntry(javaProject);
var path = containerEntry != null ? containerEntry.getPath()
: new Path(BazelCoreSharedContstants.CLASSPATH_CONTAINER_ID);

var sourceAttachmentProperties = getSourceAttachmentProperties(javaProject.getProject());
var transativeClasspath = classpath.transitive().stream().map(ClasspathEntry::build).collect(toList());
var transativeClasspath = classpath.additionalRuntimeEntries().stream().map(ClasspathEntry::build).collect(toList());

var container = new BazelClasspathContainer(
path,
configureClasspathWithSourceAttachments(
classpath.compile(),
classpath.compileEntries(),
sourceAttachmentProperties,
monitor.slice(1)),
transativeClasspath.toArray(new IClasspathEntry[transativeClasspath.size()]));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*-
* Copyright (c) 2024 Salesforce and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Salesforce - adapted from M2E, JDT or other Eclipse project
*/
package com.salesforce.bazel.eclipse.core.classpath;

import java.util.Collection;
import java.util.LinkedHashSet;

import com.salesforce.bazel.eclipse.core.model.discovery.classpath.ClasspathEntry;

/**
* Represents a classpath as two parts, compile being the classpath entries that are loaded into the project model, and
* transitive as classpath that are part of the transitive set for runtime only.
*/
public record CompileAndRuntimeClasspath(
Collection<ClasspathEntry> compileEntries,
Collection<ClasspathEntry> additionalRuntimeEntries) {

public static class Builder {
private final LinkedHashSet<ClasspathEntry> compileEntries = new LinkedHashSet<>();
private final LinkedHashSet<ClasspathEntry> additionalRuntimeEntries = new LinkedHashSet<>();

public boolean addCompileEntry(ClasspathEntry entry) {
return compileEntries.add(entry);
}

public boolean addRuntimeEntry(ClasspathEntry entry) {
return additionalRuntimeEntries.add(entry);
}

public CompileAndRuntimeClasspath build() {
return new CompileAndRuntimeClasspath(compileEntries, additionalRuntimeEntries);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.eclipse.core.runtime.jobs.Job;

import com.google.idea.blaze.base.model.primitives.Label;
import com.salesforce.bazel.eclipse.core.classpath.ClasspathHolder;
import com.salesforce.bazel.eclipse.core.classpath.CompileAndRuntimeClasspath;
import com.salesforce.bazel.eclipse.core.classpath.InitializeOrRefreshClasspathJob;
import com.salesforce.bazel.eclipse.core.model.BazelProject;
import com.salesforce.bazel.eclipse.core.model.BazelTarget;
Expand Down Expand Up @@ -172,11 +172,13 @@ public IStatus runInWorkspace(final IProgressMonitor monitor) throws CoreExcepti
}

if (modified) {
var transitive = Arrays.stream(container.getTransitiveEntries())
var additionalRuntimeEntries = Arrays.stream(container.getAdditionalRuntimeClasspathEntries())
.map(ClasspathEntry::fromExisting)
.collect(toList());
classpathManager
.patchClasspathContainer(bazelProject, new ClasspathHolder(classpath, transitive), monitor);
classpathManager.patchClasspathContainer(
bazelProject,
new CompileAndRuntimeClasspath(classpath, additionalRuntimeEntries),
monitor);
}
return Status.OK_STATUS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,46 +624,6 @@ protected void analyzeProjectInfo(BazelProject project, JavaProjectInfo javaInfo
}
}

/**
* Calculates the java_library and java_imports for the provided targets, limited by the depth
*
* @param workspace
* the bazel workspace
* @param targetsToBuild
* a list of targets as part of the build to query their dependencies
* @param dependencyDepth
* the depth in the dependency graph to traverse and include in the result
* @return a set of java_library and java_imports, or null, if partial classpath is disabled
* @throws CoreException
*/
protected final Set<BazelLabel> calculateWorkspaceDependencies(BazelWorkspace workspace,
List<BazelLabel> targetsToBuild) throws CoreException {

var classpathDepth = getClasspathDepthValue(workspace);
if (classpathDepth <= 0) {
return null;
}
if (classpathDepth == 1) {
return Collections.emptySet();
}
var targetLabels = targetsToBuild.stream().map(BazelLabel::toString).collect(joining(" + "));
return workspace.getCommandExecutor()
.runQueryWithoutLock(
new BazelQueryForLabelsCommand(
workspace.getLocation().toPath(),
format(
"kind(java_library, deps(%s, %d)) + kind(java_import, deps(%s, %d))",
targetLabels,
classpathDepth,
targetLabels,
classpathDepth),
true,
format("Querying for depdendencies for projects: %s", targetLabels)))
.stream()
.map(BazelLabel::new)
.collect(toSet());
}

/**
* Collects base Java information for a given project and the targets it represents.
* <p>
Expand Down Expand Up @@ -1734,6 +1694,53 @@ public List<BazelProject> provisionProjectsForSelectedTargets(Collection<BazelTa
}
}

/**
* Queries the Bazel graph for java_library and java_imports of the provided targets, limited by the classpath_depth
* in the project view.
* <p>
* The outcome of this method is intended to be used in
* {@link #computeClasspaths(Collection, BazelWorkspace, com.salesforce.bazel.eclipse.core.classpath.BazelClasspathScope, IProgressMonitor)}
* implementation for filtering/reducing the compile classpath.
* <p>
* A depth of 0 will return <code>null</code>, which indicates unlimited/no filtering. A depth of 1 will return an
* empty collection, which indicates "direct dependencies only" for compilation.
* </p>
*
* @param workspace
* the bazel workspace
* @param targetsToBuild
* a list of targets as part of the build to query their dependencies
* @return a set of java_library and java_imports, or <code>null</code>, if partial classpath is disabled
* @throws CoreException
*/
protected final Set<BazelLabel> queryForDepsWithClasspathDepth(BazelWorkspace workspace,
List<BazelLabel> targetsToBuild) throws CoreException {

var classpathDepth = getClasspathDepthValue(workspace);
if (classpathDepth <= 0) {
return null;
}
if (classpathDepth == 1) {
return Collections.emptySet();
}
var targetLabels = targetsToBuild.stream().map(BazelLabel::toString).collect(joining(" + "));
return workspace.getCommandExecutor()
.runQueryWithoutLock(
new BazelQueryForLabelsCommand(
workspace.getLocation().toPath(),
format(
"kind(java_library, deps(%s, %d)) + kind(java_import, deps(%s, %d))",
targetLabels,
classpathDepth,
targetLabels,
classpathDepth),
true,
format("Querying for depdendencies for projects: %s", targetLabels)))
.stream()
.map(BazelLabel::new)
.collect(toSet());
}

private java.nio.file.Path resolveJavaHome(BazelWorkspace workspace, String javaHome, String description)
throws CoreException {
var resolvedJavaHomePath = java.nio.file.Path.of(javaHome);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.google.idea.blaze.base.model.primitives.WildcardTargetPattern;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.salesforce.bazel.eclipse.core.classpath.BazelClasspathScope;
import com.salesforce.bazel.eclipse.core.classpath.ClasspathHolder;
import com.salesforce.bazel.eclipse.core.classpath.CompileAndRuntimeClasspath;
import com.salesforce.bazel.eclipse.core.model.BazelPackage;
import com.salesforce.bazel.eclipse.core.model.BazelProject;
import com.salesforce.bazel.eclipse.core.model.BazelTarget;
Expand Down Expand Up @@ -195,7 +195,7 @@ public boolean isAllowedDependencyPath(BazelPackage fromPackage, BazelPackage to
new TargetDiscoveryAndProvisioningExtensionLookup();

@Override
public Map<BazelProject, ClasspathHolder> computeClasspaths(Collection<BazelProject> bazelProjects,
public Map<BazelProject, CompileAndRuntimeClasspath> computeClasspaths(Collection<BazelProject> bazelProjects,
BazelWorkspace workspace, BazelClasspathScope scope, IProgressMonitor progress) throws CoreException {
LOG.debug("Computing classpath for projects: {}", bazelProjects);
try {
Expand Down Expand Up @@ -239,7 +239,7 @@ public Map<BazelProject, ClasspathHolder> computeClasspaths(Collection<BazelProj
// use the hints to avoid circular dependencies between projects in Eclipse
var circularDependenciesHelper = new CircularDependenciesHelper(workspace.getBazelProjectView().targets());

Map<BazelProject, ClasspathHolder> classpathsByProject = new HashMap<>();
Map<BazelProject, CompileAndRuntimeClasspath> classpathsByProject = new HashMap<>();
for (BazelProject bazelProject : bazelProjects) {
monitor.subTask("Analyzing: " + bazelProject);
monitor.checkCanceled();
Expand Down Expand Up @@ -367,7 +367,7 @@ public Map<BazelProject, ClasspathHolder> computeClasspaths(Collection<BazelProj
}
}

classpathsByProject.put(bazelProject, new ClasspathHolder(classpath, Collections.emptyList()));
classpathsByProject.put(bazelProject, new CompileAndRuntimeClasspath(classpath, Collections.emptyList()));
monitor.worked(1);
}

Expand Down
Loading

0 comments on commit f786a36

Please sign in to comment.