Search in sources :

Example 16 with SdkModificator

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

the class RootsChangedTest method testModuleJdkEditing.

public void testModuleJdkEditing() throws Exception {
    final File tempDirectory = createTempDirectory();
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Module moduleA = createModule("a.iml");
        final Module moduleB = createModule("b.iml");
        assertEventsCount(2);
        final Sdk jdk;
        try {
            jdk = (Sdk) IdeaTestUtil.getMockJdk17().clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        ProjectJdkTable.getInstance().addJdk(jdk);
        assertEventsCount(0);
        final ModifiableRootModel rootModelA = ModuleRootManager.getInstance(moduleA).getModifiableModel();
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelA.setSdk(jdk);
        rootModelB.setSdk(jdk);
        ModifiableRootModel[] rootModels = { rootModelA, rootModelB };
        ModifiableModelCommitter.multiCommit(rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel());
        assertEventsCount(1);
        final SdkModificator sdkModificator = jdk.getSdkModificator();
        sdkModificator.addRoot(getVirtualFile(tempDirectory), OrderRootType.CLASSES);
        sdkModificator.commitChanges();
        assertEventsCount(1);
        ProjectJdkTable.getInstance().removeJdk(jdk);
        assertEventsCount(1);
    });
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 17 with SdkModificator

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

the class RootsChangedTest method testInheritedJdkEditing.

public void testInheritedJdkEditing() throws Exception {
    final File tempDirectory = createTempDirectory();
    ApplicationManager.getApplication().runWriteAction(() -> {
        final Module moduleA = createModule("a.iml");
        final Module moduleB = createModule("b.iml");
        assertEventsCount(2);
        final Sdk jdk;
        final Sdk jdkBBB;
        try {
            jdk = (Sdk) IdeaTestUtil.getMockJdk17("AAA").clone();
            ProjectJdkTable.getInstance().addJdk(jdk);
            assertEventsCount(0);
            jdkBBB = (Sdk) IdeaTestUtil.getMockJdk17("BBB").clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        ProjectJdkTable.getInstance().addJdk(jdk);
        assertEventsCount(0);
        ProjectRootManager.getInstance(myProject).setProjectSdk(jdkBBB);
        assertEventsCount(0);
        final ModifiableRootModel rootModelA = ModuleRootManager.getInstance(moduleA).getModifiableModel();
        final ModifiableRootModel rootModelB = ModuleRootManager.getInstance(moduleB).getModifiableModel();
        rootModelA.inheritSdk();
        rootModelB.inheritSdk();
        ModifiableRootModel[] rootModels = { rootModelA, rootModelB };
        if (rootModels.length > 0) {
            ModifiableModelCommitter.multiCommit(rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel());
        }
        assertEventsCount(1);
        ProjectRootManager.getInstance(myProject).setProjectSdk(jdk);
        assertEventsCount(1);
        final SdkModificator sdkModificator = jdk.getSdkModificator();
        sdkModificator.addRoot(getVirtualFile(tempDirectory), OrderRootType.CLASSES);
        sdkModificator.commitChanges();
        assertEventsCount(1);
        ProjectJdkTable.getInstance().removeJdk(jdk);
        assertEventsCount(1);
    });
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 18 with SdkModificator

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

the class ActionScriptHighlightingTest method testBadResolveOfSuperclass.

public void testBadResolveOfSuperclass() throws Exception {
    final Module module2 = doCreateRealModuleIn("module2", myProject, FlexModuleType.getInstance());
    final Ref<VirtualFile> fileFromModule2 = new Ref<>();
    final Ref<Sdk> sdk1 = new Ref<>();
    final Ref<Sdk> sdk2 = new Ref<>();
    myAfterCommitRunnable = () -> {
        sdk1.set(FlexTestUtils.createSdk(getTestDataPath() + BASE_PATH + "fake_sdk", "4.0.0"));
        {
            SdkModificator m = sdk1.get().getSdkModificator();
            m.removeAllRoots();
            m.addRoot(sdk1.get().getHomeDirectory().findChild("common_root"), OrderRootType.CLASSES);
            m.addRoot(sdk1.get().getHomeDirectory().findChild("flex_root"), OrderRootType.CLASSES);
            m.commitChanges();
        }
        sdk2.set(FlexTestUtils.createSdk(getTestDataPath() + BASE_PATH + "fake_sdk", "4.0.0"));
        {
            SdkModificator m = sdk2.get().getSdkModificator();
            m.removeAllRoots();
            m.addRoot(sdk2.get().getHomeDirectory().findChild("common_root"), OrderRootType.CLASSES);
            m.addRoot(sdk2.get().getHomeDirectory().findChild("air_root"), OrderRootType.CLASSES);
            m.commitChanges();
        }
        fileFromModule2.set(copyFileToModule(module2, getTestDataPath() + BASE_PATH + getTestName(false) + "2.as"));
    };
    configureByFile(BASE_PATH + getTestName(false) + "1.as");
    FlexTestUtils.modifyConfigs(myProject, e -> {
        FlexTestUtils.setSdk(e.getConfigurations(myModule)[0], sdk1.get());
        FlexTestUtils.setSdk(e.getConfigurations(module2)[0], sdk2.get());
    });
    checkHighlighting(new ExpectedHighlightingData(myEditor.getDocument(), true, true, false, myFile));
    myFile = PsiManager.getInstance(myProject).findFile(fileFromModule2.get());
    myEditor = createEditor(fileFromModule2.get());
    checkHighlighting(new ExpectedHighlightingData(myEditor.getDocument(), true, true, false, myFile));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Ref(com.intellij.openapi.util.Ref) ExpectedHighlightingData(com.intellij.testFramework.ExpectedHighlightingData) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 19 with SdkModificator

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

the class DesignerApplicationManager method setVersionString.

private static void setVersionString(Sdk sdk, String version) {
    WriteAction.run(() -> {
        SdkModificator modificator = sdk.getSdkModificator();
        modificator.setVersionString(version);
        modificator.commitChanges();
    });
}
Also used : SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 20 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