Example usage for java.lang Class newInstance

List of usage examples for java.lang Class newInstance

Introduction

In this page you can find the example usage for java.lang Class newInstance.

Prototype

@CallerSensitive
@Deprecated(since = "9")
public T newInstance() throws InstantiationException, IllegalAccessException 

Source Link

Document

Creates a new instance of the class represented by this Class object.

Usage

From source file:net.geoprism.context.ServerInitializer.java

@Request
private static void startup(List<ServerContextListenerInfo> infos) {
    for (ServerContextListenerInfo info : infos) {
        try {//from ww  w  . j  a  v a 2  s. co  m

            Class<?> clazz = LoaderDecorator.load(info.getClassName());
            Object newInstance = clazz.newInstance();

            try {
                ServerContextListener listener = (ServerContextListener) newInstance;
                listener.startup();
            } catch (ClassCastException e) {
                log.error("ClassCastException initializer", e);

                Class<? extends Object> class1 = newInstance.getClass();
                ClassLoader loader1 = class1.getClassLoader();

                log.debug("New instance class : " + class1.hashCode());
                log.debug("New instance class loader: " + loader1.hashCode());

                Class<? extends Object> class2 = ServerContextListener.class;
                ClassLoader loader2 = class2.getClassLoader();

                log.debug("Interface class : " + class2.hashCode());
                log.debug("New instance class loader: " + loader2.hashCode());

                clazz.getMethod("startup").invoke(newInstance);
            }

            log.debug("COMLPETE: " + info.getClassName() + ".setup();");
        } catch (Exception e) {
            log.error(e);

            throw new ProgrammingErrorException(
                    "Unable to startup the server context listener [" + info.getClassName() + "]", e);
        }
    }
}

From source file:com.github.ibole.infrastructure.common.utils.BeanUtil.java

public static <T> List<T> resolveMapListToObjectList(List<Map<String, ?>> list, Class<T> clazz)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    List<T> resultList = new ArrayList<>();
    for (Map<String, ?> map : list) {
        T bean = clazz.newInstance();
        BeanUtils.populate(bean, map);/*from  w  w w .j  a  v a 2 s. co  m*/
        resultList.add(bean);
    }
    return resultList;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;//w ww. ja va2 s. c om
    java.lang.reflect.Field field = null;
    int x = 0;
    int statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
        return statusBarHeight;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusBarHeight;
}

From source file:org.opensprout.osaf.util.WebServiceUtils.java

