Search in sources :

Example 36 with Constructor

use of java.lang.reflect.Constructor in project core-java by SpineEventEngine.

the class Tests method hasPrivateParameterlessCtor.

/**
     * Verifies if the passed class has private parameter-less constructor and invokes it
     * using Reflection.
     *
     * @return {@code true} if the class has private parameter-less constructor,
     *         {@code false} otherwise
     */
@CheckReturnValue
@VisibleForTesting
static boolean hasPrivateParameterlessCtor(Class<?> targetClass) {
    final Constructor constructor;
    try {
        constructor = targetClass.getDeclaredConstructor();
    } catch (NoSuchMethodException ignored) {
        return false;
    }
    if (!Modifier.isPrivate(constructor.getModifiers())) {
        return false;
    }
    constructor.setAccessible(true);
    //noinspection OverlyBroadCatchBlock
    try {
        // Call the constructor to include it into the coverage.
        // Some of the coding conventions may encourage throwing AssertionError
        // to prevent the instantiation of the target class,
        // if it is designed as a utility class.
        constructor.newInstance();
    } catch (Exception ignored) {
        return true;
    }
    return true;
}
Also used : Constructor(java.lang.reflect.Constructor) CheckReturnValue(javax.annotation.CheckReturnValue) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 37 with Constructor

use of java.lang.reflect.Constructor in project tdi-studio-se by Talend.

the class DefaultRunProcessService method createJavaProcessor.

/**
     * DOC xue Comment method "createJavaProcessor".
     *
     * @param process
     * @param filenameFromLabel
     * @return
     */
