MentaContainer

Rev

Rev 32 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
32 soliveira 1
package org.mentacontainer.impl;
2
 
3
import java.lang.reflect.InvocationTargetException;
4
import java.lang.reflect.Method;
5
 
6
import org.mentacontainer.Component;
7
import org.mentacontainer.util.FindMethod;
8
 
9
public class GenericComponent implements Component {
10
 
11
        private final Object factory;
12
 
13
        private final Method method;
14
 
39 soliveira 15
        public GenericComponent(Object factory, String methodName) {
32 soliveira 16
 
17
                this.factory = factory;
18
 
19
                try {
20
 
21
                        this.method = FindMethod.getMethod(factory.getClass(), methodName, new Class[] { });
22
 
23
                        this.method.setAccessible(true);
24
 
25
                } catch(Exception e) {
26
 
27
                        throw new RuntimeException(e);
28
                }
29
        }
30
 
31
        public Object getInstance() throws InstantiationException {
32
 
33
                try {
34
 
35
                        return method.invoke(factory, (Object[]) null);
36
 
37
                } catch(InvocationTargetException e) {
38
 
39
                        throw new InstantiationException("InvocationTargetException: " + method.getName() + " / " + e.getMessage());
40
 
41
                } catch(IllegalAccessException e) {
42
 
43
                        throw new InstantiationException("IllegalAccessException: " + method.getName() + " / " + e.getMessage());
44
                }
45
        }
46
 
47
        public Component addInitValue(Object value) {
48
 
49
                throw new UnsupportedOperationException();
50
        }
51
 
52
        public Component addProperty(String name, Object value) {
53
 
54
                throw new UnsupportedOperationException();
55
        }
56
 
57
        public boolean isSingleton() { return false; }
58
 
59
}