運行程序向一個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 = 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<全注解名,Map<注解屬性,注解屬性值>>>
Map<String, Map<String, Map<String, Object>>> existsNameAnnotationMap = ClassAnnotationParse.generateAnnotationParse((Class<?>)classFile);

fileInputStream = new FileInputStream(oldFile);
BufferedReader inputBufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));

// 導入語句
List<String> importStrList = new ArrayList<String>();
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<Method-"方法名##參數類型,參數名-參數類型,參數名(methodName##String,name-Boolean,flag)", AnnotationModel[]>
// Map<Attribute-"屬性名", AnnotationModel[]>
// Map<ClassName-"包名.類名 ", AnnotationModel[]>
Map<String, AnnotationModel[]> toAddAnnotationModelMap = classDecorateModel.getAnnotationMap();
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 = false, existsAnnoAttrFlag = false;

String toAddSimpleAnnoName, methodNamePara = null, attributeName, packClassName;
if(toAddAnnotationModelMap != null){

String toAddKey;
for(Entry<String, AnnotationModel[]> 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<Method-"方法名##參數類型,參數名-參數類型,參數名(methodName##String,name-Boolean,flag)", AnnotationModel[]>
if(toAddKey.contains("Method-")){

// 方法名##參數類型,參數名-參數類型,參數名
methodNamePara = toAddKey.split("-")[1];
// 獲取類中該方法本來就有的注解 Map<全注解名,Map<注解屬性,注解屬性值>>
existsAnnotationMap = existsNameAnnotationMap.get(methodNamePara);

for(AnnotationModel toAddAnnotationModel : toAddAnnotationArray){

// 類中是否已經含有要添加的注解類
existsAnnoFlag = false;
existsAnnoAttrFlag = false;

if(existsAnnotationMap != null){

for(Entry<String, Map<String, Object>> existsAnnotationEntry : existsAnnotationMap.entrySet()){
// 類中已經存在該注解類
if(toAddAnnotationModel.getAnnotationFullName().equals(existsAnnotationEntry.getKey())){

// 已存在注解類的所有屬性 Map<注解屬性,注解屬性值>
existsAnnoAttrMap = existsAnnotationEntry.getValue();

// 要添加的該注解類的屬性 Map<value = "loadHtml">
toAddAttrMap = toAddAnnotationModel.getAttrMap();
for(Entry<String, String> toAddAttrEntry : toAddAttrMap.entrySet()){

existsAnnoAttrFlag = false;
for(Entry<String, Object> existsAnnoAttrEntry : existsAnnoAttrMap.entrySet()){
// 已經存在該屬性
if(existsAnnoAttrEntry.getKey().equals(toAddAttrEntry.getKey())){
existsAnnoAttrEntry.setValue(toAddAttrEntry.getValue());
existsAnnoAttrFlag = true;
break;
}
}

// 不存在該屬性
if(!existsAnnoAttrFlag){
existsAnnoAttrMap.put(toAddAttrEntry.getKey(), toAddAttrEntry.getValue());
}
}

existsAnnoFlag = true;
break;
}
}
}


// 不含有要添加的注解類
if(!existsAnnoFlag){

// Map<全注解名,Map<注解屬性,注解屬性值>>
newAnnotationMap = new HashMap<String, Map<String, Object>>();

// 注解類的屬性
if(toAddAnnotationModel.getAttrMap() != null && toAddAnnotationModel.getAttrMap().size() > 0){


// Map<注解屬性,注解屬性值>
newMap = new HashMap<String, Object>();
for(Entry<String, String> toAddAttrEntry : toAddAnnotationModel.getAttrMap().entrySet()){
newMap.put(toAddAttrEntry.getKey(), toAddAttrEntry.getValue());
}

newAnnotationMap.put(toAddAnnotationModel.getAnnotationFullName(), newMap);
}

if(existsAnnotationMap != null){
// 添加以前就存在的注解
for(Entry<String, Map<String, Object>> entry : existsAnnotationMap.entrySet()){
newAnnotationMap.put(entry.getKey(), entry.getValue());
}
}

existsNameAnnotationMap.put(methodNamePara, newAnnotationMap);
}
}
// TODO 由Map<Attribute-"類型簡稱-屬性名", AnnotationModel[]>改為Map<Attribute-"屬性名", AnnotationModel[]>
// Map<Attribute-"屬性名", AnnotationModel[]>
} else if(toAddKey.contains("Attribute-") || toAddKey.contains("Collection-")){

// 方法名##參數類型,參數名-參數類型,參數名
attributeName = toAddKey.split("-")[1];
// 獲取類中該屬性本來就有的注解 Map<全注解名,Map<注解屬性,注解屬性值>>
existsAnnotationMap = existsNameAnnotationMap.get(attributeName);

for(AnnotationModel toAddAnnotationModel : toAddAnnotationArray){

// 類中是否已經含有要添加的注解類
existsAnnoFlag = false;
existsAnnoAttrFlag = false;
toAddSimpleAnnoName = toAddAnnotationModel.getAnnotationFullName().substring(toAddAnnotationModel.getAnnotationFullName().lastIndexOf(".")+1, toAddAnnotationModel.getAnnotationFullName().length());

if(existsAnnotationMap != null){
for(Entry<String, Map<String, Object>> existsAnnotationEntry : existsAnnotationMap.entrySet()){
// 類中已經存在該注解類
if(toAddAnnotationModel.getAnnotationFullName().equals(existsAnnotationEntry.getKey())){

// 已存在注解類的所有屬性 Map<注解屬性,注解屬性值>
existsAnnoAttrMap = existsAnnotationEntry.getValue();

// 要添加的該注解類的屬性 Map<value = "loadHtml">
toAddAttrMap = toAddAnnotationModel.getAttrMap();
for(Entry<String, String> toAddAttrEntry : toAddAttrMap.entrySet()){

existsAnnoAttrFlag = false;
for(Entry<String, Object> existsAnnoAttrEntry : existsAnnoAttrMap.entrySet()){
// 已經存在該屬性
if(existsAnnoAttrEntry.getKey().equals(toAddAttrEntry.getKey())){
existsAnnoAttrEntry.setValue(toAddAttrEntry.getValue());
existsAnnoAttrFlag = true;
break;
}
}

// 不存在該屬性
if(!existsAnnoAttrFlag){
existsAnnoAttrMap.put(toAddAttrEntry.getKey(), toAddAttrEntry.getValue());
}
}

existsAnnoFlag = true;
break;
}
}
}

// 不含有要添加的注解類
if(!existsAnnoFlag){

// Map<全注解名,Map<注解屬性,注解屬性值>>
newAnnotationMap = new HashMap<String, Map<String, Object>>();

// 注解類的屬性
if(toAddAnnotationModel.getAttrMap() != null && toAddAnnotationModel.getAttrMap().size() > 0){

// Map<注解屬性,注解屬性值>
newMap = new HashMap<String, Object>();
for(Entry<String, String> toAddAttrEntry : toAddAnnotationModel.getAttrMap().entrySet()){
newMap.put(toAddAttrEntry.getKey(), toAddAttrEntry.getValue());
}

newAnnotationMap.put(toAddAnnotationModel.getAnnotationFullName(), newMap);
}

if(existsAnnotationMap != null){
// 添加以前就存在的注解
for(Entry<String, Map<String, Object>> entry : existsAnnotationMap.entrySet()){
newAnnotationMap.put(entry.getKey(), entry.getValue());
}
}

existsNameAnnotationMap.put(attributeName, newAnnotationMap);
}
}
// TODO 由Map<ClassName-" class 類名 ", AnnotationModel[]>改為Map<ClassName-"包名.類名 ", AnnotationModel[]>
// Map<ClassName-"包名.類名 ", AnnotationModel[]>
} else if(toAddKey.contains("ClassName-")){

// 包名.類名
packClassName = toAddKey.split("-")[1];
// 獲取類本身本來就有的注解 Map<全注解名,Map<注解屬性,注解屬性值>>
existsAnnotationMap = existsNameAnnotationMap.get(packClassName);

for(AnnotationModel toAddAnnotationModel : toAddAnnotationArray){

// 類中是否已經含有要添加的注解類
existsAnnoFlag = false;
existsAnnoAttrFlag = false;
toAddSimpleAnnoName = toAddAnnotationModel.getAnnotationFullName().substring(toAddAnnotationModel.getAnnotationFullName().indexOf(".")+1, toAddAnnotationModel.getAnnotationFullName().length());

if(existsAnnotationMap != null){
for(Entry<String, Map<String, Object>> existsAnnotationEntry : existsAnnotationMap.entrySet()){
// 類中已經存在該注解類
if(toAddAnnotationModel.getAnnotationFullName().equals(existsAnnotationEntry.getKey())){

// 已存在注解類的所有屬性 Map<注解屬性,注解屬性值>
existsAnnoAttrMap = existsAnnotationEntry.getValue();

// 要添加的該注解類的屬性 Map<value = "loadHtml">
toAddAttrMap = toAddAnnotationModel.getAttrMap();
for(Entry<String, String> toAddAttrEntry : toAddAttrMap.entrySet()){

existsAnnoAttrFlag = false;
for(Entry<String, Object> existsAnnoAttrEntry : existsAnnoAttrMap.entrySet()){
// 已經存在該屬性
if(existsAnnoAttrEntry.getKey().equals(toAddAttrEntry.getKey())){
existsAnnoAttrEntry.setValue(toAddAttrEntry.getValue());
existsAnnoAttrFlag = true;
break;
}
}

// 不存在該屬性
if(!existsAnnoAttrFlag){
existsAnnoAttrMap.put(toAddAttrEntry.getKey(), toAddAttrEntry.getValue());
}
}

existsAnnoFlag = true;
break;
}
}
}

// 不含有要添加的注解類
if(!existsAnnoFlag){

// Map<全注解名,Map<注解屬性,注解屬性值>>
newAnnotationMap = new HashMap<String, Map<String, Object>>();

// 注解類的屬性
if(toAddAnnotationModel.getAttrMap() != null && toAddAnnotationModel.getAttrMap().size() > 0){

// Map<注解屬性,注解屬性值>
newMap = new HashMap<String, Object>();
for(Entry<String, String> toAddAttrEntry : toAddAnnotationModel.getAttrMap().entrySet()){
newMap.put(toAddAttrEntry.getKey(), toAddAttrEntry.getValue());
}

newAnnotationMap.put(toAddSimpleAnnoName, newMap);
}

if(existsAnnotationMap != null){
// 添加以前就存在的注解
for(Entry<String, Map<String, Object>> entry : existsAnnotationMap.entrySet()){
newAnnotationMap.put(entry.getKey(), entry.getValue());
}
}

existsNameAnnotationMap.put(packClassName, newAnnotationMap);
}
}
}
}
}

