Search in sources :

Example 11 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidSdkUtils method refreshLibrariesIn.

/**
   * Refresh the library {@link VirtualFile}s in the given {@link Sdk}.
   *
   * After changes to installed Android SDK components, the contents of the {@link Sdk}s do not automatically get refreshed.
   * The referenced {@link VirtualFile}s can be obsolete, new files may be created, or files may be deleted. The result is that
   * references to Android classes may not be found in editors.
   * Removing and adding the libraries effectively refreshes the contents of the IDEA SDK, and references in editors work again.
   */
public static void refreshLibrariesIn(@NotNull Sdk sdk) {
    VirtualFile[] libraries = sdk.getRootProvider().getFiles(CLASSES);
    SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.removeRoots(CLASSES);
    sdkModificator.commitChanges();
    sdkModificator = sdk.getSdkModificator();
    for (VirtualFile library : libraries) {
        sdkModificator.addRoot(library, CLASSES);
    }
    sdkModificator.commitChanges();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 12 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidTestBase method createLatestAndroidSdk.

public static Sdk createLatestAndroidSdk() {
    String sdkPath = TestUtils.getSdk().toString();
    String platformDir = TestUtils.getLatestAndroidPlatform();
    Sdk sdk = ProjectJdkTable.getInstance().createSdk("android_test_sdk", AndroidSdkType.getInstance());
    SdkModificator sdkModificator = sdk.getSdkModificator();
    sdkModificator.setHomePath(sdkPath);
    VirtualFile androidJar = JarFileSystem.getInstance().findFileByPath(sdkPath + "/platforms/" + platformDir + "/android.jar!/");
    sdkModificator.addRoot(androidJar, OrderRootType.CLASSES);
    VirtualFile resFolder = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/platforms/" + platformDir + "/data/res");
    sdkModificator.addRoot(resFolder, OrderRootType.CLASSES);
    VirtualFile docsFolder = LocalFileSystem.getInstance().findFileByPath(sdkPath + "/docs/reference");
    if (docsFolder != null) {
        sdkModificator.addRoot(docsFolder, JavadocOrderRootType.getInstance());
    }
    AndroidSdkAdditionalData data = new AndroidSdkAdditionalData(sdk);
    AndroidSdkData sdkData = AndroidSdkData.getSdkData(sdkPath);
    assertNotNull(sdkData);
    IAndroidTarget target = null;
    IAndroidTarget[] targets = sdkData.getTargets();
    for (IAndroidTarget t : targets) {
        if (t.getLocation().contains(platformDir)) {
            target = t;
            break;
        }
    }
    assertNotNull(target);
    data.setBuildTarget(target);
    sdkModificator.setSdkAdditionalData(data);
    ExternalAnnotationsSupport.attachJdkAnnotations(sdkModificator);
    sdkModificator.commitChanges();
    return sdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Sdk(com.intellij.openapi.projectRoots.Sdk) IAndroidTarget(com.android.sdklib.IAndroidTarget) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 13 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidFacet method addFilesToSdkIfNecessary.

private static void addFilesToSdkIfNecessary(@NotNull Sdk sdk, @NotNull Collection<VirtualFile> files) {
    List<VirtualFile> newFiles = Lists.newArrayList(files);
    newFiles.removeAll(Arrays.asList(sdk.getRootProvider().getFiles(OrderRootType.CLASSES)));
    if (newFiles.size() > 0) {
        SdkModificator modificator = sdk.getSdkModificator();
        for (VirtualFile file : newFiles) {
            modificator.addRoot(file, OrderRootType.CLASSES);
        }
        modificator.commitChanges();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 14 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class AndroidSdkSourceAttachTest method testRefreshSdkSource.

@Ignore("failed in http://go/aj/job/studio-ui-test/417 and from IDEA")
@Test
public void testRefreshSdkSource() throws IOException {
    assumeTrue("Android Sdk Source for '" + mySdk.getName() + "' must be installed before running 'testRefreshSdkSource'", mySdkSourcePath.isDirectory());
    SdkModificator sdkModificator = mySdk.getSdkModificator();
    sdkModificator.removeRoots(OrderRootType.SOURCES);
    sdkModificator.commitChanges();
    guiTest.importSimpleApplication();
    final EditorFixture editor = guiTest.ideFrame().getEditor();
    final VirtualFile classFile = findActivityClassFile();
    editor.open(classFile, EditorFixture.Tab.EDITOR);
    acceptLegalNoticeIfNeeded();
    // Refresh the source.
    editor.awaitNotification("Sources for '" + mySdk.getName() + "' not found.").performAction("Refresh (if already downloaded)");
    Wait.seconds(1).expecting("source file to be opened").until(() -> !classFile.equals(editor.getCurrentFile()));
    assertIsActivityJavaFile(editor.getCurrentFile());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) EditorFixture(com.android.tools.idea.tests.gui.framework.fixture.EditorFixture) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with SdkModificator

use of com.intellij.openapi.projectRoots.SdkModificator in project android by JetBrains.

the class ExternalAnnotationsSupport method checkAnnotationsJarAttached.

// Based on similar code in MagicConstantInspection
@SuppressWarnings("ALL")
private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
    // Not yet used
    if (false) {
        final Project project = file.getProject();
        PsiClass actionBar = JavaPsiFacade.getInstance(project).findClass("android.app.ActionBar", GlobalSearchScope.allScope(project));
        if (actionBar == null) {
            // no sdk to attach
            return;
        }
        PsiMethod[] methods = actionBar.findMethodsByName("getNavigationMode", false);
        if (methods.length != 1) {
            // no sdk to attach
            return;
        }
        PsiMethod getModifiers = methods[0];
        ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
        PsiAnnotation annotation = annotationsManager.findExternalAnnotation(getModifiers, MagicConstant.class.getName());
        if (annotation != null) {
            return;
        }
        final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
        if (virtualFile == null) {
            // no sdk to attach
            return;
        }
        final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
        Sdk sdk = null;
        for (OrderEntry orderEntry : entries) {
            if (orderEntry instanceof JdkOrderEntry) {
                sdk = ((JdkOrderEntry) orderEntry).getJdk();
                if (sdk != null) {
                    break;
                }
            }
        }
        if (sdk == null) {
            // no sdk to attach
            return;
        }
        final Sdk finalSdk = sdk;
        String path = finalSdk.getHomePath();
        String text = "No IDEA annotations attached to the Android SDK " + finalSdk.getName() + (path == null ? "" : " (" + FileUtil.toSystemDependentName(path) + ")") + ", some issues will not be found";
        holder.registerProblem(file, text, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix() {

            @NotNull
            @Override
            public String getName() {
                return "Attach annotations";
            }

            @NotNull
            @Override
            public String getFamilyName() {
                return getName();
            }

            @Override
            public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                    @Override
                    public void run() {
                        SdkModificator modifier = finalSdk.getSdkModificator();
                        attachJdkAnnotations(modifier);
                        modifier.commitChanges();
                    }
                });
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MagicConstant(org.intellij.lang.annotations.MagicConstant) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Sdk(com.intellij.openapi.projectRoots.Sdk) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Aggregations

SdkModificator (com.intellij.openapi.projectRoots.SdkModificator)51 Sdk (com.intellij.openapi.projectRoots.Sdk)32 VirtualFile (com.intellij.openapi.vfs.VirtualFile)30 File (java.io.File)8 Module (com.intellij.openapi.module.Module)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)6 AndroidSdkAdditionalData (org.jetbrains.android.sdk.AndroidSdkAdditionalData)5 NotNull (org.jetbrains.annotations.NotNull)5 ProjectJdkTable (com.intellij.openapi.projectRoots.ProjectJdkTable)4 Library (com.intellij.openapi.roots.libraries.Library)4 Nullable (org.jetbrains.annotations.Nullable)4 JSTestOptions (com.intellij.lang.javascript.JSTestOptions)3 Project (com.intellij.openapi.project.Project)3 ProjectJdkImpl (com.intellij.openapi.projectRoots.impl.ProjectJdkImpl)3 FirstRunWizardMode (com.android.tools.idea.welcome.config.FirstRunWizardMode)2 FlexSdkType2 (com.intellij.lang.javascript.flex.sdk.FlexSdkType2)2 OrderRoot (com.intellij.openapi.roots.libraries.ui.OrderRoot)2 IOException (java.io.IOException)2 AndroidSdkData (org.jetbrains.android.sdk.AndroidSdkData)2 Contract (org.jetbrains.annotations.Contract)2