Package org.apache.tapestry5.ioc.services

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


    }

    @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

    }

    @Test
    public void add_noop_method() throws Exception
    {
        ClassFab cf = newClassFab("NoOp", Object.class);
        cf.addInterface(Runnable.class);

        cf.addNoOpMethod(new MethodSignature(void.class, "run", null, null));
        cf.addNoOpMethod(new MethodSignature(int.class, "getInt", null, null));
        cf.addNoOpMethod(new MethodSignature(double.class, "getDouble", null, null));

        Class clazz = cf.createClass();

        Runnable instance = (Runnable) clazz.newInstance();

        instance.run();
View Full Code Here

   

    @Test
    public void add_annotation() throws Exception
    {
        final ClassFab cf = newClassFab("AnnotatedClass", Object.class);

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

        cf.proxyMethodsToDelegate(AnnotatedService.class, "_delegate", "Bla bla");
        cf.copyClassAnnotationsFromDelegate(AnnotatedServiceImpl.class);
        cf.copyMethodAnnotationsFromDelegate(AnnotatedService.class, AnnotatedServiceImpl.class);

        final Class targetClass = cf.createClass();

        final TestAnnotation a = (TestAnnotation) targetClass.getAnnotation(TestAnnotation.class);

        assertNotNull(a);
       
View Full Code Here

    public void new_class_with_name_and_base_class() throws Exception
    {
        ClassFactory factory = new ClassFactoryImpl();
        String name = ClassFabUtils.generateClassName(Runnable.class);

        ClassFab cf = factory.newClass(name, Object.class);
        cf.addInterface(Runnable.class);

        addRunMethod(cf);

        Class newClass = cf.createClass();

        Runnable instance = (Runnable) newClass.newInstance();

        instance.run();
    }
View Full Code Here

    public void new_class_with_non_object_base_class() throws Exception
    {
        ClassFactory factory = new ClassFactoryImpl();
        String name = ClassFabUtils.generateClassName(Runnable.class);

        ClassFab cf = factory.newClass(name, BaseClass.class);
        cf.addInterface(Runnable.class);

        Class newClass = cf.createClass();

        Runnable instance = (Runnable) newClass.newInstance();

        instance.run();
    }
View Full Code Here

TOP

Related Classes of org.apache.tapestry5.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.