Skip to content

Commit

Permalink
17.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FirokOtaku committed Mar 30, 2022
1 parent 6bef7d2 commit 4f2070b
Show file tree
Hide file tree
Showing 20 changed files with 752 additions and 122 deletions.
12 changes: 2 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>firok.spring</groupId>
<artifactId>mvci</artifactId>
<version>17.1.1</version>
<version>17.2.0</version>
<description>An annotation processing tool to generate MVC code from JavaBean(s).</description>
<dependencies>
<dependency>
Expand All @@ -24,23 +24,15 @@

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>

</plugins>

<resources>
Expand Down
40 changes: 39 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,57 @@
> 生成 `a.b.c.Test2Bean` 实体时将使用 `##TEST## = "test2"`,
> 生成 `a.b.DemoBean` 实体时将使用 `##TEST## = "test"`
### 运行时数据类

`17.2.0` 版本之后的 MVCI 在参与编译时会在 `firok.spring.mvci.runtime` 包下生成数个类,
操作这些类可以获取本项目中所有标注了 `@MVCIntrospective` 注解并生成了相应结构的实体信息.

所有信息都按照两种方式存放在 `XXX Names``XXX Classes` 类内.

> **为什么要以两种方式储存信息**
>
> 根据 [虚拟机规范(Java SE 17)对类初始化的描述](https://docs.oracle.com/javase/specs/jls/se17/html/jls-12.html#:~:text=12.4.1.%C2%A0-,When%20Initialization%20Occurs,-A%20class%20or),
> 某个类初次实例化之前或其静态字段初次被主动引用之前,
> 此类一定会先进行类初始化.
> GraalVM 下经测试,
> 仅是访问某个类的 Class 对象不会触发其类加载.
>
> 为了避免不同编译器或解释器实现可能出现的不同实现,
> MVCI 仍储存了一份字符串类型的信息,
> 以完全避免期望之外的类加载.
## 注意

### 关于代码生成模板

**MVCI 本身** 不基于 SpringBoot 和 MybatisPlus,
但是 **MVCI 默认的结构代码模板** 基于 SpringBoot 和 MybatisPlus.
所以默认情况下需为编译环境引入相关依赖, 否则项目无法通过编译.

此外, 还需要正确提供数据库驱动等依赖, 否则项目可能无法正常运行.

MVCI 仅于 **Java17 环境** 下通过测试.
### 关于运行时数据类

`firok.spring.mvci.runtime` 包下的各信息类的字段和方法内容会在编译期动态扩展.

* `firok.spring.mvci.runtime.CurrentBeanNames``firok.spring.mvci.runtime.CurrentBeanClasses`
包含有项目中 **所有** 标注了 `@MVCIntrospective` 注解的实体类信息
* 其它 `firok.spring.mvci.runtime.CurrentXXXNames``firok.spring.mvci.runtime.CurrentXXXClasses`
包含有项目中 **由 MVCI 生成了相应结构** 的实体信息

比如, 如果实体 `TestBean` 没有配置生成 `Controller` 结构, 在 `CurrentControllerNames.NAMES` 数组内就不会包含相应信息, 且使用 `TestBean` 的完整限定名作为参数调用 `#getByFullQualifiedBeanName` 将会返回 `null`.

### 关于 Java 版本

MVCI 17.x 仅于 **Java17 环境** 下通过测试.
更低 Java 版本中仍可能使用, 但是您需要手动调整 `firok.spring.mvci.MVCIntrospectProcessor` 上的 `@SupportedSourceVersion` 注解值和部分 MVCI 代码.

## 变动记录

### 17.2.0

* 现在 MVCI 将会额外生成数个包含生成数据的信息的类

### 17.1.1

* 细微代码改动
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/firok/spring/mvci/MVCIntrospectProcessor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package firok.spring.mvci;

import firok.spring.mvci.internal.BeanContext;
import firok.spring.mvci.internal.RegexPipeline;
import firok.spring.mvci.internal.ResourceCache;
import firok.spring.mvci.internal.RuntimeGenerate;
import lombok.SneakyThrows;

import javax.annotation.processing.*;
Expand All @@ -11,13 +14,13 @@
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;

@SuppressWarnings({"unused", "FieldCanBeLocal"})
@SupportedAnnotationTypes({
"firok.spring.mvci.MVCIntrospective",
"firok.spring.mvci.MVCConfig",
Expand All @@ -29,15 +32,15 @@ public class MVCIntrospectProcessor extends AbstractProcessor
private Elements elementUtils;
private Filer filer;
private Messager messager;
private void printNote(Object obj)
public void printNote(Object obj)
{
messager.printMessage(Diagnostic.Kind.NOTE,"[MVCI] " + obj);
}
private void printWarning(Object obj)
public void printWarning(Object obj)
{
messager.printMessage(Diagnostic.Kind.WARNING,"[MVCI] " + obj);
}
private void printError(Object obj)
public void printError(Object obj)
{
messager.printMessage(Diagnostic.Kind.ERROR,"[MVCI] " + obj);
}
Expand All @@ -60,13 +63,14 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
* 省得出问题 还是加上个锁吧
*/
private static final Object LOCK_JFO_API = new Object();
public Writer createSourceFileWrite(String location) throws Exception
public Writer createSourceFileWrite(String location) throws IOException
{
JavaFileObject jfo;
synchronized (LOCK_JFO_API) { jfo = filer.createSourceFile(location); return jfo.openWriter(); }

}

@SuppressWarnings("ReflectionForUnavailableAnnotation")
@SneakyThrows
private void checkAnnotation(Annotation anno)
{
Expand Down Expand Up @@ -118,6 +122,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
var setAnno = roundEnv.getElementsAnnotatedWith(MVCIntrospective.class);
if(setAnno.isEmpty()) return true;

// 所有生成的实体数据
var listContext = new Vector<BeanContext>();

setAnno.stream().parallel().forEach(ele -> {
if(ele instanceof TypeElement eleBean)
{
Expand Down Expand Up @@ -167,19 +174,22 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

// 开始生成
context.generate();
listContext.add(context);
}
});

// 根据实体数据生成runtime类
printNote("开始生成运行时内容...");
RuntimeGenerate.startGenAll(listContext, this);

printNote("完成生成");
}
catch (Exception e)
{
// if(e.getStackTrace() != null)
// for(var st : e.getStackTrace())
// {
// printWarning(st.toString());
// }
printError(e);
}

return true;
}

}
Loading

0 comments on commit 4f2070b

Please sign in to comment.