Skip to content

Commit

Permalink
Minor API improvement for the ASM visitor wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Mar 14, 2016
1 parent dbb8762 commit de37472
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
17 changes: 14 additions & 3 deletions byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java
Expand Up @@ -8,6 +8,7 @@
import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator; import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.implementation.bytecode.StackSize; import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.matcher.ElementMatcher;
import org.objectweb.asm.*; import org.objectweb.asm.*;


import java.io.IOException; import java.io.IOException;
Expand Down Expand Up @@ -101,7 +102,7 @@ protected Advice(Dispatcher.Resolved.ForMethodEnter methodEnter, Dispatcher.Reso
* @param type The type declaring the advice. * @param type The type declaring the advice.
* @return A method visitor wrapper representing the supplied advice. * @return A method visitor wrapper representing the supplied advice.
*/ */
public static AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper to(Class<?> type) { public static Advice to(Class<?> type) {
return to(type, ClassFileLocator.ForClassLoader.of(type.getClassLoader())); return to(type, ClassFileLocator.ForClassLoader.of(type.getClassLoader()));
} }


Expand All @@ -112,7 +113,7 @@ public static AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper to(Class
* @param classFileLocator The class file locator for locating the advisory class's class file. * @param classFileLocator The class file locator for locating the advisory class's class file.
* @return A method visitor wrapper representing the supplied advice. * @return A method visitor wrapper representing the supplied advice.
*/ */
public static AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper to(Class<?> type, ClassFileLocator classFileLocator) { public static Advice to(Class<?> type, ClassFileLocator classFileLocator) {
return to(new TypeDescription.ForLoadedType(type), classFileLocator); return to(new TypeDescription.ForLoadedType(type), classFileLocator);
} }


Expand All @@ -123,7 +124,7 @@ public static AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper to(Class
* @param classFileLocator The class file locator for locating the advisory class's class file. * @param classFileLocator The class file locator for locating the advisory class's class file.
* @return A method visitor wrapper representing the supplied advice. * @return A method visitor wrapper representing the supplied advice.
*/ */
public static AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper to(TypeDescription typeDescription, ClassFileLocator classFileLocator) { public static Advice to(TypeDescription typeDescription, ClassFileLocator classFileLocator) {
try { try {
Dispatcher methodEnter = Dispatcher.Inactive.INSTANCE, methodExit = Dispatcher.Inactive.INSTANCE; Dispatcher methodEnter = Dispatcher.Inactive.INSTANCE, methodExit = Dispatcher.Inactive.INSTANCE;
for (MethodDescription.InDefinedShape methodDescription : typeDescription.getDeclaredMethods()) { for (MethodDescription.InDefinedShape methodDescription : typeDescription.getDeclaredMethods()) {
Expand Down Expand Up @@ -161,6 +162,16 @@ private static Dispatcher resolve(Class<? extends Annotation> annotation, Dispat
} }
} }


/**
* Returns an ASM visitor wrapper that matches the given matcher and applies this advice to the matched methods.
*
* @param matcher The matcher identifying methods to apply the advice to.
* @return A suitable ASM visitor wrapper with the <i>compute frames</i> option enabled.
*/
public AsmVisitorWrapper.ForDeclaredMethods on(ElementMatcher<? super MethodDescription.InDefinedShape> matcher) {
return new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES).method(matcher, this);
}

@Override @Override
public MethodVisitor wrap(TypeDescription instrumentedType, MethodDescription.InDefinedShape methodDescription, MethodVisitor methodVisitor) { public MethodVisitor wrap(TypeDescription instrumentedType, MethodDescription.InDefinedShape methodDescription, MethodVisitor methodVisitor) {
if (methodDescription.isAbstract() || methodDescription.isNative()) { if (methodDescription.isAbstract() || methodDescription.isNative()) {
Expand Down
Expand Up @@ -5,7 +5,6 @@
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.objectweb.asm.ClassWriter;


import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
Expand Down Expand Up @@ -44,7 +43,7 @@ public AdviceSuppressionTest(Class<?> type) {
public void testIllegalAssignment() throws Exception { public void testIllegalAssignment() throws Exception {
Class<?> dynamicType = new ByteBuddy() Class<?> dynamicType = new ByteBuddy()
.redefine(Sample.class) .redefine(Sample.class)
.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES).method(named(FOO), Advice.to(type))) .visit(Advice.to(type).on(named(FOO)))
.make() .make()
.load(null, ClassLoadingStrategy.Default.WRAPPER) .load(null, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded(); .getLoaded();
Expand Down

0 comments on commit de37472

Please sign in to comment.