Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
优化代码合并逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Apr 19, 2018
1 parent 1a27d0f commit 6a706a5
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 68 deletions.
Binary file modified data/h2db.mv.db
Binary file not shown.
6 changes: 6 additions & 0 deletions hsweb-code-generator-core/pom.xml
Expand Up @@ -28,5 +28,11 @@
<version>4.3.9.RELEASE</version>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.javaparser/javaparser-core -->
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,84 @@
package org.hswebframework.generator.core;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import org.hswebframework.utils.file.FileUtils;

import java.io.File;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author zhouhao
* @since 3.0
*/
public class ClassWriter {

public static void writeClass(String file, String code) throws Exception {
File oldClassFile = new File(file);
if (oldClassFile.exists()) {
try {
CompilationUnit old = JavaParser.parse(oldClassFile);
CompilationUnit newClazz = JavaParser.parse(code);
Map<String, FieldDeclaration> oldFields = old
.findAll(FieldDeclaration.class)
.stream()
.collect(Collectors.toMap(declaration -> declaration.getVariable(0).getNameAsString(), Function.identity()));


Map<String, MethodDeclaration> oldMethod = old
.findAll(MethodDeclaration.class)
.stream()
.collect(Collectors.toMap(NodeWithSimpleName::getNameAsString, Function.identity()));

newClazz.findAll(FieldDeclaration.class).forEach(declaration -> {
String name = declaration.getVariable(0).getNameAsString();
if (oldFields.get(name) == null) {
VariableDeclarator declarator = declaration.getVariable(0);
FieldDeclaration newField = old.getType(0)
.addField(declarator.getType(), declarator.getNameAsString(),
declaration.getModifiers().toArray(new Modifier[]{}));

declaration.getJavadocComment().ifPresent(newField::setJavadocComment);
for (Comment comment : declaration.getAllContainedComments()) {
newField.setComment(comment);
}
for (AnnotationExpr annotationExpr : declaration.getAnnotations()) {
newField.addAnnotation(annotationExpr.clone());
}
}
});
newClazz.findAll(MethodDeclaration.class).forEach(declaration -> {
String name = declaration.getNameAsString();
if (oldMethod.get(name) == null) {
MethodDeclaration newMethod = old.getType(0)
.addMethod(name, declaration.getModifiers().toArray(new Modifier[]{}));

declaration.getJavadocComment().ifPresent(newMethod::setJavadocComment);
for (Comment comment : declaration.getAllContainedComments()) {
newMethod.setComment(comment);
}
for (AnnotationExpr annotationExpr : declaration.getAnnotations()) {
newMethod.addAnnotation(annotationExpr.clone());
}
}
});
code = old.toString();
} catch (Exception e) {
System.out.println(file);
System.out.println(code);
e.printStackTrace();
}
}

FileUtils.writeString2File(code, file, "utf-8");
}
}
Expand Up @@ -29,7 +29,7 @@ private void writeCode(String path, GeneratedCode code) {
String fileName = path + "/" + code.getFile();
String replaceMod = code.getRepeat();
File codeFile = new File(fileName);
if (codeFile.exists() && replaceMod != null) {
if (codeFile.exists() && replaceMod != null && !fileName.endsWith(".java")) {
switch (replaceMod) {
case "ignore":
return;
Expand All @@ -41,7 +41,11 @@ private void writeCode(String path, GeneratedCode code) {
break;
}
}
FileUtils.writeString2File(template, fileName, "utf-8");
if (fileName.endsWith(".java")) {
ClassWriter.writeClass(fileName, template);
} else {
FileUtils.writeString2File(template, fileName, "utf-8");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
@@ -0,0 +1,103 @@
package org.hswebframework.generator.core;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* TODO 完成注释
*
* @author zhouhao
* @since
*/
public class ClassWriterTests {
public static void main(String[] args) {
CompilationUnit old = JavaParser.parse("" +
"package org.hswebframework.web.dictionary.simple;\n" +
"\n" +
"import org.hswebframework.web.dictionary.api.DictionaryService;\n" +
"import org.hswebframework.web.dictionary.api.entity.DictionaryEntity;\n" +
"import org.hswebframework.web.dictionary.simple.dao.DictionaryDao;\n" +
"import org.hswebframework.web.id.IDGenerator;\n" +
"import org.hswebframework.web.service.EnableCacheAllEvictGenericEntityService;\n" +
"import org.springframework.beans.factory.annotation.Autowired;\n" +
"import org.springframework.cache.annotation.CacheConfig;\n" +
"import org.springframework.stereotype.Service;\n " +
"" +
"@Getter" +
"@Setter" +
"\n" +
"public class Test{" +
"\n//姓名\n" +
"private String name;" +
"}");

CompilationUnit newClazz = JavaParser.parse("public class Test{" +
"\n@NotNull(value=\"1\")\n" +
"\n/**测试*/\n" +
"private String name2;" +
"\n" +
"/**测试" +
"*/" +
"\n" +
"@NotNull\n" +
"public void addName(){}" +
"}");

Map<String, FieldDeclaration> oldFields = old
.findAll(FieldDeclaration.class)
.stream()
.collect(Collectors.toMap(declaration -> declaration.getVariable(0).getNameAsString(), Function.identity()));


Map<String, MethodDeclaration> oldMethod = old
.findAll(MethodDeclaration.class)
.stream()
.collect(Collectors.toMap(NodeWithSimpleName::getNameAsString, Function.identity()));

newClazz.findAll(FieldDeclaration.class).forEach(declaration -> {
String name = declaration.getVariable(0).getNameAsString();
if (oldFields.get(name) == null) {
VariableDeclarator declarator = declaration.getVariable(0);
FieldDeclaration newField = old.getType(0)
.addField(declarator.getType(), declarator.getNameAsString(),
declaration.getModifiers().toArray(new Modifier[]{}));

declaration.getJavadocComment().ifPresent(newField::setJavadocComment);
for (Comment comment : declaration.getAllContainedComments()) {
newField.setComment(comment);
}
for (AnnotationExpr annotationExpr : declaration.getAnnotations()) {
newField.addAnnotation(annotationExpr.clone());
}
}
});
newClazz.findAll(MethodDeclaration.class).forEach(declaration -> {
String name =declaration.getNameAsString();
if (oldMethod.get(name) == null) {
MethodDeclaration newMethod = old.getType(0)
.addMethod(name, declaration.getModifiers().toArray(new Modifier[]{}));

declaration.getJavadocComment().ifPresent(newMethod::setJavadocComment);
for (Comment comment : declaration.getAllContainedComments()) {
newMethod.setComment(comment);
}
for (AnnotationExpr annotationExpr : declaration.getAnnotations()) {
newMethod.addAnnotation(annotationExpr.clone());
}
}
});


System.out.println(old.toString());
}
}
16 changes: 16 additions & 0 deletions hsweb-code-generator-ui/pom.xml
Expand Up @@ -11,5 +11,21 @@

<artifactId>hsweb-code-generator-ui</artifactId>

<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<plugin>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
65 changes: 0 additions & 65 deletions ui/modules/pages/123/123Manger.js

This file was deleted.

0 comments on commit 6a706a5

Please sign in to comment.