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.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

import org.osoa.sca.annotations.Reference;

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

/**
 * @version $Rev: 4286 $ $Date: 2008-05-21 20:56:34 +0100 (Wed, 21 May 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) {
        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) {
        Class superClass = clazz.getSuperclass();
        if (superClass != null) {
            walk(implementation, superClass, context);
        }
    }

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

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

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

    private void walkMethods(I implementation, Class clazz, IntrospectionContext context) {
        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) {
        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) {
        AnnotationProcessor processor = getProcessor(annotation);
        if (processor != null) {
            processor.visitType(annotation, clazz, implementation, context);
        }
    }

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

    private  void visitMethod(A annotation, Method method, I implementation, IntrospectionContext context) {
        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) {
        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) {
        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) {
        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());
    }
}