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:net.sf.ideais.ApplicationManager.java

/**
 * Load an application adapter./* ww  w .  j a va 2s. c o  m*/
 * 
 * @param clazz The class for the application adapter.
 * @param conf The configuration to be used to load the application adapter.
 * @return The application adapter or null if couldn't load any.
 */
private Application loadApplication(Class<? extends Application> clazz, Configuration conf) {
    Application app = null;
    log.debug("Loading " + clazz.getName() + " application adapter");

    try {
        Constructor<? extends Application> constructor = clazz.getConstructor(Configuration.class);
        app = constructor.newInstance(conf);
    } catch (NoSuchMethodException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
    }

    if (app == null) {
        log.error("Couldn't load " + clazz.getName() + " application adapter");
    } else {
        log.debug("Loaded application adapter " + clazz.getName());
    }

    return app;
}

From source file:com.alta189.cyborg.api.plugin.CommonPluginManager.java

public void registerPluginLoader(Class<? extends PluginLoader> loader) {
    PluginLoader instance = null;//  www.  j  av  a 2 s. c om

    try {
        Constructor<? extends PluginLoader> constructor = loader.getConstructor(new Class[] { Cyborg.class });

        instance = constructor.newInstance(cyborg);
    } catch (Exception e) {
        throw new IllegalArgumentException("Error registering plugin loader!", e);
    }

    synchronized (this) {
        for (Pattern pattern : instance.getPatterns()) {
            loaders.put(pattern, instance);
        }
    }
}

From source file:com.microrisc.simply.SimpleDeviceObjectFactory.java

private BaseDeviceObject createBaseDeviceObject(String networkId, String nodeId, Class implClass)
        throws Exception {
    // Parameter types of the constructor
    Class[] paramsTypes = new Class[] { String.class, String.class };

    // Find the constructor
    java.lang.reflect.Constructor constructor;
    constructor = implClass.getConstructor(paramsTypes);

    // Parameters for the constructor
    Object[] params = new Object[] { networkId, nodeId };
    BaseDeviceObject deviceObj = (BaseDeviceObject) constructor.newInstance(params);
    return deviceObj;
}

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

public Plot create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException {
    Class rendererClass = params.getClass(ChartParams.RENDERER);
    if (rendererClass == null || CategoryItemRenderer.class.isAssignableFrom(rendererClass)) {
        CategoryItemRenderer renderer;/* w w w. j av a2s .co  m*/
        if (rendererClass != null) {
            try {
                Constructor ctor = rendererClass.getConstructor(new Class[] {});
                renderer = (CategoryItemRenderer) ctor.newInstance(new Object[] {});
            } catch (Throwable e) {
                throw new GenerateException(e);
            }
        } else {
            renderer = new BarRenderer();
        }
        return CategoryPlotFactory.getInstance().create(dataSource, renderer, params);
    } else if (XYItemRenderer.class.isAssignableFrom(rendererClass)) {
        XYItemRenderer renderer;
        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 XYBarRenderer(); // will never run
        }
        ChartCustomizer.customizeXYItemRenderer(renderer, params);

        return XYPlotFactory.getInstance().create(dataSource, renderer, params);
    } else {
        throw new GenerateException("Incompatible renderer class: " + rendererClass);
    }
}

From source file:me.prokopyl.commandtools.commands.Commands.java

private void addCommand(Class<? extends Command> commandClass) {
    Constructor<? extends Command> constructor;
    try {//from w  w  w  .  j  a va 2s  .  c  o m
        constructor = commandClass.getConstructor(Commands.class);
        commands.add(constructor.newInstance(this));
    } catch (Exception ex) {
        PluginLogger.LogWarning("Exception while initializing command", ex);
    }
}

From source file:com.adaptris.core.services.jmx.ConstantValueTranslator.java

private Object convert(Object value, String type) throws CoreException {
    try {//from w  w w .  jav a2 s .co m
        Class<?> clazz = Class.forName(type);
        if (type.equals(String.class.getName()))
            return value;
        if (type.equals(Date.class.getName()))
            return new Date(Long.parseLong((String) value));
        else
            return clazz.getConstructor(String.class).newInstance(value);
    } catch (Exception e) {
        throw new CoreException(e);
    }
}

From source file:com.uwsoft.editor.view.ui.box.resourcespanel.UIAnimationsTabMediator.java

