/** * {@inheritDoc} */ public MethodDescription.InDefinedShape getEnclosingMethod(TypePool typePool) { TypeDescription enclosingType = getEnclosingType(typePool); MethodList<MethodDescription.InDefinedShape> enclosingMethod = enclosingType.getDeclaredMethods() .filter(hasMethodName(methodName).and(hasDescriptor(methodDescriptor))); if (enclosingMethod.isEmpty()) { throw new IllegalStateException(methodName + methodDescriptor + " not declared by " + enclosingType); } return enclosingMethod.getOnly(); }
private AnnotatedFieldDescription findField(String owner, String name, String desc) { //Classpool#describe does not accept '/' in the description name as it expects a class name final String cleanedOwner = owner.replace( '/', '.' ); final TypePool.Resolution resolution = classPool.describe( cleanedOwner ); if ( !resolution.isResolved() ) { final String msg = String.format( "Unable to perform extended enhancement - Unable to locate [%s]", cleanedOwner ); throw new EnhancementException( msg ); } FieldList<?> fields = resolution.resolve().getDeclaredFields().filter( named( name ).and( hasDescriptor( desc ) ) ); if ( fields.size() != 1 ) { final String msg = String.format( "Unable to perform extended enhancement - No unique field [%s] defined by [%s]", name, cleanedOwner ); throw new EnhancementException( msg ); } return new AnnotatedFieldDescription( enhancementContext, fields.getOnly() ); }
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions); MethodList<?> methodList = typeDescription.getDeclaredMethods().filter((name.equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME) ? isConstructor() : ElementMatchers.<MethodDescription>named(name)).and(hasDescriptor(desc))); if (methodList.size() == 1 && methodList.getOnly().getParameters().hasExplicitMetaData()) { for (ParameterDescription parameterDescription : methodList.getOnly().getParameters()) { methodVisitor.visitParameter(parameterDescription.getName(), parameterDescription.getModifiers()); } return new MethodParameterStrippingMethodVisitor(methodVisitor); } else { return methodVisitor; } } }
if (resolution.isResolved()) { FieldList<FieldDescription.InDefinedShape> candidates = resolution.resolve().getDeclaredFields().filter(strict ? ElementMatchers.<FieldDescription>named(internalName).and(hasDescriptor(descriptor)) : ElementMatchers.<FieldDescription>failSafe(named(internalName).and(hasDescriptor(descriptor)))); if (!candidates.isEmpty()) { Replacement.Binding binding = replacement.bind(candidates.getOnly(), opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC);
if (opcode == Opcodes.INVOKESPECIAL && internalName.equals(MethodDescription.CONSTRUCTOR_INTERNAL_NAME)) { candidates = resolution.resolve().getDeclaredMethods().filter(strict ? ElementMatchers.<MethodDescription>isConstructor().and(hasDescriptor(descriptor)) : ElementMatchers.<MethodDescription>failSafe(isConstructor().and(hasDescriptor(descriptor)))); } else if (opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKESPECIAL) { candidates = resolution.resolve().getDeclaredMethods().filter(strict ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor)) : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor)))); } else { // Invokevirtual and invokeinterface can represent a private, non-static method from Java 11. candidates = resolution.resolve().getDeclaredMethods().filter(strict ? ElementMatchers.<MethodDescription>isPrivate().and(not(isStatic())).and(named(internalName).and(hasDescriptor(descriptor))) : ElementMatchers.<MethodDescription>failSafe(isPrivate().<MethodDescription>and(not(isStatic())).and(named(internalName).and(hasDescriptor(descriptor))))); if (candidates.isEmpty()) { candidates = methodGraphCompiler.compile(resolution.resolve(), instrumentedType).listNodes().asMethodList().filter(strict ? ElementMatchers.<MethodDescription>named(internalName).and(hasDescriptor(descriptor)) : ElementMatchers.<MethodDescription>failSafe(named(internalName).and(hasDescriptor(descriptor))));
private AnnotatedFieldDescription findField(String owner, String name, String desc) { //Classpool#describe does not accept '/' in the description name as it expects a class name final String cleanedOwner = owner.replace( '/', '.' ); final TypePool.Resolution resolution = classPool.describe( cleanedOwner ); if ( !resolution.isResolved() ) { final String msg = String.format( "Unable to perform extended enhancement - Unable to locate [%s]", cleanedOwner ); throw new EnhancementException( msg ); } FieldList<?> fields = resolution.resolve().getDeclaredFields().filter( named( name ).and( hasDescriptor( desc ) ) ); if ( fields.size() != 1 ) { final String msg = String.format( "Unable to perform extended enhancement - No unique field [%s] defined by [%s]", name, cleanedOwner ); throw new EnhancementException( msg ); } return new AnnotatedFieldDescription( enhancementContext, fields.getOnly() ); }