use of java.lang.reflect.Constructor in project jodd by oblac.
the class IntrospectorTest method testCtors.
@Test
public void testCtors() {
ClassDescriptor cd = ClassIntrospector.lookup(Ac.class);
CtorDescriptor[] ctors = cd.getAllCtorDescriptors();
int c = 0;
for (CtorDescriptor ctor : ctors) {
if (ctor.isPublic())
c++;
}
assertEquals(1, c);
ctors = cd.getAllCtorDescriptors();
assertEquals(2, ctors.length);
assertNotNull(cd.getDefaultCtorDescriptor(true));
assertNull(cd.getDefaultCtorDescriptor(false));
Constructor ctor = cd.getCtorDescriptor(new Class[] { Integer.class }, true).getConstructor();
assertNotNull(ctor);
cd = ClassIntrospector.lookup(Bc.class);
ctors = cd.getAllCtorDescriptors();
c = 0;
for (CtorDescriptor ccc : ctors) {
if (ccc.isPublic())
c++;
}
assertEquals(1, c);
ctors = cd.getAllCtorDescriptors();
assertEquals(1, ctors.length);
assertNull(cd.getDefaultCtorDescriptor(false));
assertNull(cd.getDefaultCtorDescriptor(true));
CtorDescriptor ctorDescriptor = cd.getCtorDescriptor(new Class[] { Integer.class }, true);
assertNull(ctorDescriptor);
ctor = cd.getCtorDescriptor(new Class[] { String.class }, true).getConstructor();
assertNotNull(ctor);
}
use of java.lang.reflect.Constructor in project jodd by oblac.
the class PetiteBeans method registerPetiteCtorInjectionPoint.
// ---------------------------------------------------------------- injection points
/**
* Registers constructor injection point.
*
* @param beanName bean name
* @param paramTypes constructor parameter types, may be <code>null</code>
* @param references references for arguments
*/
public void registerPetiteCtorInjectionPoint(String beanName, Class[] paramTypes, String[] references) {
BeanDefinition beanDefinition = lookupExistingBeanDefinition(beanName);
String[][] ref = PetiteUtil.convertRefToReferences(references);
ClassDescriptor cd = ClassIntrospector.lookup(beanDefinition.type);
Constructor constructor = null;
if (paramTypes == null) {
CtorDescriptor[] ctors = cd.getAllCtorDescriptors();
if (ctors != null && ctors.length > 0) {
if (ctors.length > 1) {
throw new PetiteException(ctors.length + " suitable constructor found as injection point for: " + beanDefinition.type.getName());
}
constructor = ctors[0].getConstructor();
}
} else {
CtorDescriptor ctorDescriptor = cd.getCtorDescriptor(paramTypes, true);
if (ctorDescriptor != null) {
constructor = ctorDescriptor.getConstructor();
}
}
if (constructor == null) {
throw new PetiteException("Constructor not found: " + beanDefinition.type.getName());
}
beanDefinition.ctor = injectionPointFactory.createCtorInjectionPoint(constructor, ref);
}
use of java.lang.reflect.Constructor in project jodd by oblac.
the class ActionRequest method createActionMethodArgument.
/**
* Creates action method arguments.
*/
@SuppressWarnings({ "unchecked", "NullArgumentToVariableArgMethod" })
protected Object createActionMethodArgument(Class type) {
try {
if (type.getEnclosingClass() == null || Modifier.isStatic(type.getModifiers())) {
// regular or static class
Constructor ctor = type.getDeclaredConstructor(null);
ctor.setAccessible(true);
return ctor.newInstance();
} else {
// member class
Constructor ctor = type.getDeclaredConstructor(type.getDeclaringClass());
ctor.setAccessible(true);
return ctor.newInstance(action);
}
} catch (Exception ex) {
throw new MadvocException(ex);
}
}
use of java.lang.reflect.Constructor in project jodd by oblac.
the class Target method createValueInstance.
/**
* Creates new instance of a type and stores it in the value.
*/
@SuppressWarnings({ "unchecked", "NullArgumentToVariableArgMethod" })
protected void createValueInstance() {
try {
Constructor ctor = type.getDeclaredConstructor(null);
ctor.setAccessible(true);
value = ctor.newInstance();
} catch (Exception ex) {
throw new MadvocException(ex);
}
}
use of java.lang.reflect.Constructor in project midpoint by Evolveum.
the class FocusMainPanel method createTabPanel.
protected WebMarkupContainer createTabPanel(String panelId, FormSpecificationType formSpecificationType, PageAdminObjectDetails<F> parentPage) {
String panelClassName = formSpecificationType.getPanelClass();
Class<?> panelClass;
try {
panelClass = Class.forName(panelClassName);
} catch (ClassNotFoundException e) {
throw new SystemException("Panel class '" + panelClassName + "' as specified in admin GUI configuration was not found", e);
}
if (AbstractFocusTabPanel.class.isAssignableFrom(panelClass)) {
Constructor<?> constructor;
try {
constructor = panelClass.getConstructor(String.class, Form.class, LoadableModel.class, LoadableModel.class, LoadableModel.class, PageBase.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new SystemException("Unable to locate constructor (String,Form,LoadableModel,LoadableModel,LoadableModel,PageBase) in " + panelClass + ": " + e.getMessage(), e);
}
AbstractFocusTabPanel<F> tabPanel;
try {
tabPanel = (AbstractFocusTabPanel<F>) constructor.newInstance(panelId, getMainForm(), getObjectModel(), assignmentsModel, projectionModel, parentPage);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new SystemException("Error instantiating " + panelClass + ": " + e.getMessage(), e);
}
return tabPanel;
} else if (AbstractObjectTabPanel.class.isAssignableFrom(panelClass)) {
Constructor<?> constructor;
try {
constructor = panelClass.getConstructor(String.class, Form.class, LoadableModel.class, PageBase.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new SystemException("Unable to locate constructor (String,Form,LoadableModel,PageBase) in " + panelClass + ": " + e.getMessage(), e);
}
AbstractObjectTabPanel<F> tabPanel;
try {
tabPanel = (AbstractObjectTabPanel<F>) constructor.newInstance(panelId, getMainForm(), getObjectModel(), parentPage);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new SystemException("Error instantiating " + panelClass + ": " + e.getMessage(), e);
}
return tabPanel;
} else {
throw new UnsupportedOperationException("Tab panels that are not subclasses of AbstractObjectTabPanel or AbstractFocusTabPanel are not supported yet (got " + panelClass + ")");
}
}
Aggregations