Search in sources :

Example 6 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class Generator method annotFieldAnnotation.

/**
 * Get description annotation of the annotation attribute.
 * @param annotationNode parent node.
 * @param annotAttribute annotation attribute.
 * @return description of the annotation attribute.
 */
private static String annotFieldAnnotation(BLangAnnotation annotationNode, BLangAnnotAttribute annotAttribute) {
    List<? extends AnnotationAttachmentNode> annotationAttachments = getAnnotationAttachments(annotationNode);
    for (AnnotationAttachmentNode annotation : annotationAttachments) {
        if ("Field".equals(annotation.getAnnotationName().getValue())) {
            BLangRecordLiteral bLangRecordLiteral = (BLangRecordLiteral) annotation.getExpression();
            BLangExpression bLangLiteral = bLangRecordLiteral.getKeyValuePairs().get(0).getValue();
            String value = bLangLiteral.toString();
            if (value.startsWith(annotAttribute.getName().getValue())) {
                String[] valueParts = value.split(":");
                return valueParts.length == 2 ? valueParts[1] : valueParts[0];
            }
        }
    }
    return "";
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 7 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class DocumentationTest method testDocAnnotation.

@Test(description = "Test doc annotation.")
public void testDocAnnotation() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/annotation.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for Test annotation\n");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "a");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " annotation `field a` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "b");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " annotation `field b` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "c");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " annotation `field c` documentation");
    docNodes = ((BLangAnnotation) packageNode.getAnnotations().get(0)).docAttachments;
    dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
}
Also used : BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 8 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class DocumentationTest method testDocStruct.

@Test(description = "Test doc struct.")
public void testDocStruct() {
    CompileResult compileResult = BCompileUtil.compile("test-src/documentation/struct.bal");
    Assert.assertEquals(0, compileResult.getWarnCount());
    PackageNode packageNode = compileResult.getAST();
    List<BLangDocumentation> docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
    BLangDocumentation dNode = docNodes.get(0);
    Assert.assertNotNull(dNode);
    Assert.assertEquals(dNode.documentationText, " Documentation for Test struct\n");
    Assert.assertEquals(dNode.getAttributes().size(), 3);
    Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "a");
    Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " struct `field a` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "b");
    Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " struct `field b` documentation\n");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "c");
    Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " struct `field c` documentation");
}
Also used : BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) CompileResult(org.ballerinalang.launcher.util.CompileResult) PackageNode(org.ballerinalang.model.tree.PackageNode) Test(org.testng.annotations.Test)

Example 9 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class HtmlDocTest method testAnonymousStructs.

@Test(description = "Tests whether anonymous structs are documented.")
public void testAnonymousStructs() {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description { value:\"Represents a person\"}" + "@Field {value:\"id: The identification number\"}\n" + "@Field {value:\"address: The address of the person.\"}" + "public struct Person {" + "  string id;" + "  struct {" + "     string address1;" + "     string address2;" + "     string state = \"MN\";" + "  } address;" + "}");
    Page page = generatePage(bLangPackage);
    Assert.assertEquals(page.constructs.size(), 1);
    Assert.assertTrue(page.constructs.get(0) instanceof StructDoc, "Documentable of type StructDoc expected.");
    StructDoc personStructDoc = (StructDoc) page.constructs.get(0);
    Assert.assertEquals(personStructDoc.fields.size(), 2, "2 fields are expected.");
    Assert.assertEquals(personStructDoc.fields.get(0).name, "id", "Field \"id\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).name, "address", "Field \"address\" expected.");
    Assert.assertEquals(personStructDoc.fields.get(1).description, "The address of the person.");
    Assert.assertEquals(personStructDoc.fields.get(1).dataType, "struct {string address1, string address2, string state}");
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Page(org.ballerinalang.docgen.model.Page) StructDoc(org.ballerinalang.docgen.model.StructDoc) Test(org.testng.annotations.Test)

Example 10 with FIELD

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.FIELD in project ballerina by ballerina-lang.

the class HtmlDocTest method testAnnotationPropertiesExtracted.

@Test(description = "Annotation properties should be available via construct", enabled = false)
public void testAnnotationPropertiesExtracted() throws Exception {
    BLangPackage bLangPackage = createPackage("package x.y; " + "@Description {value: \"AnnotationDoc to upgrade connection from HTTP to WS in the " + "same base path\"}\n" + "@Field {value:\"upgradePath:Upgrade path for the WebSocket service from " + "HTTP to WS\"}\n" + "@Field {value:\"serviceName:Name of the WebSocket service where the HTTP service should " + "               upgrade to\"}\n" + "public annotation webSocket attach service<> {\n" + "    string upgradePath;\n" + "    string serviceName;\n" + "}");
    AnnotationDoc annotationDoc = Generator.createDocForNode(bLangPackage.getAnnotations().get(0));
    Assert.assertEquals(annotationDoc.name, "webSocket", "Annotation name should be extracted");
    Assert.assertEquals(annotationDoc.description, "AnnotationDoc to upgrade connection from HTTP to WS " + "in the same base path", "Description of the annotation should be extracted");
    // Annotation Fields
    Assert.assertEquals(annotationDoc.attributes.get(0).name, "upgradePath", "Annotation attribute name " + "should be extracted");
    Assert.assertEquals(annotationDoc.attributes.get(0).dataType, "string", "Annotation attribute type " + "should be extracted");
    Assert.assertEquals(annotationDoc.attributes.get(0).description, "Upgrade path for the WebSocket service " + "from HTTP to WS", "Description of the annotation attribute should be extracted");
}
Also used : AnnotationDoc(org.ballerinalang.docgen.model.AnnotationDoc) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Test(org.testng.annotations.Test)

Aggregations

BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)14 Test (org.testng.annotations.Test)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)10 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)10 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)8 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)8 Name (org.wso2.ballerinalang.compiler.util.Name)8 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)7 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)7 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)7 CompileResult (org.ballerinalang.launcher.util.CompileResult)6 PackageNode (org.ballerinalang.model.tree.PackageNode)6 BLangDocumentation (org.wso2.ballerinalang.compiler.tree.BLangDocumentation)6 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)5 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)5 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)5