Package org.apache.tapestry.ioc.services

Examples of org.apache.tapestry.ioc.services.ClassFab


    }

    @Test
    public void add_constructor_from_base_class() throws Exception
    {
        ClassFab cf = newClassFab("MyIntHolder", AbstractIntWrapper.class);

        cf.addField("_intValue", int.class);
        cf.addConstructor(new Class[]{int.class}, null, "{ _intValue = $1; }");

        cf.addMethod(Modifier.PUBLIC, new MethodSignature(int.class, "getIntValue", null, null), "return _intValue;");

        Class targetClass = cf.createClass();
        Constructor c = targetClass.getConstructors()[0];

        AbstractIntWrapper targetBean = (AbstractIntWrapper) c.newInstance(new Object[]{new Integer(137)});

        assertEquals(targetBean.getIntValue(), 137);
View Full Code Here


    }

    @Test
    public void invalid_super_class() throws Exception
    {
        ClassFab cf = newClassFab("InvalidSuperClass", List.class);

        try
        {
            cf.createClass();
            unreachable();
        }
        catch (RuntimeException ex)
        {
            assertExceptionSubstring(ex, "Unable to create class InvalidSuperClass");
View Full Code Here

    }

    @Test
    public void add_interface() throws Exception
    {
        ClassFab cf = newClassFab("SimpleService", Object.class);

        cf.addInterface(SimpleService.class);

        cf.addMethod(Modifier.PUBLIC, new MethodSignature(int.class, "add", new Class[]{int.class, int.class}, null),
                     "return $1 + $2;");

        Class targetClass = cf.createClass();

        SimpleService s = (SimpleService) targetClass.newInstance();

        assertEquals(207, s.add(99, 108));
    }
View Full Code Here

    }

    @Test
    public void attempt_to_subclass_from_final_class() throws Exception
    {
        ClassFab cf = newClassFab("StringSubclass", String.class);

        try
        {
            cf.createClass();
        }
        catch (RuntimeException ex)
        {
            assertExceptionRegexp(ex, "Unable to create class StringSubclass\\: .*");
        }
View Full Code Here

    }

    @Test
    public void create_class_within_non_default_package() throws Exception
    {
        ClassFab cf = newClassFab("org.apache.hivemind.InPackage", Object.class);

        Class c = cf.createClass();

        Object o = c.newInstance();

        assertEquals("org.apache.hivemind.InPackage", o.getClass().getName());
    }
View Full Code Here

    }

    @Test
    public void invalid_method_body() throws Exception
    {
        ClassFab cf = newClassFab("BadMethodBody", Object.class);

        cf.addInterface(Runnable.class);

        try
        {
            cf.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "run", null, null), "fail;");
        }
        catch (RuntimeException ex)
        {
            assertExceptionSubstring(ex, "Unable to add method void run() to class BadMethodBody:");
        }
View Full Code Here

    }

    @Test
    public void add_duplicate_method_signature() throws Exception
    {
        ClassFab cf = newClassFab("DupeMethodAdd", Object.class);

        cf.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "foo", null, null), "{}");

        try
        {
            cf.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, "foo", null, null), "{}");
            unreachable();
        }
        catch (RuntimeException ex)
        {
            assertEquals("Attempt to redefine method void foo() of class DupeMethodAdd.", ex
View Full Code Here

    }

    @Test
    public void invalid_constructor_body() throws Exception
    {
        ClassFab cf = newClassFab("BadConstructor", Object.class);

        try
        {
            cf.addConstructor(null, null, " woops!");
        }
        catch (RuntimeException ex)
        {
            assertExceptionSubstring(ex, "Unable to add constructor to class BadConstructor");
        }
View Full Code Here

    }

    @Test
    public void invalid_field() throws Exception
    {
        ClassFab cf = newClassFab("InvalidField", Object.class);

        // You'd think some of these would fail, but the ultimate failure
        // occurs when we create the class.

        cf.addField("a%b", String.class);
        cf.addField("", int.class);

        // Aha! Adding a duplicate fails!

        cf.addField("buffy", int.class);

        try
        {
            cf.addField("buffy", String.class);
            unreachable();
        }
        catch (RuntimeException ex)
        {
            assertEquals(ex.getMessage(), "Unable to add field buffy to class InvalidField: duplicate field: buffy");
View Full Code Here

    }

    @Test
    public void to_string() throws Exception
    {
        ClassFab cf = newClassFab("FredRunnable", BaseLocatable.class);

        cf.addInterface(Runnable.class);
        cf.addInterface(Serializable.class);

        cf.addField("_map", Map.class);

        cf.addConstructor(new Class[]{Map.class, Runnable.class},
                          new Class[]{IllegalArgumentException.class, DataFormatException.class}, "{ _map = $1; }");

        MethodSignature sig = new MethodSignature(Map.class, "doTheNasty", new Class[]{int.class, String.class},
                                                  new Class[]{InstantiationException.class,
                                                              IllegalAccessException.class});

        cf.addMethod(Modifier.PUBLIC + Modifier.FINAL + Modifier.SYNCHRONIZED, sig, "{ return _map; }");

        String toString = cf.toString();

        assertContains(toString,
                       "public class FredRunnable extends " + BaseLocatable.class.getName() + "\n" + "  implements java.lang.Runnable, java.io.Serializable");

        assertContains(toString, "private java.util.Map _map;");
View Full Code Here

TOP

Related Classes of org.apache.tapestry.ioc.services.ClassFab

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.