本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCLiteral.getStartPosition方法的典型用法代码示例。如果您正苦于以下问题:Java JCLiteral.getStartPosition方法的具体用法?Java JCLiteral.getStartPosition怎么用?Java JCLiteral.getStartPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.tree.JCTree.JCLiteral
的用法示例。
在下文中一共展示了JCLiteral.getStartPosition方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLongLiteral
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的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;
}
示例2: literalHasStartPosition
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
private Matcher<LiteralTree> literalHasStartPosition(final int startPosition) {
return new Matcher<LiteralTree>() {
@Override
public boolean matches(LiteralTree tree, VisitorState state) {
JCLiteral literal = (JCLiteral) tree;
return literal.getStartPosition() == startPosition;
}
};
}
示例3: getLongLiteral
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的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: matchBinary
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
if (!BINARY_TREE_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
/*
* For shift amounts in [32, 63], cast the left operand to long. Otherwise change the shift
* amount to whatever would actually be used.
*/
int intValue = ((Number) ((LiteralTree) tree.getRightOperand()).getValue()).intValue();
SuggestedFix fix = new SuggestedFix();
if (intValue >= 32 && intValue <= 63) {
if (tree.getLeftOperand().getKind() == Kind.INT_LITERAL) {
fix = fix.postfixWith(tree.getLeftOperand(), "L");
} else {
fix = fix.prefixWith(tree, "(long) ");
}
} else {
JCLiteral jcLiteral = (JCLiteral) tree.getRightOperand();
// This is the equivalent shift distance according to JLS 15.19.
String actualShiftDistance = Integer.toString(intValue & 0x1f);
int actualStart = ASTHelpers.getActualStartPosition(jcLiteral, state.getSourceCode());
if (actualStart != jcLiteral.getStartPosition()) {
fix = fix.replace(tree.getRightOperand(), actualShiftDistance,
actualStart - jcLiteral.getStartPosition(), 0);
} else {
fix = fix.replace(tree.getRightOperand(), actualShiftDistance);
}
}
return describeMatch(tree, fix);
}
示例5: describe
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
/**
* Suggested fixes are as follows. For the byte case, convert the literal to its byte
* representation. For example, "255" becomes "-1. For the character case, replace the
* comparison with "true"/"false" since it's not clear what was intended and that is
* semantically equivalent.
*
* TODO(eaftan): Suggested fixes don't handle side-effecting expressions, such as
* (d = reader.read()) == -1. Maybe add special case handling for assignments.
*/
public Description describe(BinaryTree tree, VisitorState state) {
@SuppressWarnings("unchecked")
List<ExpressionTree> binaryTreeMatches = ASTHelpers.matchBinaryTree(tree,
Arrays.asList(Matchers.<ExpressionTree>isInstance(JCLiteral.class),
Matchers.<ExpressionTree>anything()),
state);
if (binaryTreeMatches == null) {
throw new IllegalStateException("Expected one of the operands to be a literal");
}
JCLiteral literal = (JCLiteral) binaryTreeMatches.get(0);
JCTree nonLiteralOperand = (JCTree) binaryTreeMatches.get(1);
boolean byteMatch = state.getTypes().isSameType(nonLiteralOperand.type,
state.getSymtab().byteType);
boolean willEvaluateTo = (tree.getKind() != Kind.EQUAL_TO);
SuggestedFix fix = new SuggestedFix();
String customDiagnosticMessage;
if (byteMatch) {
String replacement = Byte.toString(((Number) literal.getValue()).byteValue());
// Correct for poor javac 6 literal parsing.
int actualStart = ASTHelpers.getActualStartPosition(literal, state.getSourceCode());
if (actualStart != literal.getStartPosition()) {
fix.replace(literal, replacement, actualStart - literal.getStartPosition(), 0);
} else {
fix.replace(literal, replacement);
}
customDiagnosticMessage = getDiagnosticMessage("byte", (int) Byte.MIN_VALUE,
(int) Byte.MAX_VALUE, literal.toString(), Boolean.toString(willEvaluateTo));
} else {
fix.replace(tree, Boolean.toString(willEvaluateTo));
customDiagnosticMessage = getDiagnosticMessage("char", (int) Character.MIN_VALUE,
(int) Character.MAX_VALUE, literal.toString(), Boolean.toString(willEvaluateTo));
}
return new Description(tree, pattern, customDiagnosticMessage, fix);
}
示例6: getActualStartPosition
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
/**
* Hacky fix for poor javac 6 literal parsing. javac 6 doesn't set the AST node start
* position correctly when a numeric literal is preceded by -. So we scan the source
* backwards starting at the provided start position, looking for whitespace, until we find
* the true start position. javac 7 gets this right.
*
* @return The actual start position of the literal. May be the same as the start position
* given by the tree node itself.
*/
public static int getActualStartPosition(JCLiteral tree, CharSequence source) {
// This only applies to negative numeric literals.
Object value = tree.getValue();
if ((value instanceof Number) && (((Number) value).doubleValue() < 0)) {
int start = tree.getStartPosition() - 1;
while (WHITESPACE_CHARS.contains(source.charAt(start))) {
start--;
}
if (source.charAt(start) == '-') {
return start;
}
}
return tree.getStartPosition();
}