Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

Commit

Permalink
Stash #89.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongl committed Dec 21, 2012
1 parent f4508a6 commit 6dba53a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/main/scala/com/github/zhongl/housemd/instrument/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public abstract class Advice {
private static final Advice nullAdvice;
private static final ClassLoader loader;

private static final ThreadLocal<Boolean> cycleAdviceFlag = new ThreadLocal<Boolean>() {
@Override
protected Boolean initialValue() {
return false;
}
};

static {
try {
ON_METHOD_BEGIN = Advice.class.getMethod("onMethodBegin", String.class, String.class, String.class, Object.class, Object[].class);
Expand All @@ -78,6 +85,9 @@ public static void setDefaultDelegate() {
}

public static void onMethodBegin(String className, String methodName, String descriptor, Object thisObject, Object[] arguments) {
if (cycleAdviceFlag.get()) return;
cycleAdviceFlag.set(true);

Map<String, Object> context = new HashMap<String, Object>();
context.put(CLASS, className);
context.put(METHOD, methodName);
Expand All @@ -91,6 +101,8 @@ public static void onMethodBegin(String className, String methodName, String des
context.put(THREAD, Thread.currentThread());
invoke("enterWith", context);
stackPush(context);

cycleAdviceFlag.remove();
}

private static void invoke(String name, Map<String, Object> context) {
Expand All @@ -103,10 +115,15 @@ private static void invoke(String name, Map<String, Object> context) {
}

public static void onMethodEnd(Object resultOrException) {
if (cycleAdviceFlag.get()) return;
cycleAdviceFlag.set(true);

Map<String, Object> context = stackPop();
context.put(STOPPED, System.currentTimeMillis());
context.put(RESULT, resultOrException);
invoke("exitWith", context);

cycleAdviceFlag.remove();
}

private static void stackPush(Map<String, Object> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ package com.github.zhongl.housemd.instrument
import java.lang.instrument.{ClassFileTransformer, Instrumentation}
import java.security.ProtectionDomain
import java.util.concurrent.atomic.AtomicInteger
import actors.Actor._

import com.github.zhongl.housemd.misc.ReflectionUtils._
import com.github.zhongl.yascli.Loggable
import java.lang.System.{currentTimeMillis => now}
import actors.TIMEOUT
import java.util

/**
Expand All @@ -42,18 +40,18 @@ class Transform extends ((Instrumentation, Filter, Seconds, Int, Loggable, Hook)

@inline
def skipClass(description: String)(cause: Class[_] => Boolean) =
if (cause(c)) {log.warn("Skip %1$s %2$s" format(c, description)); false} else true
if (cause(c)) {log.warn("Skip %1$s %2$s" format(c, description)); false } else true

@inline
def isNotBelongsHouseMD = skipClass("belongs to HouseMD") {_.getName.startsWith("com.github.zhongl.housemd")}
def isNotBelongsHouseMD = skipClass("belongs to HouseMD") { _.getName.startsWith("com.github.zhongl.housemd") }

@inline
def isNotInterface = skipClass("") {_.isInterface}
def isNotInterface = skipClass("") { _.isInterface }

@inline
def isNotFromBootClassLoader = skipClass("loaded from bootclassloader") {isFromBootClassLoader}
def isNotFromBootClassLoader = skipClass("loaded from bootclassloader") { isFromBootClassLoader(_) }

filter(c) && isNotBelongsHouseMD && isNotInterface && isNotFromBootClassLoader
filter(c) && isNotBelongsHouseMD && isNotInterface //&& isNotFromBootClassLoader
}

if (candidates.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TransformCommandSpec extends FunSpec with ShouldMatchers with AdviceReflec
concrete.parse(arguments.split("\\s+"))

val host = self
actor {concrete.run(); host ! "exit"}
actor { concrete.run(); host ! "exit" }

var cond = true
while (cond) {
Expand Down Expand Up @@ -102,7 +102,7 @@ class TransformCommandSpec extends FunSpec with ShouldMatchers with AdviceReflec
}
}

it("should not probe class loaded by boot classloader") {
ignore("should not probe class loaded by boot classloader") {
parseAndRun("String") { out =>
out.split("\n").head should be("WARN : Skip " + classOf[String] + " loaded from bootclassloader")
}
Expand Down

0 comments on commit 6dba53a

Please sign in to comment.