0

I'm writing a ton of boilerplate like this. I feel like there has to be a better way. What I'm looking for is getting the new Object[] automatically from the method call. Something like Reflection.getCurrentMethodCall().getArgumentsAsList().toArray(), but I'm not sure what keywords I should be looking for

Thanks!

[ ... 50 identical methods ... ]
                @Override
            public int get_ProxyFactoryMapping(PointerByReference factoryMapping) {
                Function f = Function.getFunction(vTable[48], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), factoryMapping});
            }

            @Override
            public int GetPropertyProgrammaticName(int property, PointerByReference name) {
                Function f = Function.getFunction(vTable[49], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), property, name});
            }

            @Override
            public int GetPatternProgrammaticName(int pattern, PointerByReference name) {
                Function f = Function.getFunction(vTable[50], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), pattern, name});
            }
            @Override
            public int GetPatternProgrammaticName(0..n arguments) {
                Function f = Function.getFunction(vTable[running index], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), the same arguments as this object was called with});
            }
[ ... 150 identical methods ... ]
2
  • Please edit your question and show three more boilerplate methods, so we can see the pattern. Are you overriding all those methods because you must? Can you change the design of the class and its inheritance? Commented Feb 20, 2018 at 18:57
  • @Override public int GetPatternProgrammaticName([0..n parameters]) { Function f = Function.getFunction(vTable[index], Function.ALT_CONVENTION); return f.invokeInt(new Object[]{getInterface(), [the same parameters]}); } Can't change the design much, this is a bridge between a Java interface and a legacy vTable object. We grab the appropriate function pointer from the vTable and call it with the functions given to the bridge. Commented Feb 21, 2018 at 20:00

1 Answer 1

2

Have you tried java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler?

Basically you implement the InvocationHandler interface, which has one single method invoke that can give you what you want:

Object invoke(Object proxy, // the proxy object
              Method method, // the method called
              Object[] args) // the argument list
       throws Throwable

Then you pass an instance of the handler to Proxy class to get a proxy object (Code copied from Java Doc):

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                      new Class[] { Foo.class },
                                      handler);

Where Foo is a interface.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.