Example usage for java.lang Class getConstructor

List of usage examples for java.lang Class getConstructor

Introduction

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

Prototype

@CallerSensitive
public Constructor<T> getConstructor(Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

Usage

From source file:org.jboss.arquillian.spring.integration.javaconfig.container.AnnotationRemoteApplicationContextProducer.java

/**
 * <p>Retrieves the constructor of the specified type.</p>
 *
 * @param type           the class type for which the constructor will be retrieved
 * @param parameterTypes the types of the parameters
 * @param <T>            the type of the application context
 *
 * @return the retrieved constructor//from  w ww .  j a  v a  2s . c  o m
 */
private <T extends ApplicationContext> Constructor<T> getConstructor(Class<T> type, Class... parameterTypes) {

    try {
        return type.getConstructor(parameterTypes);
    } catch (NoSuchMethodException e) {

        throw new RuntimeException("Could not find a proper constructor for type: " + type.getName(), e);
    }
}

From source file:com.xpn.xwiki.plugin.charts.plots.PiePlotFactory.java

public Plot create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
    DefaultPieDataset dataset = new DefaultPieDataset();
    String dataSeries = params.getString(ChartParams.SERIES);
    if (dataSeries.equals("columns")) {
        for (int row = 0; row < dataSource.getRowCount(); row++) {
            if (dataSource.hasHeaderColumn()) {
                String category = dataSource.getHeaderColumnValue(row);
                dataset.setValue(category, dataSource.getCell(row, 0));
            } else {
                dataset.setValue("Category " + (row + 1), dataSource.getCell(row, 0));
            }//  w  w w  . j  a v a2s.  c  o m
        }
    } else if (dataSeries.equals("rows")) {
        for (int column = 0; column < dataSource.getColumnCount(); column++) {
            if (dataSource.hasHeaderRow()) {
                String category = dataSource.getHeaderRowValue(column);
                dataset.setValue(category, dataSource.getCell(0, column));
            } else {
                dataset.setValue("Category " + (column + 1), dataSource.getCell(0, column));
            }
        }
    } else {
        throw new GenerateException("Invalid series parameter:" + dataSeries);
    }

    Class plotClass = params.getClass(ChartParams.RENDERER);
    PiePlot plot;
    if (plotClass != null) {
        try {
            Constructor ctor = plotClass.getConstructor(new Class[] { PieDataset.class });
            plot = (PiePlot) ctor.newInstance(new Object[] { dataset });
        } catch (Throwable e) {
            throw new GenerateException(e);
        }
    } else {
        plot = new PiePlot(dataset);
    }
    ChartCustomizer.customizePiePlot(plot, params);
    return plot;
}

From source file:com.xpn.xwiki.plugin.charts.plots.TimePlotFactory.java

public Plot create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {

    Class rendererClass = params.getClass(ChartParams.RENDERER);
    XYItemRenderer renderer;//from www.  ja  v a2  s . c om
    if (rendererClass != null) {
        try {
            Constructor ctor = rendererClass.getConstructor(new Class[] {});
            renderer = (XYItemRenderer) ctor.newInstance(new Object[] {});
        } catch (Throwable e) {
            throw new GenerateException(e);
        }
    } else {
        renderer = new XYLineAndShapeRenderer();
    }
    ChartCustomizer.customizeXYItemRenderer(renderer, params);

    DateAxis domainAxis = new DateAxis();
    ChartCustomizer.customizeDateAxis(domainAxis, params, ChartParams.AXIS_DOMAIN_PREFIX);

    NumberAxis rangeAxis = new NumberAxis();
    rangeAxis.setAutoRangeIncludesZero(false); // override default
    ChartCustomizer.customizeNumberAxis(rangeAxis, params, ChartParams.AXIS_RANGE_PREFIX);

    XYDataset dataset = TimeSeriesCollectionFactory.getInstance().create(dataSource, params);

    XYPlot plot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

    ChartCustomizer.customizeXYPlot(plot, params);

    return plot;
}

From source file:org.openwms.core.aop.FireAfterTransactionAspect.java

/**
 * Only {@link ApplicationEvent}s are created and published over Springs
 * {@link ApplicationContext}./*from   w w  w.  ja  v a  2  s . com*/
 * 
 * @param publisher
 *            The instance that is publishing the event
 * @param events
 *            A list of event classes to fire
 * @throws Exception
 *             Any exception is re-thrown
 */
public void fireEvent(Object publisher, FireAfterTransaction events) throws Exception {
    for (int i = 0; i < events.events().length; i++) {
        Class<? extends EventObject> event = events.events()[i];
        if (ApplicationEvent.class.isAssignableFrom(event)) {
            ctx.publishEvent((ApplicationEvent) event.getConstructor(Object.class).newInstance(publisher));
        }
    }
}

From source file:com.hp.application.automation.tools.octane.tests.xml.TestResultXmlWriter.java

private XMLStreamWriter possiblyCreateIndentingWriter(XMLStreamWriter writer) {
    try {//from www .  j a  v  a 2 s.c o  m
        Class<?> clazz = Class.forName("com.sun.xml.txw2.output.IndentingXMLStreamWriter");
        XMLStreamWriter xmlStreamWriter = (XMLStreamWriter) clazz.getConstructor(XMLStreamWriter.class)
                .newInstance(writer);
        clazz.getMethod("setIndentStep", String.class).invoke(xmlStreamWriter, " ");
        return xmlStreamWriter;
    } catch (Exception e) {
        // do without indentation
        return writer;
    }
}

