Advertisement
kdelemme

Java Agent with BCEL

May 5th, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package com.agent.instrumentation;
  2.  
  3. import java.lang.instrument.ClassFileTransformer;
  4. import java.lang.instrument.IllegalClassFormatException;
  5. import java.lang.instrument.Instrumentation;
  6. import java.security.ProtectionDomain;
  7.  
  8.  
  9. import org.apache.bcel.classfile.*;
  10.  
  11.  
  12.  
  13. public class SimpleInstrumentation implements ClassFileTransformer {
  14.  
  15.     /** This is where the counts will be printed to **/
  16.     @SuppressWarnings("unused")
  17.     private static java.io.PrintStream output = System.err;
  18.  
  19.  
  20.     public static void premain(String options, Instrumentation ins) {
  21.         if (options != null)
  22.             try {
  23.                 SimpleInstrumentation.output = new java.io.PrintStream(new java.io.FileOutputStream(options));
  24.             }
  25.             catch (java.io.FileNotFoundException e) {
  26.                 System.err.println(options + " - file not found");
  27.             }
  28.        
  29.         ins.addTransformer(new SimpleInstrumentation());
  30.        
  31.         Class<?> classes[] = ins.getAllLoadedClasses();
  32.         System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
  33.         for (Class<?> c : classes)
  34.             if (!c.getName().startsWith("java") && !c.getName().startsWith("sun") && !c.getName().startsWith("[Ljava"))
  35.                 System.out.println("CLASS LOADED: " + c.toString());
  36.         System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
  37.     }
  38.  
  39.     public byte[] transform(ClassLoader loader, String className,  Class<?> cBR, ProtectionDomain pD, byte[] classfileBuffer) throws IllegalClassFormatException {
  40.         if (isSystemClass(className))
  41.             return null;
  42.        
  43.         try {
  44.             ClassParser cp = new ClassParser(new java.io.ByteArrayInputStream(classfileBuffer), className + ".java");
  45.             JavaClass jc = cp.parse();
  46.             Method methods[] = jc.getMethods();
  47.             Field fields[] = jc.getFields();
  48.            
  49.             /*
  50.              * Get all methods from the className
  51.              */
  52.             System.out.println("CLASS " + className + " has MATHODS:");
  53.             for (Method m : methods)
  54.                 System.out.println("\t" + m.toString());
  55.             /*
  56.              * Get all attributes from the className
  57.              */
  58.             System.out.println("CLASS " + className + " has FIELDS:");
  59.             for (Field f : fields)
  60.                 System.out.println("\t" + f.toString() + " ===> Current Value: " + f.getConstantValue());
  61.  
  62.             System.out.println("################################\n################################");
  63.         }
  64.         catch (Throwable exc) {
  65.             System.err.println(exc);
  66.         }
  67.        
  68.         return null;
  69.     }
  70.  
  71.   /** Return true if this is a class we don't want to instrument **/
  72.     private boolean isSystemClass(String className) {
  73.         return ((className.startsWith("java/")) || (className.startsWith("javax/")) || (className.startsWith("sun/")));
  74.     }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement