protected Object createWidget()

in jelly-tags/swt/src/main/java/org/apache/commons/jelly/tags/swt/WidgetTag.java [230:271]


    protected Object createWidget(Class theClass, Widget parent, int style)
        throws JellyTagException {
        if (theClass == null) {
            throw new JellyTagException("No Class available to create the new widget");
        }

        try {
            if (parent == null) {
                // lets try call a constructor with a single style
                Class[] types = { int.class };
                Constructor constructor = theClass.getConstructor(types);
                if (constructor != null) {
                    Object[] arguments = { new Integer(style)};
                    return constructor.newInstance(arguments);
                }
            } else {
                // lets try to find the constructor with 2 arguments with the 2nd argument being an int
                Constructor[] constructors = theClass.getConstructors();
                if (constructors != null) {
                    for (int i = 0, size = constructors.length; i < size; i++) {
                        Constructor constructor = constructors[i];
                        Class[] types = constructor.getParameterTypes();
                        if (types.length == 2 && types[1].isAssignableFrom(int.class)) {
                            if (types[0].isAssignableFrom(parent.getClass())) {
                                Object[] arguments = { parent, new Integer(style)};
                                return constructor.newInstance(arguments);
                            }
                        }
                    }
                }
            }
            return theClass.getConstructor().newInstance();
        } catch (NoSuchMethodException e) {
            throw new JellyTagException(e);
        } catch (InstantiationException e) {
            throw new JellyTagException(e);
        } catch (IllegalAccessException e) {
            throw new JellyTagException(e);
        } catch (InvocationTargetException e) {
            throw new JellyTagException(e);
        }
    }