Search in sources :

Example 16 with Context

use of com.sun.tools.javac.util.Context in project error-prone by google.

the class ErrorProneJavacPluginTest method hello.

@Test
public void hello() throws IOException {
    FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
    Path source = fileSystem.getPath("Test.java");
    Files.write(source, ImmutableList.of("import java.util.HashSet;", "import java.util.Set;", "class Test {", "  public static void main(String[] args) {", "    Set<Short> s = new HashSet<>();", "    for (short i = 0; i < 100; i++) {", "      s.add(i);", "      s.remove(i - 1);", "    }", "    System.out.println(s.size());", "  }", "}"), UTF_8);
    JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
    JavacTask task = JavacTool.create().getTask(null, fileManager, diagnosticCollector, ImmutableList.of("-Xplugin:ErrorProne"), ImmutableList.of(), fileManager.getJavaFileObjects(source));
    assertThat(task.call()).isFalse();
    Diagnostic<? extends JavaFileObject> diagnostic = diagnosticCollector.getDiagnostics().stream().filter(d -> d.getKind() == Diagnostic.Kind.ERROR).collect(onlyElement());
    assertThat(diagnostic.getMessage(ENGLISH)).contains("[CollectionIncompatibleType]");
}
Also used : Path(java.nio.file.Path) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Context(com.sun.tools.javac.util.Context) Configuration(com.google.common.jimfs.Configuration) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) JavacTask(com.sun.source.util.JavacTask) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) RunWith(org.junit.runner.RunWith) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) FileSystem(java.nio.file.FileSystem) MoreCollectors.onlyElement(com.google.common.collect.MoreCollectors.onlyElement) JavaFileObject(javax.tools.JavaFileObject) ImmutableList(com.google.common.collect.ImmutableList) JavacTool(com.sun.tools.javac.api.JavacTool) Jimfs(com.google.common.jimfs.Jimfs) Diagnostic(javax.tools.Diagnostic) Context(com.sun.tools.javac.util.Context) ENGLISH(java.util.Locale.ENGLISH) Path(java.nio.file.Path) DiagnosticCollector(javax.tools.DiagnosticCollector) JavaFileObject(javax.tools.JavaFileObject) FileSystem(java.nio.file.FileSystem) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) Test(org.junit.Test)

Example 17 with Context

use of com.sun.tools.javac.util.Context in project error-prone by google.

the class SubContextTest method testOverride.

@Test
public void testOverride() {
    Context base = new Context();
    base.put(KEY1, "key1");
    base.put(Enum1.class, Enum1.VALUE1);
    Context overlay = new SubContext(base);
    overlay.put(KEY1, "key2");
    overlay.put(Enum1.class, Enum1.VALUE2);
    assertEquals("key2", overlay.get(KEY1));
    assertEquals(Enum1.VALUE2, overlay.get(Enum1.class));
    assertEquals("key1", base.get(KEY1));
    assertEquals(Enum1.VALUE1, base.get(Enum1.class));
}
Also used : Context(com.sun.tools.javac.util.Context) Test(org.junit.Test)

Example 18 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class T6725036 method run.

void run() throws Exception {
    RelativeFile TEST_ENTRY_NAME = new RelativeFile("java/lang/String.class");
    File f = new File(System.getProperty("java.home"));
    if (!f.getName().equals("jre"))
        f = new File(f, "jre");
    File rt_jar = new File(new File(f, "lib"), "rt.jar");
    JarFile j = new JarFile(rt_jar);
    JarEntry je = j.getJarEntry(TEST_ENTRY_NAME.getPath());
    long jarEntryTime = je.getTime();
    ZipFileIndexCache zfic = ZipFileIndexCache.getSharedInstance();
    ZipFileIndex zfi = zfic.getZipFileIndex(rt_jar, null, false, null, false);
    long zfiTime = zfi.getLastModified(TEST_ENTRY_NAME);
    check(je, jarEntryTime, zfi + ":" + TEST_ENTRY_NAME.getPath(), zfiTime);
    Context context = new Context();
    JavacFileManager fm = new JavacFileManager(context, false, null);
    ZipFileIndexArchive zfia = new ZipFileIndexArchive(fm, zfi);
    JavaFileObject jfo = zfia.getFileObject(TEST_ENTRY_NAME.dirname(), TEST_ENTRY_NAME.basename());
    long jfoTime = jfo.getLastModified();
    check(je, jarEntryTime, jfo, jfoTime);
    if (errors > 0)
        throw new Exception(errors + " occurred");
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) ZipFileIndexArchive(com.sun.tools.javac.file.ZipFileIndexArchive) JavaFileObject(javax.tools.JavaFileObject) ZipFileIndex(com.sun.tools.javac.file.ZipFileIndex) RelativeFile(com.sun.tools.javac.file.RelativePath.RelativeFile) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) RelativeFile(com.sun.tools.javac.file.RelativePath.RelativeFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFileIndexCache(com.sun.tools.javac.file.ZipFileIndexCache)

Example 19 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class JavacTool method getStandardFileManager.

public JavacFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
    Context context = new Context();
    context.put(Locale.class, locale);
    if (diagnosticListener != null)
        context.put(DiagnosticListener.class, diagnosticListener);
    PrintWriter pw = (charset == null) ? new PrintWriter(System.err, true) : new PrintWriter(new OutputStreamWriter(System.err, charset), true);
    context.put(Log.outKey, pw);
    return new JavacFileManager(context, true, charset);
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 20 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class JavapFileManager method create.

public static JavapFileManager create(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log) {
    Context javac_context = new Context();
    if (dl != null)
        javac_context.put(DiagnosticListener.class, dl);
    javac_context.put(com.sun.tools.javac.util.Log.outKey, log);
    return new JavapFileManager(javac_context, null);
}
Also used : Context(com.sun.tools.javac.util.Context) DiagnosticListener(javax.tools.DiagnosticListener)

Aggregations

Context (com.sun.tools.javac.util.Context)65 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)19 Test (org.junit.Test)9 Options (com.sun.tools.javac.util.Options)8 IOException (java.io.IOException)8 PrintWriter (java.io.PrintWriter)8 JavaFileObject (javax.tools.JavaFileObject)7 ListBuffer (com.sun.tools.javac.util.ListBuffer)6 JavacTask (com.sun.source.util.JavacTask)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStreamWriter (java.io.OutputStreamWriter)4 Path (java.nio.file.Path)4 DiagnosticListener (javax.tools.DiagnosticListener)4 ImmutableList (com.google.common.collect.ImmutableList)3 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 StringWriter (java.io.StringWriter)3