// Map<包名.類名或字段名或方法名##參數類型,參數名-參數類型,參數名-參數類型,參數名,Map<簡單注解名,Map<注解屬性,注解屬性值>>>
Map<String, Map<String, Map<String, Object>>> allNameAnnotationMap = new HashMap<String, Map<String, Map<String, Object>>>();
Map<String, Map<String, Object>> oldAllAnnoMap = new HashMap<String, Map<String, Object>>();
Map<String, Map<String, Object>> allAnnoMap = null;
Map<String, Object> newSubMap = new HashMap<String, Object>();
String annoFullName, annoSimpleName, annoAttrValueName, annoAttrValueSimpleName;
// Map<包名.類名或字段名或方法名##參數類型,參數名-參數類型,參數名-參數類型,參數名,Map<全注解名,Map<注解屬性,注解屬性值>>>
for(Entry<String, Map<String, Map<String, Object>>> entry : existsNameAnnotationMap.entrySet()){

oldAllAnnoMap = entry.getValue();
allAnnoMap = new HashMap<String, Map<String, Object>>();
for(Entry<String, Map<String, Object>> subEntry : oldAllAnnoMap.entrySet()){

annoFullName = subEntry.getKey();
if(!importStrList.contains("import " + annoFullName + ";")){
importStrList.add("import " + annoFullName + ";");
}

Map<String, Object> subMap = subEntry.getValue();
newSubMap = new HashMap<String, Object>();
for(Entry<String, Object> subSubEntry : subMap.entrySet()){

// 屬性值枚舉類型
if(subSubEntry.getKey().split("-").length == 3 && (subSubEntry.getKey().split("-")[2]).equals("Enum")){
annoAttrValueName = ((String)subSubEntry.getValue()).substring(0, ((String)subSubEntry.getValue()).lastIndexOf("."));

if(!importStrList.contains("import " + annoAttrValueName + ";")){
importStrList.add("import " + annoAttrValueName + ";");
}

annoAttrValueSimpleName = ((String)subSubEntry.getValue()).replace(annoAttrValueName.substring(0, annoAttrValueName.lastIndexOf(".")) + ".", "");

newSubMap.put(subSubEntry.getKey(), annoAttrValueSimpleName);
} else {
newSubMap.put(subSubEntry.getKey(), subSubEntry.getValue());
}
}

annoSimpleName = annoFullName.substring(annoFullName.lastIndexOf(".") + 1, annoFullName.length());
allAnnoMap.put(annoSimpleName, newSubMap);
}

allNameAnnotationMap.put(entry.getKey(), allAnnoMap);
}

