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:com.floragunn.searchguard.util.SecurityUtil.java

private static boolean isWindowsAdmin() {

    try {//  w  w  w. j  a  v  a  2s  . com
        final Class ntSystemClass = Class.forName("com.sun.security.auth.module.NTSystem");
        final Object ntSystem = ntSystemClass.newInstance();
        final String[] groups = (String[]) ntSystemClass.getDeclaredMethod("getGroupIDs").invoke(ntSystem);
        for (final String group : groups) {
            if (group.equals("S-1-5-32-544")) {
                return true;
            }
        }
        return false;
    } catch (final Exception e) {
        return false;
    }
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

public static <T> T newInstance(Class<T> clazz) {
    T result = null;//w ww . j  av a 2 s .co  m
    try {
        result = clazz.newInstance();
    } catch (InstantiationException e) {
        MappingUtils.throwMappingException(e);
    } catch (IllegalAccessException e) {
        MappingUtils.throwMappingException(e);
    }
    return result;
}

From source file:com.kangdainfo.common.util.BeanUtil.java

/**
 * @param attributes/*  w  ww  .  ja  v a2  s.c o  m*/
 * @param clazz
 *
 * @return
 *
 * @see BeanUtil#populate(Map, Object)
 */
public static Object populate(Map attributes, Class clazz) {

    try {

        Object instance = clazz.newInstance();

        return BeanUtil.populate(attributes, instance);

    } catch (InstantiationException e) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("Could not populate a bean from class ");
        buffer.append(clazz.getName()).append(". ");
        buffer.append("This have a public empty contructor?");

        throw new RuntimeException(buffer.toString(), e);

    } catch (IllegalAccessException e) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("Could not populate a bean from class ");
        buffer.append(clazz.getName()).append(". ");
        buffer.append("This class have a public contructor?");

        throw new RuntimeException(buffer.toString(), e);

    }

}

From source file:com.github.jjYBdx4IL.utils.fma.FMAConfig.java

@SuppressWarnings("deprecation")
public static Object readConfig(String filename, Class<?> clazz) throws IOException {
    try {//from  www  .  j a  v a 2  s  .c  o m
        File configFile = new File(CFG_DIR, filename);

        XStream xstream = new XStream(new StaxDriver());
        xstream.autodetectAnnotations(true);

        if (configFile.exists()) {
            return xstream.fromXML(configFile);
        }

        // save empty config so user is able to add his details
        configFile.getParentFile().mkdirs();
        Object config = clazz.newInstance();
        String xml = xstream.toXML(config);
        try (OutputStream os = new FileOutputStream(configFile)) {
            IOUtils.write(formatXml(xml), os);
        }
        return config;
    } catch (InstantiationException | IllegalAccessException ex) {
        throw new IOException(ex);
    }
}

From source file:com.microsoft.tfs.core.util.UserNameUtil.java

private static NTUserInfo lookUpNTUserInfo() {
    try {//from  www.j  av  a  2s . c  o m
        final Class c = Class.forName("com.sun.security.auth.module.NTSystem"); //$NON-NLS-1$
        final Object instance = c.newInstance();

        final String userName = (String) c.getMethod("getName", (Class[]) null).invoke(instance, //$NON-NLS-1$
                (Object[]) null);
        final String domain = (String) c.getMethod("getDomain", (Class[]) null).invoke(instance, //$NON-NLS-1$
                (Object[]) null);
        final String domainSID = (String) c.getMethod("getDomainSID", (Class[]) null).invoke(instance, //$NON-NLS-1$
                (Object[]) null);
        final String userSID = (String) c.getMethod("getUserSID", (Class[]) null).invoke(instance, //$NON-NLS-1$
                (Object[]) null);
        final String primaryGroupID = (String) c.getMethod("getPrimaryGroupID", (Class[]) null).invoke( //$NON-NLS-1$
                instance, (Object[]) null);
        final String[] groupIDs = (String[]) c.getMethod("getGroupIDs", (Class[]) null).invoke(instance, //$NON-NLS-1$
                (Object[]) null);
        final long impersonationToken = ((Long) c.getMethod("getImpersonationToken", (Class[]) null).invoke( //$NON-NLS-1$
                instance, (Object[]) null)).longValue();

        return new NTUserInfo(userName, domain, domainSID, userSID, primaryGroupID, groupIDs,
                impersonationToken);
    } catch (final Throwable t) {
        if (log.isDebugEnabled()) {
            log.debug("Unable to get NT user info", t); //$NON-NLS-1$
        }

        return null;
    }
}

