Search in sources :

Example 6 with CompileResult

use of meghanada.analyze.CompileResult in project meghanada-server by mopemope.

the class Project method compileJava.

public CompileResult compileJava(boolean force) throws IOException {
    final String origin = Config.getProjectRoot();
    try {
        Config.setProjectRoot(projectRootPath);
        List<File> files = Project.collectJavaFiles(sources);
        if (nonNull(files) && !files.isEmpty()) {
            if (this.callerMap.size() == 0) {
                force = true;
            }
            if (force) {
                this.callerMap.clear();
            }
            final Stopwatch stopwatch = Stopwatch.createStarted();
            files = force ? files : FileUtils.getModifiedSources(this.projectRoot, files, this.getSourcesAndResources(), this.output);
            if (!force) {
                files = this.getRelatedSources(this.getSourcesAndResources(), files);
            }
            final String classpath = this.classpath();
            this.prepareCompile(files);
            final CompiledSourceHandler handler = new CompiledSourceHandler(this);
            final CompileResult compileResult = clearMemberCache(getJavaAnalyzer().analyzeAndCompile(files, classpath, output.getCanonicalPath(), true, handler));
            log.info("project {} compile and analyze (java) {} files. force:{} problem:{} elapsed:{}", this.name, files.size(), force, compileResult.getDiagnostics().size(), stopwatch.stop());
            Config.setProjectRoot(projectRootPath);
            return compileResult;
        }
        return new CompileResult(true);
    } catch (Throwable t) {
        log.catching(t);
        final CompileResult result = new CompileResult(false);
        final Diagnostic<? extends JavaFileObject> diagnostic = CompileResult.getDiagnosticFromThrowable(t);
        if (diagnostic != null) {
            result.getDiagnostics().add(diagnostic);
        }
        return result;
    } finally {
        Config.setProjectRoot(origin);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Stopwatch(com.google.common.base.Stopwatch) Diagnostic(javax.tools.Diagnostic) CompileResult(meghanada.analyze.CompileResult) File(java.io.File)

Example 7 with CompileResult

use of meghanada.analyze.CompileResult in project meghanada-server by mopemope.

the class Project method compileFile.

public CompileResult compileFile(List<File> files, final boolean force) throws IOException {
    boolean isTest = false;
    // sampling
    String filepath = files.get(0).getCanonicalPath();
    for (File source : this.testSources) {
        String testPath = source.getCanonicalPath();
        if (filepath.startsWith(testPath)) {
            isTest = true;
            break;
        }
    }
    String output;
    if (isTest) {
        output = this.testOutput.getCanonicalPath();
    } else {
        output = this.output.getCanonicalPath();
    }
    final Set<File> sources = isTest ? this.getAllSources() : this.getSourcesAndResources();
    final Stopwatch stopwatch = Stopwatch.createStarted();
    files = force ? files : FileUtils.getModifiedSources(projectRoot, files, sources, new File(output));
    files = this.getRelatedSources(sources, files);
    if (isTest) {
        this.prepareTestCompile(files);
    } else {
        this.prepareCompile(files);
    }
    final CompiledSourceHandler handler = new CompiledSourceHandler(this);
    final CompileResult compileResult = clearMemberCache(getJavaAnalyzer().analyzeAndCompile(files, this.allClasspath(), output, true, handler));
    log.info("project {} compile and analyze {} files. force:{} problem:{} elapsed:{}", this.name, files.size(), force, compileResult.getDiagnostics().size(), stopwatch.stop());
    return compileResult;
}
Also used : Stopwatch(com.google.common.base.Stopwatch) CompileResult(meghanada.analyze.CompileResult) File(java.io.File)

Example 8 with CompileResult

use of meghanada.analyze.CompileResult in project meghanada-server by mopemope.

the class Project method compileTestJava.

public CompileResult compileTestJava(boolean force) throws IOException {
    final String origin = Config.getProjectRoot();
    try {
        Config.setProjectRoot(projectRootPath);
        List<File> files = Project.collectJavaFiles(testSources);
        if (files != null && !files.isEmpty()) {
            if (this.callerMap.size() == 0) {
                force = true;
            }
            if (force) {
                this.callerMap.clear();
            }
            final Stopwatch stopwatch = Stopwatch.createStarted();
            files = force ? files : FileUtils.getModifiedSources(projectRoot, files, this.getTestSourcesAndResources(), this.testOutput);
            if (!force) {
                files = this.getRelatedSources(this.getTestSourcesAndResources(), files);
            }
            final String classpath = this.allClasspath();
            this.prepareTestCompile(files);
            final CompiledSourceHandler handler = new CompiledSourceHandler(this);
            final CompileResult compileResult = clearMemberCache(getJavaAnalyzer().analyzeAndCompile(files, classpath, testOutput.getCanonicalPath(), true, handler));
            log.info("project {} compile and analyze (test) {} files. force:{} problem:{} elapsed:{}", this.name, files.size(), force, compileResult.getDiagnostics().size(), stopwatch.stop());
            Config.setProjectRoot(projectRootPath);
            return compileResult;
        }
        return new CompileResult(true);
    } catch (Throwable t) {
        log.catching(t);
        final CompileResult result = new CompileResult(false);
        final Diagnostic<? extends JavaFileObject> diagnostic = CompileResult.getDiagnosticFromThrowable(t);
        if (diagnostic != null) {
            result.getDiagnostics().add(diagnostic);
        }
        return result;
    } finally {
        Config.setProjectRoot(origin);
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Stopwatch(com.google.common.base.Stopwatch) Diagnostic(javax.tools.Diagnostic) CompileResult(meghanada.analyze.CompileResult) File(java.io.File)

Example 9 with CompileResult

use of meghanada.analyze.CompileResult in project meghanada-server by mopemope.

the class SexpOutputFormatter method diagnostics.

@Override
public String diagnostics(final long id, final CompileResult compileResult, final String path) {
    if (compileResult.isSuccess() && !compileResult.hasDiagnostics()) {
        return success(LPAREN + "success" + RPAREN);
    }
    final List<Diagnostic<? extends JavaFileObject>> list = compileResult.getDiagnostics();
    final StringBuilder sb = new StringBuilder(256);
    sb.append(LPAREN);
    sb.append("error");
    sb.append(LIST_SEP);
    final Map<String, Set<Diagnostic<? extends JavaFileObject>>> res = new HashMap<>();
    list.forEach(wrapIOConsumer(d -> {
        final JavaFileObject fileObject = d.getSource();
        String key = path;
        if (nonNull(fileObject) && fileObject.getKind().equals(JavaFileObject.Kind.SOURCE)) {
            final URI uri = fileObject.toUri();
            final File file = new File(uri);
            key = file.getCanonicalPath();
        }
        if (res.containsKey(key)) {
            final Set<Diagnostic<? extends JavaFileObject>> set = res.get(key);
            set.add(d);
            res.put(key, set);
        } else {
            final Set<Diagnostic<? extends JavaFileObject>> set = new HashSet<>();
            set.add(d);
            res.put(key, set);
        }
    }));
    sb.append(LPAREN);
    res.forEach((k, v) -> {
        sb.append(LPAREN);
        sb.append(doubleQuote(k));
        sb.append(LIST_SEP);
        sb.append(LPAREN);
        v.forEach(d -> {
            sb.append(LPAREN);
            sb.append(String.join(LIST_SEP, Long.toString(d.getLineNumber()), Long.toString(d.getColumnNumber()), doubleQuote(d.getKind().toString()), doubleQuote(d.getMessage(null))));
            sb.append(RPAREN);
            sb.append(LIST_SEP);
        });
        sb.append(RPAREN);
        sb.append(RPAREN);
    });
    sb.append(RPAREN);
    sb.append(RPAREN);
    return success(sb.toString());
}
Also used : Location(meghanada.location.Location) HashMap(java.util.HashMap) FunctionUtils.wrapIOConsumer(meghanada.utils.FunctionUtils.wrapIOConsumer) HashSet(java.util.HashSet) Reference(meghanada.reference.Reference) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) CandidateUnit(meghanada.reflect.CandidateUnit) OutputFormatter(meghanada.server.OutputFormatter) LocalVariable(meghanada.completion.LocalVariable) Objects.isNull(java.util.Objects.isNull) URI(java.net.URI) Nullable(javax.annotation.Nullable) Collection(java.util.Collection) CompileResult(meghanada.analyze.CompileResult) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) JavaFileObject(javax.tools.JavaFileObject) List(java.util.List) Declaration(meghanada.docs.declaration.Declaration) TypeInfo(meghanada.typeinfo.TypeInfo) Logger(org.apache.logging.log4j.Logger) Objects.nonNull(java.util.Objects.nonNull) LogManager(org.apache.logging.log4j.LogManager) JavaFileObject(javax.tools.JavaFileObject) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Diagnostic(javax.tools.Diagnostic) URI(java.net.URI) File(java.io.File)

Example 10 with CompileResult

use of meghanada.analyze.CompileResult in project meghanada-server by mopemope.

the class Session method compileProject.

public synchronized CompileResult compileProject(final String path, final boolean force) throws IOException {
    final Project project = currentProject;
    final CompileResult result = project.compileJava(force);
    if (result.hasDiagnostics()) {
        log.warn("project {} compile report:{}", project.getName(), result.getDiagnosticsSummary());
    }
    final CompileResult testResult = project.compileTestJava(force);
    if (testResult.hasDiagnostics()) {
        for (final Diagnostic<? extends JavaFileObject> diagnostic : testResult.getDiagnostics()) {
            result.getDiagnostics().add(diagnostic);
        }
        log.warn("peoject {} test compile report:{}", project.getName(), testResult.getDiagnosticsSummary());
    }
    return result;
}
Also used : MavenProject(meghanada.project.maven.MavenProject) GradleProject(meghanada.project.gradle.GradleProject) Project(meghanada.project.Project) MeghanadaProject(meghanada.project.meghanada.MeghanadaProject) CompileResult(meghanada.analyze.CompileResult)

Aggregations

CompileResult (meghanada.analyze.CompileResult)15 File (java.io.File)9 Stopwatch (com.google.common.base.Stopwatch)6 Diagnostic (javax.tools.Diagnostic)3 JavaFileObject (javax.tools.JavaFileObject)3 Collection (java.util.Collection)2 Config (meghanada.config.Config)2 Project (meghanada.project.Project)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Test (org.junit.Test)2 Subscribe (com.google.common.eventbus.Subscribe)1 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1