BUG-4329: switch to javaparser
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / SieASTVisitor.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
9
10 import static org.junit.Assert.assertEquals;
11 import com.github.javaparser.ast.Node;
12 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
13 import com.github.javaparser.ast.body.MethodDeclaration;
14 import com.github.javaparser.ast.comments.Comment;
15 import com.github.javaparser.ast.comments.JavadocComment;
16 import com.github.javaparser.ast.expr.NormalAnnotationExpr;
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.opendaylight.controller.config.api.annotations.Description;
20 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
21
22 class SieASTVisitor extends AbstractVerifier {
23     private final Map<String, String> methodDescriptions = new HashMap<>();
24     protected String descriptionAnotValue;
25     protected String extnds;
26     protected String javadoc;
27     protected String sieAnnotValue;
28     protected String sieAnnotOsgiRegistrationType;
29
30     SieASTVisitor(final String expectedPackageName, final String fileName) {
31         super(expectedPackageName, fileName);
32     }
33
34     @Override
35     public void visit(final ClassOrInterfaceDeclaration n, final Void arg) {
36         extnds = n.getExtends().toString();
37
38         final Comment c = n.getComment();
39         if (c instanceof JavadocComment) {
40             javadoc = c.toString();
41         }
42
43         super.visit(n, arg);
44     }
45
46     @Override
47     public void visit(final NormalAnnotationExpr expr, final Void arg) {
48         final String fqcn = expr.getName().toString();
49         if (fqcn.equals(Description.class.getCanonicalName())) {
50             final Node parent = expr.getParentNode();
51             final String value = expr.getPairs().get(0).toString();
52             if (parent instanceof ClassOrInterfaceDeclaration) {
53                 descriptionAnotValue = value;
54             } else if (parent instanceof MethodDeclaration) {
55                 methodDescriptions.put(((MethodDeclaration) parent).getName(), value);
56             }
57         } else if (fqcn.equals(ServiceInterfaceAnnotation.class.getCanonicalName())) {
58             String text1 = expr.getPairs().get(0).toString();
59             String text2 = expr.getPairs().get(1).toString();
60             if (text1.contains("value")) {
61                 sieAnnotValue = text1;
62                 sieAnnotOsgiRegistrationType = text2;
63             } else {
64                 sieAnnotValue = text2;
65                 sieAnnotOsgiRegistrationType = text1;
66             }
67         }
68
69         super.visit(expr, arg);
70     }
71
72     final void assertMethodDescriptions(final int expected) {
73         assertEquals("Incorrenct number of generated method descriptions", expected, methodDescriptions.size());
74     }
75 }