Java Class New Instance newInstancesViaMetaAnnotation(Class declarator, Class metaAnnotationClass, Class expected)

Here you can find the source of newInstancesViaMetaAnnotation(Class declarator, Class metaAnnotationClass, Class expected)

Description

new Instances Via Meta Annotation

License

Open Source License

Declaration

public static <T> List<T> newInstancesViaMetaAnnotation(Class<?> declarator,
            Class<? extends Annotation> metaAnnotationClass, Class<T> expected) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.List;
import com.google.common.collect.Lists;

public class Main {
    public static <T> List<T> newInstancesViaMetaAnnotation(Class<?> declarator,
            Class<? extends Annotation> metaAnnotationClass, Class<T> expected) {
        List<T> result = Lists.newArrayList();
        for (Annotation annotation : declarator.getAnnotations()) {
            Annotation metaAnnotation = annotation.annotationType().getAnnotation(metaAnnotationClass);
            T r = newInstanceViaAnnotation(declarator, metaAnnotation, expected, annotation);
            if (r != null)
                result.add(r);//from  w  ww  .ja va 2 s . c o m
        }
        return result;
    }

    public static <T> List<T> newInstancesViaMetaAnnotation(Method declarator,
            Class<? extends Annotation> metaAnnotationClass, Class<T> expected) {
        List<T> result = Lists.newArrayList();
        for (Annotation annotation : declarator.getAnnotations()) {
            Annotation metaAnnotation = annotation.annotationType().getAnnotation(metaAnnotationClass);
            T r = newInstanceViaAnnotation(null, metaAnnotation, expected, annotation);
            if (r != null)
                result.add(r);
        }
        return result;
    }

    public static <T> List<T> newInstancesViaMetaAnnotation(Method declarator, int paramIndex,
            Class<? extends Annotation> metaAnnotationClass, Class<T> expected) {
        List<T> result = Lists.newArrayList();
        Annotation[][] annotations = declarator.getParameterAnnotations();
        if (paramIndex >= 0 && paramIndex < annotations.length)
            for (Annotation annotation : annotations[paramIndex]) {
                Annotation metaAnnotation = annotation.annotationType().getAnnotation(metaAnnotationClass);
                T r = newInstanceViaAnnotation(null, metaAnnotation, expected, annotation);
                if (r != null)
                    result.add(r);
            }
        return result;
    }

    @SuppressWarnings("unchecked")
    public static <T> T newInstanceViaAnnotation(Class<?> declarator, Annotation annotation, Class<T> expected,
            Annotation parameter) {
        if (annotation != null)
            for (Method f : annotation.annotationType().getDeclaredMethods())
                if (f.getReturnType() == Class.class) {
                    try {
                        Object objtype = f.invoke(annotation);
                        if (objtype instanceof Class<?> && expected.isAssignableFrom((Class<?>) objtype)) {
                            T result = newInstance((Class<T>) objtype, declarator, parameter);
                            if (result != null)
                                return result;
                        }
                    } catch (IllegalArgumentException e) {
                    } catch (IllegalAccessException e) {
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException(e);
                    }
                }
        return null;
    }

    public static <T> T newInstanceViaAnnotation(Class<?> declarator, Class<? extends Annotation> AnnotationClass,
            Class<T> expected) {
        Annotation annotation = declarator.getAnnotation(AnnotationClass);
        return newInstanceViaAnnotation(declarator, annotation, expected, annotation);
    }

    @SuppressWarnings("unchecked")
    public static <T> T newInstance(Class<T> type, Class<?> declarator, Annotation annotation)
            throws IllegalAccessException {
        if (declarator != null)
            try {
                Constructor<?> c = type.getDeclaredConstructor(declarator.getClass(), annotation.annotationType());
                c.setAccessible(true);
                if (c != null)
                    return (T) c.newInstance(declarator, annotation);
            } catch (SecurityException e) {
            } catch (NoSuchMethodException e) {
            } catch (InstantiationException e) {
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        try {
            Constructor<?> c = type.getConstructor(annotation.annotationType());
            if (c != null)
                return (T) c.newInstance(annotation);
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        try {
            Constructor<?> c = type.getConstructor();
            if (c != null)
                return (T) c.newInstance();
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        } catch (InstantiationException e) {
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}

Related

  1. newInstanceFromUnknownArgumentTypes(Class cls, Object[] args)
  2. newInstanceHard(String name)
  3. newInstanceOf(Class clazz)
  4. newInstanceOf(String className)
  5. newInstanceOrThrow(final Class clazz)
  6. newInstanceViaAnnotation(Class declarator, Annotation annotation, Class expected, Annotation parameter)
  7. newInstanceWithDefaults(Class annotationType)
  8. newInstanceWithFill(Class componentType, int length, Object filledValue)
  9. newInstanceWithoutInit(Class clazz)