2828
2929package gr .gousiosg .javacg .dyn ;
3030
31+ import java .io .ByteArrayInputStream ;
32+ import java .io .IOException ;
3133import java .lang .instrument .ClassFileTransformer ;
3234import java .lang .instrument .Instrumentation ;
3335import java .util .ArrayList ;
@@ -60,7 +62,7 @@ public static void premain(String argument, Instrumentation instrumentation) {
6062
6163 String [] tokens = argument .split (";" );
6264
63- if (tokens .length <= 1 ) {
65+ if (tokens .length < 1 ) {
6466 err ("Missing delimeter ;" );
6567 return ;
6668 }
@@ -83,13 +85,13 @@ public static void premain(String argument, Instrumentation instrumentation) {
8385
8486 for (String pattern : patterns ) {
8587 Pattern p = null ;
86- err ("Compiling " + argtype + " pattern:" + pattern );
88+ err ("Compiling " + argtype + " pattern:" + pattern + "$" );
8789 try {
88- p = Pattern .compile (pattern );
90+ p = Pattern .compile (pattern + "$" );
8991 } catch (PatternSyntaxException pse ) {
9092 err ("pattern: " + pattern + " not valid, ignoring" );
9193 }
92- if (argtype .equals ("inc " ))
94+ if (argtype .equals ("incl " ))
9395 pkgIncl .add (p );
9496 else
9597 pkgExcl .add (p );
@@ -104,7 +106,6 @@ public byte[] transform(ClassLoader loader, String className, Class clazz,
104106 boolean enhanceClass = false ;
105107
106108 String name = className .replace ("/" , "." );
107- err ("Examining class: " + name );
108109
109110 for (Pattern p : pkgIncl ) {
110111 Matcher m = p .matcher (name );
@@ -117,6 +118,7 @@ public byte[] transform(ClassLoader loader, String className, Class clazz,
117118 for (Pattern p : pkgExcl ) {
118119 Matcher m = p .matcher (name );
119120 if (m .matches ()) {
121+ err ("Not enhansing class: " + name );
120122 enhanceClass = false ;
121123 break ;
122124 }
@@ -125,7 +127,6 @@ public byte[] transform(ClassLoader loader, String className, Class clazz,
125127 if (enhanceClass ) {
126128 return enhanceClass (className , bytes );
127129 } else {
128- err ("Examining class: " + name );
129130 return bytes ;
130131 }
131132 }
@@ -134,8 +135,9 @@ private byte[] enhanceClass(String name, byte[] b) {
134135 ClassPool pool = ClassPool .getDefault ();
135136 CtClass clazz = null ;
136137 try {
137- clazz = pool .makeClass (new java . io . ByteArrayInputStream (b ));
138+ clazz = pool .makeClass (new ByteArrayInputStream (b ));
138139 if (!clazz .isInterface ()) {
140+ err ("Enhancing class: " + name );
139141 CtBehavior [] methods = clazz .getDeclaredBehaviors ();
140142 for (int i = 0 ; i < methods .length ; i ++) {
141143 if (!methods [i ].isEmpty ()) {
@@ -144,10 +146,14 @@ private byte[] enhanceClass(String name, byte[] b) {
144146 }
145147 b = clazz .toBytecode ();
146148 }
147- } catch (Exception e ) {
149+ } catch (CannotCompileException e ) {
148150 e .printStackTrace ();
149- throw new RuntimeException ("Could not instrument " + name
150- + ", exception : " + e .getMessage ());
151+ err ("Cannot compile: " + e .getMessage ());
152+ } catch (NotFoundException e ) {
153+ e .printStackTrace ();
154+ err ("Cannot find: " + e .getMessage ());
155+ } catch (IOException e ) {
156+ err ("Error writing: " + e .getMessage ());
151157 } finally {
152158 if (clazz != null ) {
153159 clazz .detach ();
@@ -158,9 +164,9 @@ private byte[] enhanceClass(String name, byte[] b) {
158164
159165 private void enhanceMethod (CtBehavior method , String className )
160166 throws NotFoundException , CannotCompileException {
161- method .insertBefore ("gr.gousiosg.javacg.dyn.push(\" " + className
162- + "\" , \" " + method .getName () + "\" );" );
163- method .insertAfter ("gr.gousiosg.javacg.dyn.pop();" );
167+ method .insertBefore ("gr.gousiosg.javacg.dyn.Graph. push(\" " + className
168+ + ": " + method .getName () + "\" );" );
169+ method .insertAfter ("gr.gousiosg.javacg.dyn.Graph. pop();" );
164170 }
165171
166172 private static void err (String msg ) {
0 commit comments