一 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