本文整理汇总了Java中com.google.errorprone.VisitorState.getSourceCode方法的典型用法代码示例。如果您正苦于以下问题:Java VisitorState.getSourceCode方法的具体用法?Java VisitorState.getSourceCode怎么用?Java VisitorState.getSourceCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.errorprone.VisitorState
的用法示例。
在下文中一共展示了VisitorState.getSourceCode方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBroadEndPosition
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
/**
* Returns a more fine-tuned ending position of a {@code current} node in the source code.
*
* <p>See {@link #getBroadStartPosition(VisitorState, Tree, Tree)} for more information.
*
* @param current the node for which we retrieve the position
* @param next a node after the {@code current} one (optional)
* @return more useful ending position of the {@code current} node
*/
private static int getBroadEndPosition(VisitorState state, Tree current, @Nullable Tree next) {
CharSequence source = state.getSourceCode();
int currentEndPosition = state.getEndPosition(current);
int nextStartPosition;
if (next != null) {
nextStartPosition = ((JCTree) next).getStartPosition();
} else {
nextStartPosition = source.length();
}
String newline = System.lineSeparator();
int newlinePosition = indexOf(source, newline, currentEndPosition, nextStartPosition);
return (newlinePosition < 0) ? currentEndPosition : newlinePosition;
}
示例2: matchMethodInvocation
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
CharSequence sourceCode = state.getSourceCode();
Optional<Integer> endPosition = Comments.computeEndPosition(tree, sourceCode, state);
if (!endPosition.isPresent()) {
return Description.NO_MATCH;
}
int startPosition = endPosition.get();
do {
startPosition--;
} while (sourceCode.charAt(startPosition) != '\n');
return buildDescription(tree)
.setMessage(sourceCode.subSequence(startPosition + 1, endPosition.get()).toString())
.build();
}
示例3: getLongLiteral
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
/**
* Extracts the long literal corresponding to a given {@link LiteralTree} node from the source
* code as a string. Returns null if the source code is not available.
*/
private static String getLongLiteral(LiteralTree literalTree, VisitorState state) {
JCLiteral longLiteral = (JCLiteral) literalTree;
CharSequence sourceFile = state.getSourceCode();
if (sourceFile == null) {
return null;
}
int start = longLiteral.getStartPosition();
java.util.regex.Matcher matcher =
LONG_LITERAL_PATTERN.matcher(sourceFile.subSequence(start, sourceFile.length()));
if (matcher.lookingAt()) {
return matcher.group();
}
return null;
}
示例4: checkArrayDimensions
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
private Description checkArrayDimensions(Tree tree, Tree type, VisitorState state) {
if (!(type instanceof ArrayTypeTree)) {
return NO_MATCH;
}
CharSequence source = state.getSourceCode();
for (; type instanceof ArrayTypeTree; type = ((ArrayTypeTree) type).getType()) {
Tree elemType = ((ArrayTypeTree) type).getType();
int start = state.getEndPosition(elemType);
int end = state.getEndPosition(type);
if (start >= end) {
continue;
}
String dim = source.subSequence(start, end).toString();
if (dim.isEmpty()) {
continue;
}
ImmutableList<ErrorProneToken> tokens = ErrorProneTokens.getTokens(dim.trim(), state.context);
if (tokens.size() > 2 && tokens.get(0).kind() == TokenKind.IDENTIFIER) {
int nonWhitespace = CharMatcher.isNot(' ').indexIn(dim);
int idx = dim.indexOf("[]", nonWhitespace);
if (idx > nonWhitespace) {
String replacement = dim.substring(idx, dim.length()) + dim.substring(0, idx);
return describeMatch(tree, SuggestedFix.replace(start, end, replacement));
}
}
}
return NO_MATCH;
}
示例5: getLongLiteral
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
/**
* Extracts the long literal corresponding to a given {@link LiteralTree} node from the source
* code as a string. Returns null if the source code is not available.
*/
private static String getLongLiteral(LiteralTree literalTree, VisitorState state) {
JCLiteral longLiteral = (JCLiteral) literalTree;
CharSequence sourceFile = state.getSourceCode();
if (sourceFile == null) {
return null;
}
int start = longLiteral.getStartPosition();
java.util.regex.Matcher matcher = LONG_LITERAL_PATTERN.matcher(
sourceFile.subSequence(start, sourceFile.length()));
if (matcher.lookingAt()) {
return matcher.group();
}
return null;
}
示例6: findCommentsForArguments
import com.google.errorprone.VisitorState; //导入方法依赖的package包/类
private static ImmutableList<Commented<ExpressionTree>> findCommentsForArguments(
Tree tree, List<? extends ExpressionTree> arguments, int startPosition, VisitorState state) {
if (arguments.isEmpty()) {
return ImmutableList.of();
}
CharSequence sourceCode = state.getSourceCode();
Optional<Integer> endPosition = computeEndPosition(tree, sourceCode, state);
if (!endPosition.isPresent()) {
return noComments(arguments);
}
CharSequence source = sourceCode.subSequence(startPosition, endPosition.get());
if (CharMatcher.is('/').matchesNoneOf(source)) {
return noComments(arguments);
}
// The token position of the end of the method invocation
int invocationEnd = state.getEndPosition(tree) - startPosition;
ErrorProneTokens errorProneTokens = new ErrorProneTokens(source.toString(), state.context);
ImmutableList<ErrorProneToken> tokens = errorProneTokens.getTokens();
LineMap lineMap = errorProneTokens.getLineMap();
ArgumentTracker argumentTracker = new ArgumentTracker(arguments, startPosition, state, lineMap);
TokenTracker tokenTracker = new TokenTracker(lineMap);
argumentTracker.advance();
tokenLoop:
for (ErrorProneToken token : tokens) {
tokenTracker.advance(token);
if (tokenTracker.atStartOfLine() && !tokenTracker.wasPreviousLineEmpty()) {
for (Comment c : token.comments()) {
if (tokenTracker.isCommentOnPreviousLine(c)
&& token.pos() <= argumentTracker.currentArgumentStartPosition
&& argumentTracker.isPreviousArgumentOnPreviousLine()) {
// token was on the previous line so therefore we should add it to the previous comment
// unless the previous argument was not on the the previous line with it
argumentTracker.addCommentToPreviousArgument(c);
} else {
// if the comment comes after the end of the invocation and its not on the same line
// as the final argument then we need to ignore it
if (c.getSourcePos(0) <= invocationEnd
|| lineMap.getLineNumber(c.getSourcePos(0))
<= lineMap.getLineNumber(argumentTracker.currentArgumentEndPosition)) {
argumentTracker.addCommentToCurrentArgument(c);
}
}
}
} else {
argumentTracker.addAllCommentsToCurrentArgument(token.comments());
}
if (token.pos() >= argumentTracker.currentArgumentEndPosition) {
// We are between arguments so wait for a (lexed) comma to delimit them
if (token.kind() == TokenKind.COMMA) {
if (!argumentTracker.hasMoreArguments()) {
break tokenLoop;
}
argumentTracker.advance();
}
}
}
return argumentTracker.build();
}