-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/generate-java-code' into develop
- Loading branch information
Showing
15 changed files
with
271 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 生成代码 | ||
|
||
> 生成造数的Java Code,理论上其他JVM语言也可以用 | ||
⚠️目前依赖在GitHub package,并没有在中央仓库。[github 仓库使用参考](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry) | ||
|
||
## 使用 | ||
|
||
执行`code`命令即可。 | ||
|
||
|
||
如下图 | ||
 | ||
|
||
## 注意事项 | ||
|
||
+ **请手动修改 依赖版本为你使用的版本** | ||
|
||
+ **使用方式参考 生成代码的main方法** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# 生成代码 | ||
|
||
> 生成造数的Java Code,理论上其他JVM语言也可以用 | ||
⚠️目前依赖在GitHub package,并没有在中央仓库。[github 仓库使用参考](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry) | ||
|
||
## 使用 | ||
直接点击`生成Java代码`按钮即可。 | ||
|
||
如下图: | ||
|
||
 | ||
|
||
## 注意事项 | ||
|
||
+ **请手动修改 依赖版本为你使用的版本** | ||
|
||
+ **使用方式参考 生成代码的main方法** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
sugar_random_core/src/main/java/me/mikusugar/random/core/service/GenerateCodeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package me.mikusugar.random.core.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import me.mikusugar.random.core.bean.SugarJsonNode; | ||
import me.mikusugar.random.core.utils.GetAllService; | ||
import me.mikusugar.random.core.utils.SugarJsonNodeSerialization; | ||
import me.mikusugar.random.core.utils.SugarJsonUtils; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* 给生成的造数代码调研 | ||
* | ||
* @author mikusugar | ||
*/ | ||
public class GenerateCodeService { | ||
private SugarJsonNode sugarJsonNode; | ||
|
||
private Map<String, AbstractRandomService> randomServiceMap; | ||
|
||
public GenerateCodeService(String configJson) | ||
throws InstantiationException, IllegalAccessException, JsonProcessingException { | ||
|
||
this.randomServiceMap = GetAllService.getAllService(); | ||
this.sugarJsonNode = SugarJsonNodeSerialization.read(configJson, randomServiceMap); | ||
} | ||
|
||
/** | ||
* @return 生成一条随机json | ||
*/ | ||
public String getJson(){ | ||
final StringBuilder res = new StringBuilder(); | ||
SugarJsonUtils.toJsonStr(null,sugarJsonNode,res); | ||
return res.toString(); | ||
} | ||
|
||
|
||
} |
57 changes: 57 additions & 0 deletions
57
sugar_random_core/src/main/java/me/mikusugar/random/core/utils/GenerateCodeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package me.mikusugar.random.core.utils; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import me.mikusugar.random.core.bean.SugarJsonNode; | ||
|
||
/** | ||
* @author mikusugar | ||
*/ | ||
public class GenerateCodeUtil { | ||
|
||
public static String getCode(SugarJsonNode sugarJsonNode) throws JsonProcessingException { | ||
final String json = SugarJsonNodeSerialization.write(sugarJsonNode); | ||
|
||
return "import com.fasterxml.jackson.core.JsonProcessingException;\n" + | ||
"import me.mikusugar.random.core.service.GenerateCodeService;\n" + | ||
"\n" + | ||
"/**\n" + | ||
" * 请添加以下依赖\n" + | ||
" * <dependency>\n" + | ||
" * <groupId>me.mikusugar.random</groupId>\n" + | ||
" * <artifactId>sugar_random_core</artifactId>\n" + | ||
" * <version>xxx</version>\n" + | ||
" * </dependency>\n" + | ||
" *\n" + | ||
" * @author mikusugar\n" + | ||
" */\n" + | ||
"public class SugarRandom {\n" + | ||
"\n" + | ||
" public static void main(String[] args) throws JsonProcessingException, InstantiationException, IllegalAccessException {\n" + | ||
" System.out.println(new SugarRandom().next());\n" + | ||
" }\n" + | ||
"\n" + | ||
" private final GenerateCodeService generateCodeService;\n" + | ||
"\n" + | ||
" public SugarRandom() throws JsonProcessingException, InstantiationException, IllegalAccessException {\n" + | ||
" String json=\""+helpJson(json)+"\";\n" + | ||
" this.generateCodeService = new GenerateCodeService(json);\n" + | ||
" }\n" + | ||
"\n" + | ||
" public String next() {\n" + | ||
" return this.generateCodeService.getJson();\n" + | ||
" }\n" + | ||
"\n" + | ||
"}\n"; | ||
} | ||
|
||
private static String helpJson(String json) { | ||
final StringBuilder res = new StringBuilder(); | ||
for (char c:json.toCharArray()){ | ||
if(c=='"'){ | ||
res.append("\\"); | ||
} | ||
res.append(c); | ||
} | ||
return res.toString(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
sugar_random_core/src/main/java/me/mikusugar/random/core/utils/GetAllService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package me.mikusugar.random.core.utils; | ||
|
||
import me.mikusugar.random.core.constant.ServiceName; | ||
import me.mikusugar.random.core.service.AbstractRandomService; | ||
import me.mikusugar.random.core.service.RandomCoreService; | ||
import org.reflections.Reflections; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* 得到me.mikusugar.random.core.service 包下的所有实例 | ||
* | ||
* @author mikusugar | ||
*/ | ||
@SuppressWarnings("ALL") | ||
public class GetAllService { | ||
|
||
public final static String SERVICE_PACKAGE = "me.mikusugar.random.core.service.impl"; | ||
|
||
|
||
//TEST | ||
public static void main(String[] args) throws InstantiationException, IllegalAccessException { | ||
final Map<String, AbstractRandomService> allService = getAllService(); | ||
final AbstractRandomService abstractRandomService = allService.get(ServiceName.UUID); | ||
final RandomCoreService randomCoreService = abstractRandomService.createRandomCoreService(""); | ||
int x=10; | ||
while (x-->0) System.out.println(randomCoreService.getRandomUtilInterface().next()); | ||
} | ||
|
||
|
||
/** | ||
* 获取service实例 | ||
* 也许有更好的实现 | ||
* @return | ||
* @throws InstantiationException | ||
* @throws IllegalAccessException | ||
*/ | ||
public static Map<String, AbstractRandomService> getAllService() throws InstantiationException, IllegalAccessException { | ||
final Reflections reflections = new Reflections(SERVICE_PACKAGE); | ||
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Service.class); | ||
final Map<String, AbstractRandomService> res = new HashMap<>(); | ||
for (Class<?> clz : classes) { | ||
final String name = clz.getAnnotationsByType(Service.class)[0].value(); | ||
final AbstractRandomService randomService = (AbstractRandomService) clz.newInstance(); | ||
res.put(name, randomService); | ||
} | ||
return res; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import me.mikusugar.random.core.service.GenerateCodeService; | ||
//生成代码的示例 | ||
/** | ||
* 请添加以下依赖 | ||
* <dependency> | ||
* <groupId>me.mikusugar.random</groupId> | ||
* <artifactId>sugar_random_core</artifactId> | ||
* <version>xxx</version> | ||
* </dependency> | ||
* | ||
* @author mikusugar | ||
*/ | ||
public class SugarRandom { | ||
|
||
public static void main(String[] args) throws JsonProcessingException, InstantiationException, IllegalAccessException { | ||
System.out.println(new SugarRandom().next()); | ||
} | ||
|
||
private final GenerateCodeService generateCodeService; | ||
|
||
public SugarRandom() throws JsonProcessingException, InstantiationException, IllegalAccessException { | ||
String json = "{\"name\":\"root\",\"type\":\"OBJECT\",\"desc\":\"默认根节点\",\"randomServiceName\":\"随机对象\",\"input\":\"\",\"nexts\":[{\"name\":\"name\",\"type\":\"STRING\",\"desc\":\"输入0随机生成女性名字,输入1随机生成男性名字,不输入都生成\",\"randomServiceName\":\"随机中文姓名\",\"input\":\"\",\"nexts\":[]},{\"name\":\"uuid\",\"type\":\"STRING\",\"desc\":\"java 原生的UUID,无需输入\",\"randomServiceName\":\"UUID\",\"input\":\"\",\"nexts\":[]},{\"name\":\"citys\",\"type\":\"ARRAY\",\"desc\":\"设置数组长度,输入 2 代表数组长度为2 ,输入 2,3 代表数组长度为 2到3随机\",\"randomServiceName\":\"随机数组长度\",\"input\":\"1,5\",\"nexts\":[{\"name\":\"city\",\"type\":\"STRING\",\"desc\":\"随机生成国内城市,无需输入~ (北上广等概率大)\",\"randomServiceName\":\"默认国内城市\",\"input\":\"\",\"nexts\":[]}]},{\"name\":\"other\",\"type\":\"OBJECT\",\"desc\":\"什么都不需要输入\",\"randomServiceName\":\"随机对象\",\"input\":\"\",\"nexts\":[{\"name\":\"phone\",\"type\":\"STRING\",\"desc\":\"随机生成国内手机号,无需输入\",\"randomServiceName\":\"随机中国大陆手机号\",\"input\":\"\",\"nexts\":[]},{\"name\":\"mac\",\"type\":\"STRING\",\"desc\":\"随机生成MAC地址,无需输入\",\"randomServiceName\":\"MAC\",\"input\":\"\",\"nexts\":[]}]}]}"; | ||
this.generateCodeService = new GenerateCodeService(json); | ||
} | ||
|
||
public String next() { | ||
return this.generateCodeService.getJson(); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.