Search in sources :

Example 11 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project intellij-community by JetBrains.

the class NotNullInstrumentingBuilder method instrument.

// todo: probably instrument other NotNull-like annotations defined in project settings?
@Override
@Nullable
protected BinaryContent instrument(CompileContext context, CompiledClass compiledClass, ClassReader reader, ClassWriter writer, InstrumentationClassFinder finder) {
    try {
        final ProjectDescriptor pd = context.getProjectDescriptor();
        final List<String> notNulls = JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(pd.getProject()).getNotNullAnnotations();
        if (NotNullVerifyingInstrumenter.processClassFile((FailSafeClassReader) reader, writer, ArrayUtil.toStringArray(notNulls))) {
            return new BinaryContent(writer.toByteArray());
        }
    } catch (Throwable e) {
        LOG.error(e);
        final Collection<File> sourceFiles = compiledClass.getSourceFiles();
        String msg = "Cannot instrument " + ContainerUtil.map(sourceFiles, file -> file.getName()) + ": " + e.getMessage();
        context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, msg, ContainerUtil.getFirstItem(compiledClass.getSourceFilesPaths())));
    }
    return null;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) ProjectDescriptor(org.jetbrains.jps.cmdline.ProjectDescriptor) Collection(java.util.Collection) BinaryContent(org.jetbrains.jps.incremental.BinaryContent) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project intellij-community by JetBrains.

the class RmiStubsGenerator method generateRmiStubs.

private ExitCode generateRmiStubs(final CompileContext context, Map<ModuleBuildTarget, Collection<ClassItem>> remoteClasses, ModuleChunk chunk, OutputConsumer outputConsumer) {
    ExitCode exitCode = ExitCode.NOTHING_DONE;
    final Collection<File> classpath = ProjectPaths.getCompilationClasspath(chunk, false);
    final StringBuilder buf = new StringBuilder();
    for (File file : classpath) {
        if (buf.length() > 0) {
            buf.append(File.pathSeparator);
        }
        buf.append(file.getPath());
    }
    final String classpathString = buf.toString();
    final String rmicPath = getPathToRmic(chunk);
    final RmicCompilerOptions options = getOptions(context);
    final List<ModuleBuildTarget> targetsProcessed = new ArrayList<>(remoteClasses.size());
    for (Map.Entry<ModuleBuildTarget, Collection<ClassItem>> entry : remoteClasses.entrySet()) {
        try {
            final ModuleBuildTarget target = entry.getKey();
            final Collection<String> cmdLine = createStartupCommand(target, rmicPath, classpathString, options, entry.getValue());
            final Process process = Runtime.getRuntime().exec(ArrayUtil.toStringArray(cmdLine));
            final BaseOSProcessHandler handler = new BaseOSProcessHandler(process, StringUtil.join(cmdLine, " "), null) {

                @NotNull
                @Override
                protected Future<?> executeOnPooledThread(@NotNull Runnable task) {
                    return SharedThreadPool.getInstance().executeOnPooledThread(task);
                }
            };
            final RmicOutputParser stdOutParser = new RmicOutputParser(context, getPresentableName());
            final RmicOutputParser stdErrParser = new RmicOutputParser(context, getPresentableName());
            handler.addProcessListener(new ProcessAdapter() {

                @Override
                public void onTextAvailable(ProcessEvent event, Key outputType) {
                    if (outputType == ProcessOutputTypes.STDOUT) {
                        stdOutParser.append(event.getText());
                    } else if (outputType == ProcessOutputTypes.STDERR) {
                        stdErrParser.append(event.getText());
                    }
                }

                @Override
                public void processTerminated(ProcessEvent event) {
                    super.processTerminated(event);
                }
            });
            handler.startNotify();
            handler.waitFor();
            targetsProcessed.add(target);
            if (stdErrParser.isErrorsReported() || stdOutParser.isErrorsReported()) {
                break;
            } else {
                final int exitValue = handler.getProcess().exitValue();
                if (exitValue != 0) {
                    context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, "RMI stub generation failed"));
                    break;
                }
            }
        } catch (IOException e) {
            context.processMessage(new CompilerMessage(getPresentableName(), e));
            break;
        }
    }
    // registering generated files
    final Map<File, File[]> fsCache = new THashMap<>(FileUtil.FILE_HASHING_STRATEGY);
    for (ModuleBuildTarget target : targetsProcessed) {
        final Collection<ClassItem> items = remoteClasses.get(target);
        for (ClassItem item : items) {
            File[] children = fsCache.get(item.parentDir);
            if (children == null) {
                children = item.parentDir.listFiles();
                if (children == null) {
                    children = EMPTY_FILE_ARRAY;
                }
                fsCache.put(item.parentDir, children);
            }
            final Collection<File> files = item.selectGeneratedFiles(children);
            if (!files.isEmpty()) {
                final Collection<String> sources = item.compiledClass.getSourceFilesPaths();
                for (File generated : files) {
                    try {
                        outputConsumer.registerOutputFile(target, generated, sources);
                    } catch (IOException e) {
                        context.processMessage(new CompilerMessage(getPresentableName(), e));
                    }
                }
            }
        }
    }
    return exitCode;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) NotNull(org.jetbrains.annotations.NotNull) THashMap(gnu.trove.THashMap) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) RmicCompilerOptions(org.jetbrains.jps.model.java.compiler.RmicCompilerOptions) IOException(java.io.IOException) BaseOSProcessHandler(com.intellij.execution.process.BaseOSProcessHandler) File(java.io.File) THashMap(gnu.trove.THashMap) Key(com.intellij.openapi.util.Key)

