summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060518/java/sca/core/src/main/java/org/apache/tuscany/core/config/impl/Java5ComponentTypeIntrospector.java
blob: fdbb6e09428266317eca6da3fad5ce3a64d170cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.core.config.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.tuscany.core.config.ComponentTypeIntrospector;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.JavaIntrospectionHelper;
import org.apache.tuscany.core.config.processor.ProcessorUtils;
import org.apache.tuscany.core.extension.config.ImplementationProcessor;
import org.apache.tuscany.core.system.annotation.Autowire;
import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory;
import org.apache.tuscany.model.assembly.AssemblyFactory;
import org.apache.tuscany.model.assembly.ComponentType;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.ComponentName;

/**
 * Introspects Java annotation-based metata data
 *
 * @version $Rev$ $Date$
 */
@org.osoa.sca.annotations.Service(ComponentTypeIntrospector.class)
public class Java5ComponentTypeIntrospector implements ComponentTypeIntrospector {

    private AssemblyFactory factory;

    private List<ImplementationProcessor> processors = new ArrayList<ImplementationProcessor>();

    public Java5ComponentTypeIntrospector() {
    }

    public Java5ComponentTypeIntrospector(AssemblyFactory factory) {
        this.factory = factory;
    }

    @Autowire
    public void setFactory(SystemAssemblyFactory factory) {
        this.factory = factory;
        //FIXME JFM HACK
        List<ImplementationProcessor> processors = ProcessorUtils.createCoreProcessors(factory);
        for (ImplementationProcessor processor : processors) {
            this.registerProcessor(processor);
        }
        // END hack
    }

    @ComponentName
    protected String name;

    @Init(eager = true)
    public void init(){
    }

    public void registerProcessor(ImplementationProcessor processor) {
        processors.add(processor);
    }

    public void unregisterProcessor(ImplementationProcessor processor) {
        processors.remove(processor);
    }

    /**
     * Visits the given implementation type and calls back to {@link org.apache.tuscany.core.extension.config.ImplementationProcessor}s
     * registered with this introspector to build up a {@link ComponentType}
     *
     * @return ComponentType representing the implementation type metadata
     * @throws ConfigurationLoadException if there is an error introspecting the implementation type
     */
    public ComponentType introspect(Class<?> implClass) throws ConfigurationLoadException {
        ComponentType compType = factory.createComponentType();
        return introspect(implClass, compType);
    }

    public ComponentType introspect(Class<?> implClass, ComponentType compType) throws ConfigurationLoadException {
        for (ImplementationProcessor processor : processors) {
            processor.visitClass(implClass, compType);
        }
        Constructor[] constructors = implClass.getConstructors();
        for (Constructor constructor : constructors) {
            for (ImplementationProcessor processor : processors) {
                processor.visitConstructor(constructor, compType);
            }
        }
        Method[] methods = implClass.getMethods();
        for (Method method : methods) {
            for (ImplementationProcessor processor : processors) {
                processor.visitMethod(method, compType);
            }
        }
        Set<Field> fields = JavaIntrospectionHelper.getAllPublicAndProtectedFields(implClass);
        for (Field field : fields) {
            for (ImplementationProcessor processor : processors) {
                processor.visitField(field, compType);
            }
        }
        Class superClass = implClass.getSuperclass();
        if (superClass != null) {
            visitSuperClass(superClass, compType);
        }
        for (ImplementationProcessor processor : processors) {
            processor.visitEnd(implClass, compType);
        }
        return compType;
    }

    private void visitSuperClass(Class<?> superClass, ComponentType compType) throws ConfigurationLoadException {
        if (!Object.class.equals(superClass)) {
            for (ImplementationProcessor processor : processors) {
                processor.visitSuperClass(superClass, compType);
            }
            superClass = superClass.getSuperclass();
            if (superClass != null) {
                visitSuperClass(superClass, compType);
            }
        }
    }

}