Java Reflection Interface Get getInterfaceInstance(Class interfaceType)

Here you can find the source of getInterfaceInstance(Class interfaceType)

Description

get Interface Instance

License

Open Source License

Declaration

static <T> T getInterfaceInstance(Class<T> interfaceType)
            throws InstantiationException, IllegalAccessException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    private static final Map<Class<?>, Class<?>> interfaceImplementations = new HashMap<>();

    static <T> T getInterfaceInstance(Class<T> interfaceType)
            throws InstantiationException, IllegalAccessException {
        Class impl = interfaceImplementations.get(interfaceType);
        if (impl == null) {
            throw new InstantiationException("Cannot instantiate interface [ " + interfaceType.getName() + "]");
        }/*w  w w . j  a va2 s  .  c o m*/
        return (T) impl.newInstance();
    }
}

Related

  1. getClassHierarchy(Class c, boolean includeInterfaces)
  2. getDeclaredInterfaces(Class claz, Class... assignableClasses)
  3. getImplementedInterfaceParents(Class clazz, Set classesResult)
  4. getImplementedInterfaces(Class clazz)
  5. getImplementedInterfaces(Class cl)
  6. getInterfaceNames(Class c)
  7. getInterfaces(Class clazz)
  8. getInterfaces(Class c)
  9. getInterfaces(Class clazz)