Skip to content

Commit

Permalink
Support configuration exception message & Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
moyada committed Dec 30, 2018
1 parent d690f97 commit 42b589d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 88 deletions.
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
[![Build Status](https://travis-ci.org/moyada/medivh.svg?branch=master)](https://travis-ci.org/moyada/medivh)
![version](https://img.shields.io/badge/java-%3E%3D6-red.svg)
![java lifecycle](https://img.shields.io/badge/java%20lifecycle-compile-yellow.svg)
[![Maven Central](https://img.shields.io/badge/maven%20central-0.1.2-brightgreen.svg)](https://search.maven.org/search?q=g:%22io.github.moyada%22%20AND%20a:%22medivh%22)
[![Maven Central](https://img.shields.io/badge/maven%20central-1.0.0-brightgreen.svg)](https://search.maven.org/search?q=g:%22io.github.moyada%22%20AND%20a:%22medivh%22)
[![license](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/moyada/medivh/blob/master/LICENSE)

A Java annotation processor, generate verification of method input by configuration rule.
[简体中文](README_CN.md) | English

[中文说明](README_CN.md)
A Java annotation processor, generate verify logic for method input by configuration rule.

## Features

* By modify the syntax tree at `compile time`, to adding verify logic of method input parameter.
* By modify the syntax tree at `compile time`, to adding verify logic for method input parameter.

* Supported types are primitives (such as int and Integer), String, Array, Collection, Map.
* Support null check for Object type.

* Support range check for byte, short, int, long, float and double.

* Support length check for String and array.

* Support capacity check for Collection and Map.

## Requirement

Expand All @@ -24,61 +30,59 @@ JDK 1.6 or higher.

### Adding dependencies to your project

#### Maven
Using Maven

```
<dependencies>
<dependency>
<groupId>io.github.moyada</groupId>
<artifactId>medivh</artifactId>
<version>0.1.2</version>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependencies/>
```

#### Gradle
Using Gradle

```
dependencies {
compileOnly 'io.github.moyada:medivh:0.1.2'
compileOnly 'io.github.moyada:medivh:1.0.0'
// or previous version
// provided 'io.github.moyada:medivh:0.1.2'
// provided 'io.github.moyada:medivh:1.0.0'
}
```

#### Without build tool

You can download last jar from
[![release](https://img.shields.io/badge/release-v0.1.2-blue.svg)](https://github.com/moyada/medivh/releases/latest)
Without build tool, you can download last jar from
[![release](https://img.shields.io/badge/release-v1.0.0-blue.svg)](https://github.com/moyada/medivh/releases/latest)
or
[![Maven Central](https://img.shields.io/maven-central/v/io.github.moyada/medivh.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.moyada%22%20AND%20a:%22medivh%22)
.

### Configure annotation in you program

An example of the use of annotations is [here](#Example).

#### Annotation description
Annotation usage

| Annotation Class | Action Scope | Effect |
| :---- | :----- | :---- |
| io.moyada.medivh.annotation.Rule | field | provide validate rule for enclose class. |
| io.moyada.medivh.annotation.Verify | non-abstract method | enable verification function for this method. |
| io.moyada.medivh.annotation.Check | method parameter | configure check logic for method parameterexcept the primitive type. |
| io.moyada.medivh.annotation.Check | method parameter | configure check logic for method parameter, except the primitive type. |

#### Annotation attribute
Attribute description

| Attribute | Effect |
| :--- | :--- |
| Rule.nullable() | indicates whether or not this field allowed to be nullexcept primitive type. |
| Rule.nullable() | indicates whether or not this field allowed to be null, except primitive type. |
| Rule.min() | set the minimum allowed value of number type. |
| Rule.max() | set the maximum allowed value of number type. |
| Rule.maxLength() | set the maximum allowed length or capacity of String, Array, Collection, Map. |
| Rule.maxLength() | set the maximum allowed length or capacity of String, Array, Collection and Map. |
| Check.invalid() | set the exception thrown when the argument is invalid, the exception type must have a String constructor. |
| Check.message() | message head of thrown exception. |
| Check.nullable() | indicates whether or not this parameter allowed to be null. |
| Verify.value() | configure the name of temporary variable, generated by the verify logic. |
| Verify.var() | configure the name of temporary variable, generated by the verify logic. |

* A use example is [here](#example).

### Compile project

Expand All @@ -90,8 +94,9 @@ Or use java compile command, like `javac -cp medivh.jar MyApp.java`.

| Property | Effect |
| :--- | :--- |
| -Dmedivh.method | configure the name of validate method,default is invalid0 . |
| -Dmedivh.var | configure the name of default temporary variable,default is mvar_0 . |
| -Dmedivh.method | configure the name of validate method, default is invalid0 . |
| -Dmedivh.var | configure the name of default temporary variable, default is mvar_0 . |
| -Dmedivh.message | configure the default message head of exception, default is invalid argument . |

After compilation phase, the verification logic will be generated.

Expand Down Expand Up @@ -134,7 +139,7 @@ public class MyApp {
}
```

As the example codethe compiled content will be:
As the example code, the compiled content will be:


```
Expand Down
47 changes: 27 additions & 20 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
[![Build Status](https://travis-ci.org/moyada/medivh.svg?branch=master)](https://travis-ci.org/moyada/medivh)
![version](https://img.shields.io/badge/java-%3E%3D6-red.svg)
![java lifecycle](https://img.shields.io/badge/java%20lifecycle-compile-yellow.svg)
[![Maven Central](https://img.shields.io/badge/maven%20central-0.1.2-brightgreen.svg)](https://search.maven.org/search?q=g:%22io.github.moyada%22%20AND%20a:%22medivh%22)
[![Maven Central](https://img.shields.io/badge/maven%20central-1.0.0-brightgreen.svg)](https://search.maven.org/search?q=g:%22io.github.moyada%22%20AND%20a:%22medivh%22)
[![license](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/moyada/medivh/blob/master/LICENSE)

Java 语言的注解处理器,根据配置规则生成方法的入参校验。
简体中文 | [English](README.md)

Java 的注解处理器,根据配置规则生成方法的入参校验逻辑。

## 特性

* 通过在 `编译期` 对语法树进行修改,增加方法入参的校验逻辑。

* 支持校验的属性有 基础类型 (如 int 和 Integer)、String、数组、集合、Map。
* 支持 对象类型的非空校验。

* 支持 byte、short、int、long、float、double 的范围校验。

* 支持 String、数组的长度校验。

* 支持 集合、Map 的容量校验。

## 要求

Expand All @@ -22,50 +30,46 @@ JDK 1.6 及以上版本。

### 添加依赖

#### Maven
使用 Maven

```
<dependencies>
<dependency>
<groupId>io.github.moyada</groupId>
<artifactId>medivh</artifactId>
<version>0.1.2</version>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependencies/>
```

#### Gradle
使用 Gradle

```
dependencies {
compileOnly 'io.github.moyada:medivh:0.1.2'
compileOnly 'io.github.moyada:medivh:1.0.0'
// 或历史版本方式
// provided 'io.github.moyada:medivh:0.1.2'
// provided 'io.github.moyada:medivh:1.0.0'
}
```

#### 普通工程

可以通过
[![release](https://img.shields.io/badge/release-v0.1.2-blue.svg)](https://github.com/moyada/medivh/releases/latest)
普通工程可以通过
[![release](https://img.shields.io/badge/release-v1.0.0-blue.svg)](https://github.com/moyada/medivh/releases/latest)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.moyada/medivh.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.moyada%22%20AND%20a:%22medivh%22)
下载最新 jar 包。

### 在程序中配置注解

注解的使用示例见[这里](#示例)

#### 注解描述
注解描述

| 注解类 | 作用域 | 作用 |
| :---- | :----- | :---- |
| io.moyada.medivh.annotation.Rule | 类字段 | 为类提供相关字段校验规则。 |
| io.moyada.medivh.annotation.Verify | 非静态方法 | 开启方法的校验功能。 |
| io.moyada.medivh.annotation.Check | 方法参数 | 配置参数的校验逻辑,基础类型无效。 |

#### 注解属性
属性说明

| 属性 | 作用 |
| :--- | :--- |
Expand All @@ -76,20 +80,23 @@ dependencies {
| Check.invalid() | 设置参数校验失败时抛出异常,异常类需要拥有字符串构造方法。 |
| Check.message() | 异常信息头。 |
| Check.nullable() | 设置方法参数是否允许为空。 |
| Verify.value() | 配置方法生成逻辑时产生的临时变量名称。 |
| Verify.var() | 配置方法生成逻辑时产生的临时变量名称。 |

使用示例见[这里](#示例)

### 编译项目

使用构建工具的编译命令, 如 `mvn compile``gradle build`

或者使用 Java 命令进行编译,如 `javac -cp medivh.jar MyApp.java`

#### 可选系统参数
#### 系统可选参数

| 参数 | 作用 |
| :--- | :--- |
| -Dmedivh.method | 配置校验方法名,默认为 invalid0 。 |
| -Dmedivh.var | 配置默认临时变量名称,默认为 mvar_0 。 |
| -Dmedivh.message | 配置默认异常信息头,默认为 invalid argument 。 |

经过编译期后,即可生成校验逻辑。

Expand All @@ -105,8 +112,8 @@ public class MyApp {
@Verify
public void run(@Check(invalid = RuntimeException.class) Args args,
@Check(message = "something error", nullable = true) Info info,
@Check String name, // can be check null value for normal Object
@Check int num // ineffective type
@Check String name, // 可以对普通对象进行非空检查
@Check int num // 不支持基本类型
) {
// process
...
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.moyada</groupId>
<artifactId>medivh</artifactId>
<version>0.1.2</version>
<version>1.0.0-SNAPSHOT</version>

<name>Medivh</name>
<description>A Java annotation processor for automatic generate method verify at compile time.</description>
Expand Down Expand Up @@ -40,7 +40,7 @@
<maven.source.version>3.0.1</maven.source.version>
<maven.javadoc.version>3.0.1</maven.javadoc.version>
<maven.gpg.version>1.6</maven.gpg.version>
<gpg.skip>false</gpg.skip>
<gpg.skip>true</gpg.skip>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/moyada/medivh/annotation/Verify.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
/**
* 临时对象名称
*/
String value() default Element.DEFAULT_VARIABLE_NAME;
String var() default Element.DEFAULT_VARIABLE_NAME;
}
Loading

0 comments on commit 42b589d

Please sign in to comment.