Package org.apache.tapestry.ioc.services

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


    }

    @SuppressWarnings("unchecked")
    private <S> Class<S> createClass(Class<S> serviceInterface)
    {
        ClassFab cf = _classFactory.newClass(serviceInterface);

        MethodIterator mi = new MethodIterator(serviceInterface);

        while (mi.hasNext())
        {
            MethodSignature sig = mi.next();

            cf.addNoOpMethod(sig);
        }

        if (!mi.getToString()) cf.addToString(format("<NoOp %s>", serviceInterface.getName()));

        return cf.createClass();
    }
View Full Code Here


    public ClassFab newClass(Class serviceInterface)
    {
        String name = ClassFabUtils.generateClassName(serviceInterface);

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

        cf.addInterface(serviceInterface);

        return cf;
    }
View Full Code Here

        // for the same interface may occur. We just let that happen, and there'll
        // be two different classes corresponding to the same interface.

        String name = ClassFabUtils.generateClassName(commandInterface);

        ClassFab cf = _classFactory.newClass(name, Object.class);

        addInfrastructure(cf, commandInterface);

        addMethods(cf, commandInterface);

        return cf.createClass();
    }
View Full Code Here

        ClassFactory factory = new ClassFactoryImpl();

        Class clazz = SimpleBean.class;

        ClassFab cf = factory.newClass(clazz.getName() + "$$Proxy", clazz);

        cf.addInterface(Serializable.class);

        Class proxyClass = cf.createClass();

        SimpleBean simple = (SimpleBean) proxyClass.newInstance();

        assertTrue(simple instanceof Serializable);
View Full Code Here

    private Class createBindingClass(Class targetClass, String pathExpression, String propertyName,
            Class propertyType, Method readMethod, Method writeMethod)
    {
        String name = ClassFabUtils.generateClassName("PropBinding");

        ClassFab classFab = _classFactory.newClass(name, BasePropBinding.class);

        classFab.addField("_target", targetClass);

        classFab
                .addConstructor(
                        new Class[]
                        { targetClass, Class.class, String.class, AnnotationProvider.class,
                                Location.class },
                        null,
                        "{ super($2, $3, $4, $5); _target = $1; }");

        if (readMethod != null)
        {
            String body = String.format("return ($w) %s.%s();", pathExpression, readMethod
                    .getName());

            classFab.addMethod(Modifier.PUBLIC, GET_SIGNATURE, body);
        }

        if (writeMethod != null)
        {
            BodyBuilder builder = new BodyBuilder();
            builder.begin();

            String propertyTypeName = propertyType.getName();
            builder.add("%s value = ", propertyTypeName);

            if (propertyType.isPrimitive())
            {
                String wrapperType = ClassFabUtils.getWrapperTypeName(propertyTypeName);
                String unwrapMethod = ClassFabUtils.getUnwrapMethodName(propertyTypeName);

                // Cast the value to the wrapper type, and then extract the primitive
                // value from that.

                builder.addln("((%s) $1).%s();", wrapperType, unwrapMethod);
            }
            else
                builder.addln("(%s) $1;", propertyTypeName);

            builder.addln("%s.%s(value);", pathExpression, writeMethod.getName());
            builder.end();

            classFab.addMethod(Modifier.PUBLIC, SET_SIGNATURE, builder.toString());
        }

        return classFab.createClass();
    }
View Full Code Here

    {
        String componentClassName = _ctClass.getName();

        String name = ClassFabUtils.generateClassName("Instantiator");

        ClassFab cf = _classFactory.newClass(name, AbstractInstantiator.class);

        BodyBuilder constructor = new BodyBuilder();

        // This is realy -1 + 2: The first value in _constructorArgs is the InternalComponentResources, which doesn't
        // count toward's the Instantiator's constructor ... then we add in the Model and String description.
        // It's tricky because there's the constructor parameters for the Instantiator, most of which are stored
        // in fields and then used as the constructor parameters for the Component.

        Class[] constructorParameterTypes = new Class[_constructorArgs.size() + 1];
        Object[] constructorParameterValues = new Object[_constructorArgs.size() + 1];

        constructorParameterTypes[0] = ComponentModel.class;
        constructorParameterValues[0] = _componentModel;

        constructorParameterTypes[1] = String.class;
        constructorParameterValues[1] = String.format("Instantiator[%s]", componentClassName);

        BodyBuilder newInstance = new BodyBuilder();

        newInstance.add("return new %s($1", componentClassName);

        constructor.begin();

        // Pass the model and description to AbstractInstantiator

        constructor.addln("super($1, $2);");

        // Again, skip the (implicit) InternalComponentResources field, that's
        // supplied to the Instantiator's newInstance() method.

        for (int i = 1; i < _constructorArgs.size(); i++)
        {
            ConstructorArg arg = _constructorArgs.get(i);

            CtClass argCtType = arg.getType();
            Class argType = toClass(argCtType.getName());

            boolean primitive = argCtType.isPrimitive();

            Class fieldType = primitive ? ClassFabUtils.getPrimitiveType(argType) : argType;

            String fieldName = "_param_" + i;

            constructorParameterTypes[i + 1] = argType;
            constructorParameterValues[i + 1] = arg.getValue();

            cf.addField(fieldName, fieldType);

            // $1 is model, $2 is description, to $3 is first dynamic parameter.

            constructor.add("%s = $%d", fieldName, i + 2);

            if (primitive)
            {
                String methodName = ClassFabUtils.getUnwrapMethodName(argType);

                constructor.add(".%s()", methodName);
            }

            constructor.addln(";");

            newInstance.add(", %s", fieldName);
        }

        constructor.end();
        newInstance.addln(");");

        cf.addConstructor(constructorParameterTypes, null, constructor.toString());

        cf.addMethod(Modifier.PUBLIC, NEW_INSTANCE_SIGNATURE, newInstance.toString());

        Class instantiatorClass = cf.createClass();

        try
        {
            Object instance = instantiatorClass.getConstructors()[0].newInstance(constructorParameterValues);
View Full Code Here

        }
    }

    private Class buildProxyClass(Class serviceType)
    {
        ClassFab classFab = _classFactory.newClass(serviceType);

        classFab.addField("_environment", Environment.class);
        classFab.addField("_serviceType", Class.class);

        classFab.addConstructor(new Class[] { Environment.class, Class.class }, null,
                                "{ _environment = $1; _serviceType = $2; }");

        classFab.addMethod(Modifier.PRIVATE, new MethodSignature(serviceType, "_delegate", null, null),
                           "return ($r) _environment.peekRequired(_serviceType); ");

        classFab.proxyMethodsToDelegate(serviceType, "_delegate()",
                                        format("<EnvironmentalProxy for %s>", serviceType.getName()));

        return classFab.createClass();
    }
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,
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.