webtier@glassfish.java.net

Re: Programmatic or dynamic construction of composite components

From: <webtier_at_javadesktop.org>
Date: Sun, 28 Feb 2010 14:04:48 PST

Since I already was that far, I've decided to hack it via reflection. Here's the code I won't show my employer:

[pre] public CompositeComponentTagHandler(Resource ccResource,
                        ComponentConfig config) {
                super(config);
                this.ccResource = ccResource;
                this.binding = config.getTag().getAttributes().get("binding");
                final TagHandlerDelegate tagHandlerDelegate = this
                                .getTagHandlerDelegate();

                // HACK
                try {
                        ClassLoader cl = ComponentTagHandlerDelegateImpl.class
                                        .getClassLoader();
                        Class<?> createComponentDelegateClass = cl
                                        .loadClass("com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl$CreateComponentDelegate");

                        Object instance = Proxy.newProxyInstance(cl,
                                        new Class<?>[] { createComponentDelegateClass },
                                        new InvocationHandler() {

                                                @Override
                                                public Object invoke(Object proxy, Method method,
                                                                Object[] args) throws Throwable {
                                                        // TODO Auto-generated method stub
                                                        return CompositeComponentTagHandler.this
                                                                        .createComponent((FaceletContext) args[0]);
                                                }
                                        });

                        Method setCreateCompositeComponentDelegate = tagHandlerDelegate
                                        .getClass().getDeclaredMethod(
                                                        "setCreateCompositeComponentDelegate",
                                                        createComponentDelegateClass);
                        setCreateCompositeComponentDelegate.setAccessible(true);
                        setCreateCompositeComponentDelegate.invoke(tagHandlerDelegate,
                                        instance);

                } catch (Exception ex) {
                        throw new RuntimeException(ex);
                }
        }
[/pre]

What I basically did here is:
* Create an implementation of CreateComponentDelegate via reflection as proxy.
* Provide this implementation to the tagHandlerDelegate via reflection (package-protected method).

Apart from that I also had to make the tagHandlerDelegate accessible from the outside so I overridden protected method as public:

[pre]public TagHandlerDelegate getTagHandlerDelegate() {
                return super.getTagHandlerDelegate();
        }[/pre]

But compared to the reflected creation of invisible package-protected interface it's not even a hack.
[Message sent by forum member 'lexi' (valikov_at_gmx.net)]

http://forums.java.net/jive/thread.jspa?messageID=389251