第三方插件antlr代码自动生成和自定义监听器原理分析

一  demo步骤

第三方插件编译之后打包:

1.pom引入相关插件:设定源文件和编译代码生成的目录

2.maven clean  ->maven package 注意maven编译和idea编译的区别,idea编译是不会反射生成antlr代码的

3.编译即可,在编译代码生成的目录看见代码

4.反射调用代码,因为是在编译期生成的代码,所以只能用反射

二 demo实例

利用antlr校验表达式

<plugin>
    <groupId>org.antlr</groupId>
    <artifactId>antlr4-maven-plugin</artifactId>
    <version>${antlr4-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>antlr</id>
            <goals>
                <goal>antlr4</goal>
            </goals>
            <phase>generate-sources</phase>
        </execution>
    </executions>
    <configuration>
        <sourceDirectory>${basedir}/src/main/resources/antlr4</sourceDirectory>
        <outputDirectory>${project.build.directory}/classes/com/huawei/workflowmgnt/utils/validator/expr/antlr4</outputDirectory>
        <listener>false</listener>
        <visitor>false</visitor>
        <treatWarningsAsErrors>true</treatWarningsAsErrors>
    </configuration>
</plugin>

     antlr表达式反射生成代码以及隔离符的设定  

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
 */

package com.huawei.workflowmgnt.utils.validator.expr;

import com.huawei.workflowmgnt.utils.MessageSourcesUtil;
import com.huawei.workflowmgnt.exception.ErrorCode;
import com.huawei.workflowmgnt.utils.ApplicationUtil;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFa
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值