没有实现类的接口实例 [英] Instance of an interface without an implementation class

查看:116
本文介绍了没有实现类的接口实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JET模板,用于为接口实现类生成代码。我无法想出一个打印出这个生成的代码的可执行测试类,因为我无法获得从JET模板创建的 generate 方法的参数的对象。

I have a JET Template that is meant to generate code for an interface implementation class. I am having trouble coming up with an executable test class that prints out this generated code because I am unable to get an object for the argument of the generate method created from the JET Template.

我希望测试类的工作方式如下:

I want the test class to work something like this:

/**
 * An executable test class that prints out exemplary generator output
 * and demonstrates that the JET template does what it should.
 */
public class TestClass {
    public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        String className = "A"; // "A" is the name of the interface in the same package.

        Class c = Class.forName(className);
        Object o = c.newInstance();

        Q2Generator g = new Q2Generator(); // Class created from the JET Template
        String result = g.generate(o);
        System.out.println(result);
    }
}

但显然, c。 newInstance(); 不适用于接口。有没有其他方法可以将接口的对象提供给 generate 方法?我需要接口的对象,因为在Q2Generator的 generate 方法中,它从对象参数中获取有关接口中方法声明的信息。

But obviously, c.newInstance(); doesn't work for an interface. Is there another way I could possibly feed an object of the interface through to the generate method? I need the object of the interface because in the Q2Generator's generate method, it takes information about the method declarations in the interface from the object argument.

我不确定这是否提供了足够的上下文,但如果还不够,我在这里问的另一个问题还有更多细节:使用JET生成代码:缩进代码

I'm not sure if that provides enough context, but if it isn't enough there are more details in another question I asked here: Using JET to generate code: Indenting code

谢谢。

推荐答案

如果我理解你要做什么,你应该能够用动态代理。下面是在运行时实现接口而不明确知道接口类型的示例:

If I understand what you're trying to do, you should be able to pull it off with dynamic proxying. Here's an example of implementing an interface at runtime without explicitly knowing the interface's type:

import java.lang.reflect.*;

public class ImplementInterfaceWithReflection {
    public static void main(String[] args) throws Exception {
        String interfaceName = Foo.class.getName();
        Object proxyInstance = implementInterface(interfaceName);
        Foo foo = (Foo) proxyInstance;
        System.out.println(foo.getAnInt());
        System.out.println(foo.getAString());
    }

    static Object implementInterface(String interfaceName)
            throws ClassNotFoundException {
        // Note that here we know nothing about the interface except its name
        Class clazz = Class.forName(interfaceName);
        return Proxy.newProxyInstance(
            clazz.getClassLoader(),
            new Class[]{clazz},
            new TrivialInvocationHandler());
    }

    static class TrivialInvocationHandler implements InvocationHandler {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) {
            System.out.println("Method called: " + method);
            if (method.getReturnType() == Integer.TYPE) {
                return 42;
            } else {
                return "I'm a string";
            }
        }
    }

    interface Foo {
        int getAnInt();
        String getAString();
    }
}

这篇关于没有实现类的接口实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