Search in sources :

Example 41 with SdkModificator

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

the class PsiTestUtil method addRootsToJdk.

@NotNull
@Contract(pure = true)
public static Sdk addRootsToJdk(@NotNull Sdk sdk, @NotNull OrderRootType rootType, @NotNull VirtualFile... roots) {
    Sdk clone;
    try {
        clone = (Sdk) sdk.clone();
    } catch (CloneNotSupportedException e) {
        throw new RuntimeException(e);
    }
    SdkModificator sdkModificator = clone.getSdkModificator();
    for (VirtualFile root : roots) {
        sdkModificator.addRoot(root, rootType);
    }
    sdkModificator.commitChanges();
    return clone;
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Contract(org.jetbrains.annotations.Contract)

Example 42 with SdkModificator

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

the class MagicConstantInspection method checkAnnotationsJarAttached.

private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
    final Project project = file.getProject();
    if (!holder.isOnTheFly()) {
        final Boolean found = project.getUserData(NO_ANNOTATIONS_FOUND);
        if (found != null)
            return;
    }
    PsiClass event = JavaPsiFacade.getInstance(project).findClass("java.awt.event.InputEvent", GlobalSearchScope.allScope(project));
    // no jdk to attach
    if (event == null)
        return;
    PsiMethod[] methods = event.findMethodsByName("getModifiers", false);
    // no jdk to attach
    if (methods.length != 1)
        return;
    PsiMethod getModifiers = methods[0];
    PsiAnnotation annotation = ExternalAnnotationsManager.getInstance(project).findExternalAnnotation(getModifiers, MagicConstant.class.getName());
    if (annotation != null)
        return;
    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
    // no jdk to attach
    if (virtualFile == null)
        return;
    final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    Sdk jdk = null;
    for (OrderEntry orderEntry : entries) {
        if (orderEntry instanceof JdkOrderEntry) {
            jdk = ((JdkOrderEntry) orderEntry).getJdk();
            if (jdk != null)
                break;
        }
    }
    // no jdk to attach
    if (jdk == null)
        return;
    if (!holder.isOnTheFly()) {
        project.putUserData(NO_ANNOTATIONS_FOUND, Boolean.TRUE);
    }
    final Sdk finalJdk = jdk;
    String path = finalJdk.getHomePath();
    String text = "No IDEA annotations attached to the JDK " + finalJdk.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 getFamilyName() {
            return "Attach annotations";
        }

        @Nullable
        @Override
        public PsiElement getElementToMakeWritable(@NotNull PsiFile file) {
            return null;
        }

        @Override
        public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
            SdkModificator modificator = finalJdk.getSdkModificator();
            JavaSdkImpl.attachJdkAnnotations(modificator);
            modificator.commitChanges();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MagicConstant(org.intellij.lang.annotations.MagicConstant) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) Sdk(com.intellij.openapi.projectRoots.Sdk) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with SdkModificator

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

the class ExternalAnnotationsManagerImpl method appendChosenAnnotationsRoot.

private void appendChosenAnnotationsRoot(@NotNull final OrderEntry entry, @NotNull final VirtualFile vFile) {
    if (entry instanceof LibraryOrderEntry) {
        Library library = ((LibraryOrderEntry) entry).getLibrary();
        LOG.assertTrue(library != null);
        final Library.ModifiableModel model = library.getModifiableModel();
        model.addRoot(vFile, AnnotationOrderRootType.getInstance());
        model.commit();
    } else if (entry instanceof ModuleSourceOrderEntry) {
        final ModifiableRootModel model = ModuleRootManager.getInstance(entry.getOwnerModule()).getModifiableModel();
        final JavaModuleExternalPaths extension = model.getModuleExtension(JavaModuleExternalPaths.class);
        extension.setExternalAnnotationUrls(ArrayUtil.mergeArrays(extension.getExternalAnnotationsUrls(), vFile.getUrl()));
        model.commit();
    } else if (entry instanceof JdkOrderEntry) {
        final SdkModificator sdkModificator = ((JdkOrderEntry) entry).getJdk().getSdkModificator();
        sdkModificator.addRoot(vFile, AnnotationOrderRootType.getInstance());
        sdkModificator.commitChanges();
    }
    dropCache();
}
Also used : Library(com.intellij.openapi.roots.libraries.Library) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator)

Example 44 with SdkModificator

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

the class PluginRunConfiguration method getState.