private void createAnimationResources(Set<String> strings, Class resourceClass,
        BiFunction<String, Vector2, Boolean> factoryFunction, String searchText) {
    for (String animationName : strings) {
        if (!animationName.contains(searchText))
            continue;
        try {/* w  ww  .  j  av  a  2s  .  co  m*/
            Constructor constructor = resourceClass.getConstructor(String.class);
            DraggableResource draggableResource = new DraggableResource(
                    (DraggableResourceView) constructor.newInstance(animationName));
            draggableResource.initDragDrop();
            draggableResource.setFactoryFunction(factoryFunction);
            animationBoxes.add(draggableResource);
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException
                | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.eclipse.winery.repository.Utils.java

/**
 * Returns the stored type for the given template
 *
 * Goes to the repository to retrieve stored data
 *
 * @param template the template to determine the type for
 *///from   w  w  w.j a v a2 s .c  om
// we suppress "unchecked" as we use Class.forName
@SuppressWarnings("unchecked")
public static TEntityType getTypeForTemplate(TEntityTemplate template) {
    QName type = template.getType();

    // Possibilities:
    // a) try all possibly types whether an appropriate QName exists
    // b) derive type class from template class. Determine appropriate resource afterwards.
    // We go for b)

    String instanceResourceClassName = template.getClass().toString();
    int idx = instanceResourceClassName.lastIndexOf('.');
    // get everything from ".T", where "." is the last dot
    instanceResourceClassName = instanceResourceClassName.substring(idx + 2);
    // strip off "Template"
    instanceResourceClassName = instanceResourceClassName.substring(0,
            instanceResourceClassName.length() - "Template".length());
    // add "Type"
    instanceResourceClassName += "Type";

    // an id is required to instantiate the resource
    String idClassName = "org.eclipse.winery.common.ids.definitions." + instanceResourceClassName + "Id";

    String packageName = "org.eclipse.winery.repository.resources.entitytypes."
            + instanceResourceClassName.toLowerCase() + "s";
    // convert from NodeType to NodeTypesResource
    instanceResourceClassName += "Resource";
    instanceResourceClassName = packageName + "." + instanceResourceClassName;

    Utils.LOGGER.debug("idClassName: {}", idClassName);
    Utils.LOGGER.debug("className: {}", instanceResourceClassName);

    // Get instance of id class having "type" as id
    Class<? extends TOSCAComponentId> idClass;
    try {
        idClass = (Class<? extends TOSCAComponentId>) Class.forName(idClassName);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Could not determine id class", e);
    }
    Constructor<? extends TOSCAComponentId> idConstructor;
    try {
        idConstructor = idClass.getConstructor(QName.class);
    } catch (NoSuchMethodException | SecurityException e) {
        throw new IllegalStateException("Could not get QName id constructor", e);
    }
    TOSCAComponentId typeId;
    try {
        typeId = idConstructor.newInstance(type);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        throw new IllegalStateException("Could not instantiate type", e);
    }

    // now instantiate the resource, where the type belongs to
    Class<? extends AbstractComponentInstanceResource> instanceResourceClass;
    try {
        instanceResourceClass = (Class<? extends AbstractComponentInstanceResource>) Class
                .forName(instanceResourceClassName);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException("Could not determine component instance resource class", e);
    }
    Constructor<? extends AbstractComponentInstanceResource> resConstructor;
    try {
        resConstructor = instanceResourceClass.getConstructor(typeId.getClass());
    } catch (NoSuchMethodException | SecurityException e) {
        throw new IllegalStateException("Could not get contructor", e);
    }
    AbstractComponentInstanceResource typeResource;
    try {
        typeResource = resConstructor.newInstance(typeId);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        throw new IllegalStateException("Could not instantiate resoruce", e);
    }

    // read the data from the resource and store it
    return (TEntityType) typeResource.getElement();
}

From source file:org.openhim.mediator.engine.MediatorRequestHandler.java

private void routeToActor(Class<? extends Actor> clazz, MediatorHTTPRequest request) {
    ActorRef actor = null;/*ww  w  .j  a v  a 2s.  c  o  m*/
    try {
        //can we pass the mediator config through?
        if (clazz.getConstructor(MediatorConfig.class) != null) {
            actor = getContext().actorOf(Props.create(clazz, config));
        }
    } catch (NoSuchMethodException | SecurityException ex) {
        //no matter. use default
        actor = getContext().actorOf(Props.create(clazz));
    }

    actor.tell(request, getSelf());
}

From source file:com.alta189.chavabot.plugin.CommonPluginManager.java

public void registerPluginLoader(Class<? extends PluginLoader> loader) {
    PluginLoader instance = null;/* w  w w.  jav  a 2 s.  c  om*/

    try {
        Constructor<? extends PluginLoader> constructor = loader
                .getConstructor(new Class[] { ChavaManager.class });

        instance = constructor.newInstance(new Object[] { chavamanager });
    } catch (Exception e) {
        throw new IllegalArgumentException("Error registering plugin loader!", e);
    }

    synchronized (this) {
        for (Pattern pattern : instance.getPatterns()) {
            loaders.put(pattern, instance);
        }
    }
}