Skip to content

Commit

Permalink
🔥 refactor QuasarInstruments and MethodDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
pron committed Aug 6, 2016
1 parent 432091d commit 064e1e8
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
/*
/*
* Copyright (c) 2008-2013, Matthias Mann
* All rights reserved.
*
Expand Down Expand Up @@ -41,12 +41,19 @@
*/
package co.paralleluniverse.fibers.instrument;

import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

/**
* <p>
Expand Down Expand Up @@ -80,6 +87,7 @@ public class InstrumentationTask extends Task {
private boolean allowBlocking;
private boolean debug;
private boolean writeClasses = true;
private final ArrayList<WorkListEntry> workList = new ArrayList<>();

public void addFileSet(FileSet fs) {
filesets.add(fs);
Expand Down Expand Up @@ -116,7 +124,7 @@ public void execute() throws BuildException {
for (FileSet fs : filesets)
urls.add(fs.getDir().toURI().toURL());
final ClassLoader cl = new URLClassLoader(urls.toArray(new URL[0]), getClass().getClassLoader());
final QuasarInstrumentor instrumentor = new QuasarInstrumentor(true, cl, new DefaultSuspendableClassifier(cl));
final QuasarInstrumentor instrumentor = new QuasarInstrumentor(true);

instrumentor.setCheck(check);
instrumentor.setVerbose(verbose);
Expand Down Expand Up @@ -156,17 +164,18 @@ public void error(String msg, Throwable ex) {
for (String filename : includedFiles) {
if (filename.endsWith(".class")) {
File file = new File(fs.getDir(), filename);
if (file.isFile())
instrumentor.checkClass(file);
else
if (file.isFile()) {
final String className = instrumentor.checkClass(cl, file);
workList.add(new WorkListEntry(className, file));
} else
log("File not found: " + filename);
}
}
}

instrumentor.log(LogLevel.INFO, "Instrumenting " + instrumentor.getWorkList().size() + " classes");
instrumentor.log(LogLevel.INFO, "Instrumenting " + workList.size() + " classes");

for (MethodDatabase.WorkListEntry f : instrumentor.getWorkList())
for (WorkListEntry f : workList)
instrumentClass(cl, instrumentor, f);

} catch (Exception ex) {
Expand All @@ -175,7 +184,7 @@ public void error(String msg, Throwable ex) {
}
}

private void instrumentClass(ClassLoader cl, QuasarInstrumentor instrumentor, MethodDatabase.WorkListEntry entry) {
private void instrumentClass(ClassLoader cl, QuasarInstrumentor instrumentor, WorkListEntry entry) {
if (!instrumentor.shouldInstrument(entry.name))
return;
try {
Expand All @@ -192,4 +201,14 @@ private void instrumentClass(ClassLoader cl, QuasarInstrumentor instrumentor, Me
throw new BuildException("Instrumenting file " + entry.file, ex);
}
}

public static class WorkListEntry {
public final String name;
public final File file;

public WorkListEntry(String name, File file) {
this.name = name;
this.file = file;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
/*
/*
* Copyright (c) 2008-2013, Matthias Mann
*
* All rights reserved.
Expand Down Expand Up @@ -40,7 +40,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
/*
* Copyright (c) 2012, Enhanced Four
* All rights reserved.
*
Expand Down Expand Up @@ -103,7 +103,7 @@ public static void premain(String agentArguments, Instrumentation instrumentatio
System.err.println("Retransforming classes is not supported!");

final ClassLoader cl = Thread.currentThread().getContextClassLoader();
final QuasarInstrumentor instrumentor = new QuasarInstrumentor(false, cl, new DefaultSuspendableClassifier(cl));
final QuasarInstrumentor instrumentor = new QuasarInstrumentor(false);
ACTIVE = true;
SuspendableHelper.javaAgent = true;

Expand Down Expand Up @@ -150,7 +150,7 @@ public void error(String msg, Throwable exc) {
});

Retransform.instrumentation = instrumentation;
Retransform.db = instrumentor.getMethodDatabase(cl);
Retransform.instrumentor = instrumentor;
Retransform.classLoaders = classLoaders;

instrumentation.addTransformer(new Transformer(instrumentor), true);
Expand Down Expand Up @@ -184,9 +184,9 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
classLoaders.add(new WeakReference<>(loader));

try {
if (loader == null) {
if (loader == null)
loader = Thread.currentThread().getContextClassLoader();
}

final byte[] transformed = instrumentor.instrumentClass(loader, className, classfileBuffer);

if (transformed != null)
Expand Down
Loading

0 comments on commit 064e1e8

Please sign in to comment.