本文整理汇总了Java中com.sun.tools.javac.tree.JCTree.JCLiteral.getKind方法的典型用法代码示例。如果您正苦于以下问题:Java JCLiteral.getKind方法的具体用法?Java JCLiteral.getKind怎么用?Java JCLiteral.getKind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.tree.JCTree.JCLiteral
的用法示例。
在下文中一共展示了JCLiteral.getKind方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateGuess
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
/**
* Turns an expression into a guessed intended literal. Only works for
* literals, as you can imagine.
*
* Will for example turn a TrueLiteral into 'Boolean.valueOf(true)'.
*/
public static Object calculateGuess(JCExpression expr) {
if (expr instanceof JCLiteral) {
JCLiteral lit = (JCLiteral) expr;
if (lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL) {
return ((Number) lit.value).intValue() == 0 ? false : true;
}
return lit.value;
} else if (expr instanceof JCIdent || expr instanceof JCFieldAccess) {
String x = expr.toString();
if (x.endsWith(".class")) x = x.substring(0, x.length() - 6);
else {
int idx = x.lastIndexOf('.');
if (idx > -1) x = x.substring(idx + 1);
}
return x;
} else
return null;
}
示例2: calculateGuess
import com.sun.tools.javac.tree.JCTree.JCLiteral; //导入方法依赖的package包/类
/**
* Turns an expression into a guessed intended literal. Only works for literals, as you can imagine.
*
* Will for example turn a TrueLiteral into 'Boolean.valueOf(true)'.
*/
public static Object calculateGuess(JCExpression expr) {
if (expr instanceof JCLiteral) {
JCLiteral lit = (JCLiteral)expr;
if (lit.getKind() == com.sun.source.tree.Tree.Kind.BOOLEAN_LITERAL) {
return ((Number)lit.value).intValue() == 0 ? false : true;
}
return lit.value;
} else if (expr instanceof JCIdent || expr instanceof JCFieldAccess) {
String x = expr.toString();
if (x.endsWith(".class")) x = x.substring(0, x.length() - 6);
else {
int idx = x.lastIndexOf('.');
if (idx > -1) x = x.substring(idx + 1);
}
return x;
} else return null;
}
示例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;
}
}