JavaAnalyzer { @SuppressWarnings("try") private CompileResult runAnalyzeAndCompile( final String classpath, final String out, final List<File> compileFiles, final boolean generate, final boolean isDiagnostics) throws IOException { final Config config = Config.load(); try (final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, StandardCharsets.UTF_8); TelemetryUtils.ScopedSpan scope = TelemetryUtils.startScopedSpan("JavaAnalyzer.runAnalyzeAndCompile")) { TelemetryUtils.ScopedSpan.addAnnotation( TelemetryUtils.annotationBuilder() .put("classpath", classpath) .put("out", out) .put("compileFiles", compileFiles.toString()) .put("generate", generate) .put("isDiagnostics", isDiagnostics) .build("args")); final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(compileFiles); final DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>(); final List<String> opts = Arrays.asList( "-cp", classpath, "-g", config.getJavacArg(), "-d", out, "-source", this.compileSource, "-target", this.compileTarget, "-encoding", "UTF-8"); List<String> compileOptions = new ArrayList<>(16); setJavacArgs(config, compileOptions); compileOptions.addAll(opts); final JavaCompiler.CompilationTask compilerTask = compiler.getTask( null, fileManager, diagnosticCollector, compileOptions, null, compilationUnits); final JavacTask javacTask = (JavacTask) compilerTask; Iterable<? extends CompilationUnitTree> parsedIter; try (TelemetryUtils.ScopedSpan child = TelemetryUtils.startScopedSpan("javacTask.parse")) { TelemetryUtils.ScopedSpan.addAnnotation( TelemetryUtils.annotationBuilder() .put("javac arg", config.getJavacArg()) .put("size", compileFiles.size()) .build("compile option")); parsedIter = javacTask.parse(); } try (TelemetryUtils.ScopedSpan child = TelemetryUtils.startScopedSpan("javacTask.analyze")) { TelemetryUtils.ScopedSpan.addAnnotation( TelemetryUtils.annotationBuilder() .put("javac arg", config.getJavacArg()) .put("size", compileFiles.size()) .build("compile option")); javacTask.analyze(); } try (TelemetryUtils.ScopedSpan child = TelemetryUtils.startScopedSpan("JavaAnalyzer.analyzeAST")) { final List<Diagnostic<? extends JavaFileObject>> diagnostics = diagnosticCollector.getDiagnostics(); final Set<File> errorFiles = JavaAnalyzer.getErrorFiles(diagnostics); final Map<File, Source> analyzedMap = analyze(parsedIter, errorFiles); if (generate && !Config.load().useExternalBuilder()) { try (TelemetryUtils.ScopedSpan child2 = TelemetryUtils.startScopedSpan("javacTask.generate")) { javacTask.generate(); CachedASMReflector.getInstance().updateClassIndexFromDirectory(); } } Executor.getInstance().getEventBus().post(new AnalyzedEvent(analyzedMap, isDiagnostics)); final boolean success = errorFiles.size() == 0; return new CompileResult(success, analyzedMap, diagnostics, errorFiles); } } } JavaAnalyzer(String compileSource, String compileTarget); CompileResult analyzeAndCompile(final List<File> files, final String classpath, String out); CompileResult analyzeAndCompile(
      final List<File> files, final String classpath, final String out, final boolean generate); CompileResult analyzeAndCompile(
      final List<File> files,
      final String classpath,
      final String out,
      final boolean generate,
      final boolean isDiagnostic); @SuppressWarnings("try") CompileResult runAnalyzeAndCompile(
      final String classpath,
      final String out,
      final String sourcePath,
      final String sourceCode,
      final boolean generate,
      final boolean isDiagnostics);  }
@Test public void analyzeFromString01() throws Exception { final JavaAnalyzer analyzer = getAnalyzer(); final String cp = getClasspath(); final String tmp = System.getProperty("java.io.tmpdir"); String path = project.getProjectRootPath() + "./src/test/java/meghanada/Hello.java"; String code = "class Hello {}"; CompileResult result = analyzer.runAnalyzeAndCompile(cp, tmp, path, code, false, true); String summary = result.getDiagnosticsSummary(); assertEquals(true, result.isSuccess()); }
@Test public void analyzeFromString02() throws Exception { final JavaAnalyzer analyzer = getAnalyzer(); final String cp = getClasspath(); final String tmp = System.getProperty("java.io.tmpdir"); String path = project.getProjectRootPath() + "./src/test/java/meghanada/Hello.java"; String code = "class Hello {a}"; CompileResult result = analyzer.runAnalyzeAndCompile(cp, tmp, path, code, false, true); String summary = result.getDiagnosticsSummary(); assertEquals(false, result.isSuccess()); }