From source file:net.joinedminds.masserr.util.IdentifiableBeanConverter.java

@Override
public Object convert(Class type, Object value) {
    if (type.isAssignableFrom(value.getClass())) {
        return value;
    }//from  www  .jav a2  s  .  c o  m
    Constructor constructor;
    try {
        try {
            constructor = type.getConstructor(value.getClass());
            return constructor.newInstance(value);
        } catch (NoSuchMethodException e) {
            try {
                constructor = type.getConstructor(String.class);
                return constructor.newInstance(value.toString());
            } catch (NoSuchMethodException e1) {
                throw new ConversionException("Could not find single parameter constructor.", e1);
            }
        }
    } catch (Exception e) {
        throw new ConversionException("Could not construct " + type.getName(), e);
    }
}

From source file:com.swordlord.gozer.ui.gozerframe.GozerFrameLink.java

@Override
public void onClick() {
    IGozerFrameExtension gfe = null;//  w  w w.  j  a v  a 2  s . c  o  m

    try {
        Class<? extends IGozerFrameExtension> clazz = getGozerFrameExtension();
        Constructor<? extends IGozerFrameExtension> c = clazz
                .getConstructor(new Class[] { IGozerSessionInfo.class });
        gfe = c.newInstance(new Object[] { _sessionInfo });
    } catch (InvocationTargetException e) {
        LOG.error(e.getCause());
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        LOG.error(e.getCause());
        e.printStackTrace();
    } catch (InstantiationException e) {
        LOG.error(e.getCause());
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        LOG.error(e.getCause());
        e.printStackTrace();
    } catch (SecurityException e) {
        LOG.error(e.getCause());
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        LOG.error(e.getCause());
        e.printStackTrace();
    }

    GozerPage page = null;

    if (_clazz != null) {
        page = GozerPage.getFrame(gfe, _clazz);
    } else {
        page = GozerPage.getFrame(gfe);
    }

    if (page != null) {
        setResponsePage(page);
    } else {
        LOG.error("GozerFile does not exist, is null or empty: " + gfe.getGozerLayoutFileName());
    }
}

From source file:edu.umn.msi.tropix.common.reflect.ReflectionHelperImpl.java

public <C> Constructor<C> getConstructor(final Class<C> clazz, final Class<?>... args) {
    Constructor<C> constructor = null;
    try {//from   w  w  w  .  j  a  v a  2s .co m
        constructor = clazz.getConstructor(args);
    } catch (final Exception e) {
        throw new ReflectionRuntimeException(e);
    }
    return constructor;
}

From source file:com.puppycrawl.tools.checkstyle.checks.indentation.HandlerFactory.java

/**
 * registers a handler// ww  w.  j ava 2s .  co  m
 *
 * @param type
 *                type from TokenTypes
 * @param handlerClass
 *                the handler to register
 */
private void register(int type, Class<?> handlerClass) {
    try {
        final Constructor<?> ctor = handlerClass
                .getConstructor(new Class[] { IndentationCheck.class, DetailAST.class, // current AST
                        ExpressionHandler.class, // parent
        });
        typeHandlers.put(type, ctor);
    }
    ///CLOVER:OFF
    catch (final NoSuchMethodException e) {
        throw new RuntimeException("couldn't find ctor for " + handlerClass);
    } catch (final SecurityException e) {
        LOG.debug("couldn't find ctor for " + handlerClass, e);
        throw new RuntimeException("couldn't find ctor for " + handlerClass);
    }
    ///CLOVER:ON
}

From source file:org.openspaces.rest.space.SpacePojoReadAPIController.java

/**
 * REST ReadById by query request handler - readMultiple
 * //  w  w  w  .  j  av a  2s  . c o  m
 */
@RequestMapping(value = "/readById", method = RequestMethod.GET)
public ModelAndView readById(@RequestParam String spaceName, @RequestParam String locators,
        @RequestParam String classname, @RequestParam String id, @RequestParam String idClass,
        @RequestParam String routing, @RequestParam String routingClass, HttpServletResponse response)
        throws Exception {

    log.fine(String.format("readById called params: classname=%s id=%s idClass=%s routing=%s routingClass=%s",
            classname, id, idClass, routing, routingClass));

    Class<?> _valueClass = Class.forName(classname);
    Class<?> _idClass = Class.forName(idClass);
    Class<?> _routingClass = Class.forName(routingClass);

    Object idobj = _idClass.getConstructor(String.class).newInstance(id);
    Object routingobj = _routingClass.getConstructor(String.class).newInstance(routing);
    GigaSpace gigaSpace = ControllerUtils.xapCache.get(spaceName, locators);
    log.fine(String.format("reading: gigaSpace=%s _valueClass=%s idobj=%s routingobj=%s", gigaSpace,
            _valueClass, idobj, routingobj));
    Object obj = gigaSpace.readById(_valueClass, idobj, routingobj);

    ModelAndView mv = new ModelAndView("jsonView");
    if (obj != null) {
        mv.addObject(id, obj);
    }
    response.setHeader("Access-Control-Allow-Origin", "*");
    return mv;
}