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.axiomine.largecollections.utilities.KryoUtils.java

public static void registerDefaultKryoClasses(Kryo kryo) throws Exception {
    final Properties props = new Properties();
    props.load(KryoUtils.class.getClassLoader().getResourceAsStream("KryoRegistration.properties"));
    Set ks = props.keySet();/*w w w  . j  a  v a 2 s . com*/
    for (Object k : ks) {
        //            System.out.println(k);
        Class c = Class.forName((String) k);
        Class s = Class.forName(props.getProperty((String) k));
        kryo.register(c, (Serializer) s.newInstance());
    }

}

From source file:com.axiomine.largecollections.utilities.KryoUtils.java

public static void registerKryoClasses(Kryo kryo, String propFile) throws Exception {
    FileReader fReader = new FileReader(new File(propFile));
    Properties props = new Properties();
    props.load(fReader);//from www  . ja  v a 2s.co m
    Set ks = props.keySet();
    for (Object k : ks) {
        Class c = Class.forName((String) k);
        Class s = Class.forName(props.getProperty((String) k));
        Serializer ss = (Serializer) s.newInstance();
        kryo.register(c, ss);
    }

}

From source file:org.frontcache.hystrix.fr.FallbackResolverFactory.java

/**
 * //from  www.  ja  va 2 s .  c o m
 * @return
 */
private static FallbackResolver getFallbackResolver() {
    String implStr = FCConfig.getProperty("front-cache.fallback-resolver.impl");
    if (null == implStr) {
        logger.info("Default implementation is loaded: " + FileBasedFallbackResolver.class.getCanonicalName());
        return new FileBasedFallbackResolver();
    }

    try {

        @SuppressWarnings("rawtypes")
        Class clazz = Class.forName(implStr);
        Object obj = clazz.newInstance();
        if (null != obj && obj instanceof FallbackResolver) {
            logger.info("FallbackResolver implementation loaded: " + implStr);
            FallbackResolver fallbackResolver = (FallbackResolver) obj;

            return fallbackResolver;
        }
    } catch (Exception ex) {
        logger.error("Cant instantiate " + implStr + ". Default implementation is loaded: "
                + FileBasedFallbackResolver.class.getCanonicalName());
        return new FileBasedFallbackResolver();
    }

    return new FileBasedFallbackResolver();
}

From source file:PoolServerBase.java

static protected boolean init(Class clobj, int threads) {
    try {//from  w  w w  . j  ava 2s .  c o m
        for (int i = 0; i < threads; i++) {
            PoolServerBase thread = (PoolServerBase) clobj.newInstance();
            thread.next = head;
            head = thread;
            thread.start();
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static <T> T converMap2Object(Map<String, String> map, Class<T> cls) {

    Field[] fields = cls.getDeclaredFields();

    T t = null;/*from   w  ww. j a v a  2 s. co m*/
    try {
        t = (T) cls.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    for (Field f : fields) {
        f.setAccessible(true);
        invokeSet(t, f.getName(), map.get(f.getName()));
    }
    return t;
}

From source file:controllers.modules.opinionMiners.base.OpinionMiner.java

public static <T extends OpinionMiner> T createSubMiner(String code, Class<T> minerBase) {
    Class<? extends T> minerClass = findSubMiner(code, minerBase);
    try {//from   w  ww  .  ja  va 2s.c  o  m
        return minerClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        return null;
    }
}

From source file:dk.statsbiblioteket.doms.surveillance.surveyor.SurveyorFactory.java

/**
 * Get the surveyor singleton instance. As this produces a singleton,
 * a new instance will only be generated on the first call, after this the
 * same instance will be returned. If the configuration that defines the
 * implementing class is changed, though, a new instance of the new class
 * will be produced. This method is synchronized.
 *
 * @return Surveyor singleton instance./*w w w.ja va 2s  . c om*/
 *
 * @throws SurveyorInstantiationException on trouble instantiating the
 * singleton.
 */
public static synchronized Surveyor getSurveyor() throws SurveyorInstantiationException {
    log.trace("Enter getSurveyor");
    String implementation = ConfigCollection.getProperties().getProperty(SURVEYORCLASS_CONFIGURATION_PARAMETER);
    if (implementation == null || implementation.equals("")) {
        implementation = DEFAULT_IMPLEMENTATION;
    }
    if ((surveyor == null) || !surveyor.getClass().getName().equals(implementation)) {
        log.info("Initializing surveyor class '" + implementation + "'");
        try {
            Class surveyorClass = Class.forName(implementation);
            surveyor = (Surveyor) surveyorClass.newInstance();
        } catch (Exception e) {
            throw new SurveyorInstantiationException(
                    "Cannot instantiate Surveyor class '" + implementation + "': " + e.getMessage(), e);
        }
    }
    return surveyor;
}

From source file:Main.java

public static int getStatusHeight(Activity activity) {
    int statusHeight = 0;
    Rect localRect = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
    statusHeight = localRect.top;/*from   w  ww . j a  v a2 s  .  c o  m*/
    if (0 == statusHeight) {
        Class<?> localClass;
        try {
            localClass = Class.forName("com.android.internal.R$dimen");
            Object localObject = localClass.newInstance();
            int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
            statusHeight = activity.getResources().getDimensionPixelSize(i5);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    return statusHeight;
}

From source file:cn.wanghaomiao.seimi.core.SeimiBeanResolver.java

public static <T> T parse(Class<T> target, String text) throws Exception {
    T bean = target.newInstance();
    final List<Field> props = new LinkedList<>();
    ReflectionUtils.doWithFields(target, new ReflectionUtils.FieldCallback() {
        @Override// www.java 2s  .  c o  m
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            props.add(field);
        }
    });
    JXDocument jxDocument = new JXDocument(text);
    for (Field f : props) {
        Xpath xpathInfo = f.getAnnotation(Xpath.class);
        if (xpathInfo != null) {
            String xpath = xpathInfo.value();
            List<Object> res = jxDocument.sel(xpath);
            boolean accessFlag = f.isAccessible();
            f.setAccessible(true);
            f.set(bean, defaultCastToTargetValue(target, f, res));
            f.setAccessible(accessFlag);
        }
    }
    return bean;
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.ConceptSearchServiceUtils.java

public static List<Concept> getSearchResults(ServletContext context, VitroRequest vreq) throws Exception {
    String searchServiceName = getSearchServiceUri(vreq);
    String searchServiceClassName = getConceptSearchServiceClassName(searchServiceName);

    ExternalConceptService conceptServiceClass = null;

    Object object = null;/*  w  w w.  j a v  a  2s .com*/
    try {
        Class classDefinition = Class.forName(searchServiceClassName);
        object = classDefinition.newInstance();
        conceptServiceClass = (ExternalConceptService) object;
    } catch (InstantiationException e) {
        System.out.println(e);
    } catch (IllegalAccessException e) {
        System.out.println(e);
    } catch (ClassNotFoundException e) {
        System.out.println(e);
    }

    if (conceptServiceClass == null) {
        log.error("could not find Concept Search Class for " + searchServiceName);
        return null;
    }

    //Get search
    String searchTerm = getSearchTerm(vreq);
    List<Concept> conceptResults = conceptServiceClass.getConcepts(searchTerm);
    return conceptResults;
}