Create muzzle references from ldc instructions

This commit is contained in:
Andrew Kent 2018-08-29 15:00:15 -07:00
parent c06983ef34
commit 61aaebfc97
3 changed files with 29 additions and 0 deletions

View File

@ -347,5 +347,20 @@ public class ReferenceCreator extends ClassVisitor {
.build()); .build());
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
} }
@Override
public void visitLdcInsn(final Object value) {
if (value instanceof Type) {
final Type type = underlyingType((Type) value);
if (type.getSort() == Type.OBJECT) {
addReference(
new Reference.Builder(type.getInternalName())
.withSource(refSourceClassName, currentLineNumber)
.withFlag(computeMinimumClassAccess(refSourceType, type))
.build());
}
}
super.visitLdcInsn(value);
}
} }
} }

View File

@ -57,6 +57,14 @@ class ReferenceCreatorTest extends AgentTestRunner {
findMethod(bMethods, "protectedMethod", "()V").getFlags().contains(Reference.Flag.PROTECTED_OR_HIGHER) findMethod(bMethods, "protectedMethod", "()V").getFlags().contains(Reference.Flag.PROTECTED_OR_HIGHER)
} }
def "ldc creates references" () {
setup:
Map<String, Reference> references = ReferenceCreator.createReferencesFrom(LdcAdvice.getName(), this.getClass().getClassLoader())
expect:
references.get('muzzle.TestClasses$MethodBodyAdvice$A') != null
}
private static Reference.Method findMethod(Set<Reference.Method> methods, String methodName, String methodDesc) { private static Reference.Method findMethod(Set<Reference.Method> methods, String methodName, String methodDesc) {
for (Reference.Method method : methods) { for (Reference.Method method : methods) {
if (method == new Reference.Method(methodName, methodDesc)) { if (method == new Reference.Method(methodName, methodDesc)) {

View File

@ -68,4 +68,10 @@ public class TestClasses {
public interface AnotherInterface extends SomeInterface {} public interface AnotherInterface extends SomeInterface {}
} }
public static class LdcAdvice {
public static void ldcMethod() {
MethodBodyAdvice.A.class.getName();
}
}
} }