From source file:net.jradius.packet.PacketFactory.java

private static RadiusPacket createPacket(Integer code) throws Exception {
    Class<?> c = (Class<?>) codeMap.get(code);
    if (c == null) {
        throw new RadiusException("bad radius code " + code);
    }// w w  w. java2  s  .c o m
    RadiusPacket p = (RadiusPacket) c.newInstance();
    // System.err.println("Created packet " + p.toString());
    return p;
}

From source file:com.aurel.track.screen.dashboard.bl.DashboardUtil.java

public static IPluginDashboard getPlugin(String className) {
    if (cachePlugins == null) {
        cachePlugins = new HashMap();
    }// w w w .  j a v  a 2s . c om
    Class pluginClass = (Class) cachePlugins.get(className);
    if (pluginClass == null) {
        try {
            pluginClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            LOGGER.error(ExceptionUtils.getStackTrace(e));
            return null;
        }
        cachePlugins.put(className, pluginClass);
    }
    try {
        IPluginDashboard plugin = (IPluginDashboard) pluginClass.newInstance();
        return plugin;
    } catch (InstantiationException e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    } catch (IllegalAccessException e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    return null;//plugin class problem
}

From source file:com.erudika.para.core.ParaObjectUtils.java

/**
 * Returns a map of the core data types.
 * @return a map of type plural - type singular form
 *//*from   w  w  w.  j av a 2s . c o  m*/
public static Map<String, String> getCoreTypes() {
    if (coreTypes.isEmpty()) {
        try {
            for (Class<? extends ParaObject> clazz : ParaObjectUtils.getCoreClassesMap().values()) {
                ParaObject p = clazz.newInstance();
                coreTypes.put(p.getPlural(), p.getType());
            }
        } catch (Exception ex) {
            logger.error(null, ex);
        }
    }
    return Collections.unmodifiableMap(coreTypes);
}

From source file:Main.java

/**
 * This method loads fragment in a backstack.
 * /*w  ww .  j av a 2 s  . c o m*/
 * @param fragmentActivity
 * @param fragmentContainerId
 * @param fragmentClass
 * @param bundle
 * @param tag
 * @return true if loaded successfully, false otherwise
 */
public static boolean loadFragmentInBackstack(FragmentActivity fragmentActivity, int fragmentContainerId,
        Class<? extends Fragment> fragmentClass, Bundle bundle, String tag) {
    // TODO Auto-generated method stub
    boolean status = false;
    try {
        FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                android.R.anim.fade_in, android.R.anim.fade_out);
        Fragment fragment = fragmentClass.newInstance();
        fragment.setArguments(bundle);
        fragmentTransaction.replace(fragmentContainerId, fragment, tag).addToBackStack(null).commit();
        // finish pending transactions
        fragmentManager.executePendingTransactions();
        status = true;
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return status;
}

From source file:com.bosscs.spark.commons.utils.Utils.java

/**
 * Creates a new instance of the given class.
 *
 * @param <T>   the type parameter
 * @param clazz the class object for which a new instance should be created.
 * @return the new instance of class clazz.
 *//* w ww . j  ava  2 s  . c om*/
public static <T extends IType> T newTypeInstance(Class<T> clazz) {
    try {
        return clazz.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        throw new GenericException(e);
    }
}