Example 13 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project intellij-community by JetBrains.

the class PatternValidatorBuilder method instrument.

@Nullable
@Override
protected BinaryContent instrument(CompileContext context, CompiledClass compiled, ClassReader reader, ClassWriter writer, InstrumentationClassFinder finder) {
    final JpsIntelliLangConfiguration config = JpsIntelliLangExtensionService.getInstance().getConfiguration(context.getProjectDescriptor().getModel().getGlobal());
    final PatternInstrumenter instrumenter = new PatternInstrumenter(config.getPatternAnnotationClass(), writer, config.getInstrumentationType(), finder);
    try {
        reader.accept(instrumenter, 0);
        if (instrumenter.instrumented()) {
            return new BinaryContent(writer.toByteArray());
        }
    } catch (InstrumentationException e) {
        context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, e.getMessage()));
    }
    return null;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) BinaryContent(org.jetbrains.jps.incremental.BinaryContent) JpsIntelliLangConfiguration(org.jetbrains.jps.intellilang.model.JpsIntelliLangConfiguration) InstrumentationException(org.jetbrains.jps.intellilang.model.InstrumentationException) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project intellij-community by JetBrains.

the class ChainingFilterTransformer method doTransform.

private Reader doTransform(ResourceRootFilter filter, Reader original) {
    if ("RenamingCopyFilter".equals(filter.filterType)) {
        final Matcher matcher = (Matcher) filter.getProperties().get("matcher");
        final String replacement = (String) filter.getProperties().get("replacement");
        if (matcher == null || replacement == null)
            return original;
        matcher.reset(myOutputFileRef.get().getName());
        if (matcher.find()) {
            final String newFileName = matcher.replaceFirst(replacement);
            myOutputFileRef.set(new File(myOutputFileRef.get().getParentFile(), newFileName));
        }
        return original;
    }
    try {
        Class<?> clazz = Class.forName(filter.filterType);
        if (!FilterReader.class.isAssignableFrom(clazz)) {
            myContext.processMessage(new CompilerMessage(GradleResourcesBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, String.format("Error - Invalid filter specification for %s. It should extend java.io.FilterReader.", filter.filterType), null));
        }
        Constructor constructor = clazz.getConstructor(Reader.class);
        FilterReader result = (FilterReader) constructor.newInstance(original);
        final Map<Object, Object> properties = filter.getProperties();
        if (!properties.isEmpty()) {
            if (ExpandProperties.class.getName().equals(filter.filterType)) {
                final Map<Object, Object> antProps = new HashMap<>(properties);
                final Project project = new Project();
                for (Map.Entry<Object, Object> entry : antProps.entrySet()) {
                    project.setProperty(entry.getKey().toString(), entry.getValue().toString());
                }
                properties.clear();
                properties.put("project", project);
            }
            ConfigureUtil.configureByMap(properties, result);
        }
        return result;
    } catch (Throwable th) {
        myContext.processMessage(new CompilerMessage(GradleResourcesBuilder.BUILDER_NAME, BuildMessage.Kind.WARNING, String.format("Error - Failed to apply filter(%s): %s", filter.filterType, th.getMessage()), null));
    }
    return original;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) Matcher(java.util.regex.Matcher) Constructor(java.lang.reflect.Constructor) ExpandProperties(org.apache.tools.ant.filters.ExpandProperties) Project(org.apache.tools.ant.Project) File(java.io.File) FilterReader(java.io.FilterReader)

