Search in sources :

Example 1 with LibraryArtifact

use of com.google.idea.blaze.base.ideinfo.LibraryArtifact in project intellij by bazelbuild.

the class BlazeClassJarProvider method findModuleClassFile.

@Override
@Nullable
public VirtualFile findModuleClassFile(String className, Module module) {
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return null;
    }
    TargetMap targetMap = blazeProjectData.targetMap;
    ArtifactLocationDecoder decoder = blazeProjectData.artifactLocationDecoder;
    AndroidResourceModuleRegistry registry = AndroidResourceModuleRegistry.getInstance(project);
    TargetIdeInfo target = blazeProjectData.targetMap.get(registry.getTargetKey(module));
    if (target == null || target.javaIdeInfo == null) {
        return null;
    }
    // As a potential optimization, we could choose an arbitrary android_binary target
    // that depends on the library to provide a single complete resource jar,
    // instead of having to rely on dynamic class generation.
    // TODO: benchmark to see if optimization is worthwhile.
    String classNamePath = className.replace('.', File.separatorChar) + SdkConstants.DOT_CLASS;
    List<LibraryArtifact> jarsToSearch = Lists.newArrayList(target.javaIdeInfo.jars);
    jarsToSearch.addAll(TransitiveDependencyMap.getInstance(project).getTransitiveDependencies(target.key).stream().map(targetMap::get).filter(Objects::nonNull).flatMap(BlazeClassJarProvider::getNonResourceJars).collect(Collectors.toList()));
    List<File> missingClassJars = Lists.newArrayList();
    for (LibraryArtifact jar : jarsToSearch) {
        if (jar.classJar == null || jar.classJar.isSource()) {
            continue;
        }
        File classJarFile = decoder.decode(jar.classJar);
        VirtualFile classJarVF = VirtualFileSystemProvider.getInstance().getSystem().findFileByIoFile(classJarFile);
        if (classJarVF == null) {
            if (classJarFile.exists()) {
                missingClassJars.add(classJarFile);
            }
            continue;
        }
        VirtualFile classFile = findClassInJar(classJarVF, classNamePath);
        if (classFile != null) {
            return classFile;
        }
    }
    maybeRefreshJars(missingClassJars, pendingJarsRefresh);
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap) Nullable(javax.annotation.Nullable)

Example 2 with LibraryArtifact

use of com.google.idea.blaze.base.ideinfo.LibraryArtifact 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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AndroidResourceModuleRegistry(com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) ArtifactLocationDecoder(com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder) TargetKey(com.google.idea.blaze.base.ideinfo.TargetKey) JavaIdeInfo(com.google.idea.blaze.base.ideinfo.JavaIdeInfo) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) TargetMap(com.google.idea.blaze.base.ideinfo.TargetMap)

Example 3 with LibraryArtifact

use of com.google.idea.blaze.base.ideinfo.LibraryArtifact in project intellij by bazelbuild.

the class BlazeAndroidJavaSyncAugmenter method addJarsForSourceTarget.

@Override
public void addJarsForSourceTarget(WorkspaceLanguageSettings workspaceLanguageSettings, ProjectViewSet projectViewSet, TargetIdeInfo target, Collection<BlazeJarLibrary> jars, Collection<BlazeJarLibrary> genJars) {
    if (!workspaceLanguageSettings.isLanguageActive(LanguageClass.ANDROID)) {
        return;
    }
    AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
    if (androidIdeInfo == null) {
        return;
    }
    LibraryArtifact idlJar = androidIdeInfo.idlJar;
    if (idlJar != null) {
        genJars.add(new BlazeJarLibrary(idlJar));
    }
    Set<String> whitelistedGenResourcePaths = projectViewSet.listItems(GeneratedAndroidResourcesSection.KEY).stream().map(genfilesPath -> genfilesPath.relativePath).collect(Collectors.toSet());
    if (BlazeAndroidWorkspaceImporter.shouldGenerateResources(androidIdeInfo) && !BlazeAndroidWorkspaceImporter.shouldGenerateResourceModule(androidIdeInfo, whitelistedGenResourcePaths)) {
        // Add blaze's output unless it's a top level rule.
        // In these cases the resource jar contains the entire
        // transitive closure of R classes. It's unlikely this is wanted to resolve in the IDE.
        boolean discardResourceJar = target.kindIsOneOf(Kind.ANDROID_BINARY, Kind.ANDROID_TEST);
        if (!discardResourceJar) {
            LibraryArtifact resourceJar = androidIdeInfo.resourceJar;
            if (resourceJar != null) {
                jars.add(new BlazeJarLibrary(resourceJar));
            }
        }
    }
}
Also used : LanguageClass(com.google.idea.blaze.base.model.primitives.LanguageClass) GeneratedAndroidResourcesSection(com.google.idea.blaze.android.projectview.GeneratedAndroidResourcesSection) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact) Collection(java.util.Collection) Set(java.util.Set) BlazeAndroidWorkspaceImporter(com.google.idea.blaze.android.sync.importer.BlazeAndroidWorkspaceImporter) WorkspaceLanguageSettings(com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings) Collectors(java.util.stream.Collectors) BlazeJavaSyncAugmenter(com.google.idea.blaze.java.sync.BlazeJavaSyncAugmenter) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) Kind(com.google.idea.blaze.base.model.primitives.Kind) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact)

