Package org.apache.tapestry.ioc.services

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


        }
    }

    private Class createProxyClass(String serviceId, Class serviceInterface, String proxyDescription)
    {
        ClassFab cf = _registry.newClass(serviceInterface);

        cf.addField("_creator", Modifier.PRIVATE | Modifier.FINAL, ObjectCreator.class);

        cf.addConstructor(new Class[]
        { ObjectCreator.class }, null, "_creator = $1;");

        addDelegateGetter(cf, serviceInterface, serviceId);

        cf.proxyMethodsToDelegate(serviceInterface, "_delegate()", proxyDescription);

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


        return serviceInterface.cast(interceptor);
    }

    private Class createInterceptorClass(Class serviceInterface, String serviceId)
    {
        ClassFab cf = _classFactory.newClass(serviceInterface);

        cf.addField("_delegate", Modifier.PRIVATE | Modifier.FINAL, serviceInterface);
        cf.addField("_logger", Modifier.PRIVATE | Modifier.FINAL, ServiceLogger.class);

        cf.addConstructor(new Class[]
        { serviceInterface, ServiceLogger.class }, null, "{ _delegate = $1; _logger = $2; }");

        addMethods(cf, serviceInterface, serviceId);

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

     */
    private PropertyConduit build(Class rootClass, String expression)
    {
        String name = ClassFabUtils.generateClassName("PropertyConduit");

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

        classFab.addConstructor(new Class[]
        { Class.class, AnnotationProvider.class, String.class }, null, "super($$);");

        String[] terms = expression.split("\\.");

        final Method readMethod = buildGetter(rootClass, classFab, expression, terms);
        final Method writeMethod = buildSetter(rootClass, classFab, expression, terms);

        // A conduit is either readable or writeable, otherwise there will already have been
        // an error about unknown method name or property name.

        Class propertyType = readMethod != null ? readMethod.getReturnType() : writeMethod
                .getParameterTypes()[0];

        String description = String.format(
                "PropertyConduit[%s %s]",
                rootClass.getName(),
                expression);

        Class conduitClass = classFab.createClass();

        AnnotationProvider provider = new AnnotationProvider()
        {

            public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
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 PropertyConduit build(Class rootClass, String expression)
    {
        String name = ClassFabUtils.generateClassName("PropertyConduit");

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

        classFab.addConstructor(new Class[]
        { Class.class, AnnotationProvider.class, String.class }, null, "super($$);");

        String[] terms = expression.split("\\.");

        final Method readMethod = buildGetter(rootClass, classFab, expression, terms);
        final Method writeMethod = buildSetter(rootClass, classFab, expression, terms);

        // A conduit is either readable or writeable, otherwise there will already have been
        // an error about unknown method name or property name.

        Class propertyType = readMethod != null ? readMethod.getReturnType() : writeMethod
                .getParameterTypes()[0];

        String description = String.format(
                "PropertyConduit[%s %s]",
                rootClass.getName(),
                expression);

        Class conduitClass = classFab.createClass();

        AnnotationProvider provider = new AnnotationProvider()
        {

            public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
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

     */
    private PropertyConduit build(Class rootClass, String expression)
    {
        String name = ClassFabUtils.generateClassName("PropertyConduit");

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

        classFab.addConstructor(new Class[]
        { Class.class, AnnotationProvider.class, String.class }, null, "super($$);");

        String[] terms = expression.split("\\.");

        final Method readMethod = buildGetter(rootClass, classFab, expression, terms);
        final Method writeMethod = buildSetter(rootClass, classFab, expression, terms);

        // A conduit is either readable or writeable, otherwise there will already have been
        // an error about unknown method name or property name.

        Class propertyType = readMethod != null ? readMethod.getReturnType() : writeMethod
                .getParameterTypes()[0];

        String description = String.format(
                "PropertyConduit[%s %s]",
                rootClass.getName(),
                expression);

        Class conduitClass = classFab.createClass();

        AnnotationProvider provider = new AnnotationProvider()
        {

            public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
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

    {
        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

        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

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.