简体   繁体   English

跟踪 java 中的方法调用

[英]trace method calls in java

I am trying to trace method calls in java, currently I am using javassist library and my code is:我正在尝试跟踪 java 中的方法调用,目前我正在使用 javassist 库,我的代码是:

for (CtMethod meth : methods) {
String method = meth.getName();
try {
                    meth.instrument(new ExprEditor() {
                        public void edit(MethodCall m) throws CannotCompileException { 
                            StringBuffer buffer = new StringBuffer();
                            StringBuffer bufferEnd = new StringBuffer();
                            String formatMessage = "String instMessage=new java.text.MessageFormat(\"{0},{1},{2}, has started execution,{3},{4}\").format(new String[]{\""
                                    + "returnType" + " " + meth.getLongName()
                                    + "\",\"" + cc.getSimpleName() + "\",\"" + method
                                    + "\", new java.util.Date().toString()"
                                    + ",java.lang.Thread.currentThread().getId()+\"\"});";
                            buffer.append(formatMessage);
                            buffer.append("org.springframework.samples.jpetstore.domain.logic.Log.trace( instMessage);");
                            String formatMessageEnd = "String instMessageEnd=new java.text.MessageFormat(\"{0},{1},{2}, has finished execution,{3},{4}\").format(new String[]{\""
                                    +"returnType" + " " + meth.getLongName()
                                    + "\",\"" + cc.getSimpleName() + "\",\"" + method
                                    + "\", new java.util.Date().toString()"
                                    + ",java.lang.Thread.currentThread().getId()+\"\"});";
                            bufferEnd.append(formatMessageEnd);
                            bufferEnd.append("org.springframework.samples.jpetstore.domain.logic.Log.trace(instMessageEnd);");
                            String start = buffer.toString();
                            String stop = bufferEnd.toString();

                            m.replace("{ try {" + start + " $_ = $proceed($$); } finally { " + stop + " } }");
                        }
                    });
                } catch (CannotCompileException e) {
                    System.out.println("******error:" + cc.getSimpleName() + "  -  " + method);
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
}

that's worked for me except for getter methods, for example if I have class called A with getB() method It still like the original without instrument, any idea how can I fix this.这对我有用,除了 getter 方法,例如,如果我用 getB() 方法调用 A 的 class 它仍然像没有仪器的原始方法,知道我该如何解决这个问题。 note I will appreciate if you have another method to get method calls (without using AOP) to get for example:请注意,如果您有另一种方法来获取方法调用(不使用 AOP),我将不胜感激,例如:

ClassA.method() **called** ClassB.method() at date()
ClassB.method() **return to** ClassA.method() at date()

Over the years, I have found this is one of the most challenging Java problems, to get a framework or library to do.多年来,我发现这是最具挑战性的 Java 问题之一,需要一个框架或库来完成。 We have to use AOP or reflection, please also try AspectJ我们必须使用 AOP 或反射,也请尝试 AspectJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM