Search in sources :

Example 1 with StringLiteralExpr

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;
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Example 2 with StringLiteralExpr

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());
}
Also used : NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Test(org.junit.Test)

Example 3 with StringLiteralExpr

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()));
}
Also used : InferenceVariable(com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable) Expression(com.github.javaparser.ast.expr.Expression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ConstraintFormula(com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula) Test(org.junit.Test)

Example 4 with StringLiteralExpr

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);
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Example 5 with StringLiteralExpr

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());
    }
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Aggregations

StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)6 NameExpr (com.github.javaparser.ast.expr.NameExpr)3 Node (com.github.javaparser.ast.Node)2 NodeList (com.github.javaparser.ast.NodeList)2 SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)2 Test (org.junit.Test)2 Modifier (com.github.javaparser.ast.Modifier)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 Printable (com.github.javaparser.printer.Printable)1 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ConstraintFormula (com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula)1