Search in sources :

Example 1 with CompilerPlugin

use of org.ballerinalang.compiler.plugins.CompilerPlugin in project ballerina by ballerina-lang.

the class ProgramFileWriter method writeProgram.

public static void writeProgram(ProgramFile programFile, Path execFilePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(execFilePath));
    writeProgram(programFile, bos);
    // TODO Fix this properly. Load and invoke compiler plugins
    // TODO This will get properly fixed with the new packerina
    ServiceLoader<CompilerPlugin> processorServiceLoader = ServiceLoader.load(CompilerPlugin.class);
    processorServiceLoader.forEach(plugin -> {
        plugin.codeGenerated(execFilePath);
    });
}
Also used : CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with CompilerPlugin

use of org.ballerinalang.compiler.plugins.CompilerPlugin in project ballerina by ballerina-lang.

the class CompilerPluginRunner method notifyProcessors.

private void notifyProcessors(List<BLangAnnotationAttachment> attachments, BiConsumer<CompilerPlugin, List<AnnotationAttachmentNode>> notifier) {
    Map<CompilerPlugin, List<AnnotationAttachmentNode>> attachmentMap = new HashMap<>();
    for (BLangAnnotationAttachment attachment : attachments) {
        DefinitionID aID = new DefinitionID(attachment.annotationSymbol.pkgID.getName().value, attachment.annotationName.value);
        if (!processorMap.containsKey(aID)) {
            continue;
        }
        List<CompilerPlugin> procList = processorMap.get(aID);
        procList.forEach(proc -> {
            List<AnnotationAttachmentNode> attachmentNodes = attachmentMap.computeIfAbsent(proc, k -> new ArrayList<>());
            attachmentNodes.add(attachment);
        });
    }
    for (CompilerPlugin processor : attachmentMap.keySet()) {
        notifier.accept(processor, Collections.unmodifiableList(attachmentMap.get(processor)));
    }
}
Also used : HashMap(java.util.HashMap) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) ArrayList(java.util.ArrayList) List(java.util.List) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Example 3 with CompilerPlugin

use of org.ballerinalang.compiler.plugins.CompilerPlugin in project ballerina by ballerina-lang.

the class CompilerPluginRunner method handleEndpointProcesses.

private void handleEndpointProcesses(CompilerPlugin plugin) {
    // Get the list of endpoint of that this particular compiler plugin is interested in.
    SupportEndpointTypes supportEndpointTypes = plugin.getClass().getAnnotation(SupportEndpointTypes.class);
    if (supportEndpointTypes == null) {
        return;
    }
    final SupportEndpointTypes.EndpointType[] endpointTypes = supportEndpointTypes.value();
    if (endpointTypes.length == 0) {
        return;
    }
    DefinitionID[] definitions = Arrays.stream(endpointTypes).map(endpointType -> new DefinitionID(endpointType.packageName(), endpointType.name())).toArray(DefinitionID[]::new);
    for (DefinitionID definitionID : definitions) {
        if (isValidEndpoints(definitionID, plugin)) {
            List<CompilerPlugin> processorList = endpointProcessorMap.computeIfAbsent(definitionID, k -> new ArrayList<>());
            processorList.add(plugin);
        }
    }
}
Also used : Arrays(java.util.Arrays) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAnnotation(org.wso2.ballerinalang.compiler.tree.BLangAnnotation) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) HashMap(java.util.HashMap) BLangNodeVisitor(org.wso2.ballerinalang.compiler.tree.BLangNodeVisitor) ArrayList(java.util.ArrayList) SupportEndpointTypes(org.ballerinalang.compiler.plugins.SupportEndpointTypes) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) BLangResource(org.wso2.ballerinalang.compiler.tree.BLangResource) PackageCache(org.wso2.ballerinalang.compiler.PackageCache) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Names(org.wso2.ballerinalang.compiler.util.Names) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangDiagnosticLog(org.wso2.ballerinalang.compiler.util.diagnotic.BLangDiagnosticLog) SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv) CompilerPhase(org.ballerinalang.compiler.CompilerPhase) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangForever(org.wso2.ballerinalang.compiler.tree.statements.BLangForever) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) PackageID(org.ballerinalang.model.elements.PackageID) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ServiceLoader(java.util.ServiceLoader) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangXMLNS(org.wso2.ballerinalang.compiler.tree.BLangXMLNS) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) SymTag(org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) Objects(java.util.Objects) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangPackageDeclaration(org.wso2.ballerinalang.compiler.tree.BLangPackageDeclaration) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) Collections(java.util.Collections) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) SupportEndpointTypes(org.ballerinalang.compiler.plugins.SupportEndpointTypes)

