Java Code Examples for javax.tools.JavaFileObject#getCharContent()

The following examples show how to use javax.tools.JavaFileObject#getCharContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileObjectsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRegularGetCharContent () throws Exception {
    final File wd = this.getWorkDir();
    final File testFile = createTestFile (wd);
    JavaFileObject jfo = FileObjects.fileFileObject(testFile, wd, null, null);
    CharSequence content = jfo.getCharContent(true);
    String expectedData = DATA+"\n";
    assertTrue (expectedData.contentEquals(content));
    
    Filter f = new Filter (null);
    jfo = FileObjects.fileFileObject(testFile, wd, f, null);
    content = jfo.getCharContent(true);
    expectedData = DATA+"\n";
    assertTrue (expectedData.contentEquals(content));
    assertEquals(EnumSet.of(Call.READER), f.calls);
    
    f = new Filter (PAD);
    jfo = FileObjects.fileFileObject(testFile, wd, f, null);
    content = jfo.getCharContent(true);
    expectedData = PAD + DATA+"\n";
    assertTrue (expectedData.contentEquals(content));
    assertEquals(EnumSet.of(Call.READER), f.calls);        
}
 
Example 2
Source File: AptinaTestCase.java    From doma with Apache License 2.0 6 votes vote down vote up
public String getGeneratedSource(final String className)
    throws IllegalStateException, IOException, SourceNotGeneratedException {
  assertNotEmpty("className", className);
  assertCompiled();
  final JavaFileObject javaFileObject =
      testingJavaFileManager.getJavaFileForInput(
          StandardLocation.SOURCE_OUTPUT, className, Kind.SOURCE);
  if (javaFileObject == null) {
    throw new SourceNotGeneratedException(className);
  }
  final CharSequence content = javaFileObject.getCharContent(true);
  if (content == null) {
    throw new SourceNotGeneratedException(className);
  }
  return content.toString();
}
 
Example 3
Source File: DocCommentTester.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
void check(TreePath path, Name name) throws Exception {
    JavaFileObject fo = path.getCompilationUnit().getSourceFile();
    final CharSequence cs = fo.getCharContent(true);

    final DCDocComment dc = (DCDocComment) trees.getDocCommentTree(path);
    DCTree t = (DCTree) trees.getDocCommentTree(path);

    DocTreeScanner scanner = new DocTreeScanner<Void,Void>() {
        @Override
        public Void scan(DocTree node, Void ignore) {
            if (node != null) {
                try {
                    String expect = getExpectText(node);
                    long pos = ((DCTree) node).getSourcePosition(dc);
                    String found = getFoundText(cs, (int) pos, expect.length());
                    if (!found.equals(expect)) {
                        System.err.println("expect: " + expect);
                        System.err.println("found:  " + found);
                        error("mismatch");
                    }

                } catch (StringIndexOutOfBoundsException e) {
                    error(node.getClass() + ": " + e.toString());
                        e.printStackTrace();
                }
            }
            return super.scan(node, ignore);
        }
    };

    scanner.scan(t, null);
}
 
Example 4
Source File: DiagnosticSource.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected char[] initBuf(JavaFileObject fileObject) throws IOException {
    char[] buf;
    CharSequence cs = fileObject.getCharContent(true);
    if (cs instanceof CharBuffer) {
        CharBuffer cb = (CharBuffer) cs;
        buf = JavacFileManager.toArray(cb);
        bufLen = cb.limit();
    } else {
        buf = cs.toString().toCharArray();
        bufLen = buf.length;
    }
    refBuf = new SoftReference<char[]>(buf);
    return buf;
}
 
Example 5
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingFileWithGetCharContentShouldBeTracked() throws IOException {
  JavaFileObject javaFileObject =
      fileManager.getJavaFileForInput(null, SINGLE_FILE_NAME, JavaFileObject.Kind.CLASS);

  javaFileObject.getCharContent(false);
  assertFilesRead(TEST_JAR_PATH, SINGLE_FILE_NAME);
}
 
Example 6
Source File: DiagnosticSource.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected char[] initBuf(JavaFileObject fileObject) throws IOException {
    char[] buf;
    CharSequence cs = fileObject.getCharContent(true);
    if (cs instanceof CharBuffer) {
        CharBuffer cb = (CharBuffer) cs;
        buf = JavacFileManager.toArray(cb);
        bufLen = cb.limit();
    } else {
        buf = cs.toString().toCharArray();
        bufLen = buf.length;
    }
    refBuf = new SoftReference<>(buf);
    return buf;
}
 
