Skip to content

Commit bc7ecb4

Browse files
committed
refactor(SimpleTracer,FastTracer): introduce AbstractTracer
1 parent 1007b8e commit bc7ecb4

File tree

5 files changed

+76
-127
lines changed

5 files changed

+76
-127
lines changed

src/main/java/fr/inria/align/treediff/FastRemoteReader.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010

1111
public class FastRemoteReader {
1212

13-
/*public static void main(String arg[]) throws InterruptedException {
14-
FastRemoteReader r = new FastRemoteReader();
15-
r.logger = FastLogger.getInstance();
16-
File dd = new File("/home/nharrand/Documents/helloworld");
17-
r.dir = new File(dd,"d2");
18-
r.logger.setLogFile(new File("out.json"));
19-
r.read();
20-
r.logger.flush();
21-
}*/
22-
2313
public FastRemoteReader(FastTracking logger, File inTraceDir) {
2414
this.logger = logger;
2515
this.dir= inTraceDir;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package fr.inria.yajta.api;
2+
3+
import javassist.CannotCompileException;
4+
import javassist.ClassPool;
5+
import javassist.CtBehavior;
6+
import javassist.CtClass;
7+
import javassist.NotFoundException;
8+
9+
import java.net.URL;
10+
11+
public abstract class AbstractTracer {
12+
13+
public boolean verbose = false;
14+
ClassList cl;
15+
ClassPool pool = ClassPool.getDefault();
16+
17+
public byte[] transform( final ClassLoader loader, final String className, final Class clazz,
18+
final java.security.ProtectionDomain domain, final byte[] bytes ) {
19+
//if(verbose) System.out.println("className: " + className + " ? ");
20+
URL classURL = loader.getResource(className + ".class");
21+
String classFilePath = classURL == null ? null : classURL.getFile().replace("file:","");
22+
if(classFilePath == null || !cl.isInJars(classFilePath)) return bytes;
23+
if(verbose) System.out.println("className: " + className + " -> " + cl.isToBeProcessed(className));
24+
if( cl.isToBeProcessed(className) ) {
25+
return doClass( className, clazz, bytes );
26+
} else {
27+
return bytes;
28+
}
29+
}
30+
31+
public byte[] doClass( final String name, final Class clazz, byte[] b ) {
32+
CtClass cl = null;
33+
try {
34+
cl = pool.makeClass( new java.io.ByteArrayInputStream( b ) );
35+
if( cl.isInterface() == false ) {
36+
37+
doClass(cl,name);
38+
39+
b = cl.toBytecode();
40+
41+
if(verbose) System.err.println( "-> Instrument " + name);
42+
}
43+
} catch( Exception e ) {
44+
if(verbose) System.err.println( "Could not instrument " + name + ", exception : " + e.getMessage() );
45+
} finally {
46+
47+
if( cl != null ) {
48+
cl.detach();
49+
}
50+
}
51+
52+
return b;
53+
}
54+
55+
public void doClass(CtClass cl, String name) throws NotFoundException, CannotCompileException {
56+
CtBehavior[] methods = cl.getDeclaredBehaviors();
57+
58+
for( int i = 0; i < methods.length; i++ ) {
59+
60+
if( methods[i].isEmpty() == false ) {
61+
doMethod( methods[i] , name);
62+
}
63+
}
64+
}
65+
66+
private void doMethod( final CtBehavior method , String className) throws NotFoundException, CannotCompileException {
67+
doMethod(method,className,false,null);
68+
}
69+
70+
abstract void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException;
71+
}

src/main/java/fr/inria/yajta/api/FastTracer.java

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@
2323
import java.lang.reflect.Method;
2424
import java.net.URL;
2525

26-
public class FastTracer implements TracerI {
26+
public class FastTracer extends AbstractTracer implements TracerI {
2727

28-
public boolean verbose = false;
2928
public boolean strictIncludes = false;
30-
ClassList cl;
3129

3230
String loggerInstance;
3331
FastTracking realLoggerInstance;
3432
boolean logValue = false;
3533
boolean logBranch = false;
36-
ClassPool pool = ClassPool.getDefault();
3734

3835

3936
public FastTracer (ClassList cl) {
@@ -70,44 +67,6 @@ public boolean implementsInterface(Class cl, Class interf) {
7067
return false;
7168
}
7269

73-
public byte[] transform( final ClassLoader loader, final String className, final Class clazz,
74-
final java.security.ProtectionDomain domain, final byte[] bytes ) {
75-
//if(verbose) System.out.println("className: " + className + " ? ");
76-
URL classURL = loader.getResource(className + ".class");
77-
String classFilePath = classURL == null ? null : classURL.getFile().replace("file:","");
78-
if(classFilePath == null || !cl.isInJars(classFilePath)) return bytes;
79-
if(verbose) System.out.println("className: " + className + " -> " + cl.isToBeProcessed(className));
80-
if( cl.isToBeProcessed(className) ) {
81-
return doClass( className, clazz, bytes );
82-
} else {
83-
return bytes;
84-
}
85-
}
86-
87-
public byte[] doClass( final String name, final Class clazz, byte[] b ) {
88-
CtClass cl = null;
89-
try {
90-
cl = pool.makeClass( new java.io.ByteArrayInputStream( b ) );
91-
if( cl.isInterface() == false ) {
92-
93-
doClass(cl,name);
94-
95-
b = cl.toBytecode();
96-
97-
if(verbose) System.err.println( "-> Instrument " + name);
98-
}
99-
} catch( Exception e ) {
100-
if(verbose) System.err.println( "Could not instrument " + name + ", exception : " + e.getMessage() );
101-
} finally {
102-
103-
if( cl != null ) {
104-
cl.detach();
105-
}
106-
}
107-
108-
return b;
109-
}
110-
11170
@Override
11271
public void setTrackingClass(Class<? extends Tracking> trackingClass) throws MalformedTrackingClassException {
11372
throw new UnsupportedOperationException("FastTracer only supports TracingInstance that implements FastTracking");
@@ -154,21 +113,6 @@ public void setTrackingClass(Class<? extends FastTracking> trackingClass, FastTr
154113
this.realLoggerInstance = realLoggerInstance;
155114
}
156115

157-
public void doClass(CtClass cl, String name) throws NotFoundException, CannotCompileException {
158-
CtBehavior[] methods = cl.getDeclaredBehaviors();
159-
160-
for( int i = 0; i < methods.length; i++ ) {
161-
162-
if( methods[i].isEmpty() == false ) {
163-
doMethod( methods[i] , name);
164-
}
165-
}
166-
}
167-
168-
private void doMethod( final CtBehavior method , String className) throws NotFoundException, CannotCompileException {
169-
doMethod(method,className,false,null);
170-
}
171-
172116
private Bytecode getBytecode(String print, CtClass cc) throws CompileError {
173117
Javac jv = new Javac(cc);
174118
jv.compileStmnt(print);
@@ -209,7 +153,7 @@ private boolean isBlockEmpty(CodeIterator iterator, int begin, int end) {
209153
return true;
210154
}
211155

212-
private void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
156+
protected void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
213157
if(!Modifier.isNative(method.getModifiers())) {
214158
if(verbose) System.err.println("[Vanilla] " + className + " " + method.getName());
215159
String params = "(";

src/main/java/fr/inria/yajta/api/FastTracking.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.inria.yajta.api;
22

33
import java.io.File;
4+
import java.lang.invoke.MethodHandles;
45

56
/**
67
* This interface can be extended by the user to create new logging structure.

src/main/java/fr/inria/yajta/api/SimpleTracer.java

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@
2626
import java.net.URL;
2727
import java.util.Arrays;
2828

29-
//public class SimpleTracer implements ClassFileTransformer {
30-
public class SimpleTracer implements TracerI {
29+
public class SimpleTracer extends AbstractTracer implements TracerI {
3130

32-
public boolean verbose = true;
3331
public boolean strictIncludes = false;
34-
ClassList cl;
3532

3633
String loggerInstance;
3734
boolean logValue = false;
3835
boolean logBranch = false;
39-
ClassPool pool = ClassPool.getDefault();
4036

4137

4238
public SimpleTracer (ClassList cl) {
@@ -121,59 +117,6 @@ public void setFastTrackingClass(Class<? extends FastTracking> trackingClass) th
121117
throw new UnsupportedOperationException("SimpleTracer only supports TracingInstance that implements ValueTracking or Tracking");
122118
}
123119

124-
public byte[] transform( final ClassLoader loader, final String className, final Class clazz,
125-
final java.security.ProtectionDomain domain, final byte[] bytes ) {
126-
if(verbose) System.out.println("className: " + className + " ? ");
127-
URL classURL = loader.getResource(className + ".class");
128-
String classFilePath = classURL == null ? null : classURL.getFile().replace("file:","");
129-
if(classFilePath == null || !cl.isInJars(classFilePath)) return bytes;
130-
if(verbose) System.out.println("className: " + className + " -> " + cl.isToBeProcessed(className));
131-
if( cl.isToBeProcessed(className) ) {
132-
return doClass( className, clazz, bytes );
133-
} else {
134-
return bytes;
135-
}
136-
}
137-
138-
public byte[] doClass( final String name, final Class clazz, byte[] b ) {
139-
CtClass cl = null;
140-
try {
141-
cl = pool.makeClass( new java.io.ByteArrayInputStream( b ) );
142-
if( cl.isInterface() == false ) {
143-
144-
doClass(cl,name);
145-
146-
b = cl.toBytecode();
147-
148-
if(verbose) System.err.println( "-> Instrument " + name);
149-
}
150-
} catch( Exception e ) {
151-
if(verbose) System.err.println( "Could not instrument " + name + ", exception : " + e.getMessage() );
152-
} finally {
153-
154-
if( cl != null ) {
155-
cl.detach();
156-
}
157-
}
158-
159-
return b;
160-
}
161-
162-
public void doClass(CtClass cl, String name) throws NotFoundException, CannotCompileException {
163-
CtBehavior[] methods = cl.getDeclaredBehaviors();
164-
165-
for( int i = 0; i < methods.length; i++ ) {
166-
167-
if( methods[i].isEmpty() == false ) {
168-
doMethod( methods[i] , name);
169-
}
170-
}
171-
}
172-
173-
private void doMethod( final CtBehavior method , String className) throws NotFoundException, CannotCompileException {
174-
doMethod(method,className,false,null);
175-
}
176-
177120
private Bytecode getBytecode(String print, CtClass cc) throws CompileError {
178121
Javac jv = new Javac(cc);
179122
jv.compileStmnt(print);
@@ -214,7 +157,7 @@ private boolean isBlockEmpty(CodeIterator iterator, int begin, int end) {
214157
return true;
215158
}
216159

217-
private void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
160+
protected void doMethod( final CtBehavior method , String className, boolean isIsotope, String isotope) throws NotFoundException, CannotCompileException {
218161
//System.out.println("\t\tMethod: " + method.getLongName() + " -> " + !Modifier.isNative(method.getModifiers()));
219162
if(!Modifier.isNative(method.getModifiers())) {
220163
if(verbose) System.err.println("[Vanilla] " + className + " " + method.getName());

0 commit comments

Comments
 (0)