Skip to content

Commit

Permalink
[Java] Refinements to alignment agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Dec 12, 2019
1 parent e2bb0ef commit ff52ae5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static synchronized void agent(final boolean shouldRedefine, final Instr
.or(nameMatches(".*String[^W].*").and(not(ElementMatchers.takesArguments(int.class, int.class))));

alignmentTransformer = new AgentBuilder.Default(new ByteBuddy().with(TypeValidation.DISABLED))
.with(LISTENER)
.with(new AgentBuilderListener())
.disableClassFormatChanges()
.with(shouldRedefine ?
AgentBuilder.RedefinitionStrategy.RETRANSFORMATION :
Expand All @@ -91,7 +91,20 @@ private static synchronized void agent(final boolean shouldRedefine, final Instr
.installOn(instrumentation);
}

private static final AgentBuilder.Listener LISTENER = new AgentBuilder.Listener()
public static synchronized void removeTransformer()
{
if (alignmentTransformer != null)
{
instrumentation.removeTransformer(alignmentTransformer);
instrumentation.removeTransformer(new AgentBuilder.Default()
.type(isSubTypeOf(DirectBuffer.class).and(not(isInterface())))
.transform(AgentBuilder.Transformer.NoOp.INSTANCE).installOn(instrumentation));
alignmentTransformer = null;
instrumentation = null;
}
}

static class AgentBuilderListener implements AgentBuilder.Listener
{
public void onDiscovery(
final String typeName,
Expand Down Expand Up @@ -125,8 +138,8 @@ public void onError(
final boolean loaded,
final Throwable throwable)
{
System.out.println("ERROR " + typeName);
throwable.printStackTrace(System.out);
System.err.println("ERROR " + typeName);
throwable.printStackTrace(System.err);
}

public void onComplete(
Expand All @@ -136,18 +149,5 @@ public void onComplete(
final boolean loaded)
{
}
};

public static synchronized void removeTransformer()
{
if (alignmentTransformer != null)
{
instrumentation.removeTransformer(alignmentTransformer);
instrumentation.removeTransformer(new AgentBuilder.Default()
.type(isSubTypeOf(DirectBuffer.class).and(not(isInterface())))
.transform(AgentBuilder.Transformer.NoOp.INSTANCE).installOn(instrumentation));
alignmentTransformer = null;
instrumentation = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Runtime Exception thrown by {@link BufferAlignmentAgent} when an unaligned memory access is detected.
* <p>
* Package-protected to discourage catching since this agent should be used only for testing and debugging
* Package-protected to discourage catching since this as agent should be used only for testing and debugging.
*/
class BufferAlignmentException extends RuntimeException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import net.bytebuddy.asm.Advice;

@SuppressWarnings("unused")
public class BufferAlignmentInterceptor
{
abstract static class Verifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,10 @@ private void assertUnaligned(final int index, final IntConsumer methodUnderTest)
catch (final BufferAlignmentException ex)
{
final Matcher matcher = EXCEPTION_MESSAGE_PATTERN.matcher(ex.getMessage());
matcher.find();
matcher.find();
assertTrue(matcher.find());
assertTrue(matcher.find());
final int indexFound = Integer.parseInt(matcher.group());
matcher.find();
assertTrue(matcher.find());
final int offsetFound = Integer.parseInt(matcher.group());

assertEquals("BufferAlignmentException reported wrong index", index, indexFound);
Expand Down

0 comments on commit ff52ae5

Please sign in to comment.