Search in sources :

Example 21 with CompilerManager

use of com.intellij.openapi.compiler.CompilerManager in project intellij-community by JetBrains.

the class CompilingEvaluatorImpl method compile.

@Override
@NotNull
protected Collection<ClassObject> compile(@Nullable JavaSdkVersion debuggeeVersion) throws EvaluateException {
    if (myCompiledClasses == null) {
        Module module = ReadAction.compute(() -> ModuleUtilCore.findModuleForPsiElement(myPsiContext));
        List<String> options = new ArrayList<>();
        options.add("-encoding");
        options.add("UTF-8");
        List<File> platformClasspath = new ArrayList<>();
        List<File> classpath = new ArrayList<>();
        AnnotationProcessingConfiguration profile = null;
        if (module != null) {
            assert myProject.equals(module.getProject()) : module + " is from another project";
            profile = CompilerConfiguration.getInstance(myProject).getAnnotationProcessingConfiguration(module);
            ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
            for (String s : rootManager.orderEntries().compileOnly().recursively().exportedOnly().withoutSdk().getPathsList().getPathList()) {
                classpath.add(new File(s));
            }
            for (String s : rootManager.orderEntries().compileOnly().sdkOnly().getPathsList().getPathList()) {
                platformClasspath.add(new File(s));
            }
        }
        JavaBuilder.addAnnotationProcessingOptions(options, profile);
        Pair<Sdk, JavaSdkVersion> runtime = BuildManager.getJavacRuntimeSdk(myProject);
        JavaSdkVersion buildRuntimeVersion = runtime.getSecond();
        // if compiler or debuggee version or both are unknown, let source and target be the compiler's defaults
        if (buildRuntimeVersion != null && debuggeeVersion != null) {
            JavaSdkVersion minVersion = buildRuntimeVersion.ordinal() > debuggeeVersion.ordinal() ? debuggeeVersion : buildRuntimeVersion;
            String sourceOption = getSourceOption(minVersion.getMaxLanguageLevel());
            options.add("-source");
            options.add(sourceOption);
            options.add("-target");
            options.add(sourceOption);
        }
        CompilerManager compilerManager = CompilerManager.getInstance(myProject);
        File sourceFile = null;
        try {
            sourceFile = generateTempSourceFile(compilerManager.getJavacCompilerWorkingDir());
            File srcDir = sourceFile.getParentFile();
            List<File> sourcePath = Collections.emptyList();
            Set<File> sources = Collections.singleton(sourceFile);
            myCompiledClasses = compilerManager.compileJavaCode(options, platformClasspath, classpath, Collections.emptyList(), sourcePath, sources, srcDir);
        } catch (CompilationException e) {
            StringBuilder res = new StringBuilder("Compilation failed:\n");
            for (CompilationException.Message m : e.getMessages()) {
                if (m.getCategory() == CompilerMessageCategory.ERROR) {
                    res.append(m.getText()).append("\n");
                }
            }
            throw new EvaluateException(res.toString());
        } catch (Exception e) {
            throw new EvaluateException(e.getMessage());
        } finally {
            if (sourceFile != null) {
                FileUtil.delete(sourceFile);
            }
        }
    }
    return myCompiledClasses;
}
Also used : CompilationException(com.intellij.openapi.compiler.CompilationException) JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) CompilerManager(com.intellij.openapi.compiler.CompilerManager) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) AnnotationProcessingConfiguration(org.jetbrains.jps.model.java.compiler.AnnotationProcessingConfiguration) CompilationException(com.intellij.openapi.compiler.CompilationException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) IOException(java.io.IOException) PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with CompilerManager

use of com.intellij.openapi.compiler.CompilerManager in project intellij-community by JetBrains.

the class BaseClassesAnalysisAction method compileAndAnalyze.

private void compileAndAnalyze(@NotNull Project project, @NotNull AnalysisScope scope) {
    if (project.isDisposed()) {
        return;
    }
    final CompilerManager compilerManager = CompilerManager.getInstance(project);
    compilerManager.make(compilerManager.createProjectCompileScope(project), new CompileStatusNotification() {

        @Override
        public void finished(final boolean aborted, final int errors, final int warnings, final CompileContext compileContext) {
            if (aborted || errors != 0)
                return;
            ApplicationManager.getApplication().invokeLater(() -> doAnalyze(project, scope));
        }
    });
}
Also used : CompileStatusNotification(com.intellij.openapi.compiler.CompileStatusNotification) CompilerManager(com.intellij.openapi.compiler.CompilerManager) CompileContext(com.intellij.openapi.compiler.CompileContext)

Aggregations

CompilerManager (com.intellij.openapi.compiler.CompilerManager)22 CompileContext (com.intellij.openapi.compiler.CompileContext)9 Module (com.intellij.openapi.module.Module)6 CompileScope (com.intellij.openapi.compiler.CompileScope)5 CompileStatusNotification (com.intellij.openapi.compiler.CompileStatusNotification)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 NotNull (org.jetbrains.annotations.NotNull)5 CompileTask (com.intellij.openapi.compiler.CompileTask)3 Project (com.intellij.openapi.project.Project)3 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 FileType (com.intellij.openapi.fileTypes.FileType)2 ArtifactCompileScope (com.intellij.packaging.impl.compiler.ArtifactCompileScope)2 PsiFile (com.intellij.psi.PsiFile)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 JavaArtifact (com.android.builder.model.JavaArtifact)1 AndroidGradleBuildConfiguration (com.android.tools.idea.gradle.project.build.compiler.AndroidGradleBuildConfiguration)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 CompilerConfiguration (com.intellij.compiler.CompilerConfiguration)1