Example 15 with CompilerMessage

use of org.jetbrains.jps.incremental.messages.CompilerMessage in project intellij-community by JetBrains.

the class GreclipseBuilder method performCompilation.

private boolean performCompilation(List<String> args, StringWriter out, StringWriter err, Map<String, List<String>> outputs, CompileContext context, ModuleChunk chunk) {
    try {
        Class<?> mainClass = Class.forName(GreclipseMain.class.getName(), true, myGreclipseLoader);
        Constructor<?> constructor = mainClass.getConstructor(PrintWriter.class, PrintWriter.class, Map.class, Map.class);
        Method compileMethod = mainClass.getMethod("compile", String[].class);
        HashMap<String, Object> customDefaultOptions = ContainerUtil.newHashMap();
        // without this greclipse won't load AST transformations
        customDefaultOptions.put("org.eclipse.jdt.core.compiler.groovy.groovyClassLoaderPath", getClasspathString(chunk));
        // used by greclipse to cache transform loaders
        // names should be different for production & tests
        customDefaultOptions.put("org.eclipse.jdt.core.compiler.groovy.groovyProjectName", chunk.getPresentableShortName());
        Object main = constructor.newInstance(new PrintWriter(out), new PrintWriter(err), customDefaultOptions, outputs);
        return (Boolean) compileMethod.invoke(main, new Object[] { ArrayUtil.toStringArray(args) });
    } catch (Exception e) {
        context.processMessage(new CompilerMessage(getPresentableName(), e));
        return false;
    }
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) Method(java.lang.reflect.Method) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)75 File (java.io.File)34 IOException (java.io.IOException)22 ProgressMessage (org.jetbrains.jps.incremental.messages.ProgressMessage)18 JpsModule (org.jetbrains.jps.model.module.JpsModule)15 HashMap (com.intellij.util.containers.HashMap)11 JpsAndroidModuleExtension (org.jetbrains.jps.android.model.JpsAndroidModuleExtension)11 TObjectLongHashMap (gnu.trove.TObjectLongHashMap)9 ArrayList (java.util.ArrayList)9 THashSet (gnu.trove.THashSet)8 Nullable (org.jetbrains.annotations.Nullable)8 ProjectBuildException (org.jetbrains.jps.incremental.ProjectBuildException)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 NotNull (org.jetbrains.annotations.NotNull)7 BuildMessage (org.jetbrains.jps.incremental.messages.BuildMessage)6 AndroidCompilerMessageKind (org.jetbrains.android.util.AndroidCompilerMessageKind)5 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)4 JpsDummyElement (org.jetbrains.jps.model.JpsDummyElement)4 Pair (com.intellij.openapi.util.Pair)3 THashMap (gnu.trove.THashMap)3