Search in sources :

Example 6 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final SingleMemberAnnotationExpr node1, final Node other) {
    SingleMemberAnnotationExpr node2 = (SingleMemberAnnotationExpr) other;
    defaultAction(node1, node2);
    node1.getMemberValue().accept(this, node2.getMemberValue());
    node1.getName().accept(this, node2.getName());
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)

Example 7 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveReflectionSingleMemberAnnotation.

@Test
void solveReflectionSingleMemberAnnotation() {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CC");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    SingleMemberAnnotationExpr annotationExpr = (SingleMemberAnnotationExpr) method.getBody().get().getStatement(0).asExpressionStmt().getExpression().asVariableDeclarationExpr().getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("java.lang.SuppressWarnings", resolved.getQualifiedName());
    assertEquals("java.lang", resolved.getPackageName());
    assertEquals("SuppressWarnings", resolved.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) Test(org.junit.jupiter.api.Test)

Example 8 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project javaparser by javaparser.

the class AnnotationsResolutionTest method solveJavaParserSingleMemberAnnotation.

@Test
void solveJavaParserSingleMemberAnnotation() {
    // parse compilation unit and get annotation expression
    CompilationUnit cu = parseSample("Annotations");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "CC");
    SingleMemberAnnotationExpr annotationExpr = (SingleMemberAnnotationExpr) clazz.getAnnotation(0);
    // resolve annotation expression
    ResolvedAnnotationDeclaration resolved = annotationExpr.resolve();
    // check that the expected annotation declaration equals the resolved annotation declaration
    assertEquals("foo.bar.MyAnnotationWithSingleValue", resolved.getQualifiedName());
    assertEquals("foo.bar", resolved.getPackageName());
    assertEquals("MyAnnotationWithSingleValue", resolved.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedAnnotationDeclaration(com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration) Test(org.junit.jupiter.api.Test)

Example 9 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project kogito-runtimes by kiegroup.

the class PMMLRestResourceGeneratorTest method setPathValue.

@Test
void setPathValue() {
    final Optional<SingleMemberAnnotationExpr> retrievedOpt = TEMPLATE.findFirst(SingleMemberAnnotationExpr.class);
    assertTrue(retrievedOpt.isPresent());
    SingleMemberAnnotationExpr retrieved = retrievedOpt.get();
    assertEquals("Path", retrieved.getName().asString());
    pmmlRestResourceGenerator.setPathValue(TEMPLATE);
    String classPrefix = getSanitizedClassName(KIE_PMML_MODEL.getName());
    String expected = URLEncoder.encode(classPrefix).replaceAll("\\+", " ");
    assertEquals(expected, retrieved.getMemberValue().asStringLiteralExpr().asString());
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) Test(org.junit.jupiter.api.Test)

Example 10 with SingleMemberAnnotationExpr

use of com.github.javaparser.ast.expr.SingleMemberAnnotationExpr in project automatiko-engine by automatiko-io.

the class DependencyInjectionAnnotator method withSecurityRoles.

/**
 * Annotates given node with set of roles to enforce security
 *
 * @param node node to be annotated
 * @param roles roles that are allowed
 */
default <T extends NodeWithAnnotations<?>> T withSecurityRoles(T node, String[] roles) {
    if (roles != null && roles.length > 0) {
        List<Expression> rolesExpr = new ArrayList<>();
        for (String role : roles) {
            rolesExpr.add(new StringLiteralExpr(role.trim()));
        }
        node.addAnnotation(new SingleMemberAnnotationExpr(new Name("javax.annotation.security.RolesAllowed"), new ArrayInitializerExpr(NodeList.nodeList(rolesExpr))));
    }
    return node;
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) Expression(com.github.javaparser.ast.expr.Expression) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) ArrayList(java.util.ArrayList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Name(com.github.javaparser.ast.expr.Name)

Aggregations

SingleMemberAnnotationExpr (com.github.javaparser.ast.expr.SingleMemberAnnotationExpr)24 Name (com.github.javaparser.ast.expr.Name)9 CompilationUnit (com.github.javaparser.ast.CompilationUnit)8 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 NormalAnnotationExpr (com.github.javaparser.ast.expr.NormalAnnotationExpr)7 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)6 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)6 Expression (com.github.javaparser.ast.expr.Expression)5 MarkerAnnotationExpr (com.github.javaparser.ast.expr.MarkerAnnotationExpr)5 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)5 NameExpr (com.github.javaparser.ast.expr.NameExpr)5 Test (org.junit.jupiter.api.Test)5 ArrayInitializerExpr (com.github.javaparser.ast.expr.ArrayInitializerExpr)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 NodeList (com.github.javaparser.ast.NodeList)3 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 ResolvedAnnotationDeclaration (com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration)2