Example 7
Source File: DocCommentTester.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
void check(TreePath path, Name name) throws Exception {
    JavaFileObject fo = path.getCompilationUnit().getSourceFile();
    final CharSequence cs = fo.getCharContent(true);

    final DCDocComment dc = (DCDocComment) trees.getDocCommentTree(path);
    DCTree t = (DCTree) trees.getDocCommentTree(path);

    DocTreeScanner scanner = new DocTreeScanner<Void,Void>() {
        @Override
        public Void scan(DocTree node, Void ignore) {
            if (node != null) {
                try {
                    String expect = getExpectText(node);
                    long pos = ((DCTree) node).getSourcePosition(dc);
                    String found = getFoundText(cs, (int) pos, expect.length());
                    if (!found.equals(expect)) {
                        System.err.println("expect: " + expect);
                        System.err.println("found:  " + found);
                        error("mismatch");
                    }

                } catch (StringIndexOutOfBoundsException e) {
                    error(node.getClass() + ": " + e.toString());
                        e.printStackTrace();
                }
            }
            return super.scan(node, ignore);
        }
    };

    scanner.scan(t, null);
}
 
Example 8
Source File: TestLog.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
Example 9
Source File: DocCommentTester.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
void check(TreePath path, Name name) throws Exception {
    JavaFileObject fo = path.getCompilationUnit().getSourceFile();
    final CharSequence cs = fo.getCharContent(true);

    final DCDocComment dc = (DCDocComment) trees.getDocCommentTree(path);
    DCTree t = (DCTree) trees.getDocCommentTree(path);

    DocTreeScanner scanner = new DocTreeScanner<Void,Void>() {
        @Override
        public Void scan(DocTree node, Void ignore) {
            if (node != null) {
                try {
                    String expect = getExpectText(node);
                    long pos = ((DCTree) node).getSourcePosition(dc);
                    String found = getFoundText(cs, (int) pos, expect.length());
                    if (!found.equals(expect)) {
                        System.err.println("expect: " + expect);
                        System.err.println("found:  " + found);
                        error("mismatch");
                    }

                } catch (StringIndexOutOfBoundsException e) {
                    error(node.getClass() + ": " + e.toString());
                        e.printStackTrace();
                }
            }
            return super.scan(node, ignore);
        }
    };

    scanner.scan(t, null);
}
 
Example 10
Source File: DiagnosticSource.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected char[] initBuf(JavaFileObject fileObject) throws IOException {
    char[] buf;
    CharSequence cs = fileObject.getCharContent(true);
    if (cs instanceof CharBuffer) {
        CharBuffer cb = (CharBuffer) cs;
        buf = JavacFileManager.toArray(cb);
        bufLen = cb.limit();
    } else {
        buf = cs.toString().toCharArray();
        bufLen = buf.length;
    }
    refBuf = new SoftReference<char[]>(buf);
    return buf;
}
 
Example 11
Source File: JavaCompiler.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Try to open input stream with given name.
 *  Report an error if this fails.
 *  @param filename   The file name of the input stream to be opened.
 */
public CharSequence readSource(JavaFileObject filename) {
    try {
        inputFiles.add(filename);
        return filename.getCharContent(false);
    } catch (IOException e) {
        log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
        return null;
    }
}
 
Example 12
Source File: DiagnosticSource.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected char[] initBuf(JavaFileObject fileObject) throws IOException {
    char[] buf;
    CharSequence cs = fileObject.getCharContent(true);
    if (cs instanceof CharBuffer) {
        CharBuffer cb = (CharBuffer) cs;
        buf = JavacFileManager.toArray(cb);
        bufLen = cb.limit();
    } else {
        buf = cs.toString().toCharArray();
        bufLen = buf.length;
    }
    refBuf = new SoftReference<char[]>(buf);
    return buf;
}
 
Example 13
Source File: JavaCompiler.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Try to open input stream with given name.
 * Report an error if this fails.
 *
 * @param filename The file name of the input stream to be opened.
 */
public CharSequence readSource(JavaFileObject filename) {
    try {
        inputFiles.add(filename);
        return filename.getCharContent(false);
    } catch (IOException e) {
        log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
        return null;
    }
}
 
