java8如何给一个类动态添加注解?

2021-03-17 20:32发布

4条回答
收获啦SVIP
2021-03-21 19:41

/**
* 根据CopyModel对未完成的Java文件(class类)添加包名、import、extends、implements、注解等

* @param oldFile
* @param classDecorateModel

* 组件名
* 文件名
* 包名
* 父类全名(包括包名)
* 方法字符串
* String[] 接口全名(包括包名)数组
* String[] import 全名(包括包名)数组

* Map<类名,Map>
* Map<类名,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<全注解名,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<全注解名,Map<注解属性,注解属性值>>
existsAnnotationMap = existsNameAnnotationMap.get(methodNamePara);

for(AnnotationModel toAddAnnotationModel : toAddAnnotationArray){

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

if(existsAnnotationMap != null){

for(Entry> existsAnnotationEntry : existsAnnotationMap.entrySet()){
// 类中已经存在该注解类
if(toAddAnnotationModel.getAnnotationFullName().equals(existsAnnotationEntry.getKey())){

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

// 要添加的该注解类的属性 Map
toAddAttrMap = toAddAnnotationModel.getAttrMap();
for(Entry toAddAttrEntry : toAddAttrMap.entrySet()){

existsAnnoAttrFlag = false;
for(Entry 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>();

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


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

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

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

existsNameAnnotationMap.put(methodNamePara, newAnnotationMap);
}
}
// TODO 由Map改为Map
// Map
} 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> existsAnnotationEntry : existsAnnotationMap.entrySet()){
// 类中已经存在该注解类
if(toAddAnnotationModel.getAnnotationFullName().equals(existsAnnotationEntry.getKey())){

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

// 要添加的该注解类的属性 Map
toAddAttrMap = toAddAnnotationModel.getAttrMap();
for(Entry toAddAttrEntry : toAddAttrMap.entrySet()){

existsAnnoAttrFlag = false;
for(Entry 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>();

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

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

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

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

existsNameAnnotationMap.put(attributeName, newAnnotationMap);
}
}
// TODO 由Map改为Map
// Map
} 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> existsAnnotationEntry : existsAnnotationMap.entrySet()){
// 类中已经存在该注解类
if(toAddAnnotationModel.getAnnotationFullName().equals(existsAnnotationEntry.getKey())){

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

// 要添加的该注解类的属性 Map
toAddAttrMap = toAddAnnotationModel.getAttrMap();
for(Entry toAddAttrEntry : toAddAttrMap.entrySet()){

existsAnnoAttrFlag = false;
for(Entry 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>();

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

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

newAnnotationMap.put(toAddSimpleAnnoName, newMap);
}

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

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

// Map<包名.类名或字段名或方法名##参数类型,参数名-参数类型,参数名-参数类型,参数名,Map<简单注解名,Map<注解属性,注解属性值>>>
Map>> allNameAnnotationMap = new HashMap>>();
Map> oldAllAnnoMap = new HashMap>();
Map> allAnnoMap = null;
Map newSubMap = new HashMap();
String annoFullName, annoSimpleName, annoAttrValueName, annoAttrValueSimpleName;
// Map<包名.类名或字段名或方法名##参数类型,参数名-参数类型,参数名-参数类型,参数名,Map<全注解名,Map<注解属性,注解属性值>>>
for(Entry>> entry : existsNameAnnotationMap.entrySet()){

oldAllAnnoMap = entry.getValue();
allAnnoMap = new HashMap>();
for(Entry> subEntry : oldAllAnnoMap.entrySet()){

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

Map subMap = subEntry.getValue();
newSubMap = new HashMap();
for(Entry 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> classAnnotationMap = null, annoMap;
int leftBarckets = 0, rightBarckets = 0, accessModifierIndex, transientIndex, staticIndex, finalIndex, nameIndex, typeIndex, volatileIndex, synchronizedIndex, abstractIndex, nativeIndex, strictIndex;
Map 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;iimportStr = 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> 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
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
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>>
for(Entry>> entry : allNameAnnotationMap.entrySet()){

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

// Map<全注解名,Map<注解属性,注解属性值>>
annoMap = entry.getValue();
for(Entry> 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
paraClass = paraClassArray[i];
if(i > 0){
paraBuffer.append("-");
}

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

// 该行是Method行
// Map<包名.类名或字段名或方法名##参数类型,参数名-参数类型,参数名-参数类型,参数名,Map<简单注解名,Map<注解属性,注解属性值>>>
//Map>>
for(Entry>> 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> 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>>
// for(Entry>> entry : existsNameAnnotationMap.entrySet()){
// 
// // 不是属性的注解
// if(!entry.getKey().equals(method.getName() + "##" + paraBuffer.toString())){
// continue;
// }
// 
// // Map<全注解名,Map<注解属性,注解属性值>>
// Map> annoMap = entry.getValue();
// for(Entry> subEntry : annoMap.entrySet()){
// 
// printWriter.println("@"+ subEntry.getKey() + "(");
//
// Map 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;
}


一周热门 更多>