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;
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations