基于语法树⽣成代码ast;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
JavaModelException;
dom.AST;
dom.ASTParser;
dom.AnnotationTypeDeclaration;
dom.ArrayInitializer;
dom.CompilationUnit;
dom.Expression;
dom.FieldDeclaration;
dom.ImportDeclaration;
dom.MemberValuePair;
dom.MethodDeclaration;
dom.Modifier;
dom.Name;
dom.NormalAnnotation;
dom.PackageDeclaration;
dom.SimpleType;
dom.Type;
dom.TypeDeclaration;
dom.TypeLiteral;
dom.VariableDeclarationFragment;
write.ASTRewrite;
write.ListRewrite;
BadLocationException;
Document;
IDocument;
dits.MalformedTreeException;
dits.TextEdit;
stcode.analyze.object.ClassInfo;
public class ASTWriter {
protected static AST ast = null;
protected static CompilationUnit compilUnit = null;
protected static TypeDeclaration classType = null;
protected static List<?> types = null;
protected static ClassInfo classInfo = null;
protected static String testedClassName = "";
protected static String className = "";
protected static String instanceName = "";
protected static ASTRewrite rewriter = null;
protected static String packageName = "";
protected static String testFile = "";
protected static char[] contents = {};
protected static List<String> importDeclarationNames = new LinkedList<String>();
public static String springFilePath = "";
public static String excelFilePath = "";
public ASTWriter(char[] content, ClassInfo classInfo) {
if (content.length == 0 || content == null) {
if (content.length == 0 || content == null) {
contents = "class Tmp{\n}".toCharArray();
}
else {
contents = content;
}
ASTParser parsert = wParser(AST.JLS3);
parsert.setSource(contents);
compilUnit = (CompilationUnit) ateAST(null);
types = pes();
ast = AST();
classType = (TypeDeclaration) (0);
rewriter = ate(ast);
ASTWriter.classInfo = classInfo;
}
public ASTWriter() {
}
// This method only be used by create new test file ;
@SuppressWarnings("unchecked")
protected void addClassInfo(String className, String supperName) {
Type type = wSimpleName(supperName));
classType.setInterface(false);
classType.setSuperclassType(type);
classType.wSimpleName(className));
}
//添加package 语句,例如st.package
protected void addPackageInfo(String PackageName) {
PackageDeclaration pkgDecl = wPackageDeclaration();
pkgDecl.wName(PackageName));
compilUnit.setPackage(pkgDecl);
}
//添加import 语句,例如import java.io.*;
@SuppressWarnings("unchecked")
protected void addImportDeclaration(List<String> importNames) {
for(String importstr:importNames){
ImportDeclaration importDeclaration = wImportDeclaration();
dsWith("*")){
String simpleName = "";
simpleName = importstr.substring(0, importstr.indexOf("*")-1);
importDeclaration.wName(simpleName));
importDeclaration.setOnDemand(true);
}
else if(importstr.startsWith("static")){
String simpeName = importstr.substring(importstr.indexOf("static")+7).trim(); importDeclaration.wName(simpeName));
importDeclaration.setOnDemand(false);
importDeclaration.setStatic(true);
}
else{
String simpeName = im();
importDeclaration.wName(simpeName));
importDeclaration.setOnDemand(false);
}
compilUnit.imports().add(importDeclaration);
compilUnit.imports().add(importDeclaration);
}
}
//添加⽅法定义语句例如 public void test1(){}
@SuppressWarnings("unchecked")
protected void addDeclareMethod(MethodDeclaration methodDeclaration) {
classType.bodyDeclarations().add(methodDeclaration);
}
//批量添加⽅法定义语句
protected void addBatchMDeclareMethod(Collection<MethodDeclaration> methods) {
for (Iterator<MethodDeclaration> iter = methods.iterator(); iter.hasNext();) {
this.());
}
}
//添加注解语句
@SuppressWarnings("unchecked")
protected void addAnnOnClass(String annName,Set<String> initlist){
NormalAnnotation normalAnnotation = wNormalAnnotation();
Name name = wName(annName);
normalAnnotation.setTypeName(name);
ArrayInitializer arrayInit = wArrayInitializer();;
MemberValuePair memberValuePair = wMemberValuePair();
memberValuePair.wSimpleName("value"));
System.out.difiers().get(0).toString());
difiers().get(0).toString().contains(annName)){
NormalAnnotation normalAnnotationtmp = (difiers().get(0); MemberValuePair mp = (MemberValuePair) normalAnnotationtmp.values().get(0);
for(Object obj : ((ArrayInitializer) mp.getValue()).expressions()){
TypeLiteral tl = (TypeLiteral)obj;
SimpleType st = (SimpleType) tl.getType();
String valueName = st.getName().toString();
initlist.add(valueName);
}
for (String value : initlist) {
if (!String().contains(value)) {
TypeLiteral tL = wTypeLiteral();
import语句tL.wName(value)));
}
}
memberValuePair.setValue(arrayInit);
normalAnnotation.values().add(memberValuePair);
}
else{
for(String value : initlist){
TypeLiteral tL = wTypeLiteral();
tL.wName(value)));
}
memberValuePair.setValue(arrayInit);
normalAnnotation.values().add(memberValuePair);
}
}
//添加变量定义语句
@SuppressWarnings("unchecked")
protected void addFieldDeclaration(String className, String instanceName) {
/
/ className instanceName ;
VariableDeclarationFragment fragment = wVariableDeclarationFragment();
Expression express = wNullLiteral();
Expression express = wNullLiteral();
fragment.setInitializer(express);
fragment.wSimpleName(instanceName));
FieldDeclaration classVariDeclaration = wFieldDeclaration(fragment);
Type type = wSimpleName(className));
classVariDeclaration.setType(type);
classType.bodyDeclarations().add(classVariDeclaration);
// Object retObjc = null
VariableDeclarationFragment fragment1 = wVariableDeclarationFragment();
fragment1.wSimpleName("retObj"));
FieldDeclaration classVariDeclaration1 = wFieldDeclaration(fragment1);
Type type1 = wSimpleName("Object"));
classVariDeclaration1.setType(type1);
classType.bodyDeclarations().add(classVariDeclaration1);
// public static DBManage dbManage = new DBManage();
VariableDeclarationFragment fragment2 = wVariableDeclarationFragment();
Expression express2 = wNullLiteral();
fragment2.setInitializer(express2);
fragment2.wSimpleName("dbManage"));
FieldDeclaration classVariDeclaration2 = wFieldDeclaration(fragment2);
Type type2 = wSimpleName("DBManage"));
classVariDeclaration2.setType(type2);
classType.bodyDeclarations().add(classVariDeclaration2);
}
//修改⽅法定义语句
@SuppressWarnings("unchecked")
protected void modifyDeclareMethod(MethodDeclaration oldmethod,MethodDeclaration newMethod) { int index = classType.bodyDeclarations().indexOf(oldmethod);
classType.bodyDeclarations().add(index + 1, newMethod);
classType.bodyDeclarations().remove(index);
}
public static String getInstanceName() {
return instanceName;
}
public static void setInstanceName(String instanceName) {
ASTWriter.instanceName = instanceName;
}
protected void setTestedClassName(String testedclassName) {
}
protected String getTestedClassName() {
stedClassName;
}
protected void setClassName(String className) {
classType.wSimpleName(className));
ASTWriter.className = className;
}
protected String getClassName() {
return ASTWriter.className;
}
protected String getPackageName() {
Package().getName().toString();
}
protected int getClassModifiers() {
Modifiers();
}
protected MethodDeclaration[] getDeclaredMethod() {
Methods();
}
public CompilationUnit getCompilationUnit() {
return ASTWriterpilUnit;
}
public static void setSpringFilepath(String springFilePath) {
ASTWriter.springFilePath = springFilePath;
}
/**
* @return the spring_File_path
*/
public static final String getSpringFilepath() {
return springFilePath;
}
/*
* NormalAnnotation normalAnnotation = wNormalAnnotation(); Name name * = wName(annotationName); normalAnnotation.setTypeName(name);
* ArrayInitializer arrayInit = wArrayInitializer(); for(String value
* : values){ TypeLiteral tL = wTypeLiteral();
* tL.wName(value)));
* pressions().add(tL); }
*
* MemberValuePair memberValuePair = wMemberValuePair();
* memberValuePair.wSimpleName("value"));
* memberValuePair.setValue(arrayInit);
*
* normalAnnotation.values().add(memberValuePair);
*/
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论