Search in sources :

Example 16 with JavaSdkVersion

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

the class DebuggerManagerImpl method checkTargetJPDAInstalled.

/* Remoting */
private static void checkTargetJPDAInstalled(JavaParameters parameters) throws ExecutionException {
    final Sdk jdk = parameters.getJdk();
    if (jdk == null) {
        throw new ExecutionException(DebuggerBundle.message("error.jdk.not.specified"));
    }
    final JavaSdkVersion version = JavaSdk.getInstance().getVersion(jdk);
    String versionString = jdk.getVersionString();
    if (version == JavaSdkVersion.JDK_1_0 || version == JavaSdkVersion.JDK_1_1) {
        throw new ExecutionException(DebuggerBundle.message("error.unsupported.jdk.version", versionString));
    }
    if (SystemInfo.isWindows && version == JavaSdkVersion.JDK_1_2) {
        final VirtualFile homeDirectory = jdk.getHomeDirectory();
        if (homeDirectory == null || !homeDirectory.isValid()) {
            throw new ExecutionException(DebuggerBundle.message("error.invalid.jdk.home", versionString));
        }
        //noinspection HardCodedStringLiteral
        File dllFile = new File(homeDirectory.getPath().replace('/', File.separatorChar) + File.separator + "bin" + File.separator + "jdwp.dll");
        if (!dllFile.exists()) {
            GetJPDADialog dialog = new GetJPDADialog();
            dialog.show();
            throw new ExecutionException(DebuggerBundle.message("error.debug.libraries.missing"));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) GetJPDADialog(com.intellij.debugger.ui.GetJPDADialog)

Example 17 with JavaSdkVersion

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

the class LanguageLevelCombo method sdkUpdated.

void sdkUpdated(Sdk sdk, boolean isDefaultProject) {
    LanguageLevel newLevel = null;
    if (sdk != null) {
        JavaSdkVersion version = JavaSdk.getInstance().getVersion(sdk);
        if (version != null) {
            newLevel = version.getMaxLanguageLevel();
        }
    }
    updateDefaultLevel(newLevel, isDefaultProject);
}
Also used : JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 18 with JavaSdkVersion

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

the class JavaScratchCompilationSupport method execute.

@Override
public boolean execute(CompileContext context) {
    final Project project = context.getProject();
    final RunConfiguration configuration = CompileStepBeforeRun.getRunConfiguration(context);
    if (!(configuration instanceof JavaScratchConfiguration)) {
        return true;
    }
    final JavaScratchConfiguration scratchConfig = (JavaScratchConfiguration) configuration;
    final String scratchUrl = scratchConfig.getScratchFileUrl();
    if (scratchUrl == null) {
        context.addMessage(CompilerMessageCategory.ERROR, "Associated scratch file not found", null, -1, -1);
        return false;
    }
    @Nullable final Module module = scratchConfig.getConfigurationModule().getModule();
    final Sdk targetSdk = module != null ? ModuleRootManager.getInstance(module).getSdk() : ProjectRootManager.getInstance(project).getProjectSdk();
    if (targetSdk == null) {
        final String message = module != null ? "Cannot find associated SDK for run configuration module \"" + module.getName() + "\".\nPlease check project settings." : "Cannot find associated project SDK for the run configuration.\nPlease check project settings.";
        context.addMessage(CompilerMessageCategory.ERROR, message, scratchUrl, -1, -1);
        return true;
    }
    if (!(targetSdk.getSdkType() instanceof JavaSdkType)) {
        final String message = module != null ? "Expected Java SDK for run configuration module \"" + module.getName() + "\".\nPlease check project settings." : "Expected Java SDK for project \"" + project.getName() + "\".\nPlease check project settings.";
        context.addMessage(CompilerMessageCategory.ERROR, message, scratchUrl, -1, -1);
        return true;
    }
    final File outputDir = getScratchOutputDirectory(project);
    if (outputDir == null) {
        // should not happen for normal projects
        return true;
    }
    // perform cleanup
    FileUtil.delete(outputDir);
    try {
        final File scratchFile = new File(VirtualFileManager.extractPath(scratchUrl));
        File srcFile = scratchFile;
        if (!StringUtil.endsWith(srcFile.getName(), ".java")) {
            final File srcDir = getScratchTempDirectory(project);
            if (srcDir == null) {
                // should not happen for normal projects
                return true;
            }
            // perform cleanup
            FileUtil.delete(srcDir);
            final String srcFileName = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

                @Override
                public String compute() {
                    final VirtualFile vFile = VirtualFileManager.getInstance().findFileByUrl(scratchUrl);
                    if (vFile != null) {
                        final PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
                        if (psiFile instanceof PsiJavaFile) {
                            String name = null;
                            // take the name of the first found public top-level class, otherwise the name of any available top-level class
                            for (PsiClass aClass : ((PsiJavaFile) psiFile).getClasses()) {
                                if (name == null) {
                                    name = aClass.getName();
                                    if (isPublic(aClass)) {
                                        break;
                                    }
                                } else if (isPublic(aClass)) {
                                    name = aClass.getName();
                                    break;
                                }
                            }
                            if (name != null) {
                                return name;
                            }
                        }
                    }
                    return FileUtil.getNameWithoutExtension(scratchFile);
                }
            });
            srcFile = new File(srcDir, srcFileName + ".java");
            FileUtil.copy(scratchFile, srcFile);
        }
        final Collection<File> files = Collections.singleton(srcFile);
        final Set<File> cp = new LinkedHashSet<>();
        final List<File> platformCp = new ArrayList<>();
        final Computable<OrderEnumerator> orderEnumerator = module != null ? (Computable<OrderEnumerator>) () -> ModuleRootManager.getInstance(module).orderEntries() : (Computable<OrderEnumerator>) () -> ProjectRootManager.getInstance(project).orderEntries();
        ApplicationManager.getApplication().runReadAction(() -> {
            for (String s : orderEnumerator.compute().compileOnly().recursively().exportedOnly().withoutSdk().getPathsList().getPathList()) {
                cp.add(new File(s));
            }
            for (String s : orderEnumerator.compute().compileOnly().sdkOnly().getPathsList().getPathList()) {
                platformCp.add(new File(s));
            }
        });
        final List<String> options = new ArrayList<>();
        // always compile with debug info
        options.add("-g");
        final JavaSdkVersion sdkVersion = JavaSdk.getInstance().getVersion(targetSdk);
        if (sdkVersion != null) {
            final String langLevel = "1." + Integer.valueOf(3 + sdkVersion.getMaxLanguageLevel().ordinal());
            options.add("-source");
            options.add(langLevel);
            options.add("-target");
            options.add(langLevel);
        }
        // disable annotation processing
        options.add("-proc:none");
        final Collection<ClassObject> result = CompilerManager.getInstance(project).compileJavaCode(options, platformCp, cp, Collections.emptyList(), Collections.emptyList(), files, outputDir);
        for (ClassObject classObject : result) {
            final byte[] bytes = classObject.getContent();
            if (bytes != null) {
                FileUtil.writeToFile(new File(classObject.getPath()), bytes);
            }
        }
    } catch (CompilationException e) {
        for (CompilationException.Message m : e.getMessages()) {
            context.addMessage(m.getCategory(), m.getText(), scratchUrl, m.getLine(), m.getColumn());
        }
    } catch (IOException e) {
        context.addMessage(CompilerMessageCategory.ERROR, e.getMessage(), scratchUrl, -1, -1);
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) JavaSdkType(com.intellij.openapi.projectRoots.JavaSdkType) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JavaSdkVersion (com.intellij.openapi.projectRoots.JavaSdkVersion)18 Sdk (com.intellij.openapi.projectRoots.Sdk)13 JavaSdk (com.intellij.openapi.projectRoots.JavaSdk)10 Module (com.intellij.openapi.module.Module)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 File (java.io.File)5 JavaSdkType (com.intellij.openapi.projectRoots.JavaSdkType)3 LanguageLevel (com.intellij.pom.java.LanguageLevel)3 IOException (java.io.IOException)3 NotNull (org.jetbrains.annotations.NotNull)3 ExecutionException (com.intellij.execution.ExecutionException)2 Project (com.intellij.openapi.project.Project)2 Nullable (org.jetbrains.annotations.Nullable)2 JpsJavaSdkType (org.jetbrains.jps.model.java.JpsJavaSdkType)2 AndroidSdkHandler (com.android.sdklib.repository.AndroidSdkHandler)1 AndroidTargetManager (com.android.sdklib.repository.targets.AndroidTargetManager)1 NotificationHyperlink (com.android.tools.idea.gradle.project.sync.hyperlink.NotificationHyperlink)1 StudioLoggerProgressIndicator (com.android.tools.idea.sdk.progress.StudioLoggerProgressIndicator)1 CompilerConfiguration (com.intellij.compiler.CompilerConfiguration)1 CompilerWorkspaceConfiguration (com.intellij.compiler.CompilerWorkspaceConfiguration)1