@Override
public RunProfileState getState(@NotNull final Executor executor, @NotNull final ExecutionEnvironment env) throws ExecutionException {
    final Module module = getModule();
    if (module == null) {
        throw new ExecutionException(DevKitBundle.message("run.configuration.no.module.specified"));
    }
    final ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
    final Sdk jdk = rootManager.getSdk();
    if (jdk == null) {
        throw CantRunException.noJdkForModule(module);
    }
    final Sdk ideaJdk = IdeaJdk.findIdeaJdk(jdk);
    if (ideaJdk == null) {
        throw new ExecutionException(DevKitBundle.message("sdk.type.incorrect.common"));
    }
    String sandboxHome = ((Sandbox) ideaJdk.getSdkAdditionalData()).getSandboxHome();
    if (sandboxHome == null) {
        throw new ExecutionException(DevKitBundle.message("sandbox.no.configured"));
    }
    try {
        sandboxHome = new File(sandboxHome).getCanonicalPath();
    } catch (IOException e) {
        throw new ExecutionException(DevKitBundle.message("sandbox.no.configured"));
    }
    final String canonicalSandbox = sandboxHome;
    //copy license from running instance of idea
    IdeaLicenseHelper.copyIDEALicense(sandboxHome);
    final JavaCommandLineState state = new JavaCommandLineState(env) {

        @Override
        protected JavaParameters createJavaParameters() throws ExecutionException {
            final JavaParameters params = new JavaParameters();
            ParametersList vm = params.getVMParametersList();
            fillParameterList(vm, VM_PARAMETERS);
            fillParameterList(params.getProgramParametersList(), PROGRAM_PARAMETERS);
            Sdk usedIdeaJdk = ideaJdk;
            String alternativeIdePath = getAlternativeJrePath();
            if (isAlternativeJreEnabled() && !StringUtil.isEmptyOrSpaces(alternativeIdePath)) {
                final Sdk configuredJdk = ProjectJdkTable.getInstance().findJdk(alternativeIdePath);
                if (configuredJdk != null) {
                    usedIdeaJdk = configuredJdk;
                } else {
                    try {
                        usedIdeaJdk = (Sdk) usedIdeaJdk.clone();
                    } catch (CloneNotSupportedException e) {
                        throw new ExecutionException(e.getMessage());
                    }
                    final SdkModificator sdkToSetUp = usedIdeaJdk.getSdkModificator();
                    sdkToSetUp.setHomePath(alternativeIdePath);
                    sdkToSetUp.commitChanges();
                }
            }
            String ideaJdkHome = usedIdeaJdk.getHomePath();
            boolean fromIdeaProject = IdeaJdk.isFromIDEAProject(ideaJdkHome);
            if (!fromIdeaProject) {
                String bootPath = "/lib/boot.jar";
                vm.add("-Xbootclasspath/a:" + ideaJdkHome + toSystemDependentName(bootPath));
            }
            vm.defineProperty("idea.config.path", canonicalSandbox + File.separator + "config");
            vm.defineProperty("idea.system.path", canonicalSandbox + File.separator + "system");
            vm.defineProperty("idea.plugins.path", canonicalSandbox + File.separator + "plugins");
            vm.defineProperty("idea.classpath.index.enabled", "false");
            if (!vm.hasProperty(JetBrainsProtocolHandler.REQUIRED_PLUGINS_KEY) && PluginModuleType.isOfType(module)) {
                final String id = DescriptorUtil.getPluginId(module);
                if (id != null) {
                    vm.defineProperty(JetBrainsProtocolHandler.REQUIRED_PLUGINS_KEY, id);
                }
            }
            if (SystemInfo.isMac) {
                vm.defineProperty("idea.smooth.progress", "false");
                vm.defineProperty("apple.laf.useScreenMenuBar", "true");
                vm.defineProperty("apple.awt.fileDialogForDirectories", "true");
            }
            if (SystemInfo.isXWindow) {
                if (VM_PARAMETERS == null || !VM_PARAMETERS.contains("-Dsun.awt.disablegrab")) {
                    // See http://devnet.jetbrains.net/docs/DOC-1142
                    vm.defineProperty("sun.awt.disablegrab", "true");
                }
            }
            if (!vm.hasProperty(PlatformUtils.PLATFORM_PREFIX_KEY)) {
                String buildNumber = IdeaJdk.getBuildNumber(ideaJdkHome);
                if (buildNumber != null) {
                    String prefix = IntelliJPlatformProduct.fromBuildNumber(buildNumber).getPlatformPrefix();
                    if (prefix != null) {
                        vm.defineProperty(PlatformUtils.PLATFORM_PREFIX_KEY, prefix);
                    }
                }
            }
            params.setWorkingDirectory(ideaJdkHome + File.separator + "bin" + File.separator);
            params.setJdk(usedIdeaJdk);
            if (fromIdeaProject) {
                OrderEnumerator enumerator = OrderEnumerator.orderEntries(module).recursively();
                for (VirtualFile file : enumerator.getAllLibrariesAndSdkClassesRoots()) {
                    params.getClassPath().add(file);
                }
            } else {
                for (String path : Arrays.asList("log4j.jar", "jdom.jar", "trove4j.jar", "openapi.jar", "util.jar", "extensions.jar", "bootstrap.jar", "idea_rt.jar", "idea.jar")) {
                    params.getClassPath().add(ideaJdkHome + toSystemDependentName("/lib/" + path));
                }
            }
            params.getClassPath().addFirst(((JavaSdkType) usedIdeaJdk.getSdkType()).getToolsPath(usedIdeaJdk));
            params.setMainClass("com.intellij.idea.Main");
            return params;
        }
    };
    return state;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) IOException(java.io.IOException) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) Sandbox(org.jetbrains.idea.devkit.projectRoots.Sandbox) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 45 with SdkModificator

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

the class ModuleRootManagerTestCase method retainRtJarOnlyAndSetVersion.

@NotNull
@Contract(pure = true)
private static Sdk retainRtJarOnlyAndSetVersion(Sdk jdk) {
    try {
        jdk = (Sdk) jdk.clone();
    } catch (CloneNotSupportedException e) {
        throw new RuntimeException(e);
    }
    final SdkModificator modificator = jdk.getSdkModificator();
    VirtualFile rtJar = null;
    for (VirtualFile root : modificator.getRoots(OrderRootType.CLASSES)) {
        if (root.getName().equals("rt.jar")) {
            rtJar = root;
            break;
        }
    }
    assertNotNull("rt.jar not found in jdk: " + jdk, rtJar);
    modificator.setVersionString(IdeaTestUtil.getMockJdkVersion(jdk.getHomePath()));
    modificator.removeAllRoots();
    modificator.addRoot(rtJar, OrderRootType.CLASSES);
    modificator.commitChanges();
    return jdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Contract(org.jetbrains.annotations.Contract)

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