Search in sources :

Example 21 with Constructor

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);
}
Also used : Bc(jodd.introspector.tst.Bc) Constructor(java.lang.reflect.Constructor) Test(org.junit.Test)

Example 22 with Constructor

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);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) Constructor(java.lang.reflect.Constructor) CtorDescriptor(jodd.introspector.CtorDescriptor)

Example 23 with Constructor

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 24 with Constructor

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);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) MadvocException(jodd.madvoc.MadvocException) MadvocException(jodd.madvoc.MadvocException)

Example 25 with Constructor

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 + ")");
    }
}
Also used : Form(com.evolveum.midpoint.web.component.form.Form) Constructor(java.lang.reflect.Constructor) PageBase(com.evolveum.midpoint.gui.api.page.PageBase) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(com.evolveum.midpoint.util.exception.SystemException) CountableLoadableModel(com.evolveum.midpoint.gui.api.model.CountableLoadableModel) LoadableModel(com.evolveum.midpoint.gui.api.model.LoadableModel)

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