How to save method name and class name in database using javassist?

Viewed 85

InterceptingClassTransformer class

I want to save method name and class name in database at runtime.

  1. I want to save method name and class name. I don't know how to insert data into database. I want to save only invoked method name and class name. Once I run the java agent it will store all the method names and class names in the database but my requirement is to store when method gets invoked.

                           try {
                                 ClassPool classPool = scopedClassPoolFactory.create(loader, rootPool,
                                         ScopedClassPoolRepositoryImpl.getInstance());
                                 CtClass ctClass = classPool.makeClass(new ByteArrayInputStream(classfileBuffer));
                                 CtMethod[] methods = ctClass.getDeclaredMethods();
                                 InsertMethods m = new InsertMethods();
                                 int mid = className.lastIndexOf('/') + 1;
                                 String finalClsName = className.substring(mid);
                                 for (CtMethod method : methods) {
                                     final StringBuffer buffer = new StringBuffer();
                                     String methodName = method.getName();
                                     buffer.append("System.err.println(\"Invoked Class Name: " + finalClsName + " Method Name: "
                                             + methodName + " executed\" );");
                                     method.insertBefore(buffer.toString());
                                     method.addLocalVariable("startTime", CtClass.longType); 
                                     method.insertBefore("startTime = System.nanoTime();");
                                     method.insertAfter("System.out.println(\"Execution Duration "
                                             + "(nano sec): \"+ (System.nanoTime() - startTime) );");
                                     method.insertAfter("System.out.println(\"Logging using After Method execution\");");                        
                                     PreparedStatement st = m.insertData();                                      
                               st.setString(1,finalClsName);
                               st.setString(2,methodName);
                               st.executeUpdate();
                 }
                 }
                                 byteCode = ctClass.toBytecode();
                                 ctClass.detach();
                             } catch (Throwable ex) {
                                 log.log(Level.SEVERE, "Error in transforming the class: " + className, ex);
                             }
                         }
    
                     }
                     return byteCode;
    

    }

Jdbc Insert Statement

Here I am using JDBC to insert data into database.
`
        //Insert statement Class
        public class InsertMethods {
            
            public PreparedStatement insertData() throws SQLException {
                Connection con= DBconnection.createNewDBconnection();
                String q= "insert into method_info(m_key, m_value) values(?,?)";
                PreparedStatement st= con.prepareStatement(q);
                return st;
                
            }
        
        }
0 Answers
Related