本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCLiteral.getValue方法的典型用法代码示例。如果您正苦于以下问题:Java JCLiteral.getValue方法的具体用法?Java JCLiteral.getValue怎么用?Java JCLiteral.getValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.tree.JCTree.JCLiteral
的用法示例。
在下文中一共展示了JCLiteral.getValue方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verifyPrintf
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
private void verifyPrintf(MethodInvocationTree tree, FormatParameters parameters)
throws FormatFlagsConversionMismatchException, IllegalFormatException, FormatterException {
List<? extends ExpressionTree> args = tree.getArguments();
JCLiteral format = (JCLiteral) args.get(parameters.getFormatIndex());
String formatString = (String) format.getValue();
List<String> argTypes = new ArrayList<String>();
for (int i = parameters.getFormatIndex() + 1; i < args.size(); ++i) {
Type type = ((JCExpression) args.get(i)).type;
argTypes.add(getFormatterType(type));
}
try {
Formatter.check(formatString, argTypes.toArray(new String[0]));
} catch (ExtraFormatArgumentsException e) {
return; // We can handle this.
}
}
示例2: 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();
}
示例3: visitLiteral
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
@Override public void visitLiteral(JCLiteral node) {
Object val = node.getValue();
boolean negative = false;
Expression literal = null;
switch (node.getKind()) {
case INT_LITERAL:
int intValue = ((Number)val).intValue();
negative = intValue < 0;
if (intValue == Integer.MIN_VALUE) literal = new IntegralLiteral().astIntValue(Integer.MIN_VALUE);
else if (negative) literal = new IntegralLiteral().astIntValue(-intValue);
else literal = new IntegralLiteral().astIntValue(intValue);
break;
case LONG_LITERAL:
long longValue = ((Number)val).longValue();
negative = longValue < 0;
if (longValue == Long.MIN_VALUE) literal = new IntegralLiteral().astLongValue(Long.MIN_VALUE);
else if (negative) literal = new IntegralLiteral().astLongValue(-longValue);
else literal = new IntegralLiteral().astLongValue(longValue);
break;
case FLOAT_LITERAL:
set(node, new FloatingPointLiteral().astFloatValue(((Number)val).floatValue()));
return;
case DOUBLE_LITERAL:
set(node, new FloatingPointLiteral().astDoubleValue(((Number)val).doubleValue()));
return;
case BOOLEAN_LITERAL:
set(node, new BooleanLiteral().astValue((Boolean)val));
return;
case CHAR_LITERAL:
set(node, new CharLiteral().astValue((Character)val));
return;
case STRING_LITERAL:
set(node, new StringLiteral().astValue(val == null ? "" : val.toString()));
return;
case NULL_LITERAL:
set(node, new NullLiteral());
return;
}
if (literal != null) {
if (negative) set(node, new UnaryExpression().astOperand(setPos(node, literal)).astOperator(UnaryOperator.UNARY_MINUS));
else set(node, literal);
} else {
throw new IllegalArgumentException("Unknown JCLiteral type tag:" + node.typetag);
}
}
示例4: matches
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
// Must be an == or != comparison.
if (tree.getKind() != Kind.EQUAL_TO && tree.getKind() != Kind.NOT_EQUAL_TO) {
return false;
}
// Match trees that have one literal operand and another of the specified type.
List<ExpressionTree> binaryTreeMatches =
ASTHelpers.matchBinaryTree(
tree,
Arrays.asList(
Matchers.<ExpressionTree>isInstance(JCLiteral.class),
Matchers.<ExpressionTree>isSameType(comparisonType)),
state);
if (binaryTreeMatches == null) {
return false;
}
JCLiteral literal = (JCLiteral) binaryTreeMatches.get(0);
// Check whether literal is out of range for the specified type. Logic is based on
// JLS 5.6.2 - Binary Numeric Promotion:
// If either is double, other is converted to double.
// If either is float, other is converted to float.
// If either is long, other is converted to long.
// Otherwise, both are converted to int.
Object literalValue = literal.getValue();
switch (literal.getKind()) {
case DOUBLE_LITERAL:
double doubleValue = ((Double) literalValue).doubleValue();
return doubleValue < minValue || doubleValue > maxValue;
case FLOAT_LITERAL:
float floatValue = ((Float) literalValue).floatValue();
return floatValue < minValue || floatValue > maxValue;
case LONG_LITERAL:
long longValue = ((Long) literalValue).longValue();
return longValue < minValue || longValue > maxValue;
default:
// JCLiteral.getValue() can return Integer, Character, or Boolean.
int intValue;
if (literalValue instanceof Integer) {
intValue = ((Integer) literalValue).intValue();
} else if (literalValue instanceof Character) {
intValue = ((Character) literalValue).charValue();
} else if (literalValue instanceof Boolean) {
throw new IllegalStateException(
"Cannot compare " + comparisonType + " to boolean literal");
} else {
throw new IllegalStateException("Unexpected literal type: " + literal);
}
return intValue < minValue || intValue > maxValue;
}
}
示例5: matches
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
if (!initialized) {
init(state.getSymtab());
}
// Must be an == or != comparison.
if (tree.getKind() != Kind.EQUAL_TO && tree.getKind() != Kind.NOT_EQUAL_TO) {
return false;
}
// Match trees that have one literal operand and another of the specified type.
@SuppressWarnings("unchecked")
List<ExpressionTree> binaryTreeMatches = ASTHelpers.matchBinaryTree(tree,
Arrays.asList(Matchers.<ExpressionTree>isInstance(JCLiteral.class),
Matchers.<ExpressionTree>isSameType(comparisonType)),
state);
if (binaryTreeMatches == null) {
return false;
}
JCLiteral literal = (JCLiteral) binaryTreeMatches.get(0);
// Check whether literal is out of range for the specified type. Logic is based on
// JLS 5.6.2 - Binary Numeric Promotion:
// If either is double, other is converted to double.
// If either is float, other is converted to float.
// If either is long, other is converted to long.
// Otherwise, both are converted to int.
Object literalValue = literal.getValue();
switch (literal.getKind()) {
case DOUBLE_LITERAL:
double doubleValue = ((Double) literalValue).doubleValue();
return doubleValue < minValue || doubleValue > maxValue;
case FLOAT_LITERAL:
float floatValue = ((Float) literalValue).floatValue();
return floatValue < minValue || floatValue > maxValue;
case LONG_LITERAL:
long longValue = ((Long) literalValue).longValue();
return longValue < minValue || longValue > maxValue;
default:
// JCLiteral.getValue() can return Integer, Character, or Boolean.
int intValue;
if (literalValue instanceof Integer) {
intValue = ((Integer) literalValue).intValue();
} else if (literalValue instanceof Character) {
intValue = ((Character) literalValue).charValue();
} else if (literalValue instanceof Boolean) {
throw new IllegalStateException("Cannot compare " + comparisonType
+ " to boolean literal");
} else {
throw new IllegalStateException("Unexpected literal type: " + literal);
}
return intValue < minValue || intValue > maxValue;
}
}