MentaContainer

Rev

Rev 39 | Rev 41 | Go to most recent revision | 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
 
40 soliveira 31
        public Object getInstance()  {
32 soliveira 32
 
33
                try {
34
 
35
                        return method.invoke(factory, (Object[]) null);
36
 
40 soliveira 37
                } catch(Exception e) {
32 soliveira 38
 
40 soliveira 39
                        throw new RuntimeException("Cannot invoke method: " + method, e);
32 soliveira 40
 
40 soliveira 41
                }
32 soliveira 42
        }
43
 
44
        public Component addInitValue(Object value) {
45
 
46
                throw new UnsupportedOperationException();
47
        }
48
 
49
        public Component addProperty(String name, Object value) {
50
 
51
                throw new UnsupportedOperationException();
52
        }
53
 
54
        public boolean isSingleton() { return false; }
55
 
56
}