protected IProcessor createJavaProcessor(IProcess process, Property property, boolean filenameFromLabel) {
    boolean isTestContainer = false;
    if (GlobalServiceRegister.getDefault().isServiceRegistered(ITestContainerProviderService.class)) {
        ITestContainerProviderService testContainerService = (ITestContainerProviderService) GlobalServiceRegister.getDefault().getService(ITestContainerProviderService.class);
        if (testContainerService != null && property != null && property.getItem() != null) {
            isTestContainer = testContainerService.isTestContainerItem(property.getItem());
        }
    }
    if (isTestContainer) {
        return new MavenJavaProcessor(process, property, filenameFromLabel);
    }
    // check for ESB Runtime
    if (GlobalServiceRegister.getDefault().isServiceRegistered(IESBRunContainerService.class)) {
        IESBRunContainerService runContainerService = (IESBRunContainerService) GlobalServiceRegister.getDefault().getService(IESBRunContainerService.class);
        if (runContainerService != null) {
            IProcessor processor = runContainerService.createJavaProcessor(process, property, filenameFromLabel);
            if (processor != null) {
                return processor;
            }
        }
    }
    if (ComponentCategory.CATEGORY_4_MAPREDUCE.getName().equals(process.getComponentsType())) {
        return new MapReduceJavaProcessor(process, property, filenameFromLabel);
    } else if (ComponentCategory.CATEGORY_4_SPARK.getName().equals(process.getComponentsType())) {
        return new SparkJavaProcessor(process, property, filenameFromLabel);
    } else if (ComponentCategory.CATEGORY_4_STORM.getName().equals(process.getComponentsType())) {
        return new StormJavaProcessor(process, property, filenameFromLabel);
    } else if (ComponentCategory.CATEGORY_4_SPARKSTREAMING.getName().equals(process.getComponentsType())) {
        return new SparkJavaProcessor(process, property, filenameFromLabel);
    } else if (ComponentCategory.CATEGORY_4_CAMEL.getName().equals(process.getComponentsType())) {
        Bundle bundle = Platform.getBundle(PluginChecker.EXPORT_ROUTE_PLUGIN_ID);
        if (bundle != null) {
            try {
                Class camelJavaProcessor = bundle.loadClass("org.talend.resources.export.maven.runprocess.CamelJavaProcessor");
                Constructor constructor = camelJavaProcessor.getConstructor(IProcess.class, Property.class, boolean.class);
                return (MavenJavaProcessor) constructor.newInstance(process, property, filenameFromLabel);
            } catch (Exception e) {
                ExceptionHandler.process(e);
            }
        }
        return new MavenJavaProcessor(process, property, filenameFromLabel);
    } else {
        return new MavenJavaProcessor(process, property, filenameFromLabel);
    }
}
Also used : StormJavaProcessor(org.talend.designer.runprocess.storm.StormJavaProcessor) MavenJavaProcessor(org.talend.designer.runprocess.maven.MavenJavaProcessor) MapReduceJavaProcessor(org.talend.designer.runprocess.mapreduce.MapReduceJavaProcessor) Bundle(org.osgi.framework.Bundle) Constructor(java.lang.reflect.Constructor) SparkJavaProcessor(org.talend.designer.runprocess.spark.SparkJavaProcessor) ITestContainerProviderService(org.talend.core.ui.ITestContainerProviderService) CoreException(org.eclipse.core.runtime.CoreException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 38 with Constructor

use of java.lang.reflect.Constructor in project tdi-studio-se by Talend.

the class TalendFlyoutPaletteComposite method createActionMenu.

private Menu createActionMenu(Control parent) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
    final MenuManager manager = new MenuManager();
    MenuManager mgr = new MenuManager(PaletteMessages.DOCK_LABEL);
    changeDockActionClass.getDeclaredConstructors();
    // reflect the constructor of ChangeDockAction
    Constructor changeDockActionConstructor = changeDockActionClass.getDeclaredConstructor(FlyoutPaletteComposite.class, String.class, int.class);
    changeDockActionConstructor.setAccessible(true);
    // instance of ChangeDockAction for left
    Object changeDockActionInstance = changeDockActionConstructor.newInstance(this, PaletteMessages.LEFT_LABEL, PositionConstants.WEST);
    if (changeDockActionInstance instanceof IAction) {
        mgr.add((IAction) changeDockActionInstance);
    }
    // instance of ChangeDockAction for right
    changeDockActionInstance = changeDockActionConstructor.newInstance(this, PaletteMessages.RIGHT_LABEL, PositionConstants.EAST);
    if (changeDockActionInstance instanceof IAction) {
        mgr.add((IAction) changeDockActionInstance);
    }
    // instance of ResizeAction
    Constructor resizeActionConstructor = resizeActionClass.getDeclaredConstructor(FlyoutPaletteComposite.class);
    resizeActionConstructor.setAccessible(true);
    Object resizeActionInstance = resizeActionConstructor.newInstance(this);
    if (resizeActionInstance instanceof IAction) {
        mgr.add((IAction) resizeActionInstance);
    }
    manager.add(mgr);
    mgr.addMenuListener(new IMenuListener() {

        @Override
        public void menuAboutToShow(IMenuManager menuMgr) {
            IContributionItem[] items = menuMgr.getItems();
            for (IContributionItem item : items) {
                ((ActionContributionItem) item).update();
            }
        }
    });
    //
    // ShowStandardAction showStandardAction = ShowStandardAction.getInstance();
    // ShowFavoriteAction showFavoriteAction = ShowFavoriteAction.getInstance();
    // manager.add(showStandardAction);
    // manager.add(showFavoriteAction);
    // if (ShowFavoriteAction.state) {
    // showStandardAction.doSetEnable();
    // }
    OpenPaletteFilterAction openPaletteFilterAction = OpenPaletteFilterAction.getInstance();
    manager.add(openPaletteFilterAction);
    manager.add(mgr);
    addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            manager.dispose();
        }
    });
    return manager.createContextMenu(this);
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) IAction(org.eclipse.jface.action.IAction) OpenPaletteFilterAction(org.talend.repository.ui.actions.OpenPaletteFilterAction) Constructor(java.lang.reflect.Constructor) IContributionItem(org.eclipse.jface.action.IContributionItem) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) DisposeEvent(org.eclipse.swt.events.DisposeEvent) IMenuListener(org.eclipse.jface.action.IMenuListener)

Example 39 with Constructor

use of java.lang.reflect.Constructor in project voltdb by VoltDB.

the class JDBCSQLXML method createStAXSource.

/**
     * Retrieves a new StAXSource for reading the XML value designated by this
     * SQLXML instance. <p>
     *
     * @param sourceClass The class of the source
     * @throws java.sql.SQLException if there is an error processing the XML
     *      value or if the given <tt>sourceClass</tt> is not supported.
     * @return a new StAXSource for reading the XML value designated by this
     *      SQLXML instance
     */