String thisLine, tempStr, oldParentName = null, oldInterfaceNameStr = null, componentName, importStr;
String[] decorateArray, keyStrArray = null, keyParaArray;
boolean annotationFlag = false, bracketCloseFlag = true;
StringBuffer fileAnnoBuffer = new StringBuffer(), paraBuffer, keyBuffer = null;
Map<String, Map<String, Object>> classAnnotationMap = null, annoMap;
int leftBarckets = 0, rightBarckets = 0, accessModifierIndex, transientIndex, staticIndex, finalIndex, nameIndex, typeIndex, volatileIndex, synchronizedIndex, abstractIndex, nativeIndex, strictIndex;
Map<String, Object> classAnnoAttrMap = null;
Class<?>[] paraClassArray = null;
Class<?> paraClass = null;
Field[] fieldArray;
Method[] methodArray;

// 按行從BufferedReader中讀取代碼
while((thisLine = inputBufferedReader.readLine()) != null){

// 輸出包名和導入項
if(thisLine.startsWith("package ")){

// 把包名輸出到PrintWriter中
componentName = classDecorateModel.getComponentName();
if(componentName != null && !"".equals(componentName)){
printWriter.println("package " + componentName + "." + packageName + ";");
} else if(componentName == null || !"".equals(componentName)){
printWriter.println("package " + packageName + ";");
}
printWriter.println("");

// 把importStrList中所有import語句輸出到PrintWriter中
for(int i=0;i<importStrList.size();i++){
importStr = importStrList.get(i);
if(i == importStrList.size() - 1){
printWriter.print(importStr);
} else {
printWriter.println(importStr);
}
}
// 如果該行不是空行
} else if(!"".equals(thisLine.trim())){

// 注解里面
if(!bracketCloseFlag){

fileAnnoBuffer.append(thisLine.trim());
leftBarckets = leftBarckets + CommonUtility.stringNumbers(thisLine, "(");
rightBarckets = rightBarckets + CommonUtility.stringNumbers(thisLine, ")");
if(rightBarckets == leftBarckets){

bracketCloseFlag = true;

rightBarckets = 0;
leftBarckets = 0;
}
// 該行是注解
} else if(thisLine.trim().startsWith("@")){

// 第一行注解
if(!annotationFlag){
annotationFlag = true;
}

leftBarckets = CommonUtility.stringNumbers(thisLine, "(");
rightBarckets = CommonUtility.stringNumbers(thisLine, ")");
if(leftBarckets != rightBarckets){
bracketCloseFlag = false;
}
} else {

// 如果該行是類名行,添加該行上的所有注解
if(thisLine.contains(" class " + oldFileName)){

printWriter.println("");
// 獲取本身的注解 Map<簡單注解名,Map<注解屬性,注解屬性值>>
classAnnotationMap = allNameAnnotationMap.get(packageName + "." + oldFileName);

// Map<注解屬性,注解屬性值> 輸出
if(classAnnotationMap != null && classAnnotationMap.size() > 0){
for(Entry<String, Map<String, Object>> classAnnotationEntry : classAnnotationMap.entrySet()){
printWriter.println("@"+ classAnnotationEntry.getKey() + "(");

classAnnoAttrMap = classAnnotationEntry.getValue();

// 輸出注解屬性和屬性值
printAnnotationAttrValue(classAnnoAttrMap, printWriter);

printWriter.println(")");
}
}

// 把類名行解析成字符串數組
decorateArray = thisLine.split(" ");
if(simpleParentName != null && !"".equals(simpleParentName)){

// 該類有父類
if(thisLine.contains(" extends ")){

for(int i=0;i<decorateArray.length;i++){

tempStr = decorateArray[i];
if("extends".equals(tempStr)){
oldParentName = decorateArray[i+1];
break;
}
}

thisLine = thisLine.replace(oldParentName, simpleParentName);
// 該類沒有父類
} else {
thisLine = thisLine.replace(oldFileName, oldFileName + " extends " + simpleParentName);
}
}

// 接口
if(interfaceBuffer != null){
// 該類有接口
if(thisLine.contains(" implements ")){
for(int i=0;i<decorateArray.length;i++){

tempStr = decorateArray[i];
if("implements".equals(tempStr)){
oldInterfaceNameStr = decorateArray[i+1];
break;
}
}

thisLine = thisLine.replace(oldInterfaceNameStr, oldInterfaceNameStr + ", " + interfaceBuffer.toString());
// 該類沒有接口
} else {

thisLine = thisLine.replace("{", oldFileName + " implements " + interfaceBuffer.toString() + " {");
}
}

// 把該行代碼輸出到PrintWriter中
printWriter.println(thisLine);

// 添加方法
if(classDecorateModel.getMethodStr() != null && !"".equals(classDecorateModel.getMethodStr())){
printWriter.println("");
printWriter.println(classDecorateModel.getMethodStr());
printWriter.println("");
}

// 該行是import行
} else if(thisLine.startsWith("import ")){

// 如果importStrList中不包含該import行
if(!importStrList.contains(thisLine)){
printWriter.println(thisLine);
}
// 該行是空行
} else if(thisLine.trim().equals("")){
printWriter.println("");
// 該行不是類名行、導入行、package行、空行、注解行
} else {

// 獲取並輸出源文件的所有字段
fieldArray = classFile.getDeclaredFields();
for(Field field : fieldArray){

field.setAccessible(true);

nameIndex = thisLine.indexOf(field.getName());
typeIndex = thisLine.indexOf(field.getType().getSimpleName());
if(nameIndex != -1 && typeIndex != -1 && typeIndex < nameIndex){

// 訪問權限
if(field.getModifiers() == 2){// private
accessModifierIndex = thisLine.indexOf("private ");
if(accessModifierIndex == -1 || accessModifierIndex > typeIndex){
continue;
}
} else if(field.getModifiers() == 4){// protected
accessModifierIndex = thisLine.indexOf("protected ");
if(accessModifierIndex == -1 || accessModifierIndex > typeIndex){
continue;
}
} else if(field.getModifiers() == 1){// public
accessModifierIndex = thisLine.indexOf("public ");
if(accessModifierIndex == -1 || accessModifierIndex > typeIndex){
continue;
}
}

// 修飾符
if(field.getModifiers() == 128){// transient
transientIndex = thisLine.indexOf("transient ");
if(transientIndex == -1 || transientIndex > typeIndex){
continue;
}
}
if(field.getModifiers() == 8){// static
staticIndex = thisLine.indexOf("static ");
if(staticIndex == -1 || staticIndex > typeIndex){
continue;
}
}
if(field.getModifiers() == 16){// final
finalIndex = thisLine.indexOf("final ");
if(finalIndex == -1 || finalIndex > typeIndex){
continue;
}
} else if(field.getModifiers() == 64){// volatile
volatileIndex = thisLine.indexOf("volatile ");
if(volatileIndex == -1 || volatileIndex > typeIndex){
continue;
}
}

// 該行是field行
// Map<包名.類名或字段名或方法名##參數類型,參數名-參數類型,參數名-參數類型,參數名,Map<簡單注解名,Map<注解屬性,注解屬性值>>>
//Map<String, Map<String, Map<String, Object>>>
for(Entry<String, Map<String, Map<String, Object>>> entry : allNameAnnotationMap.entrySet()){

// 不是屬性的注解
if(!entry.getKey().equals(field.getName())){
continue;
}

// Map<全注解名,Map<注解屬性,注解屬性值>>
annoMap = entry.getValue();
for(Entry<String, Map<String, Object>> subEntry : annoMap.entrySet()){

printWriter.println("@"+ subEntry.getKey() + "(");

classAnnoAttrMap = subEntry.getValue();

// 輸出注解屬性和屬性值
printAnnotationAttrValue(classAnnoAttrMap, printWriter);

printWriter.println(")");
}
}
}
}

// 獲取並輸出源文件的所有方法
methodArray = classFile.getDeclaredMethods();
for(Method method : methodArray){

method.setAccessible(true);

nameIndex = thisLine.indexOf(method.getName());
typeIndex = thisLine.indexOf(method.getReturnType().getSimpleName());
if(nameIndex != -1 && typeIndex != -1 && typeIndex < nameIndex){

// 訪問權限
if(method.getModifiers() == 2){// private
accessModifierIndex = thisLine.indexOf("private ");
if(accessModifierIndex == -1 || accessModifierIndex > typeIndex){
continue;
}
} else if(method.getModifiers() == 4){// protected
accessModifierIndex = thisLine.indexOf("protected ");
if(accessModifierIndex == -1 || accessModifierIndex > typeIndex){
continue;
}
} else if(method.getModifiers() == 1){// public
accessModifierIndex = thisLine.indexOf("public ");
if(accessModifierIndex == -1 || accessModifierIndex > typeIndex){
continue;
}
}

// 修飾符
if(method.getModifiers() == 1024){// abstract
abstractIndex = thisLine.indexOf("abstract ");
if(abstractIndex == -1 || abstractIndex > typeIndex){
continue;
}
}
if(method.getModifiers() == 8){// static
staticIndex = thisLine.indexOf("static ");
if(staticIndex == -1 || staticIndex > typeIndex){
continue;
}
}
if(method.getModifiers() == 16){// final
finalIndex = thisLine.indexOf("final ");
if(finalIndex == -1 || finalIndex > typeIndex){
continue;
}
}
if(method.getModifiers() == 32){// synchronized
synchronizedIndex = thisLine.indexOf("synchronized ");
if(synchronizedIndex == -1 || synchronizedIndex > typeIndex){
continue;
}
}
if(method.getModifiers() == 256){// native
nativeIndex = thisLine.indexOf("native ");
if(nativeIndex == -1 || nativeIndex > typeIndex){
continue;
}
} else if(method.getModifiers() == 2048){// strictfp
strictIndex = thisLine.indexOf("strictfp ");
if(strictIndex == -1 || strictIndex > typeIndex){
continue;
}
}

// 拼接方法參數 參數類型-參數類型-參數類型
paraClassArray = method.getParameterTypes();
paraBuffer = new StringBuffer();
for(int i=0;i<paraClassArray.length;i++){

paraClass = paraClassArray[i];
if(i > 0){
paraBuffer.append("-");
}

paraBuffer.append(paraClass.getSimpleName());
}

// 該行是Method行
// Map<包名.類名或字段名或方法名##參數類型,參數名-參數類型,參數名-參數類型,參數名,Map<簡單注解名,Map<注解屬性,注解屬性值>>>
//Map<String, Map<String, Map<String, Object>>>
for(Entry<String, Map<String, Map<String, Object>>> entry : allNameAnnotationMap.entrySet()){

// 不是屬性的注解
// 方法名##參數類型,參數名
// 參數類型,參數名
// 參數類型,參數名
keyStrArray = entry.getKey().split("-");
// 方法名##參數類型-參數類型-參數類型
keyBuffer = new StringBuffer();
for(String keyStr : keyStrArray){
keyParaArray = keyStr.split("\\,");

if(!keyBuffer.toString().equals("")){
keyBuffer.append("-");
}
keyBuffer.append(keyParaArray[0]);
}
if(!keyBuffer.toString().equals(method.getName() + "##" + paraBuffer.toString())){
continue;
}

// Map<全注解名,Map<注解屬性,注解屬性值>>
annoMap = entry.getValue();
for(Entry<String, Map<String, Object>> subEntry : annoMap.entrySet()){

printWriter.println("@"+ subEntry.getKey() + "(");

classAnnoAttrMap = subEntry.getValue();

// 輸出注解屬性和屬性值
printAnnotationAttrValue(classAnnoAttrMap, printWriter);

printWriter.println(")");
}
}
}
}

// 類所有的構造函數
// Constructor<?>[] constructorArray = classFile.getDeclaredConstructors();
// for(Constructor<?> constructor : constructorArray){
//
// constructor.setAccessible(true);
//
// nameIndex = thisLine.indexOf(constructor.getName());
// if(nameIndex != -1 && typeIndex != -1 && typeIndex < nameIndex){
//
// // 訪問權限
// if(constructor.getModifiers() == 2){// private
// accessModifierIndex = thisLine.indexOf("private ");
// if(accessModifierIndex == -1 || accessModifierIndex > nameIndex){
// continue;
// }
// } else if(constructor.getModifiers() == 4){// protected
// accessModifierIndex = thisLine.indexOf("protected ");
// if(accessModifierIndex == -1 || accessModifierIndex > nameIndex){
// continue;
// }
// } else if(constructor.getModifiers() == 1){// public
// accessModifierIndex = thisLine.indexOf("public ");
// if(accessModifierIndex == -1 || accessModifierIndex > nameIndex){
// continue;
// }
// }
//
// // 該行是Method行
// // Map<包名.類名或字段名或方法名##參數類型,參數名-參數類型,參數名-參數類型,參數名,Map<全注解名,Map<注解屬性,注解屬性值>>>
// //Map<String, Map<String, Map<String, Object>>>
// for(Entry<String, Map<String, Map<String, Object>>> entry : existsNameAnnotationMap.entrySet()){
//
// // 不是屬性的注解
// if(!entry.getKey().equals(method.getName() + "##" + paraBuffer.toString())){
// continue;
// }
//
// // Map<全注解名,Map<注解屬性,注解屬性值>>
// Map<String, Map<String, Object>> annoMap = entry.getValue();
// for(Entry<String, Map<String, Object>> subEntry : annoMap.entrySet()){
//
// printWriter.println("@"+ subEntry.getKey() + "(");
//
// Map<String, Object> classAnnoAttrMap = subEntry.getValue();
//
// // 輸出注解屬性和屬性值
// printAnnotationAttrValue(classAnnoAttrMap, printWriter);
//
// printWriter.println(")");
// }
// }
// }
//
// printWriter.println(thisLine);
// }

printWriter.println(thisLine);
}
}
}
}

// 把PrintWriter刷新
printWriter.flush();
// 關閉PrintWriter
printWriter.close();
// 關閉BufferedReader
inputBufferedReader.close();

// 把文件刪除
oldFile.delete();
// 把新文件名改為源文件名
newFile.renameTo(oldFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}

return newFile;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM