use of com.github.javaparser.ast.expr.StringLiteralExpr 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.StringLiteralExpr in project javaparser by javaparser.
the class ModifierVisitorTest method makeSureParentListsCanBeModified.
@Test
public void makeSureParentListsCanBeModified() {
NodeList<StringLiteralExpr> list = new NodeList<>();
list.add(new StringLiteralExpr("t"));
list.add(new StringLiteralExpr("a"));
list.add(new StringLiteralExpr("b"));
list.add(new StringLiteralExpr("c"));
list.accept(new ModifierVisitor<Void>() {
@Override
public Visitable visit(final StringLiteralExpr n, final Void arg) {
String v = n.getValue();
list.addFirst(new StringLiteralExpr("extra " + v));
list.remove(new StringLiteralExpr("t"));
if (v.equals("a")) {
return new StringLiteralExpr("x");
}
if (v.equals("b")) {
return null;
}
return n;
}
}, null);
assertEquals("extra c", list.get(0).getValue());
assertEquals("extra b", list.get(1).getValue());
assertEquals("extra a", list.get(2).getValue());
assertEquals("extra t", list.get(3).getValue());
assertEquals("x", list.get(4).getValue());
assertEquals("c", list.get(5).getValue());
assertEquals(6, list.size());
}
use of com.github.javaparser.ast.expr.StringLiteralExpr in project javaparser by javaparser.
the class ConstraintFormulaTest method testExpressionCompatibleWithTypeReduce1.
/**
* From JLS 18.1.2
*
* From Collections.singleton("hi"), we have the constraint formula ‹"hi" → α›.
* Through reduction, this will become the constraint formula: ‹String <: α›.
*/
@Test
public void testExpressionCompatibleWithTypeReduce1() {
ResolvedTypeParameterDeclaration tp = mock(ResolvedTypeParameterDeclaration.class);
Expression e = new StringLiteralExpr("hi");
InferenceVariable inferenceVariable = new InferenceVariable("α", tp);
ExpressionCompatibleWithType formula = new ExpressionCompatibleWithType(typeSolver, e, inferenceVariable);
ConstraintFormula.ReductionResult res1 = formula.reduce(BoundSet.empty());
assertEquals(ConstraintFormula.ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, stringType, inferenceVariable)), res1);
assertEquals(ConstraintFormula.ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, stringType, inferenceVariable)), res1.getConstraint(0).reduce(BoundSet.empty()));
}
use of com.github.javaparser.ast.expr.StringLiteralExpr in project activityinfo by bedatadriven.
the class DefaultUpdatingVisitor method updateDefaultStringAnnotation.
private void updateDefaultStringAnnotation(MethodDeclaration decl, String translated) {
for (AnnotationExpr annotation : decl.getAnnotations()) {
if (annotation.getName().getName().equals(defaultAnnotationName)) {
updateAnnotationValue(annotation, translated);
return;
}
}
// If there is no annotation, add one
SingleMemberAnnotationExpr defaultExpr = new SingleMemberAnnotationExpr(new NameExpr(defaultAnnotationName), new StringLiteralExpr(translated));
decl.getAnnotations().add(defaultExpr);
}
use of com.github.javaparser.ast.expr.StringLiteralExpr in project activityinfo by bedatadriven.
the class DefaultUpdatingVisitor method updateAnnotationValue.
private void updateAnnotationValue(AnnotationExpr annotation, String literalValue) {
List<Node> children = annotation.getChildrenNodes();
if (children.size() == 2 && children.get(0) instanceof NameExpr && children.get(1) instanceof StringLiteralExpr) {
StringLiteralExpr literal = (StringLiteralExpr) children.get(1);
if (!literal.getValue().equals(literalValue)) {
literal.setValue(literalValue);
dirty = true;
}
} else if (annotation instanceof SingleMemberAnnotationExpr) {
// Annotations can contain more complex ASTs, for example "part of a string" + "part of another string"
// In this case, replace wholesale
SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotation;
smae.setMemberValue(new StringLiteralExpr(literalValue));
} else {
throw new RuntimeException("Expected @" + defaultAnnotationName + " to be of type " + SingleMemberAnnotationExpr.class.getName() + ", found: " + annotation.getClass().getName());
}
}
Aggregations