javassist와 annotation을 이용하여class의 특정method의classbytecode를 수정합니다
6748 단어 F#
package xuliangjun;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.annotation.Annotation;
public class ExceptionLog {
private static String rootPath = ClassLoader.getSystemResource(".").getPath();
private static File rootFile = new File(rootPath.replace("%20", " "));// bin
private static File tmp = new File(rootPath.replace("%20", " ")
.substring(0, rootPath.indexOf("bin")).concat("tmp"));//
private static ArrayList<String> fileList = new ArrayList<String>();// bin
/**
* bin
*
* @param rootFile
* @param fileList
* @throws IOException
*/
private static ArrayList<String> listFiles(File rootFile,
ArrayList<String> fileList) throws IOException {
File[] allFiles = rootFile.listFiles();
for (File file : allFiles) {
if (file.isDirectory()) {
listFiles(file, fileList);
} else {
String path = file.getCanonicalPath();
String clazz = path.substring(path.indexOf("bin") + 4);
fileList.add(clazz.replace("\\", ".").substring(0,clazz.lastIndexOf(".")));
}
}
return fileList;
}
private static void InsertingExceptionHandleMethod() throws IOException {
ExceptionLog.copy(rootFile, tmp);
fileList = ExceptionLog.listFiles(rootFile, fileList);
ClassPool cp = ClassPool.getDefault();
CtClass cc = null;
CtField ctf = null;
CtClass[] cte = null;
for (int i = 0; i < fileList.size(); i++) {
try {
cc = cp.get(fileList.get(i));
// System.out.println(cc);
} catch (NotFoundException e) {
System.out.println(" , ");
e.printStackTrace();
}
try {
// insert
ctf = cc.getField("ExceptionCheckingCount");
if (ctf != null)
continue;
} catch (NotFoundException e1) {
CtMethod[] ctms = cc.getMethods();
// System.out.println(ctms);
for (CtMethod m : ctms) {
// System.out.println(m);
MethodInfo minfo = m.getMethodInfo();
// System.out.println(minfo);
AnnotationsAttribute attr = (AnnotationsAttribute) minfo
.getAttribute(AnnotationsAttribute.visibleTag);
// System.out.println(attr);
if (attr != null) {
Annotation an = attr
.getAnnotation("xuliangjun.ExceptionChecking");
if (an != null) {
// System.out.println(an);
try {
m.insertBefore("System.out.println(\"Enter [ "
+ m.getLongName() + " ] \");");
m.insertAfter("System.out.println(\"Leave [ "
+ m.getLongName() + " ] \");");
ctf = CtField.make(
"private int ExceptionCheckingCount;",
cc);
cc.addField(ctf);
try {
cte = m.getExceptionTypes();
if (cte != null) {
for (CtClass ctel : cte) {
m.addCatch(" ", ctel);
}
}
m.setExceptionTypes(cte);
} catch (NotFoundException e) {
}
} catch (CannotCompileException e) {
System.out.println(" , ");
e.printStackTrace();
}
// bin
try {
cc.writeFile("bin");
cc.defrost();
} catch (CannotCompileException e) {
System.out.println(" , ");
e.printStackTrace();
}
}
}
}
}
}
}
private static void copy(File resFile, File objFolderFile)
throws IOException {
if (!resFile.exists())
return;
if (!objFolderFile.exists())
objFolderFile.mkdirs();
if (resFile.isFile()) {
File objFile = new File(objFolderFile.getPath() + File.separator
+ resFile.getName());
//
InputStream ins = new FileInputStream(resFile);
FileOutputStream outs = new FileOutputStream(objFile);
byte[] buffer = new byte[1024 * 512];
int length;
while ((length = ins.read(buffer)) != -1) {
outs.write(buffer, 0, length);
}
ins.close();
outs.flush();
outs.close();
} else {
String objFolder = objFolderFile.getPath() + File.separator
+ resFile.getName();
File _objFolderFile = new File(objFolder);
_objFolderFile.mkdirs();
for (File sf : resFile.listFiles()) {
copy(sf, new File(objFolder));
}
}
}
private static void delete(File file) {
if (!file.exists())
return;
if (file.isFile()) {
file.delete();
} else {
for (File f : file.listFiles()) {
delete(f);
}
file.delete();
}
}
private static void removingExceptionHandleMethod() {
try {
String path = tmp.getPath().concat("bin");
tmp = new File(path);
path = rootFile.getParent();
rootFile = new File(path);
copy(tmp, rootFile);
tmp = new File(rootPath.replace("%20", " ")
.substring(0, rootPath.indexOf("bin")).concat("tmp"));
delete(tmp);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void init() {
try {
ExceptionLog.InsertingExceptionHandleMethod();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void close() {
removingExceptionHandleMethod();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
F#에서 Android 앱 개발 Helloworld 편F#에서 android 앱 개발의 기본이되는 Helloworld를 출력하는 방법을 소개합니다. 먼저 Visual Studio 2019를 시작합니다. [새 프로젝트 만들기(N)]→[빈 앱(Android)]을 선택합니다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.