# 扩展依赖库说明(2.17及以上版本)

Java扩展依赖库自动生成NASL (opens new window)语法树,本工程交付方式为提供jar包,不提供源码。

考虑到可能与实现扩展依赖库的工程代码有第三方jar包依赖版本冲突,本工程代码未进行任何第三方jar包的依赖。

# 适用版本

jar包下载链接:点此下载 (opens new window)

支持平台版本:2.17及以上版本

# 使用方式

# 引用方式

此jar包可以放在maven仓库上也可以放在本地工程中进行引用。如果使用maven仓库的方式使用,为了使依赖不传递到应用中,建议设置optional为true。

  • 本地引用

若项目路径如下:

.
|── jar
|  |── nasl-metadata-collector-0.2.1.jar
|── pom.xml
|── src
|  |── main
|      |── java
|          |── *
1
2
3
4
5
6
7
8

在对应的pom.xml中配置如下:

<dependencies>
    <dependency>
        <artifactId>nasl-metadata-collector</artifactId>
        <groupId>com.netease.lowcode</groupId>
        <version>0.2.1</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/jar/nasl-metadata-collector-0.2.1.jar</systemPath>
    </dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
  • maven仓库引用

若将jar包上传到maven仓库中,则在工程的pom.xml中配置如下:

<dependencies>
    <dependency>
        <artifactId>nasl-metadata-collector</artifactId>
        <groupId>com.netease.lowcode</groupId>
        <version>0.2.1</version>
        <optional>true</optional>
    </dependency>
</dependencies>
1
2
3
4
5
6
7
8

# JSON文件生成路径

对代码进行编译后会在${CLASS_OUTPUT}/META-INF路径下生成nasl-metadata.json文件。

# 声明为NASL

对于NASL的声明使用注解的方式来进行实现,在进行编译时通过annotation processor (opens new window)。 来进行处理,无需额外配置即可生成NASL语法树。在对Java代码进行解析时,注解的解析会使用快速失败的方式,即解析某个逻辑时,如果使用到了未解析的数据结构,会先进行数据结构的解析,如果解析失败则全局失败即编译失败。
对于Logic (opens new window)Structure (opens new window)中的description信息,会解析对应的代码注释来生成,需要代码注释严格遵守JavaDoc规约 (opens new window)

# 声明基础信息

NASL中的基础信息 (opens new window)依赖于maven工程中的pom.xml文件,对于基础信息的收集会读取工程跟路径中的pom.xml,如果为父子工程,则对子工程的打包会读取子工程下的pom.xml文件。

# 声明外部语言依赖(externalDependencyMap)

maven工程中必须要存在groupIdartifactIdversion字段,如果是父子工程,可以只保留artifactId标签,在收集外部语言依赖时,会以当前项目为主,如果当前 项目未声明groupIdversion则会查看parent标签内的groupIdversion
pom.xml中定义如下:

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.netease.lowcode</groupId>
        <artifactId>base</artifactId>
        <version>1.0.3</version>
    </parent>

    <groupId>com.netease.lowcode</groupId>
    <artifactId>math-tool</artifactId>
</project>
1
2
3
4
5
6
7
8
9
10
11
12

# 声明扩展依赖库名称(name)

扩展依赖库的名称获取的是pom.xml文件中的artifactId字段。如果定义的artifactId包含-则会自动转换为_
pom.xml中定义如下:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.netease.lowcode</groupId>
    <artifactId>math-tool</artifactId>
    <version>1.0.3</version>
</project>
1
2
3
4
5
6
7

# 声明扩展依赖库版本(version)

扩展依赖库的版本获取的是pom.xml文件中的version字段。如果定义的version包含-SNAPSHOT则会自动进行去除。
pom.xml中定义如下:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.netease.lowcode</groupId>
    <artifactId>math-tool</artifactId>
    <version>1.0.3</version>
</project>
1
2
3
4
5
6
7

# 声明扩展依赖库标题(title)

