1 /******************************************************************************
2 * Copyright (C) PicoContainer Organization. All rights reserved. *
3 * ------------------------------------------------------------------------- *
4 * The software in this package is published under the terms of the BSD *
5 * style license a copy of which has been included with this distribution in *
6 * the LICENSE.txt file. *
7 * *
8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9 *****************************************************************************/
10
11 package org.picocontainer.extras;
12
13 import org.picocontainer.internals.ComponentFactory;
14 import org.picocontainer.PicoInitializationException;
15 import org.picocontainer.PicoIntrospectionException;
16 import org.picocontainer.internals.ComponentSpecification;
17
18 import java.lang.reflect.InvocationHandler;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Proxy;
21
22 public class ImplementationHidingComponentFactory implements ComponentFactory {
23 private final ComponentFactory componentFactory;
24
25 public ImplementationHidingComponentFactory(ComponentFactory componentFactory) {
26 this.componentFactory = componentFactory;
27 }
28
29 public Object createComponent(ComponentSpecification componentSpec, Object[] instanceDependencies) throws PicoInitializationException {
30 Object componentInstance = componentFactory.createComponent(componentSpec, instanceDependencies);
31 if (componentInstance == null) {
32 //TODO Should be subclass of PicoInit..
33 throw new NullPointerException("Unable to create componentInstance for componentSpec" + componentSpec);
34 }
35 // TODO: search for all interfaces for component-implementation instead
36 Class[] interfaces = new Class[]{(Class) componentSpec.getComponentKey()};
37 return Proxy.newProxyInstance(componentSpec.getComponentImplementation().getClassLoader(),
38 interfaces, new ImplementationHidingProxy(componentInstance));
39 }
40
41 public Class[] getDependencies(Class componentImplementation) throws PicoIntrospectionException {
42 return componentFactory.getDependencies(componentImplementation);
43 }
44
45 private class ImplementationHidingProxy implements InvocationHandler {
46 private Object componentInstance;
47
48 public ImplementationHidingProxy(Object componentInstance) {
49 this.componentInstance = componentInstance;
50 }
51
52 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
53 return method.invoke(componentInstance, args);
54 }
55 }
56 }
This page was automatically generated by Maven