private static void validateOnErrorResource(BLangFunction resource, DiagnosticLog dlog, boolean isClient) { List<BLangSimpleVariable> paramDetails = resource.getParameters(); validateParamDetailsSize(paramDetails, 2, resource, dlog); validateEndpointParameter(resource, dlog, paramDetails, isClient); if (paramDetails.size() < 2 || !"error".equals(paramDetails.get(1).type.toString())) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, resource.pos, String.format( "Invalid resource signature for %s resource in service : The second parameter should be an error", resource.getName().getValue())); } }
private static DocAttachmentInfo getFunctionNodeDocumentation(BLangFunction bLangFunction) { List<String> attributes = new ArrayList<>(); DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition(bLangFunction.getPosition()); List<BLangAnnotationAttachment> annotations = bLangFunction.getAnnotationAttachments(); Position docStart = getDocumentationStartPosition(bLangFunction.getPosition(), annotations); int offset = functionPos.getStartColumn(); List<BLangVariable> params = new ArrayList<>(bLangFunction.getParameters()); if (bLangFunction.getRestParameters() != null) { params.add((BLangVariable) bLangFunction.getRestParameters()); } params.addAll(bLangFunction.getDefaultableParameters() .stream() .map(bLangVariableDef -> bLangVariableDef.var) .collect(Collectors.toList())); params.sort(new FunctionArgsComparator()); params.forEach(param -> attributes.add(getDocAttributeFromBLangVariable((BLangSimpleVariable) param, offset))); if (bLangFunction.symbol.retType.getKind() != TypeKind.NIL) { attributes.add(getReturnFieldDescription(offset)); } return new DocAttachmentInfo(getDocumentationAttachment(attributes, functionPos.getStartColumn()), docStart); }
@Override public void visit(BLangFunction funcNode) { SymbolKind symbolKind = SymbolKind.Function; if (UtilSymbolKeys.NEW_KEYWORD_KEY.equals(funcNode.name.value) || funcNode.getBody() == null) { return; } this.addSymbol(funcNode, funcNode.symbol, symbolKind); if (!funcNode.getWorkers().isEmpty()) { funcNode.getWorkers().forEach(bLangWorker -> bLangWorker.accept(this)); return; } funcNode.getBody().accept(this); }
@Override public void visit(BLangFunction funcNode) { validateParameters(funcNode, funcNode.getParameters(), funcNode.getDefaultableParameters(), funcNode.restParam, DiagnosticCode.UNDOCUMENTED_PARAMETER, DiagnosticCode.NO_SUCH_DOCUMENTABLE_PARAMETER, DiagnosticCode.PARAMETER_ALREADY_DOCUMENTED); boolean hasReturn = true; if (funcNode.returnTypeNode.getKind() == NodeKind.VALUE_TYPE) { hasReturn = ((BLangValueType) funcNode.returnTypeNode).typeKind != TypeKind.NIL; } validateReturnParameter(funcNode, funcNode, hasReturn); }
void startLambdaFunctionDef(PackageID pkgID) { // Passing zero for annotation count as Lambdas can't have annotations. startFunctionDef(0); BLangFunction lambdaFunction = (BLangFunction) this.invokableNodeStack.peek(); lambdaFunction.setName(createIdentifier(anonymousModelHelper.getNextAnonymousFunctionKey(pkgID))); lambdaFunction.addFlag(Flag.LAMBDA); }
@Override public String toString() { return "LambdaRef:" + (function != null ? String.valueOf(function.getName()) : null); } }
public void validate(String serviceName, BLangFunction resource, DiagnosticLog dlog) { switch (resource.getName().getValue()) { case RESOURCE_NAME_ON_CREATE: case RESOURCE_NAME_ON_DELETE: case RESOURCE_NAME_ON_MODIFY: final List<BLangSimpleVariable> parameters = resource.getParameters(); String msg = "Invalid resource signature for %s in service %s. " + "The parameter should be a file:FileEvent with no returns."; msg = String.format(msg, resource.getName().getValue(), serviceName); if (parameters.size() != 1) { dlog.logDiagnostic(ERROR, resource.getPosition(), msg); return; if (!"file".equals(event.tsymbol.pkgID.name.value) || !FILE_SYSTEM_EVENT .equals(event.tsymbol.name.value)) { dlog.logDiagnostic(ERROR, resource.getPosition(), msg); return; dlog.logDiagnostic(ERROR, resource.getPosition(), msg); dlog.logDiagnostic(ERROR, resource.getPosition(), "Invalid resource name " + resource.getName().getValue() + " in service " + serviceName);
/** * Gets the path value of the @http:resourceConfig. * * @param resource The ballerina resource. * @return The path value. */ private String getPath(BLangFunction resource) { String path = "/" + resource.getName(); AnnotationAttachmentNode annotation = ConverterUtils .getAnnotationFromList(HttpConstants.ANN_NAME_RESOURCE_CONFIG, httpAlias, resource.getAnnotationAttachments()); if (annotation != null) { BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) annotation) .getExpression()); List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs(); Map<String, BLangExpression> attributes = ConverterUtils.listToMap(list); if (attributes.containsKey(HttpConstants.ANN_RESOURCE_ATTR_PATH)) { path = ConverterUtils.getStringLiteralValue(attributes.get(HttpConstants.ANN_RESOURCE_ATTR_PATH)); } } return path; }
@Override public void visit(BLangFunction funcNode) { String functionName = funcNode.getName().getValue(); SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, this.symbolEnv); CursorPositionResolver cpr = CursorPositionResolvers.getResolverByClass(this.cursorPositionResolver); funcNode.annAttachments.forEach(annotationAttachment -> this.acceptNode(annotationAttachment, funcEnv)); boolean withinParamContext = CompletionVisitorUtil .isWithinParameterContext(functionName, UtilSymbolKeys.FUNCTION_KEYWORD_KEY, funcEnv, lsContext, this); boolean cursorBeforeNode = cpr.isCursorBeforeNode(funcNode.getPosition(), this, this.lsContext, funcNode, funcNode.symbol); if (terminateVisitor || cursorBeforeNode || withinParamContext) { return; } if (funcNode.getBody() != null) { this.blockOwnerStack.push(funcNode); this.cursorPositionResolver = BlockStatementScopeResolver.class; this.acceptNode(funcNode.body, funcEnv); this.blockOwnerStack.pop(); } }
private static boolean validateResourceSignature(BLangFunction resourceNode, DiagnosticLog dlog, DiagnosticPos pos) { List<BLangSimpleVariable> signatureParams = resourceNode.getParameters(); final int nParams = signatureParams.size(); if (nParams < COMPULSORY_PARAM_COUNT) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, pos, "resource signature parameter count should be >= 1"); return false; } if (!isValidResourceParam(signatureParams.get(0), ENDPOINT_TYPE)) { dlog.logDiagnostic(Diagnostic.Kind.ERROR, pos, "first parameter should be of type " + ENDPOINT_TYPE); return false; } return true; }
resource.getAnnotationAttachments()); messageParameter.setName(resource.getParameters().get(0).getName().getValue()); RefModel refModel = new RefModel(); refModel.setReference(ConverterConstants.ATTR_REQUEST);
public void visit(BLangFunction funcNode) { List<BLangAnnotationAttachment> attachmentList = funcNode.getAnnotationAttachments(); notifyProcessors(attachmentList, (processor, list) -> processor.process(funcNode, list)); funcNode.endpoints.forEach(endpoint -> endpoint.accept(this)); }
@Override public void visit(BLangArrowFunction bLangArrowFunction) { BLangFunction bLangFunction = (BLangFunction) TreeBuilder.createFunctionNode(); bLangFunction.setName(bLangArrowFunction.functionName); bLangFunction.addFlag(Flag.LAMBDA); lambdaFunction.function = bLangFunction; bLangFunction.setReturnTypeNode(returnType); bLangFunction.setBody(populateArrowExprBodyBlock(bLangArrowFunction));
/** * Add function node location to locations list. * * @param node function node to calculate the location of * @param ownerPkg package of the owner * @param currentPkg package of the current node as a list of package paths */ private void addLocation(BLangFunction node, String ownerPkg, String currentPkg) { DiagnosticPos position = node.getPosition(); Set<Whitespace> wsSet = node.getWS(); if (wsSet != null && wsSet.size() > 4 && !node.annAttachments.isEmpty()) { Whitespace[] wsArray = new Whitespace[wsSet.size()]; wsSet.toArray(wsArray); Arrays.sort(wsArray); int lastAnnotationEndline = CommonUtil.getLastItem(node.annAttachments).pos.eLine; position.sLine = lastAnnotationEndline + wsArray[0].getWs().split(CommonUtil.LINE_SEPARATOR_SPLIT).length - 1; } this.locations.add(getLocation(node, ownerPkg, currentPkg)); }
/** * Get the Documentation attachment for the function. * * @param bLangPackage BLangPackage built * @param line Start line of the function in the source * @return {@link DocAttachmentInfo} Documentation attachment for the function */ private static DocAttachmentInfo getFunctionDocumentationByPosition(BLangPackage bLangPackage, int line) { List<BLangFunction> filteredFunctions = new ArrayList<>(); for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) { if (topLevelNode instanceof BLangFunction) { filteredFunctions.add((BLangFunction) topLevelNode); } else if (topLevelNode instanceof BLangTypeDefinition && ((BLangTypeDefinition) topLevelNode).typeNode instanceof BLangObjectTypeNode) { filteredFunctions .addAll(((BLangObjectTypeNode) (((BLangTypeDefinition) topLevelNode).typeNode)).getFunctions()); } } for (BLangFunction filteredFunction : filteredFunctions) { DiagnosticPos functionPos = CommonUtil.toZeroBasedPosition(filteredFunction.getPosition()); int functionStart = functionPos.getStartLine(); if (functionStart == line) { return getFunctionNodeDocumentation(filteredFunction); } } return null; }
public static BLangFunction createInitFunction(DiagnosticPos pos, String name, Name sufix) { BLangFunction initFunction = (BLangFunction) TreeBuilder.createFunctionNode(); initFunction.setName(createIdentifier(name + sufix.getValue())); initFunction.flagSet = EnumSet.of(Flag.PUBLIC); initFunction.pos = pos; BLangValueType typeNode = (BLangValueType) TreeBuilder.createValueTypeNode(); typeNode.pos = pos; typeNode.typeKind = TypeKind.NIL; initFunction.returnTypeNode = typeNode; // Create body of the init function BLangBlockStmt body = (BLangBlockStmt) TreeBuilder.createBlockNode(); body.pos = pos; initFunction.setBody(body); return initFunction; }
static BLangFunction createFunction(DiagnosticPos pos, String name) { final BLangFunction bLangFunction = (BLangFunction) TreeBuilder.createFunctionNode(); final IdentifierNode funcName = createIdentifier(pos, name); bLangFunction.setName(funcName); bLangFunction.flagSet = EnumSet.of(Flag.LAMBDA); bLangFunction.pos = pos; // Create body of the function bLangFunction.body = createBlockStmt(pos); return bLangFunction; }
private void addWorkerInfoEntries(CallableUnitInfo callableUnitInfo, BLangFunction funcNode) { UTF8CPEntry workerNameCPEntry = new UTF8CPEntry(funcNode.defaultWorkerName.value); int workerNameCPIndex = this.currentPkgInfo.addCPEntry(workerNameCPEntry); callableUnitInfo.defaultWorkerInfo = new WorkerInfo(workerNameCPIndex, funcNode.defaultWorkerName.value); for (BLangWorker worker : funcNode.getWorkers()) { workerNameCPEntry = new UTF8CPEntry(worker.name.value); workerNameCPIndex = currentPkgInfo.addCPEntry(workerNameCPEntry); WorkerInfo workerInfo = new WorkerInfo(workerNameCPIndex, worker.getName().value); callableUnitInfo.addWorkerInfo(worker.getName().value, workerInfo); } }
private BLangFunction extractMainFunction(BLangPackage myPkg) { for (BLangFunction func : myPkg.getFunctions()) { if (MAIN_FUNC_NAME.equals(func.getName().value)) { return func; } } return null; }
AnnotationAttachmentNode annotation = ConverterUtils .getAnnotationFromList(HttpConstants.ANN_NAME_RESOURCE_CONFIG, httpAlias, resource.getAnnotationAttachments()); operation.setPath(resource.getName().getValue());