@Override public CommentStyle getStyle() { return wrapped.getStyle(); }
@Override public String getText() { return wrapped.getText(); }
@Override public int getSourcePos(int i) { return wrapped.getSourcePos(i) + offset; }
private static String removeJavadoc( VisitorState state, int startPos, Comment danglingJavadoc, SuggestedFix.Builder builder) { int javadocStart = startPos + danglingJavadoc.getSourcePos(0); int javadocEnd = javadocStart + danglingJavadoc.getText().length(); // Capturing an extra newline helps the formatter. if (state.getSourceCode().charAt(javadocEnd) == '\n') { javadocEnd++; } builder.replace(javadocStart, javadocEnd, ""); return danglingJavadoc.getText(); }
/** * Extract the text body from a comment. * * <p>This currently includes asterisks that start lines in the body of block comments. Do not * rely on this behaviour. * * <p>TODO(andrewrice) Update this method to handle block comments properly if we find the need */ public static String getTextFromComment(Comment comment) { switch (comment.getStyle()) { case BLOCK: return comment.getText().replaceAll("^\\s*/\\*\\s*(.*?)\\s*\\*/\\s*", "$1"); case LINE: return comment.getText().replaceAll("^\\s*//\\s*", ""); default: return comment.getText(); } }
public static JCTree.JCExpression parseString(String guardedByString, Context context) { JavacParser parser = ParserFactory.instance(context) .newParser( guardedByString, /* keepDocComments= */ false, /* keepEndPos= */ true, /* keepLineMap= */ false); JCTree.JCExpression exp; try { exp = parser.parseExpression(); } catch (Throwable e) { throw new IllegalGuardedBy(e.getMessage()); } int len = (parser.getEndPos(exp) - exp.getStartPosition()); if (len != guardedByString.length()) { throw new IllegalGuardedBy("Didn't parse entire string."); } return exp; }
public ImmutableList<ErrorProneToken> getTokens() { Scanner scanner = new AccessibleScanner(scannerFactory, commentSavingTokenizer); ImmutableList.Builder<ErrorProneToken> tokens = ImmutableList.builder(); do { scanner.nextToken(); tokens.add(new ErrorProneToken(scanner.token())); } while (scanner.token().kind != TokenKind.EOF); return tokens.build(); }
public ErrorProneTokens(String source, Context context) { scannerFactory = ScannerFactory.instance(context); char[] buffer = source == null ? new char[] {} : source.toCharArray(); commentSavingTokenizer = new CommentSavingTokenizer(scannerFactory, buffer, buffer.length); }
@Override protected Comment processComment(int pos, int endPos, CommentStyle style) { char[] buf = reader.getRawCharacters(pos, endPos); return new CommentWithTextAndPosition( pos, endPos, new AccessibleReader(fac, buf, buf.length), style); } }
public Name name() { return token.name(); }
public String stringVal() { return token.stringVal(); }
public int radix() { return token.radix(); }
private static void fixParamComment( SuggestedFix.Builder fix, Commented<ExpressionTree> commented, VarSymbol param, Comment c) { fix.prefixWith(commented.tree(), String.format("/* %s= */ ", param.getSimpleName())) .replace(c.getSourcePos(0), c.getSourcePos(0) + c.getText().length(), ""); } }
boolean isCommentOnPreviousLine(Comment c) { int tokenLine = lineMap.getLineNumber(c.getSourcePos(0)); return tokenLine == currentLineNumber - 1; }
@Nullable private static Comment findOrphanedJavadoc(Name name, List<ErrorProneToken> tokens) { for (ErrorProneToken token : tokens) { for (Comment comment : token.comments()) { if (comment.getText().startsWith("/**")) { return comment; } } if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(name)) { return null; } } return null; } }
private static boolean hasParameterComment(ErrorProneToken token) { return token.comments().stream() .filter(c -> c.getStyle() == BLOCK) .anyMatch( c -> NamedParameterComment.PARAMETER_COMMENT_PATTERN .matcher(Comments.getTextFromComment(c)) .matches()); }
Builder<T> addComment( Comment comment, int nodePosition, int tokenizingOffset, Position position) { OffsetComment offsetComment = new OffsetComment(comment, tokenizingOffset); if (comment.getSourcePos(0) < nodePosition) { if (position.equals(Position.BEFORE) || position.equals(Position.ANY)) { beforeCommentsBuilder().add(offsetComment); } } else { if (position.equals(Position.AFTER) || position.equals(Position.ANY)) { afterCommentsBuilder().add(offsetComment); } } return this; }
private static boolean isApproximateMatchingComment(Comment comment, String formal) { switch (comment.getStyle()) { case BLOCK: case LINE: // sometimes people use comments around arguments for higher level structuring - such as // dividing two separate blocks of arguments. In these cases we want to avoid concluding // that its a match. Therefore we also check to make sure that the comment is not really // long and that it doesn't contain acsii-art style markup. String commentText = Comments.getTextFromComment(comment); boolean textMatches = Arrays.asList(commentText.split("[^a-zA-Z0-9_]+", -1)).contains(formal); boolean tooLong = commentText.length() > formal.length() + 5 && commentText.length() > 50; boolean tooMuchMarkup = CharMatcher.anyOf("-*!@<>").countIn(commentText) > 5; return textMatches && !tooLong && !tooMuchMarkup; default: return false; } }
private void handleDanglingParams(TextTree node) { Matcher matcher = PARAM_MATCHER.matcher(node.getBody()); Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment; while (matcher.find()) { int startPos = comment.getSourcePos(((DCText) node).pos + matcher.start()); int endPos = comment.getSourcePos(((DCText) node).pos + matcher.end()); String paramName = matcher.group(1); SuggestedFix fix = SuggestedFix.replace(startPos, endPos, String.format("{@code %s}", paramName)); state.reportMatch( buildDescription( getDiagnosticPosition(startPos, getCurrentPath().getTreePath().getLeaf())) .addFix(fix) .build()); } }
private Description generateReturnFix( DocTreePath docTreePath, ReturnTree returnTree, VisitorState state) { int pos = ((DCDocComment) docTreePath.getDocComment()).comment.getSourcePos(0); String description = returnTree.toString().replaceAll("^@return ", ""); SuggestedFix fix = SuggestedFix.builder() .merge(Utils.replace(returnTree, "", state)) .replace( pos, pos, String.format( "Returns %s%s\n", lowerFirstLetter(description), description.endsWith(".") ? "" : ".")) .build(); return buildDescription(diagnosticPosition(docTreePath, state)) .setMessage(String.format(CONSIDER_USING_MESSAGE, "link")) .addFix(fix) .build(); }