Search in sources :

Example 46 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class BLangPackageBuilder method endObjectInitFunctionDef.

void endObjectInitFunctionDef(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean publicFunc, boolean bodyExists) {
    BLangFunction function = (BLangFunction) this.invokableNodeStack.pop();
    endEndpointDeclarationScope();
    function.setName(this.createIdentifier(identifier));
    function.pos = pos;
    function.addWS(ws);
    if (publicFunc) {
        function.flagSet.add(Flag.PUBLIC);
    }
    if (!bodyExists) {
        function.body = null;
    }
    if (!function.deprecatedAttachments.isEmpty()) {
        function.flagSet.add(Flag.DEPRECATED);
    }
    ((BLangObject) this.objectStack.peek()).initFunction = function;
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction)

Example 47 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class BLangPackageBuilder method endObjectAttachedFunctionDef.

void endObjectAttachedFunctionDef(DiagnosticPos pos, Set<Whitespace> ws, boolean publicFunc, boolean nativeFunc, boolean bodyExists) {
    BLangFunction function = (BLangFunction) this.invokableNodeStack.pop();
    endEndpointDeclarationScope();
    function.pos = pos;
    function.addWS(ws);
    function.flagSet.add(Flag.ATTACHED);
    if (publicFunc) {
        function.flagSet.add(Flag.PUBLIC);
    }
    if (nativeFunc) {
        function.flagSet.add(Flag.NATIVE);
    }
    if (!bodyExists) {
        function.body = null;
        if (!nativeFunc) {
            function.interfaceFunction = true;
        }
    }
    function.attachedFunction = true;
    if (!function.deprecatedAttachments.isEmpty()) {
        function.flagSet.add(Flag.DEPRECATED);
    }
    this.objectStack.peek().addFunction(function);
}
Also used : BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction)

Example 48 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class TreeVisitor method visit.

public void visit(BLangFunction funcNode) {
    // Check for native functions
    BSymbol funcSymbol = funcNode.symbol;
    if (Symbols.isNative(funcSymbol)) {
        return;
    }
    String functionName = funcNode.getName().getValue();
    SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcSymbol.scope, symbolEnv);
    if (isWithinParameterContext(functionName, NODE_TYPE_FUNCTION)) {
        this.populateSymbols(this.resolveAllVisibleSymbols(funcEnv), funcEnv);
        setTerminateVisitor(true);
    } else if (!ScopeResolverConstants.getResolverByClass(cursorPositionResolver).isCursorBeforeNode(funcNode.getPosition(), funcNode, this, this.documentServiceContext)) {
        // Visit the endpoints
        funcNode.endpoints.forEach(bLangEndpoint -> this.acceptNode(bLangEndpoint, funcEnv));
        this.blockOwnerStack.push(funcNode);
        // Cursor position is calculated against the Block statement scope resolver
        cursorPositionResolver = BlockStatementScopeResolver.class;
        this.acceptNode(funcNode.body, funcEnv);
        this.blockOwnerStack.pop();
        // Process workers
        if (terminateVisitor && !funcNode.workers.isEmpty()) {
            this.setTerminateVisitor(false);
        }
        funcNode.workers.forEach(e -> this.symbolEnter.defineNode(e, funcEnv));
        funcNode.workers.forEach(e -> this.acceptNode(e, funcEnv));
    }
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BLangWorkerReceive(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerReceive) BLangBreak(org.wso2.ballerinalang.compiler.tree.statements.BLangBreak) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) LSNodeVisitor(org.ballerinalang.langserver.common.LSNodeVisitor) BLangTryCatchFinally(org.wso2.ballerinalang.compiler.tree.statements.BLangTryCatchFinally) Map(java.util.Map) ServiceScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ServiceScopeResolver) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) SymbolResolver(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) Names(org.wso2.ballerinalang.compiler.util.Names) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) ConnectorScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ConnectorScopeResolver) BLangLock(org.wso2.ballerinalang.compiler.tree.statements.BLangLock) ScopeResolverConstants(org.ballerinalang.langserver.completions.util.ScopeResolverConstants) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ObjectTypeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ObjectTypeScopeResolver) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) Collectors(java.util.stream.Collectors) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangCatch(org.wso2.ballerinalang.compiler.tree.statements.BLangCatch) List(java.util.List) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) ResourceParamScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.ResourceParamScopeResolver) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangWorker(org.wso2.ballerinalang.compiler.tree.BLangWorker) BLangBind(org.wso2.ballerinalang.compiler.tree.statements.BLangBind) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) SymbolEnter(org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolEnter) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) PackageNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.PackageNodeScopeResolver) BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BLangAbort(org.wso2.ballerinalang.compiler.tree.statements.BLangAbort) HashMap(java.util.HashMap) BLangTransaction(org.wso2.ballerinalang.compiler.tree.statements.BLangTransaction) Stack(java.util.Stack) ArrayList(java.util.ArrayList) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangMatch(org.wso2.ballerinalang.compiler.tree.statements.BLangMatch) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangForkJoin(org.wso2.ballerinalang.compiler.tree.statements.BLangForkJoin) BLangEndpointTypeNode(org.wso2.ballerinalang.compiler.tree.types.BLangEndpointTypeNode) MatchStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.MatchStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangNext(org.wso2.ballerinalang.compiler.tree.statements.BLangNext) Symbols(org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols) BLangThrow(org.wso2.ballerinalang.compiler.tree.statements.BLangThrow) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Name(org.wso2.ballerinalang.compiler.util.Name) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) TopLevelNodeScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.TopLevelNodeScopeResolver) BLangWorkerSend(org.wso2.ballerinalang.compiler.tree.statements.BLangWorkerSend) Node(org.ballerinalang.model.tree.Node) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) Collections(java.util.Collections) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BlockStatementScopeResolver(org.ballerinalang.langserver.completions.util.positioning.resolvers.BlockStatementScopeResolver) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 49 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class HoverUtil method getHoverInformation.

