Skip to content

Commit

Permalink
Completed installation life-cycle.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Mar 18, 2017
1 parent da82cb6 commit a74d333
Showing 1 changed file with 75 additions and 7 deletions.
Expand Up @@ -3573,9 +3573,11 @@ public boolean isFallback(Class<?> type, Throwable throwable) {

interface InstallationListener {

Throwable SUPPRESS_ERROR = null;

void onInstall(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer);

void onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable);
Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable);

void onReset(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer);

Expand All @@ -3589,9 +3591,29 @@ public void onInstall(Instrumentation instrumentation, ResettableClassFileTransf
}

@Override
public void onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
return throwable;
}

@Override
public void onReset(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer) {
/* do nothing */
}
}

enum ErrorSuppressing implements InstallationListener {

INSTANCE;

@Override
public void onInstall(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer) {
/* do nothing */
}

@Override
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
return SUPPRESS_ERROR;
}

@Override
public void onReset(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer) {
Expand All @@ -3607,8 +3629,8 @@ public void onInstall(Instrumentation instrumentation, ResettableClassFileTransf
}

@Override
public void onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
/* do nothing */
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
return throwable;
}

@Override
Expand All @@ -3617,6 +3639,44 @@ public void onReset(Instrumentation instrumentation, ResettableClassFileTransfor
}
}

class StreamWriting implements InstallationListener {

protected static final String PREFIX = "[Byte Buddy]";

private final PrintStream printStream;

public StreamWriting(PrintStream printStream) {
this.printStream = printStream;
}

public static InstallationListener toSystemOut() {
return new StreamWriting(System.out);
}

public static InstallationListener toSystemErr() {
return new StreamWriting(System.err);
}

@Override
public void onInstall(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer) {
printStream.printf(PREFIX + " INSTALL %s on %s%n", classFileTransformer, instrumentation);
}

@Override
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
synchronized (printStream) {
printStream.printf(PREFIX + " ERROR %s on %s%n", classFileTransformer, instrumentation);
throwable.printStackTrace(printStream);
}
return throwable;
}

@Override
public void onReset(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer) {
printStream.printf(PREFIX + " RESET %s on %s%n", classFileTransformer, instrumentation);
}
}

class Compound implements InstallationListener {

private final List<InstallationListener> installationListeners;
Expand Down Expand Up @@ -3644,10 +3704,14 @@ public void onInstall(Instrumentation instrumentation, ResettableClassFileTransf
}

@Override
public void onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
public Throwable onError(Instrumentation instrumentation, ResettableClassFileTransformer classFileTransformer, Throwable throwable) {
for (InstallationListener installationListener : installationListeners) {
installationListener.onError(instrumentation, classFileTransformer, throwable);
if (throwable == SUPPRESS_ERROR) {
break;
}
throwable = installationListener.onError(instrumentation, classFileTransformer, throwable);
}
return throwable;
}

@Override
Expand Down Expand Up @@ -7662,7 +7726,11 @@ public ResettableClassFileTransformer installOn(Instrumentation instrumentation)
ignoredTypeMatcher);
}
} catch (Throwable throwable) {
installationListener.onError(instrumentation, classFileTransformer, throwable);
throwable = installationListener.onError(instrumentation, classFileTransformer, throwable);
if (throwable != null) {
instrumentation.removeTransformer(classFileTransformer);
throw new IllegalStateException("Could not install class file transformer", throwable);
}
}
installationListener.onInstall(instrumentation, classFileTransformer);
return classFileTransformer;
Expand Down

0 comments on commit a74d333

Please sign in to comment.