标题通过解析pom.xml中的name标签来生成,此标签为可选项,如果未声明name标签,则默认为artifactId标签对应的值。
pom.xml中定义如下:

<project>
    <name>math工具类</name>
</project>
1
2
3

# 声明扩展依赖库描述(description)

描述是通过解析pom.xml中的description标签来生成,此标签为可选项,如果未声明description标签,则默认扩展依赖库描述为空字符串。
pom.xml中定义如下:

<project>
    <description>math工具类,主要用来验证扩展依赖库正确性</description>
</project>
1
2
3

# 声明为Logic

只有静态公共方法才可以定义为Logic (opens new window),如果方法定义为非公共或非静态则会编译失败,声明为Logic只需要在方法上添加@NaslLogic注解。
Java代码如下:

package com.netease.lowcode.math.tool.util;

public class MathUtil {
    /**
     * 两个整型相加,返回相加结果。
     *
     * @param a 第一个整型
     * @param b 第二个整型
     * @return 相加结果
     */
    @NaslLogic
    public static Integer add(Integer a, Integer b) {
        return a + b;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 声明为Structure

在数据结构上添加@NaslStructure注解即声明为Structure (opens new window),成员变量类型必须为public,并且需要添加属性的getset方法。

package com.netease.lowcode.math.tool.dto;

/**
 * 这是一个数据结构
 */
@NaslStructure
public class AddDTO implements Serializable {
    /**
     * 第一个属性
     */
    public Integer a;
    /**
     * 第二个属性
     */
    public Integer b;
    /**
     * 第三个属性
     */
    public List<Integer> c;

    public Integer getA() {
        return a;
    }

    public void setA(Integer a) {
        this.a = a;
    }

    public Integer getB() {
        return b;
    }

    public void setB(Integer b) {
        this.b = b;
    }

    public List<Integer> getC() {
        return c;
    }

    public void setC(List<Integer> c) {
        this.c = c;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 声明为Configuration

在类的成员变量上添加@NaslConfiguration注解即声明为Configuration (opens new window),可以设置参数在不同环境下的默认值,如果未指定的话则默认为空字符串。
默认会开启改写机制,当标记为@NaslConfiguration的成员变量通过Spring@value的方式进行注入时,会将对应的参数名称拼接上extisions.<libraryName>的前缀。

package com.netease.lowcode.math.tool.util;

import com.netease.lowcode.core.EnvironmentType;
import com.netease.lowcode.core.annotation.Environment;
import com.netease.lowcode.core.annotation.NaslConfiguration;

public class Config {
    @NaslConfiguration
    String appKey;

    @NaslConfiguration(defaultValue = @Environment(type = EnvironmentType.DEV,value="defaultSecretKey"))
    String secretKey;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

因为默认开始了参数的改写机制,所以在通过spring注入的方式来获取参数值的地方进行了参数key的改写。

package com.netease.lowcode.math.tool.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    @Value("${secretKey:springDefaultConfiguration}")
    private String secretKeyWithDefault;

    @Value("${secretKey}")
    String secretKey;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

编译后的class文件

package com.netease.lowcode.math.tool.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {
    @Value("${extensions.math_tool.custom.secretKey:springDefaultConfiguration}")
    private String secretKeyWithDefault;
    @Value("${extensions.math_tool.custom.secretKey}")
    String secretKey;

    public Controller() {
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 注意项

  • 对于java基础类型,需要定义为包装类。
  • 目前支持的java类型为:Boolean、Integer、Long、Double、String、List、Map。
  • 需要低代码平台版本大于等于2.14。
  • Class文件必须定义在命名的包内。
  • 版本号支持格式为x.y.z,并且各个版本号区间为0-99。

# Troubleshooting

  • 引入jar包后,通过idea进行Build Project而未通过maven进行编译报错
    报错信息:java: The class java.lang.IllegalArgumentException may be caused by the wrapped ProcessingEnvironment object.
    解决方式:Preferences->Build, Execution, Deployment->Compiler修改Shared build process VM options添加参数-Djps.track.ap.dependencies=false
上次更新: 2023年06月14日