/**
 * Get the hover information for the given hover context.
 *
 * @param bLangPackage resolved bLangPackage for the hover context.
 * @param hoverContext context of the hover.
 * @return hover content.
 */
public static Hover getHoverInformation(BLangPackage bLangPackage, TextDocumentServiceContext hoverContext) {
    Hover hover;
    switch(hoverContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY)) {
        case ContextConstants.FUNCTION:
            BLangFunction bLangFunction = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangFunction != null) {
                if (bLangFunction.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangFunction.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangFunction.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.STRUCT:
            BLangStruct bLangStruct = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangStruct != null) {
                if (bLangStruct.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangStruct.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangStruct.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.OBJECT:
            BLangObject bLangObject = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangObject != null) {
                if (bLangObject.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangObject.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangObject.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.ENUM:
            BLangEnum bLangEnum = bLangPackage.enums.stream().filter(bEnum -> bEnum.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangEnum != null) {
                if (bLangEnum.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangEnum.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangEnum.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.TRANSFORMER:
            BLangTransformer bLangTransformer = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangTransformer != null) {
                if (bLangTransformer.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangTransformer.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangTransformer.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.CONNECTOR:
            BLangConnector bLangConnector = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangConnector != null) {
                if (bLangConnector.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangConnector.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangConnector.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.ACTION:
            BLangAction bLangAction = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) hoverContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangAction != null) {
                if (bLangAction.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangAction.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangAction.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.ENDPOINT:
            BLangEndpoint bLangEndpoint = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangEndpoint != null) {
                hover = getAnnotationContent(bLangEndpoint.annAttachments);
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.VARIABLE:
            BLangVariable bLangVariable = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangVariable != null) {
                if (bLangVariable.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangVariable.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangVariable.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        default:
            hover = new Hover();
            List<Either<String, MarkedString>> contents = new ArrayList<>();
            contents.add(Either.forLeft(""));
            hover.setContents(contents);
            break;
    }
    return hover;
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) HashMap(java.util.HashMap) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) Hover(org.eclipse.lsp4j.Hover) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) ArrayList(java.util.ArrayList) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) ContextConstants(org.ballerinalang.langserver.common.constants.ContextConstants) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) MarkedString(org.eclipse.lsp4j.MarkedString) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangDocumentationAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangDocumentationAttribute) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) ArrayList(java.util.ArrayList) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) Hover(org.eclipse.lsp4j.Hover) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector)

Example 50 with BLangFunction

use of org.wso2.ballerinalang.compiler.tree.BLangFunction in project ballerina by ballerina-lang.

the class BallerinaParserService method generateJSON.

