Skip to content

Commit

Permalink
chore(): complete README
Browse files Browse the repository at this point in the history
  • Loading branch information
Leibnizhu committed Jun 24, 2020
1 parent 8498dc0 commit aec4533
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
120 changes: 115 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,44 @@ public List<Xxx> query(Integer userId, String keyword) {
//Do something
}
```
That's all.
For array type default value, we can use `defaultXxxArr`:
```java
@MethodOverload(field = "keyword", defaultStringArr = {""})
public List<Xxx> query(Integer userId, String[] keyword) {
//Do something
}
```
Equals to:
```java
public List<Xxx> query(Integer userId, String[] keyword) {
//Do something
}

public List<Xxx> query(Integer userId) {
return query(userId, new String[]{""});
}
```
However, array support is not so good now.
For null default value, we can use `defaultNull=true`:
```java
@MethodOverload(field = "keyword", defaultNull = true)
public List<Xxx> query(Integer userId, String[] keyword) {
//Do something
}
```
Equals to:
```java
public List<Xxx> query(Integer userId, String[] keyword) {
//Do something
}

public List<Xxx> query(Integer userId) {
return query(userId, null);
}
```

## Q&A
### Why defaultInt,defaultString,defaultXxx...?
### Why defaultInt,defaultStringArr,defaultXxx...?
In Java, annotation member types must be one of:
- primitive
- String
Expand All @@ -58,9 +92,85 @@ In Java, annotation member types must be one of:
- Class
- an array of any of the above

No `Object`, it means we can not define an annotation member to accept all type of default value; So I define annotation members like `defaultInt`, `defaultString` to accept String and primitive type and their Array. However, array support is not so good now.
### How to define a field with default value of null?
use `@MethodOverload(field = "Xxx", defaultNull = true)`
No `Object`, it means we can not define an annotation member to accept all type of default value; So I define annotation members like `defaultInt`, `defaultStringArr` to accept String and primitive type and their Array.
### All supported annotation members?
- defaultBool
- defaultBoolArr
- defaultByte
- defaultByteArr
- defaultChar
- defaultCharArr
- defaultDouble
- defaultDoubleArr
- defaultFloat
- defaultFloatArr
- defaultInt
- defaultIntArr
- defaultLong
- defaultLongArr
- defaultNull
- defaultShort
- defaultShortArr
- defaultString
- defaultStringArr

### What about method signature conflict?
When we use more than one `@MethodOverload` on the same method, method signature conflict might happen. For example:
```java
@MethodOverload(field = "userName", defaultString = "1")
@MethodOverload(field = "keyword", defaultString = "2")
public List<Xxx> query(String userName, String keyword) {
//Do something
}
```
`Nusadua` try to generate new methods with single default value at first; if method signature conflict happened at this part, `Nusadua` would throw a compiling ERROR and stop compiling, as the example above:
```bash
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project nusadua: Compilation failure
[ERROR] io.github.leibnizhu.nusadua.NusaduaTest.query(String userName,String keyword) method has a MethodOverload annotation's ERROR,Annotation definition: method with same signature (String userName) already existed! Can not continue!
```
After single default value method generation finish, `Nusadua` try to generate new methods with two, three, four...(up to `@MethodOverload` quantity) default values successively; if method signature conflict happened at this part, the new method would be dropped. For example:
```java
@MethodOverload(field = "str1", defaultString = "true")
@MethodOverload(field = "i", defaultInt = -1)
@MethodOverload(field = "str2", defaultString = "false")
public void multiSignConflict(String str1, int i, String str2) {
System.out.println(String.format("String1=%s, int=%s, String2=%s", str1, i, str2));
}
```
will be compiled to:
```java
public void multiSignConflict(String str1, int i, String str2) {
System.out.println(String.format("String1=%s, int=%s, String2=%s", str1, i, str2));
}
public void multiSignConflict(int i, String str2) {
this.multiSignConflict("true", i, str2);
}
public void multiSignConflict(String str1, int i) {
this.multiSignConflict(str1, i, "false");
}
public void multiSignConflict(String str1, String str2) {
this.multiSignConflict(str1, -1, str2);
}
public void multiSignConflict(int i) {
this.multiSignConflict("true", i, "false");
}
/**
* which String parameter will be keep is unpredictable
*/
public void multiSignConflict(String str2) {
this.multiSignConflict("true", -1, str2);
}
public void multiSignConflict() {
this.multiSignConflict("true", -1, "false");
}
```
### What's the next step?
- Enhance default array handling.
- Write a plugin for IntelliJ IDEA (Otherwise IDEA will report an compile error when editing (Even so, maven compile will not error))
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,11 @@ private void printBoxedTypeArrs(String[] str, Byte[] b, Short[] s, Integer[] i,
Arrays.toString(str), Arrays.toString(b), Arrays.toString(s), Arrays.toString(i),
Arrays.toString(l), Arrays.toString(f), Arrays.toString(d), Arrays.toString(bool), Arrays.toString(c)));
}

@MethodOverload(field = "str1", defaultString = "true")
@MethodOverload(field = "i", defaultInt = -1)
@MethodOverload(field = "str2", defaultString = "false")
public void multiSignConflict(String str1, int i, String str2) {
System.out.println(String.format("String1=%s, int=%s, String2=%s", str1, i, str2));
}
}

0 comments on commit aec4533

Please sign in to comment.