Farbeit for me to judge this use case, but it does seem slightly bizarre :)

That said, the method invocations aren't dynamic enough so that the caller
not need to know their parameter types.  This introduces a bit of a
dependency between the two (at least in your example).

Of no greater cost would be to define your class as:

class TestClass {
  @Inject @Named("name") String test;

   void testMethod() [
      ...already has "test" injected into it;
   }
}

Then your client would do:

Method method = TestClass.getMethod("testMethod");
...invoke

At this point, though, you're either:

1) calling only static methods
2) calling an instance method

For (1), make the fields static and requestStaticInjection(TestClass.class);

For (2), you can call the method directly:
TestClass testClass = injector.getInstance(TestClass.class);
testClass.testMethod();

If you're dispatching off of method names, then you can go back to the
original technique, but you'd need a target:

TestClass testClass = injector.getInstance(TestClass.class);
Method method = TestClass.getMethod("testMethod");
...invoke method on testClass

Fred

On Sun, Jan 9, 2011 at 9:42 AM, EECOLOR <[email protected]> wrote:

> Hello,
>
> I am building a small framework in which I want to call a method
> dynamically. The method can have injectable parameters. I want to use
> Guice's internal method injection to call this method, but it seems
> that this is not possible. A small (simplified) example:
>
> class TestClass
> {
>   public void testMethod(@Named("name") String test)
>   {
>        System.out.println(test);
>   }
> }
>
> In the module I want to bind it:
>
> bind(String.class).annotatedWith(Names.named("name").toInstance("value");
>
> And then at runtime do something like this:
>
> Method method = TestClass.getMethod("testMethod", String.class);
> guiceInjector.callMethodWithInjection(method);
>
> As far as I can see the way to go is to manually build a key for each
> parameter and use an injector to get the instances. Then once I have
> all the parameters, call the method. Since this seems to duplicate the
> functionality of method injection, it feels a bit odd to duplicate
> code.
>
> Any advise?
>
>
> Greetz Erik
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to