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

net.java.ao.ImplementationWrapper Maven / Gradle / Ivy

Go to download

This is the full Active Objects library, if you don't know which one to use, you probably want this one.

There is a newer version: 6.1.1
Show newest version
/*
 * Copyright 2007 Daniel Spiewak
 * 
 * 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 net.java.ao;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Daniel Spiewak
 */
class ImplementationWrapper> {
    private List implementations;
    private boolean initialized = false;

    public ImplementationWrapper() {
        implementations = new ArrayList();
    }

    public void init(T instance) {
        init(instance, instance.getEntityType());
        initialized = true;
    }

    private void init(T instance, Class> clazz) {
        Implementation implAnnotation = clazz.getAnnotation(Implementation.class);

        if (implAnnotation != null) {
            try {
                Constructor con = (Constructor) implAnnotation.value().getConstructor(clazz);
                implementations.add(con.newInstance(instance));
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            } catch (IllegalArgumentException e) {
            } catch (InstantiationException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }

        for (Class sup : clazz.getInterfaces()) {
            if (RawEntity.class.isAssignableFrom(sup)) {
                init(instance, (Class>) sup);
            }
        }
    }

    public MethodImplWrapper getMethod(String name, Class... parameterTypes) {
        if (!initialized) {
            return null;
        }

        for (Object obj : implementations) {
            try {
                return new MethodImplWrapper(obj, obj.getClass().getMethod(name, parameterTypes));
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            }
        }

        return null;
    }
}