public static JsonElement generateJSON(Node node, Map<String, Node> anonStructs) throws InvocationTargetException, IllegalAccessException {
    if (node == null) {
        return JsonNull.INSTANCE;
    }
    Set<Method> methods = ClassUtils.getAllInterfaces(node.getClass()).stream().flatMap(aClass -> Arrays.stream(aClass.getMethods())).collect(Collectors.toSet());
    JsonObject nodeJson = new JsonObject();
    JsonArray wsJsonArray = new JsonArray();
    Set<Whitespace> ws = node.getWS();
    if (ws != null && !ws.isEmpty()) {
        for (Whitespace whitespace : ws) {
            JsonObject wsJson = new JsonObject();
            wsJson.addProperty("ws", whitespace.getWs());
            wsJson.addProperty("i", whitespace.getIndex());
            wsJson.addProperty("text", whitespace.getPrevious());
            wsJson.addProperty("static", whitespace.isStatic());
            wsJsonArray.add(wsJson);
        }
        nodeJson.add("ws", wsJsonArray);
    }
    Diagnostic.DiagnosticPosition position = node.getPosition();
    if (position != null) {
        JsonObject positionJson = new JsonObject();
        positionJson.addProperty("startColumn", position.getStartColumn());
        positionJson.addProperty("startLine", position.getStartLine());
        positionJson.addProperty("endColumn", position.getEndColumn());
        positionJson.addProperty("endLine", position.getEndLine());
        nodeJson.add("position", positionJson);
    }
    /* Virtual props */
    JsonArray type = getType(node);
    if (type != null) {
        nodeJson.add(SYMBOL_TYPE, type);
    }
    if (node.getKind() == NodeKind.INVOCATION) {
        assert node instanceof BLangInvocation : node.getClass();
        BLangInvocation invocation = (BLangInvocation) node;
        if (invocation.symbol != null && invocation.symbol.kind != null) {
            nodeJson.addProperty(INVOCATION_TYPE, invocation.symbol.kind.toString());
        }
    }
    for (Method m : methods) {
        String name = m.getName();
        if (name.equals("getWS") || name.equals("getPosition")) {
            continue;
        }
        String jsonName;
        if (name.startsWith("get")) {
            jsonName = toJsonName(name, 3);
        } else if (name.startsWith("is")) {
            jsonName = toJsonName(name, 2);
        } else {
            continue;
        }
        Object prop = m.invoke(node);
        /* Literal class - This class is escaped in backend to address cases like "ss\"" and 8.0 and null */
        if (node.getKind() == NodeKind.LITERAL && "value".equals(jsonName)) {
            if (prop instanceof String) {
                nodeJson.addProperty(jsonName, '"' + StringEscapeUtils.escapeJava((String) prop) + '"');
                nodeJson.addProperty(UNESCAPED_VALUE, String.valueOf(prop));
            } else {
                nodeJson.addProperty(jsonName, String.valueOf(prop));
            }
            continue;
        }
        if (node.getKind() == NodeKind.ANNOTATION && node instanceof BLangAnnotation) {
            JsonArray attachmentPoints = new JsonArray();
            ((BLangAnnotation) node).getAttachmentPoints().stream().map(BLangAnnotationAttachmentPoint::getAttachmentPoint).map(BLangAnnotationAttachmentPoint.AttachmentPoint::getValue).map(JsonPrimitive::new).forEach(attachmentPoints::add);
            nodeJson.add("attachmentPoints", attachmentPoints);
        }
        if (node.getKind() == NodeKind.USER_DEFINED_TYPE && jsonName.equals("typeName")) {
            IdentifierNode typeNode = (IdentifierNode) prop;
            Node structNode;
            if (typeNode.getValue().startsWith("$anonStruct$") && (structNode = anonStructs.remove(typeNode.getValue())) != null) {
                JsonObject anonStruct = generateJSON(structNode, anonStructs).getAsJsonObject();
                anonStruct.addProperty("anonStruct", true);
                nodeJson.add("anonStruct", anonStruct);
                continue;
            }
        }
        if (prop instanceof List && jsonName.equals("types")) {
            // Currently we don't need any Symbols for the UI. So skipping for now.
            continue;
        }
        /* Node classes */
        if (prop instanceof Node) {
            nodeJson.add(jsonName, generateJSON((Node) prop, anonStructs));
        } else if (prop instanceof List) {
            List listProp = (List) prop;
            JsonArray listPropJson = new JsonArray();
            nodeJson.add(jsonName, listPropJson);
            for (Object listPropItem : listProp) {
                if (listPropItem instanceof Node) {
                    /* Remove top level anon func and struct */
                    if (node.getKind() == NodeKind.COMPILATION_UNIT) {
                        if (listPropItem instanceof BLangStruct && ((BLangStruct) listPropItem).isAnonymous) {
                            anonStructs.put(((BLangStruct) listPropItem).getName().getValue(), ((BLangStruct) listPropItem));
                            continue;
                        }
                        if (listPropItem instanceof BLangFunction && (((BLangFunction) listPropItem)).name.value.startsWith("$lambda$")) {
                            continue;
                        }
                    }
                    listPropJson.add(generateJSON((Node) listPropItem, anonStructs));
                } else {
                    logger.debug("Can't serialize " + jsonName + ", has a an array of " + listPropItem);
                }
            }
        /* Runtime model classes */
        } else if (prop instanceof Set && jsonName.equals("flags")) {
            Set flags = (Set) prop;
            for (Flag flag : Flag.values()) {
                nodeJson.addProperty(StringUtils.lowerCase(flag.toString()), flags.contains(flag));
            }
        } else if (prop instanceof Set) {
            // TODO : limit this else if to getInputs getOutputs of transform.
            Set vars = (Set) prop;
            JsonArray listVarJson = new JsonArray();
            nodeJson.add(jsonName, listVarJson);
            for (Object obj : vars) {
                listVarJson.add(obj.toString());
            }
        } else if (prop instanceof NodeKind) {
            String kindName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, prop.toString());
            nodeJson.addProperty(jsonName, kindName);
        } else if (prop instanceof OperatorKind) {
            nodeJson.addProperty(jsonName, prop.toString());
        /* Generic classes */
        } else if (prop instanceof String) {
            nodeJson.addProperty(jsonName, (String) prop);
        } else if (prop instanceof Number) {
            nodeJson.addProperty(jsonName, (Number) prop);
        } else if (prop instanceof Boolean) {
            nodeJson.addProperty(jsonName, (Boolean) prop);
        } else if (prop instanceof Enum) {
            nodeJson.addProperty(jsonName, StringUtils.lowerCase(((Enum) prop).name()));
        } else if (prop != null) {
            nodeJson.addProperty(jsonName, prop.toString());
            String message = "Node " + node.getClass().getSimpleName() + " contains unknown type prop: " + jsonName + " of type " + prop.getClass();
            logger.error(message);
        }
    }
    return nodeJson;
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) StringUtils(org.apache.commons.lang3.StringUtils) ClassUtils(org.apache.commons.lang3.ClassUtils) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) MediaType(javax.ws.rs.core.MediaType) OperatorKind(org.ballerinalang.model.tree.OperatorKind) Flag(org.ballerinalang.model.elements.Flag) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) Consumes(javax.ws.rs.Consumes) Gson(com.google.gson.Gson) Map(java.util.Map) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) StringEscapeUtils(org.apache.commons.lang3.StringEscapeUtils) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) Method(java.lang.reflect.Method) BLangFragmentParser(org.ballerinalang.composer.service.ballerina.parser.service.util.BLangFragmentParser) ServiceType(org.ballerinalang.composer.server.spi.ServiceType) Set(java.util.Set) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) TextDocumentServiceUtil(org.ballerinalang.langserver.TextDocumentServiceUtil) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) Whitespace(org.ballerinalang.model.Whitespace) JsonArray(com.google.gson.JsonArray) List(java.util.List) ServerConstants(org.ballerinalang.composer.server.core.ServerConstants) NodeKind(org.ballerinalang.model.tree.NodeKind) Response(javax.ws.rs.core.Response) BLangSourceFragment(org.ballerinalang.composer.service.ballerina.parser.service.model.BLangSourceFragment) Optional(java.util.Optional) ServiceInfo(org.ballerinalang.composer.server.spi.ServiceInfo) BallerinaFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BallerinaFile) GET(javax.ws.rs.GET) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) HashMap(java.util.HashMap) JsonParser(com.google.gson.JsonParser) ParserUtils(org.ballerinalang.composer.service.ballerina.parser.service.util.ParserUtils) JsonElement(com.google.gson.JsonElement) ComposerService(org.ballerinalang.composer.server.spi.ComposerService) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Constants(org.ballerinalang.composer.service.ballerina.parser.Constants) JsonPrimitive(com.google.gson.JsonPrimitive) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) CaseFormat(com.google.common.base.CaseFormat) ModelPackage(org.ballerinalang.composer.service.ballerina.parser.service.model.lang.ModelPackage) IOException(java.io.IOException) OPTIONS(javax.ws.rs.OPTIONS) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) Paths(java.nio.file.Paths) Node(org.ballerinalang.model.tree.Node) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) JsonNull(com.google.gson.JsonNull) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Set(java.util.Set) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) Node(org.ballerinalang.model.tree.Node) JsonObject(com.google.gson.JsonObject) Diagnostic(org.ballerinalang.util.diagnostic.Diagnostic) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Whitespace(org.ballerinalang.model.Whitespace) NodeKind(org.ballerinalang.model.tree.NodeKind) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) List(java.util.List) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) Method(java.lang.reflect.Method) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) Flag(org.ballerinalang.model.elements.Flag) JsonArray(com.google.gson.JsonArray) OperatorKind(org.ballerinalang.model.tree.OperatorKind) JsonObject(com.google.gson.JsonObject)

Aggregations

BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)37 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)24 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)16 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)16 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)15 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 ArrayList (java.util.ArrayList)14 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)14 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)13 List (java.util.List)11 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)10 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)10 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)10 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)9 BLangEnum (org.wso2.ballerinalang.compiler.tree.BLangEnum)9 BLangObject (org.wso2.ballerinalang.compiler.tree.BLangObject)9 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)9 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)9 Collectors (java.util.stream.Collectors)8 NodeKind (org.ballerinalang.model.tree.NodeKind)8