All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fabric3.introspection.impl.DefaultClassWalker Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
/*
 * See the NOTICE file distributed with this work for information
 * regarding copyright ownership.  This file is licensed
 * to you 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.fabric3.introspection.impl;

import java.util.Map;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;

import org.osoa.sca.annotations.Reference;

import org.fabric3.scdl.Implementation;
import org.fabric3.scdl.InjectingComponentType;
import org.fabric3.introspection.IntrospectionException;
import org.fabric3.introspection.java.AnnotationProcessor;
import org.fabric3.introspection.IntrospectionContext;
import org.fabric3.introspection.java.ClassWalker;

/**
 * @version $Rev: 3101 $ $Date: 2008-03-15 12:48:57 +0000 (Sat, 15 Mar 2008) $
 */
public class DefaultClassWalker> implements ClassWalker {

    private Map, AnnotationProcessor> processors;

    /**
     * Constructor used from the bootstrapper.
     * 
     * @param processors
     */
    public DefaultClassWalker(Map, AnnotationProcessor> processors) {
        this.processors = processors;
    }
    
    /**
     * Constructor used from the system SCDL. 
     * 
     * TODO This needs to be working once the re-injection is working properly.
     */
    @org.osoa.sca.annotations.Constructor
    public DefaultClassWalker() {
    }
    
    @Reference
    public void setProcessors(Map, AnnotationProcessor> processors) {
        this.processors = processors;
    }

    public void walk(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        if (!clazz.isInterface()) {
            walkSuperClasses(implementation, clazz, context);
        }

        walkInterfaces(implementation, clazz, context);

        walkClass(implementation, clazz, context);

        walkFields(implementation, clazz, context);

        walkMethods(implementation, clazz, context);

        walkConstructors(implementation, clazz, context);
    }

    private void walkSuperClasses(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        Class superClass = clazz.getSuperclass();
        if (superClass != null) {
            walk(implementation, superClass, context);
        }
    }

    private void walkInterfaces(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        for (Class interfaze : clazz.getInterfaces()) {
            walk(implementation, interfaze, context);
        }
    }

    private void walkClass(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        for (Annotation annotation : clazz.getDeclaredAnnotations()) {
            visitType(annotation, clazz, implementation, context);
        }
    }

    private void walkFields(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        for (Field field : clazz.getDeclaredFields()) {
            for (Annotation annotation : field.getDeclaredAnnotations()) {
                visitField(annotation, field, implementation, context);
            }
        }
    }

    private void walkMethods(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        for (Method method : clazz.getDeclaredMethods()) {
            for (Annotation annotation : method.getDeclaredAnnotations()) {
                visitMethod(annotation, method, implementation, context);
            }

            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (int i = 0; i < parameterAnnotations.length; i++) {
                Annotation[] annotations = parameterAnnotations[i];
                for (Annotation annotation : annotations) {
                    visitMethodParameter(annotation, method, i, implementation, context);
                }
            }
        }
    }

    private void walkConstructors(I implementation, Class clazz, IntrospectionContext context) throws IntrospectionException {
        for (Constructor constructor : clazz.getDeclaredConstructors()) {
            for (Annotation annotation : constructor.getDeclaredAnnotations()) {
                visitConstructor(annotation, constructor, implementation, context);
            }

            Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();
            for (int i = 0; i < parameterAnnotations.length; i++) {
                Annotation[] annotations = parameterAnnotations[i];
                for (Annotation annotation : annotations) {
                    visitConstructorParameter(annotation, constructor, i, implementation, context);
                }
            }
        }
    }

    private  void visitType(A annotation, Class clazz, I implementation, IntrospectionContext context) throws IntrospectionException {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitType(annotation, clazz, implementation, context);
        }
    }

    private  void visitField(A annotation, Field field, I implementation, IntrospectionContext context) throws IntrospectionException {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitField(annotation, field, implementation, context);
        }
    }

    private  void visitMethod(A annotation, Method method, I implementation, IntrospectionContext context) throws IntrospectionException {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitMethod(annotation, method, implementation, context);
        }
    }

    private  void visitMethodParameter(A annotation, Method method, int index, I implementation, IntrospectionContext context) throws IntrospectionException {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitMethodParameter(annotation, method, index, implementation, context);
        }
    }

    private  void visitConstructor(A annotation, Constructor constructor, I implementation, IntrospectionContext context) throws IntrospectionException {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitConstructor(annotation, constructor, implementation, context);
        }
    }

    private  void visitConstructorParameter(A annotation, Constructor constructor, int index, I implementation, IntrospectionContext context) throws IntrospectionException {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitConstructorParameter(annotation, constructor, index, implementation, context);
        }
    }

    @SuppressWarnings("unchecked")
    private  AnnotationProcessor getProcessor(A annotation) {
        return (AnnotationProcessor) processors.get(annotation.annotationType());
    }
}