Questions tagged [dynamic-proxy]

A Dynamic Proxy is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface.

A Dynamic Proxy is a class that implements a list of interfaces specified at runtime such that a method invocation through one of the interfaces on an instance of the class will be encoded and dispatched to another object through a uniform interface. Thus, a dynamic proxy class can be used to create a type-safe proxy object for a list of interfaces without requiring pre-generation of the proxy class, such as with compile-time tools. Method invocations on an instance of a dynamic proxy class are dispatched to a single method in the instance's invocation handler, and they are encoded with a java.lang.reflect.Method object identifying the method that was invoked and an array of type Object containing the arguments.

Dynamic proxy classes are useful to an application or library that needs to provide type-safe reflective dispatch of invocations on objects that present interface APIs. For example, an application can use a dynamic proxy class to create an object that implements multiple arbitrary event listener interfaces (interfaces that extend java.util.EventListener) to process a variety of events of different types in a uniform fashion, such as by logging all such events to a file.

226 questions
3
votes
1 answer

Why don't I experience any exception when I lookup bean wrapped by JDK dynamic proxy by class(instead of interface)?

Lets consider following bean: @Service @Scope(value = "prototype", proxyMode = ScopedProxyMode.INTERFACES) public class MyBeanB implements MyBeanBInterface { private static final AtomicLong COUNTER = new AtomicLong(0); private Long index; …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
3
votes
2 answers

Kotlin strange behaviour when calling extension function on java.lang.reflect.Proxy object

Today I played with some java.lang.reflect.Proxy in Kotlin, and I was surprised by this behaviour: import java.lang.reflect.Proxy interface Dog { fun bark() fun bark3Times() } class DogImpl : Dog { override fun bark() = println("Bark!") …
xinaiz
  • 7,744
  • 6
  • 34
  • 78
3
votes
2 answers

Efficient way to track object keys with JavaScript

I am using a Proxy object with traps to keep track of object keys so that I can easily iterate over and/or select a random key from the object with little performance overhead. Currently, I am storing the keys in an array as they are added. This is…
w0f
  • 908
  • 9
  • 23
3
votes
1 answer

How to map EF Dynamic Proxy with Automapper 6.1.1+

I am trying to map an Entity Framework model to a view model after saving. //Map my incoming VM to a DTO and save it Inquiry savedInquiry = InquiryLogic.Save(mapper.Map(inquiryVM)); //Map the returned Inquiry back to a VM and return…
3
votes
1 answer

Invoke an interface default method on a proxy

How do I create a proxy and invoke default interface methods as if they were implemented by the proxy super-class? For example: interface Foo { default int returnSomething() { return 1; } } interface Bar extends Foo { default…
Giovanni Lovato
  • 2,183
  • 2
  • 29
  • 53
3
votes
2 answers

FluentAssertions Should().BeEquivalentTo doesn't compare run-time derived types on EF dynamic proxies

I'm using FluentAssertions to compare two objects using Should().BeEquivalentTo() where one object is an EF dynamic proxy. However, it appears that the unification of ShouldBeEquivalentTo and ShouldAllBeEquivalentTo (#593) in 5.0.0 has broken the…
Neo
  • 4,145
  • 6
  • 53
  • 76
3
votes
0 answers

Tinymapper and Autmapper fail to map objects of the type dynamic proxy classes from entity framework

I am having ASP.Net Entities with their repositories and Viewmodels to get exposed from my Services layer. When I try to Bind any of my entity to viewmodel, I am getting an exception saying that the mapping is impossible between these two objects…
3
votes
1 answer

Is it possible to add a property to a type, via a DynamicProxy?

I'm using Castle DynamicProxy to create a proxy of a given type at runtime - including a couple mixins. I'm trying to figure out if it's possible to also add arbitrary properties to the proxy, e.g.: class BaseType { string Foo { get; set; } } and…
Bobby
  • 1,666
  • 3
  • 16
  • 27
3
votes
3 answers

EF 4.0 Dynamic Proxies POCO Object Does not match target type

I am using EF 4.0 and POCO's. I stumbled across this error while inserting to records into the data base. Property accessor 'QualityReasonID' on object 'BI.Entities.QualityReason' threw the following exception:'Object does not match target…
Tim
  • 377
  • 7
  • 19
3
votes
2 answers

How does Mockito mock interfaces?

I thought Mockito mocked interfaces through the use of dynamic proxies. But then I noticed the type of a Mockito-mocked intefaces whilst debugging: MyInterface$$EnhancerByMockitoWithCGLIB$$9654c88 indicating that CGLIB is used instead of a dynamic…
balteo
  • 23,602
  • 63
  • 219
  • 412
3
votes
2 answers

How do you give a C# Auto-Property a default value in DynamicProxy?

namespace ConsoleApplication15 { using System; using Castle.DynamicProxy; public class Test { private SubTestClass subTestClass; public string Status { get { return this.subTestClass.SubStatus; } …
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
3
votes
3 answers

Do DynamicProxy classes work well with intellisense/type safety?

I was looking at using DynamicProxy classes, and I'm fairly new to this concept. Before I got too far down this road, I was wondering how well these classes work with IntelliSense and type safety? I'm just afraid of using something like Castle…
michael
  • 14,844
  • 28
  • 89
  • 177
3
votes
2 answers

Using dynamic proxy on NHibernate objects

I'm trying to use Castle.DynamicProxy2 to cleanup code within NHibernate persisted classes. Here is a simple version of it. The Pet class: public class Pet { public int Id { get; set; } public string Name { get; set; } public int Age {…
ca7l0s
  • 71
  • 1
  • 7
3
votes
3 answers

How to fix "type exists in both assemblies" failure when using DynamicProxy types in an assembly referencing NSubstitute?

I have an application that uses DynamicProxy 3.1 to do runtime interception. I have a test assembly that uses NSubstitute for mocking. I just wrote some "integration" tests against my fully bootstrapped container (StructureMap using InterceptWith to…
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
2
votes
3 answers

PHP: Overriding parent methods with __call

I'd like to hide parent methods in some manner so that a child class's __call magic method is invoked for methods defined on the parent. For example: class C { function foo() { echo "foo\n"; } } class D extends C { function __call( $method,…
rich remer
  • 3,407
  • 2
  • 34
  • 47