MentaContainer

Rev

Rev 41 | 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.Method;
4
 
5
import org.mentacontainer.Component;
6
import org.mentacontainer.util.FindMethod;
7
 
8
public class GenericComponent implements Component {
9
 
10
        private final Object factory;
11
 
12
        private final Method method;
13
 
39 soliveira 14
        public GenericComponent(Object factory, String methodName) {
32 soliveira 15
 
16
                this.factory = factory;
17
 
18
                try {
19
 
20
                        this.method = FindMethod.getMethod(factory.getClass(), methodName, new Class[] { });
21
 
22
                        this.method.setAccessible(true);
23
 
24
                } catch(Exception e) {
25
 
26
                        throw new RuntimeException(e);
27
                }
28
        }
29
 
41 soliveira 30
        public <T> T getInstance()  {
32 soliveira 31
 
32
                try {
33
 
41 soliveira 34
                        return (T) method.invoke(factory, (Object[]) null);
32 soliveira 35
 
40 soliveira 36
                } catch(Exception e) {
32 soliveira 37
 
40 soliveira 38
                        throw new RuntimeException("Cannot invoke method: " + method, e);
32 soliveira 39
 
40 soliveira 40
                }
32 soliveira 41
        }
42
 
43
        public Component addInitValue(Object value) {
44
 
45
                throw new UnsupportedOperationException();
46
        }
47
 
48
        public Component addProperty(String name, Object value) {
49
 
50
                throw new UnsupportedOperationException();
51
        }
52
}