Example 4 with LibraryArtifact

use of com.google.idea.blaze.base.ideinfo.LibraryArtifact in project intellij by bazelbuild.

the class ExcludeLibraryAction method actionPerformedInBlazeProject.

@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
    BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
    if (blazeProjectData == null) {
        return;
    }
    Library library = LibraryActionHelper.findLibraryForAction(e);
    if (library == null) {
        return;
    }
    BlazeJarLibrary blazeLibrary = LibraryActionHelper.findLibraryFromIntellijLibrary(project, blazeProjectData, library);
    if (blazeLibrary == null) {
        Messages.showErrorDialog(project, "Could not find this library in the project.", CommonBundle.getErrorTitle());
        return;
    }
    final LibraryArtifact libraryArtifact = blazeLibrary.libraryArtifact;
    final String path = libraryArtifact.jarForIntellijLibrary().getRelativePath();
    ProjectViewEdit edit = ProjectViewEdit.editLocalProjectView(project, builder -> {
        ListSection<Glob> existingSection = builder.getLast(ExcludeLibrarySection.KEY);
        builder.replace(existingSection, ListSection.update(ExcludeLibrarySection.KEY, existingSection).add(new Glob(path)));
        return true;
    });
    if (edit == null) {
        Messages.showErrorDialog("Could not modify project view. Check for errors in your project view and try again", "Error");
        return;
    }
    edit.apply();
    BlazeSyncManager.getInstance(project).incrementalProjectSync();
}
Also used : BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) ProjectViewEdit(com.google.idea.blaze.base.projectview.ProjectViewEdit) Glob(com.google.idea.blaze.base.projectview.section.Glob) BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) Library(com.intellij.openapi.roots.libraries.Library) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact)

Example 5 with LibraryArtifact

use of com.google.idea.blaze.base.ideinfo.LibraryArtifact in project intellij by bazelbuild.

the class BlazeAndroidLiteJavaSyncAugmenter method addJarsForSourceTarget.

@Override
public void addJarsForSourceTarget(WorkspaceLanguageSettings workspaceLanguageSettings, ProjectViewSet projectViewSet, TargetIdeInfo target, Collection<BlazeJarLibrary> jars, Collection<BlazeJarLibrary> genJars) {
    if (!workspaceLanguageSettings.isLanguageActive(LanguageClass.ANDROID)) {
        return;
    }
    AndroidIdeInfo androidIdeInfo = target.androidIdeInfo;
    if (androidIdeInfo == null) {
        return;
    }
    // Add R.java jars
    LibraryArtifact resourceJar = androidIdeInfo.resourceJar;
    if (resourceJar != null) {
        jars.add(new BlazeJarLibrary(resourceJar));
    }
    LibraryArtifact idlJar = androidIdeInfo.idlJar;
    if (idlJar != null) {
        genJars.add(new BlazeJarLibrary(idlJar));
    }
}
Also used : BlazeJarLibrary(com.google.idea.blaze.java.sync.model.BlazeJarLibrary) AndroidIdeInfo(com.google.idea.blaze.base.ideinfo.AndroidIdeInfo) LibraryArtifact(com.google.idea.blaze.base.ideinfo.LibraryArtifact)

Aggregations

LibraryArtifact (com.google.idea.blaze.base.ideinfo.LibraryArtifact)14 BlazeJarLibrary (com.google.idea.blaze.java.sync.model.BlazeJarLibrary)9 TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)7 ArtifactLocation (com.google.idea.blaze.base.ideinfo.ArtifactLocation)6 ImmutableList (com.google.common.collect.ImmutableList)5 TargetKey (com.google.idea.blaze.base.ideinfo.TargetKey)4 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)4 AndroidIdeInfo (com.google.idea.blaze.base.ideinfo.AndroidIdeInfo)3 JavaIdeInfo (com.google.idea.blaze.base.ideinfo.JavaIdeInfo)3 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)3 BlazeLibrary (com.google.idea.blaze.base.model.BlazeLibrary)3 Nullable (javax.annotation.Nullable)3 AndroidResourceModuleRegistry (com.google.idea.blaze.android.sync.model.AndroidResourceModuleRegistry)2 ProtoLibraryLegacyInfo (com.google.idea.blaze.base.ideinfo.ProtoLibraryLegacyInfo)2 LanguageClass (com.google.idea.blaze.base.model.primitives.LanguageClass)2 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)2 ArtifactLocationDecoder (com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder)2 BlazeJavaSyncAugmenter (com.google.idea.blaze.java.sync.BlazeJavaSyncAugmenter)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2