MentaContainer

Rev

Rev 39 | Go to most recent revision | Details | 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 String name;
14
 
15
        private final Method method;
16
 
17
        public GenericComponent(String name, Object factory, String methodName) {
18
 
19
                this.name = name;
20
 
21
                this.factory = factory;
22
 
23
                try {
24
 
25
                        this.method = FindMethod.getMethod(factory.getClass(), methodName, new Class[] { });
26
 
27
                        this.method.setAccessible(true);
28
 
29
                } catch(Exception e) {
30
 
31
                        throw new RuntimeException(e);
32
                }
33
        }
34
 
35
        public Object getInstance() throws InstantiationException {
36
 
37
                try {
38
 
39
                        return method.invoke(factory, (Object[]) null);
40
 
41
                } catch(InvocationTargetException e) {
42
 
43
                        throw new InstantiationException("InvocationTargetException: " + method.getName() + " / " + e.getMessage());
44
 
45
                } catch(IllegalAccessException e) {
46
 
47
                        throw new InstantiationException("IllegalAccessException: " + method.getName() + " / " + e.getMessage());
48
                }
49
        }
50
 
51
        public String getName() { return name; }
52
 
53
        public Component addInitValue(Object value) {
54
 
55
                throw new UnsupportedOperationException();
56
        }
57
 
58
        public Component addProperty(String name, Object value) {
59
 
60
                throw new UnsupportedOperationException();
61
        }
62
 
63
        public boolean isSingleton() { return false; }
64
 
65
}