use of com.google.idea.blaze.base.ideinfo.JavaIdeInfo in project intellij by bazelbuild.
the class BlazeClassJarProvider method getModuleExternalLibraries.
@Override
public List<File> getModuleExternalLibraries(Module module) {
BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (blazeProjectData == null) {
return ImmutableList.of();
}
TargetMap targetMap = blazeProjectData.targetMap;
ArtifactLocationDecoder decoder = blazeProjectData.artifactLocationDecoder;
AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
TargetIdeInfo target = targetMap.get(registry.getTargetKey(module));
if (target == null) {
return ImmutableList.of();
}
AppResourceRepository repository = AppResourceRepository.getOrCreateInstance(module);
ImmutableList.Builder<File> results = ImmutableList.builder();
for (TargetKey dependencyTargetKey : TransitiveDependencyMap.getInstance(project).getTransitiveDependencies(target.key)) {
TargetIdeInfo dependencyTarget = targetMap.get(dependencyTargetKey);
if (dependencyTarget == null) {
continue;
}
// Add all import jars as external libraries.
JavaIdeInfo javaIdeInfo = dependencyTarget.javaIdeInfo;
if (javaIdeInfo != null) {
for (LibraryArtifact jar : javaIdeInfo.jars) {
if (jar.classJar != null && jar.classJar.isSource()) {
results.add(decoder.decode(jar.classJar));
}
}
}
// Tell ResourceClassRegistry which repository contains our resources and the java packages of
// the resources that we're interested in.
// When the class loader tries to load a custom view, and the view references resource
// classes, layoutlib will ask the class loader for these resource classes.
// If these resource classes are in a separate jar from the target (i.e., in a dependency),
// then offering their jars will lead to a conflict in the resource IDs.
// So instead, the resource class generator will produce dummy resource classes with
// non-conflicting IDs to satisfy the class loader.
// The resource repository remembers the dynamic IDs that it handed out and when the layoutlib
// calls to ask about the name and content of a given resource ID, the repository can just
// answer what it has already stored.
AndroidIdeInfo androidIdeInfo = dependencyTarget.androidIdeInfo;
if (androidIdeInfo != null && !Strings.isNullOrEmpty(androidIdeInfo.resourceJavaPackage) && repository != null) {
ResourceClassRegistry.get(module.getProject()).addLibrary(repository, androidIdeInfo.resourceJavaPackage);
}
}
return results.build();
}
use of com.google.idea.blaze.base.ideinfo.JavaIdeInfo in project intellij by bazelbuild.
the class BlazeJavaWorkspaceImporter method buildLibraries.
private ImmutableMap<LibraryKey, BlazeJarLibrary> buildLibraries(WorkspaceBuilder workspaceBuilder, TargetMap targetMap, List<TargetIdeInfo> libraryTargets, List<TargetIdeInfo> protoLibraries) {
// Build library maps
Multimap<TargetKey, BlazeJarLibrary> targetKeyToLibrary = ArrayListMultimap.create();
Map<String, BlazeJarLibrary> jdepsPathToLibrary = Maps.newHashMap();
// Add any output jars from source rules
for (TargetKey key : workspaceBuilder.outputJarsFromSourceTargets.keySet()) {
Collection<BlazeJarLibrary> jars = workspaceBuilder.outputJarsFromSourceTargets.get(key);
targetKeyToLibrary.putAll(key, jars);
for (BlazeJarLibrary library : jars) {
addLibraryToJdeps(jdepsPathToLibrary, library);
}
}
for (TargetIdeInfo target : libraryTargets) {
JavaIdeInfo javaIdeInfo = target.javaIdeInfo;
if (javaIdeInfo == null) {
continue;
}
List<LibraryArtifact> allJars = Lists.newArrayList();
allJars.addAll(javaIdeInfo.jars);
Collection<BlazeJarLibrary> libraries = allJars.stream().map(BlazeJarLibrary::new).collect(Collectors.toList());
targetKeyToLibrary.putAll(target.key, libraries);
for (BlazeJarLibrary library : libraries) {
addLibraryToJdeps(jdepsPathToLibrary, library);
}
}
// proto legacy jdeps support
for (TargetIdeInfo target : protoLibraries) {
ProtoLibraryLegacyInfo protoLibraryLegacyInfo = target.protoLibraryLegacyInfo;
if (protoLibraryLegacyInfo == null) {
continue;
}
for (LibraryArtifact libraryArtifact : Iterables.concat(protoLibraryLegacyInfo.jarsV1, protoLibraryLegacyInfo.jarsMutable, protoLibraryLegacyInfo.jarsImmutable)) {
addLibraryToJdeps(jdepsPathToLibrary, new BlazeJarLibrary(libraryArtifact));
}
}
Map<LibraryKey, BlazeJarLibrary> result = Maps.newHashMap();
// Collect jars from jdep references
for (String jdepsPath : workspaceBuilder.jdeps) {
if (sourceFilter.jdepsPathsForExcludedJars.contains(jdepsPath)) {
continue;
}
BlazeJarLibrary library = jdepsPathToLibrary.get(jdepsPath);
if (library == null) {
// It's in the target's jdeps, but our aspect never attached to the target building it
// Perhaps it's an implicit dependency, or not referenced in an attribute we propagate along
// Or it could be that this is a multi-configuration project, and jdeps refers to a
// configuration different from the one we picked for the TargetMap.
// Make a best-effort attempt to add it to the project anyway.
ExecutionPathFragmentAndRelativePath split = ExecutionPathFragmentAndRelativePath.split(jdepsPath);
ArtifactLocation location = ArtifactLocation.builder().setIsSource(false).setRootExecutionPathFragment(split.rootExecutionPathFragment).setRelativePath(split.relativePath).build();
library = new BlazeJarLibrary(new LibraryArtifact(location, null, ImmutableList.of()));
}
result.put(library.key, library);
}
// Collect jars referenced by direct deps from your working set
for (TargetKey deps : workspaceBuilder.directDeps) {
for (BlazeJarLibrary library : targetKeyToLibrary.get(deps)) {
result.put(library.key, library);
}
}
// Collect legacy proto libraries from direct deps
addProtoLegacyLibrariesFromDirectDeps(workspaceBuilder, targetMap, result);
// Collect generated jars from source rules
for (BlazeJarLibrary library : workspaceBuilder.generatedJarsFromSourceTargets) {
result.put(library.key, library);
}
return ImmutableMap.copyOf(result);
}
use of com.google.idea.blaze.base.ideinfo.JavaIdeInfo in project intellij by bazelbuild.
the class BlazeJavaWorkspaceImporter method addTargetAsSource.
private void addTargetAsSource(WorkspaceBuilder workspaceBuilder, TargetIdeInfo target, Collection<ArtifactLocation> javaSources) {
JavaIdeInfo javaIdeInfo = target.javaIdeInfo;
if (javaIdeInfo == null) {
return;
}
TargetKey targetKey = target.key;
Collection<String> jars = jdepsMap.getDependenciesForTarget(targetKey);
if (jars != null) {
workspaceBuilder.jdeps.addAll(jars);
}
// Add all deps if this rule is in the current working set
if (workingSet == null || workingSet.isTargetInWorkingSet(target)) {
// Add self, so we pick up our own gen jars if in working set
workspaceBuilder.directDeps.add(targetKey);
for (Dependency dep : target.dependencies) {
if (dep.dependencyType != DependencyType.COMPILE_TIME) {
continue;
}
// forward deps from java proto_library aspect targets
TargetIdeInfo depTarget = targetMap.get(dep.targetKey);
if (depTarget != null && Kind.JAVA_PROTO_LIBRARY_KINDS.contains(depTarget.kind)) {
workspaceBuilder.directDeps.addAll(depTarget.dependencies.stream().map(d -> d.targetKey).collect(Collectors.toList()));
} else {
workspaceBuilder.directDeps.add(dep.targetKey);
}
}
}
for (ArtifactLocation artifactLocation : javaSources) {
if (artifactLocation.isSource()) {
duplicateSourceDetector.add(targetKey, artifactLocation);
workspaceBuilder.sourceArtifacts.add(new SourceArtifact(targetKey, artifactLocation));
workspaceBuilder.addedSourceFiles.add(artifactLocation);
}
}
ArtifactLocation manifest = javaIdeInfo.packageManifest;
if (manifest != null) {
workspaceBuilder.javaPackageManifests.put(targetKey, manifest);
}
for (LibraryArtifact libraryArtifact : javaIdeInfo.jars) {
ArtifactLocation classJar = libraryArtifact.classJar;
if (classJar != null) {
workspaceBuilder.buildOutputJars.add(classJar);
}
}
workspaceBuilder.generatedJarsFromSourceTargets.addAll(javaIdeInfo.generatedJars.stream().map(BlazeJarLibrary::new).collect(Collectors.toList()));
if (javaIdeInfo.filteredGenJar != null) {
workspaceBuilder.generatedJarsFromSourceTargets.add(new BlazeJarLibrary(javaIdeInfo.filteredGenJar));
}
for (BlazeJavaSyncAugmenter augmenter : augmenters) {
augmenter.addJarsForSourceTarget(workspaceLanguageSettings, projectViewSet, target, workspaceBuilder.outputJarsFromSourceTargets.get(targetKey), workspaceBuilder.generatedJarsFromSourceTargets);
}
}
use of com.google.idea.blaze.base.ideinfo.JavaIdeInfo in project intellij by bazelbuild.
the class JdepsFileReader method doLoadJdepsFiles.
private JdepsState doLoadJdepsFiles(Project project, BlazeContext context, ArtifactLocationDecoder artifactLocationDecoder, @Nullable JdepsState oldState, Iterable<TargetIdeInfo> targetsToLoad) {
JdepsState state = new JdepsState();
if (oldState != null) {
state.targetToJdeps = Maps.newHashMap(oldState.targetToJdeps);
state.fileToTargetMap = Maps.newHashMap(oldState.fileToTargetMap);
}
Map<File, TargetKey> fileToTargetMap = Maps.newHashMap();
for (TargetIdeInfo target : targetsToLoad) {
assert target != null;
JavaIdeInfo javaIdeInfo = target.javaIdeInfo;
if (javaIdeInfo != null) {
ArtifactLocation jdepsFile = javaIdeInfo.jdepsFile;
if (jdepsFile != null) {
fileToTargetMap.put(artifactLocationDecoder.decode(jdepsFile), target.key);
}
}
}
List<File> updatedFiles = Lists.newArrayList();
List<File> removedFiles = Lists.newArrayList();
state.fileState = FileDiffer.updateFiles(oldState != null ? oldState.fileState : null, fileToTargetMap.keySet(), updatedFiles, removedFiles);
ListenableFuture<?> fetchFuture = PrefetchService.getInstance().prefetchFiles(project, updatedFiles, true, false);
if (!FutureUtil.waitForFuture(context, fetchFuture).timed("FetchJdeps", EventType.Prefetching).withProgressMessage("Reading jdeps files...").run().success()) {
return null;
}
for (File removedFile : removedFiles) {
TargetKey targetKey = state.fileToTargetMap.remove(removedFile);
if (targetKey != null) {
state.targetToJdeps.remove(targetKey);
}
}
AtomicLong totalSizeLoaded = new AtomicLong(0);
List<ListenableFuture<Result>> futures = Lists.newArrayList();
for (File updatedFile : updatedFiles) {
futures.add(submit(() -> {
totalSizeLoaded.addAndGet(updatedFile.length());
try (InputStream inputStream = new FileInputStream(updatedFile)) {
Deps.Dependencies dependencies = Deps.Dependencies.parseFrom(inputStream);
if (dependencies != null) {
List<String> dependencyStringList = Lists.newArrayList();
for (Deps.Dependency dependency : dependencies.getDependencyList()) {
// available for use in the same package
if (dependency.getKind() == Deps.Dependency.Kind.EXPLICIT || dependency.getKind() == Deps.Dependency.Kind.IMPLICIT) {
dependencyStringList.add(dependency.getPath());
}
}
TargetKey targetKey = fileToTargetMap.get(updatedFile);
return new Result(updatedFile, targetKey, dependencyStringList);
}
} catch (FileNotFoundException e) {
logger.info("Could not open jdeps file: " + updatedFile);
}
return null;
}));
}
try {
for (Result result : Futures.allAsList(futures).get()) {
if (result != null) {
state.fileToTargetMap.put(result.file, result.targetKey);
state.targetToJdeps.put(result.targetKey, result.dependencies);
}
}
context.output(PrintOutput.log(String.format("Loaded %d jdeps files, total size %dkB", updatedFiles.size(), totalSizeLoaded.get() / 1024)));
} catch (InterruptedException | ExecutionException e) {
logger.error(e);
return null;
}
return state;
}
use of com.google.idea.blaze.base.ideinfo.JavaIdeInfo in project intellij by bazelbuild.
the class IdeInfoFromProtobuf method makeTargetIdeInfo.
@Nullable
public static TargetIdeInfo makeTargetIdeInfo(IntellijIdeInfo.TargetIdeInfo message) {
Kind kind = getKind(message);
if (kind == null) {
return null;
}
TargetKey key = getKey(message);
ArtifactLocation buildFile = getBuildFile(message);
final Collection<Dependency> dependencies;
if (message.getDepsCount() > 0) {
dependencies = message.getDepsList().stream().map(IdeInfoFromProtobuf::makeDependency).collect(toList());
} else {
dependencies = Lists.newArrayListWithCapacity(message.getDependenciesCount() + message.getRuntimeDepsCount());
dependencies.addAll(makeDependencyListFromLabelList(message.getDependenciesList(), DependencyType.COMPILE_TIME));
dependencies.addAll(makeDependencyListFromLabelList(message.getRuntimeDepsList(), DependencyType.RUNTIME));
}
Collection<String> tags = ImmutableList.copyOf(message.getTagsList());
Collection<ArtifactLocation> sources = Lists.newArrayList();
CIdeInfo cIdeInfo = null;
if (message.hasCIdeInfo()) {
cIdeInfo = makeCIdeInfo(message.getCIdeInfo());
sources.addAll(cIdeInfo.sources);
sources.addAll(cIdeInfo.headers);
sources.addAll(cIdeInfo.textualHeaders);
}
CToolchainIdeInfo cToolchainIdeInfo = null;
if (message.hasCToolchainIdeInfo()) {
cToolchainIdeInfo = makeCToolchainIdeInfo(message.getCToolchainIdeInfo());
}
JavaIdeInfo javaIdeInfo = null;
if (message.hasJavaIdeInfo()) {
javaIdeInfo = makeJavaIdeInfo(message.getJavaIdeInfo());
Collection<ArtifactLocation> javaSources = makeArtifactLocationList(message.getJavaIdeInfo().getSourcesList());
sources.addAll(javaSources);
}
AndroidIdeInfo androidIdeInfo = null;
if (message.hasAndroidIdeInfo()) {
androidIdeInfo = makeAndroidIdeInfo(message.getAndroidIdeInfo());
}
AndroidSdkIdeInfo androidSdkIdeInfo = null;
if (message.hasAndroidSdkIdeInfo()) {
androidSdkIdeInfo = makeAndroidSdkIdeInfo(message.getAndroidSdkIdeInfo());
}
AndroidAarIdeInfo androidAarIdeInfo = null;
if (message.hasAndroidAarIdeInfo()) {
androidAarIdeInfo = makeAndroidAarIdeInfo(message.getAndroidAarIdeInfo());
}
PyIdeInfo pyIdeInfo = null;
if (message.hasPyIdeInfo()) {
pyIdeInfo = makePyIdeInfo(message.getPyIdeInfo());
sources.addAll(pyIdeInfo.sources);
}
GoIdeInfo goIdeInfo = null;
if (message.hasGoIdeInfo()) {
goIdeInfo = makeGoIdeInfo(message.getGoIdeInfo());
sources.addAll(goIdeInfo.sources);
}
JsIdeInfo jsIdeInfo = null;
if (message.hasJsIdeInfo()) {
jsIdeInfo = makeJsIdeInfo(message.getJsIdeInfo());
sources.addAll(jsIdeInfo.sources);
}
TsIdeInfo tsIdeInfo = null;
if (message.hasTsIdeInfo()) {
tsIdeInfo = makeTsIdeInfo(message.getTsIdeInfo());
sources.addAll(tsIdeInfo.sources);
}
DartIdeInfo dartIdeInfo = null;
if (message.hasDartIdeInfo()) {
dartIdeInfo = makeDartIdeInfo(message.getDartIdeInfo());
sources.addAll(dartIdeInfo.sources);
}
TestIdeInfo testIdeInfo = null;
if (message.hasTestInfo()) {
testIdeInfo = makeTestIdeInfo(message.getTestInfo());
}
ProtoLibraryLegacyInfo protoLibraryLegacyInfo = null;
if (message.hasProtoLibraryLegacyJavaIdeInfo()) {
protoLibraryLegacyInfo = makeProtoLibraryLegacyInfo(message.getProtoLibraryLegacyJavaIdeInfo());
}
JavaToolchainIdeInfo javaToolchainIdeInfo = null;
if (message.hasJavaToolchainIdeInfo()) {
javaToolchainIdeInfo = makeJavaToolchainIdeInfo(message.getJavaToolchainIdeInfo());
}
return new TargetIdeInfo(key, kind, buildFile, dependencies, tags, sources, cIdeInfo, cToolchainIdeInfo, javaIdeInfo, androidIdeInfo, androidSdkIdeInfo, androidAarIdeInfo, pyIdeInfo, goIdeInfo, jsIdeInfo, tsIdeInfo, dartIdeInfo, testIdeInfo, protoLibraryLegacyInfo, javaToolchainIdeInfo);
}
Aggregations