@SuppressWarnings("unchecked")
protected <T extends Source> T createStAXSource(Class<T> sourceClass) throws SQLException {
    StAXSource source = null;
    Constructor sourceCtor = null;
    Reader reader = null;
    XMLInputFactory factory = null;
    XMLEventReader eventReader = null;
    try {
        factory = XMLInputFactory.newInstance();
    } catch (FactoryConfigurationError ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    try {
        sourceCtor = (sourceClass == null) ? StAXSource.class.getConstructor(XMLEventReader.class) : sourceClass.getConstructor(XMLEventReader.class);
    } catch (SecurityException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (NoSuchMethodException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    reader = getCharacterStreamImpl();
    try {
        eventReader = factory.createXMLEventReader(reader);
    } catch (XMLStreamException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    try {
        source = (StAXSource) sourceCtor.newInstance(eventReader);
    } catch (SecurityException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (IllegalArgumentException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (InvocationTargetException ex) {
        throw Exceptions.sourceInstantiation(ex.getTargetException());
    } catch (ClassCastException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    return (T) source;
}
Also used : Constructor(java.lang.reflect.Constructor) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) XMLEventReader(javax.xml.stream.XMLEventReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) StAXSource(javax.xml.transform.stax.StAXSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventReader(javax.xml.stream.XMLEventReader) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 40 with Constructor

use of java.lang.reflect.Constructor in project voltdb by VoltDB.

the class JDBCSQLXML method createStAXResult.

/**
     *  Retrieves a new DOMResult for setting the XML value designated by this
     *  SQLXML instance.
     *
     *  @param resultClass The class of the result, or null.
     *  @throws java.sql.SQLException if there is an error processing the XML
     *          value
     *  @return for setting the XML value designated by this SQLXML instance.
     */
@SuppressWarnings("unchecked")
protected <T extends Result> T createStAXResult(Class<T> resultClass) throws SQLException {
    StAXResult result = null;
    OutputStream outputStream = this.setBinaryStreamImpl();
    Constructor ctor;
    XMLOutputFactory factory;
    XMLStreamWriter xmlStreamWriter;
    try {
        factory = XMLOutputFactory.newInstance();
        xmlStreamWriter = factory.createXMLStreamWriter(outputStream);
        if (resultClass == null) {
            result = new StAXResult(xmlStreamWriter);
        } else {
            ctor = resultClass.getConstructor(XMLStreamWriter.class);
            result = (StAXResult) ctor.newInstance(xmlStreamWriter);
        }
    } catch (SecurityException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (IllegalArgumentException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (InvocationTargetException ex) {
        throw Exceptions.resultInstantiation(ex.getTargetException());
    } catch (FactoryConfigurationError ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (NoSuchMethodException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (XMLStreamException ex) {
        throw Exceptions.resultInstantiation(ex);
    }
    return (T) result;
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Constructor(java.lang.reflect.Constructor) GZIPOutputStream(java.util.zip.GZIPOutputStream) ClosableByteArrayOutputStream(org.hsqldb_voltpatches.lib.ClosableByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError)

Aggregations

Constructor (java.lang.reflect.Constructor)1311 InvocationTargetException (java.lang.reflect.InvocationTargetException)281 Method (java.lang.reflect.Method)252 IOException (java.io.IOException)128 Field (java.lang.reflect.Field)111 ArrayList (java.util.ArrayList)106 Test (org.junit.Test)92 DOMTestDocumentBuilderFactory (org.w3c.domts.DOMTestDocumentBuilderFactory)74 JUnitTestSuiteAdapter (org.w3c.domts.JUnitTestSuiteAdapter)73 List (java.util.List)61 JAXPDOMTestDocumentBuilderFactory (org.w3c.domts.JAXPDOMTestDocumentBuilderFactory)58 Map (java.util.Map)50 Type (java.lang.reflect.Type)39 Annotation (java.lang.annotation.Annotation)38 HashMap (java.util.HashMap)38 HashSet (java.util.HashSet)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)31 ParameterizedType (java.lang.reflect.ParameterizedType)30 File (java.io.File)20 URL (java.net.URL)20