use of com.github.javaparser.ast.expr.IntegerLiteralExpr in project checker-framework by typetools.
the class StubParser method getValueOfExpressionInAnnotation.
/**
* Returns the value of {@code expr}, or null if some problem occurred getting the value.
*/
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) {
if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
VariableElement elem;
if (expr instanceof NameExpr) {
elem = findVariableElement((NameExpr) expr);
} else {
elem = findVariableElement((FieldAccessExpr) expr);
}
if (elem == null) {
stubWarn("Field not found: " + expr);
return null;
}
Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
if (value instanceof Number) {
return convert((Number) value, valueKind);
} else {
return value;
}
} else if (expr instanceof StringLiteralExpr) {
return ((StringLiteralExpr) expr).asString();
} else if (expr instanceof BooleanLiteralExpr) {
return ((BooleanLiteralExpr) expr).getValue();
} else if (expr instanceof CharLiteralExpr) {
return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
} else if (expr instanceof DoubleLiteralExpr) {
// double, too.
return ((DoubleLiteralExpr) expr).asDouble();
} else if (expr instanceof IntegerLiteralExpr) {
return convert(((IntegerLiteralExpr) expr).asInt(), valueKind);
} else if (expr instanceof LongLiteralExpr) {
return convert(((LongLiteralExpr) expr).asLong(), valueKind);
} else if (expr instanceof ClassExpr) {
ClassExpr classExpr = (ClassExpr) expr;
String className = classExpr.getType().toString();
if (importedTypes.containsKey(className)) {
return importedTypes.get(className).asType();
}
TypeElement typeElement = findTypeOfName(className);
if (typeElement == null) {
stubWarn("StubParser: unknown class name " + className);
return null;
}
return typeElement.asType();
} else if (expr instanceof NullLiteralExpr) {
stubWarn("Null found as value for %s. Null isn't allowed as an annotation value", name);
return null;
} else {
stubWarn("Unexpected annotation expression: " + expr);
return null;
}
}
use of com.github.javaparser.ast.expr.IntegerLiteralExpr in project javaparser by javaparser.
the class ArrayCreationLevelTransformationsTest method replacingDimension.
@Test
public void replacingDimension() throws IOException {
ArrayCreationLevel it = consider("[10]");
it.setDimension(new IntegerLiteralExpr("12"));
assertTransformedToString("[12]", it);
}
use of com.github.javaparser.ast.expr.IntegerLiteralExpr in project javaparser by javaparser.
the class AnnotationMemberDeclarationTransformationsTest method replacingDefaultValue.
@Test
public void replacingDefaultValue() {
AnnotationMemberDeclaration md = consider("int foo() default 10;");
md.setDefaultValue(new IntegerLiteralExpr("11"));
assertTransformedToString("int foo() default 11;", md);
}
use of com.github.javaparser.ast.expr.IntegerLiteralExpr in project javaparser by javaparser.
the class NodeTest method stream.
@Test
public void stream() {
Expression e = parseExpression("1+2+3");
List<IntegerLiteralExpr> ints = e.stream().filter(n -> n instanceof IntegerLiteralExpr).map(IntegerLiteralExpr.class::cast).filter(i -> i.asInt() > 1).collect(Collectors.toList());
assertEquals("[2, 3]", ints.toString());
}
use of com.github.javaparser.ast.expr.IntegerLiteralExpr in project javaparser by javaparser.
the class TokenKindGenerator method generate.
@Override
public void generate() {
Log.info("Running %s", getClass().getSimpleName());
final CompilationUnit javaTokenCu = sourceRoot.parse("com.github.javaparser", "JavaToken.java");
final ClassOrInterfaceDeclaration javaToken = javaTokenCu.getClassByName("JavaToken").orElseThrow(() -> new AssertionError("Can't find class in java file."));
final EnumDeclaration kindEnum = javaToken.findFirst(EnumDeclaration.class, e -> e.getNameAsString().equals("Kind")).orElseThrow(() -> new AssertionError("Can't find class in java file."));
kindEnum.getEntries().clear();
annotateGenerated(kindEnum);
final SwitchStmt valueOfSwitch = kindEnum.findFirst(SwitchStmt.class).orElseThrow(() -> new AssertionError("Can't find valueOf switch."));
valueOfSwitch.findAll(SwitchEntryStmt.class).stream().filter(e -> e.getLabel().isPresent()).forEach(Node::remove);
final CompilationUnit constantsCu = generatedJavaCcSourceRoot.parse("com.github.javaparser", "GeneratedJavaParserConstants.java");
final ClassOrInterfaceDeclaration constants = constantsCu.getInterfaceByName("GeneratedJavaParserConstants").orElseThrow(() -> new AssertionError("Can't find class in java file."));
for (BodyDeclaration<?> member : constants.getMembers()) {
member.toFieldDeclaration().filter(field -> {
String javadoc = field.getJavadocComment().get().getContent();
return javadoc.contains("RegularExpression Id") || javadoc.contains("End of File");
}).map(field -> field.getVariable(0)).ifPresent(var -> {
final String name = var.getNameAsString();
final IntegerLiteralExpr kind = var.getInitializer().get().asIntegerLiteralExpr();
generateEnumEntry(kindEnum, name, kind);
generateValueOfEntry(valueOfSwitch, name, kind);
});
}
}
Aggregations