/**
* 根据CopyModel对未完成的Java文件(class类)添加包名、import、extends、implements、注解等
*
* @param oldFile
* @param classDecorateModel
*
* 组件名
* 文件名
* 包名
* 父类全名(包括包名)
* 方法字符串
* String[] 接口全名(包括包名)数组
* String[] import 全名(包括包名)数组
*
* Map>
* Map>
* Map>
*
* @return
*/
public static File decorateFileByCopyModel(File oldFile, ClassDecorateModel classDecorateModel){
String oldFileName = oldFile.getName().replace(".java", "");
// 新建文件的名
String tempFileName = oldFileName + System.currentTimeMillis();
String absolutePath = oldFile.getAbsolutePath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("\\"));
File newFile = new File(absolutePath + "\\" + tempFileName);
try {
newFile.createNewFile();
// 读取文件到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 = classDecorateModel.getPackageName(), fileName;
if(packageName == null || "".equals(packageName)){
// 按行从BufferedReader中读取代码
String thisLine, packageLine;
while((thisLine = bufferedReader.readLine()) != null){
// 输出包名和导入项
if(thisLine.startsWith("package ")){
packageLine = thisLine.substring(0, thisLine.indexOf(";"));
packageName = packageLine.replace("package ", "");
break;
}
}
}
bufferedReader.close();
// 获取源文件的class对象
fileName = oldFile.getName().substring(0, oldFile.getName().indexOf("."));
Class> classFile = Class.forName(packageName + "." + fileName);
// 文件中所有注解 Map>>
Map>> existsNameAnnotationMap = ClassAnnotationParse.generateAnnotationParse((Class>)classFile);
fileInputStream = new FileInputStream(oldFile);
BufferedReader inputBufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
// 导入语句
List importStrList = new ArrayList();
String[] importArray = classDecorateModel.getImportArray();
if(importArray != null){
for(String importStr : importArray){
importStrList.add(importStr);
}
}
// 父类名
String parentClassName = classDecorateModel.getParentFullClassName();
String simpleParentName = null;
if(parentClassName != null && !"".equals(parentClassName)){
// 添加到import语句
if(!importStrList.contains("import " + parentClassName + ";")){
importStrList.add("import " + parentClassName + ";");
}
simpleParentName = parentClassName.substring(parentClassName.lastIndexOf(".") + 1, parentClassName.length());
}
// 接口名
String[] interfaceNameArray = classDecorateModel.getImplementsArray();
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(!importStrList.contains("import " + interfaceName + ";")){
importStrList.add("import " + interfaceName + ";");
}
}
}
// Map
// Map
// Map
Map toAddAnnotationModelMap = classDecorateModel.getAnnotationMap();
Map toAddAttrMap = null;
AnnotationModel[] toAddAnnotationArray = null;
Map> existsAnnotationMap = null, newAnnotationMap = null;
Map existsAnnoAttrMap = null, newMap = null;
boolean existsAnnoFlag = false, existsAnnoAttrFlag = false;
String toAddSimpleAnnoName, methodNamePara = null, attributeName, packClassName;
if(toAddAnnotationModelMap != null){
String toAddKey;
for(Entry toAddAnnotationModelEntry : toAddAnnotationModelMap.entrySet()){
// 本类所有要添加的注解
toAddAnnotationArray = toAddAnnotationModelEntry.getValue();
// 循环注解
for(AnnotationModel annotationModel : toAddAnnotationArray){
// 添加到import列表中
if(!importStrList.contains("import " + annotationModel.getAnnotationFullName() + ";")){
String fa = annotationModel.getAnnotationFullName();
fa = "import " + fa + ";";
importStrList.add(fa);
}
}
// key 属性 方法 或类本身
toAddKey = toAddAnnotationModelEntry.getKey();
// Map
if(toAddKey.contains("Method-")){
// 方法名##参数类型,参数名-参数类型,参数名
methodNamePara = toAddKey.split("-")[1];
// 获取类中该方法本来就有的注解 Map>
existsAnnotationMap = existsNameAnnotationMap.get(methodNamePara);
for(AnnotationModel toAddAnnotationModel : toAddAnnotationArray){
// 类中是否已经含有要添加的注解类
existsAnnoFlag = false;
existsAnnoAttrFlag = false;
if(existsAnnotationMap != null){
for(Entry> existsAnno