Examples of ClassFab


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

    }

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

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

        MethodSignature getStringValue = new MethodSignature(String.class, "getStringValue", null, null);

        cf.addMethod(Modifier.PUBLIC, getStringValue, "return _stringValue;");

        Class targetClass = cf.createClass();

        try
        {
            targetClass.newInstance();
            unreachable();
View Full Code Here

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

    private Object createProxyInstance(ObjectCreator creator, String serviceId, Class serviceInterface,
            Class serviceImplementation, String description)
    {
        ServiceProxyToken token = SerializationSupport.createToken(serviceId);

        ClassFab classFab = registry.newClass(serviceInterface);

        classFab.addField("creator", Modifier.PRIVATE | Modifier.FINAL, ObjectCreator.class);
        classFab.addField("token", Modifier.PRIVATE | Modifier.FINAL, ServiceProxyToken.class);

        classFab.addConstructor(new Class[]
        { ObjectCreator.class, ServiceProxyToken.class }, null, "{ creator = $1; token = $2; }");

        // Make proxies serializable by writing the token to the stream.

        classFab.addInterface(Serializable.class);

        // This is the "magic" signature that allows an object to substitute some other
        // object for itself.
        MethodSignature writeReplaceSig = new MethodSignature(Object.class, "writeReplace", null, new Class[]
        { ObjectStreamException.class });

        classFab.addMethod(Modifier.PRIVATE, writeReplaceSig, "return token;");

        // Now delegate all the methods.

        String body = format("return (%s) creator.createObject();", serviceInterface.getName());

        MethodSignature sig = new MethodSignature(serviceInterface, "delegate", null, null);

        classFab.addMethod(Modifier.PRIVATE, sig, body);

        classFab.proxyMethodsToDelegate(serviceInterface, "delegate()", description);

        if (serviceImplementation != null)
        {
            classFab.copyClassAnnotationsFromDelegate(serviceImplementation);

            classFab.copyMethodAnnotationsFromDelegate(serviceInterface, serviceImplementation);
        }

        Class proxyClass = classFab.createClass();

        try
        {
            return proxyClass.getConstructors()[0].newInstance(creator, token);
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.