public static <T, S> T[] copyToDto(List<S> list, Class<T> dtoclass, T[] blankArray,
        CopyBeanToDtoTemplate<S, T> template) {
    List<T> dtos = new ArrayList<T>();
    for (S source : list) {
        T dto;//from ww  w. java2 s. c  om
        try {
            dto = (T) dtoclass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        BeanUtils.copyProperties(source, dto);
        if (template != null)
            template.copy(source, dto);
        dtos.add(dto);
    }

    return dtos.toArray(blankArray);
}

From source file:com.workplacesystems.queuj.process.QueujFactory.java

static void setInstance() {
    try {/*from  w ww  .j  av a2  s.c  om*/
        QueujFactory<?> instance0 = null;

        String implClazzStr = System.getProperty("com.workplacesystems.queuj.QueujFactory");
        if (implClazzStr != null) {
            try {
                Class<QueujFactory> implClazz = (Class<QueujFactory>) Class.forName(implClazzStr);
                instance0 = implClazz.newInstance();
            } catch (ClassNotFoundException ex) {
            } catch (InstantiationException ex) {
            } catch (IllegalAccessException ex) {
            }
        }

        if (instance0 == null)
            instance0 = new QueujFactoryImpl();

        instance = instance0;

        log.info("Initialising Queuj " + Version.getVersionMini());
        log.info("Utilsj " + com.workplacesystems.utilsj.Version.getVersionMini());

        instance.init();
    } catch (Exception e) {
        throw new QueujException(e);
    }
}

From source file:net.geoprism.context.ServerInitializer.java

@Request
public static void destroy() {
    ServerContextListenerDocumentBuilder builder = new ServerContextListenerDocumentBuilder();
    List<ServerContextListenerInfo> infos = builder.read();

    Collections.reverse(infos);// ww w .  j  av a 2 s .c  om

    for (ServerContextListenerInfo info : infos) {
        try {

            Class<?> clazz = LoaderDecorator.load(info.getClassName());
            Object newInstance = clazz.newInstance();

            try {
                ServerContextListener listener = (ServerContextListener) newInstance;
                listener.shutdown();
            } catch (ClassCastException e) {
                log.error("ClassCastException in ServerInitializer.shutdown", e);

                Class<? extends Object> class1 = newInstance.getClass();
                ClassLoader loader1 = class1.getClassLoader();

                log.debug("New instance class : " + class1.hashCode());
                log.debug("New instance class loader: " + loader1.hashCode());

                Class<? extends Object> class2 = ServerContextListener.class;
                ClassLoader loader2 = class2.getClassLoader();

                log.debug("Interface class : " + class2.hashCode());
                log.debug("New instance class loader: " + loader2.hashCode());

                clazz.getMethod("shutdown").invoke(newInstance);
            }

            log.debug("COMLPETE: " + info.getClassName() + ".shutdown();");
        } catch (Exception e) {
            log.error(e);

            throw new ProgrammingErrorException(
                    "Unable to shutdown the server context listener [" + info.getClassName() + "]", e);
        }
    }
}

From source file:egovframework.rte.fdl.string.EgovObjectUtil.java

/**
 * ? ?   ? ./*from w  ww .j a  va2 s. c o m*/
 * @param className
 * @return
 * @throws ClassNotFoundException
 * @throws InstantiationException
 * @throws IllegalAccessException
 * @throws Exception
 */
public static Object instantiate(String className)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception {
    Class<?> clazz;

    try {
        clazz = loadClass(className);
        return clazz.newInstance();
    } catch (ClassNotFoundException e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is can not instantialized.");
        throw new ClassNotFoundException();
    } catch (InstantiationException e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is can not instantialized.");
        throw new InstantiationException();
    } catch (IllegalAccessException e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is not accessed.");
        throw new IllegalAccessException();
    } catch (Exception e) {
        if (log.isErrorEnabled())
            log.error(className + " : Class is not accessed.");
        throw new Exception(e);
    }
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

/**
 * Instantiate a class by no-arg constructor, throw {@link ConfigurationException} if we fail to do so
 *///w w w.  j  av a  2s . com
@CoverageIgnore
public static <T> T newInstance(Class<T> theType) {
    Validate.notNull(theType, "theType must not be null");
    try {
        return theType.newInstance();
    } catch (Exception e) {
        throw new ConfigurationException("Failed to instantiate " + theType.getName(), e);
    }
}

From source file:com.gistlabs.mechanize.impl.MechanizeInitializer.java

protected static void processFactoriesClassNames(final List<String> factoryClassNames) {
    for (String factoryClassName : factoryClassNames)
        try {//from ww w  . j  ava2  s .  c  o m
            @SuppressWarnings("unchecked")
            Class<ResourceFactory> pageFactoryClass = (Class<ResourceFactory>) Class.forName(factoryClassName);
            ResourceFactory pageFactory = pageFactoryClass.newInstance();
            MechanizeAgent.registerFactory(pageFactory);
        } catch (Exception e) {
            // TODO add logging...
            throw MechanizeExceptionFactory.newInitializationException(e);
        }
}

From source file:com.liferay.portal.velocity.LiferayResourceLoader.java

public static void setVelocityResourceListeners(String[] velocityResourceListeners) {

    _velocityResourceListeners = new VelocityResourceListener[velocityResourceListeners.length];

    for (int i = 0; i < velocityResourceListeners.length; i++) {
        try {// www . j a  va 2 s .c o m
            Class<?> clazz = Class.forName(velocityResourceListeners[i]);

            _velocityResourceListeners[i] = (VelocityResourceListener) clazz.newInstance();
        } catch (Exception e) {
            _log.error(e);

            _velocityResourceListeners[i] = null;
        }
    }
}