运⾏程序向⼀个Java类中动态添加注解。
/**
* 根据CopyModel对未完成的Java⽂件(class类)添加包名、import、extends、implements、注解等
*
* @param oldFile
* @param classDecorateModel
*
* 组件名
* ⽂件名
* 包名
* ⽗类全名(包括包名)
* ⽅法字符串
* String[] 接⼝全名(包括包名)数组
* String[] import 全名(包括包名)数组
*
* Map<;类名,Map<Method-"⽅法名##参数类型,参数名-参数类型,参数名(methodName##String,name-Boolean,flag)", AnnotationModel[]>>
* Map<;类名,Map<Attribute-"属性名", AnnotationModel[]>>
* Map<;类名,Map<ClassName-"包名.类名 ", AnnotationModel[]>>
*
* @return
*/
public static File decorateFileByCopyModel(File oldFile, ClassDecorateModel classDecorateModel){
String oldFileName = Name().replace(".java", "");
// 新建⽂件的名
String tempFileName = oldFileName + System.currentTimeMillis();
String absolutePath = AbsolutePath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("\\"));
File newFile = new File(absolutePath + "\\" + tempFileName);
try {
// 读取⽂件到FileInputStream中
FileInputStream fileInputStream = new FileInputStream(oldFile);
// 读取FileInputStream到InputStreamReader中,然后再读取到BufferedReader中
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
// 新建的⽂件绑定到FileOutputStream上
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
// 把FileOutputStream绑定到PrintWriter上
PrintWriter printWriter = new PrintWriter(fileOutputStream);
String packageName = PackageName(), fileName;
if(packageName == null || "".equals(packageName)){
// 按⾏从BufferedReader中读取代码
String thisLine, packageLine;
while((thisLine = adLine()) != null){
// 输出包名和导⼊项
if(thisLine.startsWith("package ")){
packageLine = thisLine.substring(0, thisLine.indexOf(";"));
packageName = place("package ", "");
break;
}
}
}
bufferedReader.clo();
// 获取源⽂件的class对象
fileName = Name().substring(0, Name().indexOf("."));
Class<?> classFile = Class.forName(packageName + "." + fileName);
/
/ ⽂件中所有注解 Map<;包名.类名或字段名或⽅法名##参数类型,参数名-参数类型,参数名-参数类型,参数名,Map<;全注解名,Map<;注解属性,注解属性值>>>
Map<String, Map<String, Map<String, Object>>> existsNameAnnotationMap =
fileInputStream = new FileInputStream(oldFile);
BufferedReader inputBufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
// 导⼊语句
List<String> importStrList = new ArrayList<String>();
String[] importArray = ImportArray();
if(importArray != null){
for(String importStr : importArray){
importStrList.add(importStr);
}
}
// ⽗类名
String parentClassName = ParentFullClassName();
String simpleParentName = null;
if(parentClassName != null && !"".equals(parentClassName)){
// 添加到import语句
if(!ains("import " + parentClassName + ";")){
importStrList.add("import " + parentClassName + ";");
}
simpleParentName = parentClassName.substring(parentClassName.lastIndexOf(".") + 1, parentClassName.length());
}
// 接⼝名
String[] interfaceNameArray = ImplementsArray();
StringBuffer interfaceBuffer = null;
if(interfaceNameArray != null){
interfaceBuffer = new StringBuffer();
String simpleInterfaceName;
for(String interfaceName : interfaceNameArray){
simpleInterfaceName = interfaceName.substring(interfaceName.lastIndexOf(".") + 1, interfaceName.length());
if(!interfaceBuffer.equals("")){
interfaceBuffer.append(",");
}
interfaceBuffer.append(simpleInterfaceName);
// 添加到import语句
if(!ains("import " + interfaceName + ";")){
importStrList.add("import " + interfaceName + ";");
}
}
}
// Map<Method-"⽅法名##参数类型,参数名-参数类型,参数名(methodName##String,name-Boolean,flag)", AnnotationModel[]> // Map<Attribute-"属性名", AnnotationModel[]>
// Map<ClassName-"包名.类名 ", AnnotationModel[]>
Map<String, AnnotationModel[]> toAddAnnotationModelMap = AnnotationMap();
Map<String, String> toAddAttrMap = null;
AnnotationModel[] toAddAnnotationArray = null;
Map<String, Map<String, Object>> existsAnnotationMap = null, newAnnotationMap = null;
Map<String, Object> existsAnnoAttrMap = null, newMap = null;
boolean existsAnnoFlag = fal, existsAnnoAttrFlag = fal;
String toAddSimpleAnnoName, methodNamePara = null, attributeName, packClassName;
if(toAddAnnotationModelMap != null){
String toAddKey;
for(Entry<String, AnnotationModel[]> toAddAnnotationModelEntry : Set()){
// 本类所有要添加的注解
toAddAnnotationArray = Value();
// 循环注解
for(AnnotationModel annotationModel : toAddAnnotationArray){
// 添加到import列表中
if(!ains("import " + AnnotationFullName() + ";")){
String fa = AnnotationFullName();
fa = "import " + fa + ";";
importStrList.add(fa);
}
}
// key 属性 ⽅法 或类本⾝
toAddKey = Key();
// Map<Method-"⽅法名##参数类型,参数名-参数类型,参数名(methodName##String,name-Boolean,flag)", AnnotationModel[]> ains("Method-")){
// ⽅法名##参数类型,参数名-参数类型,参数名
methodNamePara = toAddKey.split("-")[1];
// 获取类中该⽅法本来就有的注解 Map<;全注解名,Map<;注解属性,注解属性值>>
existsAnnotationMap = (methodNamePara);
for(AnnotationModel toAddAnnotationModel : toAddAnnotationArray){
// 类中是否已经含有要添加的注解类
existsAnnoFlag = fal;
existsAnnoAttrFlag = fal;
if(existsAnnotationMap != null){
for(Entry<String, Map<String, Object>> existsAnnotationEntry : Set()){
// 类中已经存在该注解类
AnnotationFullName().Key())){
// 已存在注解类的所有属性 Map<;注解属性,注解属性值>
existsAnnoAttrMap = Value();
// 要添加的该注解类的属性 Map<value = "loadHtml">
toAddAttrMap = AttrMap();
for(Entry<String, String> toAddAttrEntry : Set()){
existsAnnoAttrFlag = fal;
for(Entry<String, Object> existsAnnoAttrEntry : Set()){
// 已经存在该属性
Key().Key())){
existsAnnoAttrEntry.Value());
existsAnnoAttrFlag = true;
break;
}
}
// 不存在该属性
if(!existsAnnoAttrFlag){
existsAnnoAttrMap.Key(), Value());
}
}
existsAnnoFlag = true;
break;
}
}
}
// 不含有要添加的注解类
if(!existsAnnoFlag){
// Map<;全注解名,Map<;注解属性,注解属性值>>
newAnnotationMap = new HashMap<String, Map<String, Object>>();
/
/ 注解类的属性
AttrMap() != null && AttrMap().size() > 0){
// Map<;注解属性,注解属性值>
newMap = new HashMap<String, Object>();
for(Entry<String, String> toAddAttrEntry : AttrMap().entrySet()){ newMap.Key(), Value());
}
newAnnotationMap.AnnotationFullName(), newMap);
}