Example 14
Source File: TestLog.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void test(boolean genEndPos) throws IOException {
    Context context = new Context();

    Options options = Options.instance(context);
    options.put("diags", "%b:%s/%o/%e:%_%t%m|%p%m");

    Log log = Log.instance(context);
    log.multipleErrors = true;

    JavacFileManager.preRegister(context);
    ParserFactory pfac = ParserFactory.instance(context);

    final String text =
          "public class Foo {\n"
        + "  public static void main(String[] args) {\n"
        + "    if (args.length == 0)\n"
        + "      System.out.println(\"no args\");\n"
        + "    else\n"
        + "      System.out.println(args.length + \" args\");\n"
        + "  }\n"
        + "}\n";
    JavaFileObject fo = new StringJavaFileObject("Foo", text);
    log.useSource(fo);

    CharSequence cs = fo.getCharContent(true);
    Parser parser = pfac.newParser(cs, false, genEndPos, false);
    JCTree.JCCompilationUnit tree = parser.parseCompilationUnit();
    log.setEndPosTable(fo, tree.endPositions);

    TreeScanner ts = new LogTester(log, tree.endPositions);
    ts.scan(tree);

    check(log.nerrors, 4, "errors");
    check(log.nwarnings, 4, "warnings");
}
 
Example 15
Source File: JavaCompiler.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** Try to open input stream with given name.
 *  Report an error if this fails.
 *  @param filename   The file name of the input stream to be opened.
 */
public CharSequence readSource(JavaFileObject filename) {
    try {
        inputFiles.add(filename);
        return filename.getCharContent(false);
    } catch (IOException e) {
        log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
        return null;
    }
}
 
Example 16
Source File: JavaCompiler.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** Try to open input stream with given name.
 *  Report an error if this fails.
 *  @param filename   The file name of the input stream to be opened.
 */
public CharSequence readSource(JavaFileObject filename) {
    try {
        inputFiles.add(filename);
        return filename.getCharContent(false);
    } catch (IOException e) {
        log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
        return null;
    }
}
 
Example 17
Source File: DiagnosticSource.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected char[] initBuf(JavaFileObject fileObject) throws IOException {
    char[] buf;
    CharSequence cs = fileObject.getCharContent(true);
    if (cs instanceof CharBuffer) {
        CharBuffer cb = (CharBuffer) cs;
        buf = JavacFileManager.toArray(cb);
        bufLen = cb.limit();
    } else {
        buf = cs.toString().toCharArray();
        bufLen = buf.length;
    }
    refBuf = new SoftReference<>(buf);
    return buf;
}
 
Example 18
Source File: JavaCompiler.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Try to open input stream with given name.
 * Report an error if this fails.
 *
 * @param filename The file name of the input stream to be opened.
 */
public CharSequence readSource(JavaFileObject filename) {
    try {
        inputFiles.add(filename);
        return filename.getCharContent(false);
    } catch (IOException e) {
        log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
        return null;
    }
}
 
Example 19
Source File: DiagnosticSource.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected char[] initBuf(JavaFileObject fileObject) throws IOException {
    char[] buf;
    CharSequence cs = fileObject.getCharContent(true);
    if (cs instanceof CharBuffer) {
        CharBuffer cb = (CharBuffer) cs;
        buf = JavacFileManager.toArray(cb);
        bufLen = cb.limit();
    } else {
        buf = cs.toString().toCharArray();
        bufLen = buf.length;
    }
    refBuf = new SoftReference<char[]>(buf);
    return buf;
}
 
Example 20
Source File: DocCommentTester.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
void check(TreePath path, Name name) throws Exception {
    JavaFileObject fo = path.getCompilationUnit().getSourceFile();
    final CharSequence cs = fo.getCharContent(true);

    final DCDocComment dc = (DCDocComment) trees.getDocCommentTree(path);
    DCTree t = (DCTree) trees.getDocCommentTree(path);

    DocTreeScanner scanner = new DocTreeScanner<Void,Void>() {
        @Override
        public Void scan(DocTree node, Void ignore) {
            if (node != null) {
                try {
                    String expect = getExpectText(node);
                    long pos = ((DCTree) node).getSourcePosition(dc);
                    String found = getFoundText(cs, (int) pos, expect.length());
                    if (!found.equals(expect)) {
                        System.err.println("expect: " + expect);
                        System.err.println("found:  " + found);
                        error("mismatch");
                    }

                } catch (StringIndexOutOfBoundsException e) {
                    error(node.getClass() + ": " + e.toString());
                        e.printStackTrace();
                }
            }
            return super.scan(node, ignore);
        }
    };

    scanner.scan(t, null);
}