Example 4 with CompilerPlugin

use of org.ballerinalang.compiler.plugins.CompilerPlugin in project ballerina by ballerina-lang.

the class CompilerPluginRunner method handleAnnotationProcesses.

private void handleAnnotationProcesses(CompilerPlugin plugin) {
    // Get the list of packages of annotations that this particular compiler plugin is interested in.
    SupportedAnnotationPackages supportedAnnotationPackages = plugin.getClass().getAnnotation(SupportedAnnotationPackages.class);
    if (supportedAnnotationPackages == null) {
        return;
    }
    String[] annotationPkgs = supportedAnnotationPackages.value();
    if (annotationPkgs.length == 0) {
        return;
    }
    for (String annPackage : annotationPkgs) {
        // Check whether each annotation type definition is available in the AST.
        List<BAnnotationSymbol> annotationSymbols = getAnnotationSymbols(annPackage);
        annotationSymbols.forEach(annSymbol -> {
            DefinitionID definitionID = new DefinitionID(annSymbol.pkgID.name.value, annSymbol.name.value);
            List<CompilerPlugin> processorList = processorMap.computeIfAbsent(definitionID, k -> new ArrayList<>());
            processorList.add(plugin);
        });
    }
}
Also used : CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol)

Example 5 with CompilerPlugin

use of org.ballerinalang.compiler.plugins.CompilerPlugin in project ballerina by ballerina-lang.

the class BinaryFileWriter method writeExecutableBinary.

public void writeExecutableBinary(BLangPackage packageNode, String fileName) {
    if (fileName == null || fileName.isEmpty()) {
        throw new IllegalArgumentException("invalid target file name");
    }
    if (!fileName.endsWith(BLANG_EXEC_FILE_SUFFIX)) {
        fileName += BLANG_EXEC_FILE_SUFFIX;
    }
    // Generate code for the given executable
    ProgramFile programFile = this.codeGenerator.generateBALX(packageNode);
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    try {
        ProgramFileWriter.writeProgram(programFile, byteArrayOS);
    } catch (IOException e) {
        throw new BLangCompilerException("error writing program file '" + fileName + "'", e);
    }
    final Path execFilePath = this.sourceDirectory.saveCompiledProgram(new ByteArrayInputStream(byteArrayOS.toByteArray()), fileName);
    ServiceLoader<CompilerPlugin> processorServiceLoader = ServiceLoader.load(CompilerPlugin.class);
    processorServiceLoader.forEach(plugin -> {
        plugin.codeGenerated(execFilePath);
    });
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) BLangCompilerException(org.ballerinalang.compiler.BLangCompilerException) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProgramFile(org.wso2.ballerinalang.programfile.CompiledBinaryFile.ProgramFile)

Aggregations

CompilerPlugin (org.ballerinalang.compiler.plugins.CompilerPlugin)6 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 SupportedAnnotationPackages (org.ballerinalang.compiler.plugins.SupportedAnnotationPackages)2 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)2 BAnnotationSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol)2 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ServiceLoader (java.util.ServiceLoader)1 BiConsumer (java.util.function.BiConsumer)1 BLangCompilerException (org.ballerinalang.compiler.BLangCompilerException)1