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

The following examples show how to use javax.tools.FileObject#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: SourceExtraction.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence extract(ProcessingEnvironment environment, TypeElement element) throws IOException {
  try {
    FileObject resource = environment.getFiler().getResource(
        StandardLocation.SOURCE_PATH,
        "",
        toFilename(element));

    return resource.getCharContent(true);
  } catch (UnsupportedOperationException | IllegalArgumentException ex) {

    return UNABLE_TO_EXTRACT;
  }
}
 
Example 2
Source File: JavacTrees.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public DocCommentTree getDocCommentTree(FileObject fileObject) {
    JavaFileObject jfo = asJavaFileObject(fileObject);
    DiagnosticSource diagSource = new DiagnosticSource(jfo, log);

    final Comment comment = new Comment() {
        int offset = 0;
        @Override
        public String getText() {
            try {
                CharSequence rawDoc = fileObject.getCharContent(true);
                Pattern bodyPat =
                        Pattern.compile("(?is).*?<body\\b[^>]*>(.*)</body\\b.*");
                Matcher m = bodyPat.matcher(rawDoc);
                if (m.matches()) {
                    offset = m.end(1);
                    return m.group(1);
                } else {
                    // Assume doclint will do the right thing.
                    return "";
                }
            } catch (IOException ignore) {
                // do nothing
            }
            return "";
        }

        @Override
        public int getSourcePos(int index) {
            return offset + index;
        }

        @Override
        public CommentStyle getStyle() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isDeprecated() {
            throw new UnsupportedOperationException();
        }
    };

    return new DocCommentParser(parser, diagSource, comment).parse();
}
 
Example 3
Source File: JavacTrees.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override @DefinedBy(Api.COMPILER_TREE)
public DocCommentTree getDocCommentTree(FileObject fileObject) {
    JavaFileObject jfo = asJavaFileObject(fileObject);
    DiagnosticSource diagSource = new DiagnosticSource(jfo, log);

    final Comment comment = new Comment() {
        int offset = 0;
        @Override
        public String getText() {
            try {
                CharSequence rawDoc = fileObject.getCharContent(true);
                Pattern bodyPat =
                        Pattern.compile("(?is).*?<body\\b[^>]*>(.*)</body\\b.*");
                Matcher m = bodyPat.matcher(rawDoc);
                if (m.matches()) {
                    offset = m.end(1);
                    return m.group(1);
                } else {
                    // Assume doclint will do the right thing.
                    return "";
                }
            } catch (IOException ignore) {
                // do nothing
            }
            return "";
        }

        @Override
        public int getSourcePos(int index) {
            return offset + index;
        }

        @Override
        public CommentStyle getStyle() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isDeprecated() {
            throw new UnsupportedOperationException();
        }
    };

    return new DocCommentParser(parser, diagSource, comment).parse();
}