Package org.apache.tapestry.test

Source Code of org.apache.tapestry.test.Creator

/* $$ Clover has instrumented this file $$ */// Copyright 2004 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import javassist.CtClass;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.service.ClassFab;
import org.apache.hivemind.service.ClassFabUtils;
import org.apache.hivemind.service.MethodSignature;
import org.apache.hivemind.service.impl.ClassFabImpl;
import org.apache.hivemind.service.impl.CtClassSource;
import org.apache.hivemind.service.impl.HiveMindClassPool;

/**
* A utility class that is used to instantiate abstract Tapestry pages and
* components. It creates, at runtime, a subclass where all abstract properties
* are filled in (complete with instance variable, accessor and mutator methods).
* This isn't the same as how the class is enhanced at runtime, but is sufficient
* to unit test the class, especially listener methods.
*
* @author Howard Lewis Ship
* @since 3.1
*/
public class Creator
{public static com.cortexeb.tools.clover.d __CLOVER_367_0 = com.cortexeb.tools.clover.aq.getRecorder(new char[] {67,58,92,119,111,114,107,115,112,97,99,101,92,106,97,107,97,114,116,97,45,116,97,112,101,115,116,114,121,92,102,114,97,109,101,119,111,114,107,92,116,97,114,103,101,116,92,99,108,111,118,101,114,45,100,98},1096998272901L);
    private static final Log LOG = LogFactory.getLog(Creator.class);

    /**
     * Keyed on Class, value is another class (fully enhanced).
     */
    private Map _classes = new HashMap();
    private final CtClassSource _classSource;

    public Creator()
    {
        this(Thread.currentThread().getContextClassLoader());__CLOVER_367_0.S[8002]++;try { __CLOVER_367_0.M[1829]++;
    } finally { }}

    public Creator(ClassLoader loader)
    {try { __CLOVER_367_0.M[1830]++;
        __CLOVER_367_0.S[8003]++;HiveMindClassPool pool = new HiveMindClassPool();

        __CLOVER_367_0.S[8004]++;pool.appendClassLoader(loader);

        __CLOVER_367_0.S[8005]++;_classSource = new CtClassSource(pool);
    } finally { }}

    private void addAccessorMethod(ClassFab classFab, PropertyDescriptor pd, String attributeName)
    {try { __CLOVER_367_0.M[1831]++;
        __CLOVER_367_0.S[8006]++;String methodName = getMethodName(pd.getReadMethod(), "get", pd.getName());

        __CLOVER_367_0.S[8007]++;MethodSignature sig = new MethodSignature(pd.getPropertyType(), methodName, null, null);

        __CLOVER_367_0.S[8008]++;classFab.addMethod(Modifier.PUBLIC, sig, "return " + attributeName + ";");
    } finally { }}

    private void addField(ClassFab classFab, String fieldName, Class fieldType)
    {try { __CLOVER_367_0.M[1832]++;
        __CLOVER_367_0.S[8009]++;classFab.addField(fieldName, fieldType);
    } finally { }}

    private void addMissingProperties(ClassFab classFab, BeanInfo info)
    {try { __CLOVER_367_0.M[1833]++;
        __CLOVER_367_0.S[8010]++;PropertyDescriptor[] pd = info.getPropertyDescriptors();

        __CLOVER_367_0.S[8011]++;for (int i = 0; (((i < pd.length) && (++__CLOVER_367_0.CT[1375] != 0)) || (++__CLOVER_367_0.CF[1375] == 0)); i++){
            __CLOVER_367_0.S[8012]++;addMissingProperty(classFab, pd[i]);}
    } finally { }}

    private void addMissingProperty(ClassFab classFab, PropertyDescriptor pd)
    {try { __CLOVER_367_0.M[1834]++;
        __CLOVER_367_0.S[8013]++;Method readMethod = pd.getReadMethod();
        __CLOVER_367_0.S[8014]++;Method writeMethod = pd.getWriteMethod();

        __CLOVER_367_0.S[8015]++;boolean abstractRead = isAbstract(readMethod);
        __CLOVER_367_0.S[8016]++;boolean abstractWrite = isAbstract(writeMethod);

        __CLOVER_367_0.S[8017]++;if ((((!(abstractRead || abstractWrite)) && (++__CLOVER_367_0.CT[1376] != 0)) || (++__CLOVER_367_0.CF[1376] == 0))){
            __CLOVER_367_0.S[8018]++;return;}

        __CLOVER_367_0.S[8019]++;String attributeName = "_$" + pd.getName();
        __CLOVER_367_0.S[8020]++;Class propertyType = pd.getPropertyType();

        __CLOVER_367_0.S[8021]++;addField(classFab, attributeName, propertyType);

        __CLOVER_367_0.S[8022]++;if ((((abstractRead) && (++__CLOVER_367_0.CT[1377] != 0)) || (++__CLOVER_367_0.CF[1377] == 0))){
            __CLOVER_367_0.S[8023]++;addAccessorMethod(classFab, pd, attributeName);}

        __CLOVER_367_0.S[8024]++;if ((((abstractWrite) && (++__CLOVER_367_0.CT[1378] != 0)) || (++__CLOVER_367_0.CF[1378] == 0))){
            __CLOVER_367_0.S[8025]++;addMutatorMethod(classFab, pd, attributeName);}

    } finally { }}

    private void addMutatorMethod(ClassFab classFab, PropertyDescriptor pd, String attributeName)
    {try { __CLOVER_367_0.M[1835]++;
        __CLOVER_367_0.S[8026]++;String methodName = getMethodName(pd.getWriteMethod(), "set", pd.getName());

        __CLOVER_367_0.S[8027]++;MethodSignature sig =
            new MethodSignature(void.class, methodName, new Class[] { pd.getPropertyType()}, null);

        __CLOVER_367_0.S[8028]++;classFab.addMethod(Modifier.PUBLIC, sig, attributeName + " = $1;");
    } finally { }}

    private Class createEnhancedClass(Class inputClass)
    {try { __CLOVER_367_0.M[1836]++;
        __CLOVER_367_0.S[8029]++;if ((((inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray()) && (++__CLOVER_367_0.CT[1379] != 0)) || (++__CLOVER_367_0.CF[1379] == 0))){
            __CLOVER_367_0.S[8030]++;throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));}

        __CLOVER_367_0.S[8031]++;if ((((!Modifier.isAbstract(inputClass.getModifiers())) && (++__CLOVER_367_0.CT[1380] != 0)) || (++__CLOVER_367_0.CF[1380] == 0))){
        {
            __CLOVER_367_0.S[8032]++;LOG.error(ScriptMessages.classNotAbstract(inputClass));
            __CLOVER_367_0.S[8033]++;return inputClass;
        }}

        __CLOVER_367_0.S[8034]++;BeanInfo info = null;

        __CLOVER_367_0.S[8035]++;try
        {
            __CLOVER_367_0.S[8036]++;info = Introspector.getBeanInfo(inputClass, Object.class);
        }
        catch (IntrospectionException ex)
        {
            __CLOVER_367_0.S[8037]++;throw new ApplicationRuntimeException(
                ScriptMessages.unableToIntrospect(inputClass, ex));
        }

        __CLOVER_367_0.S[8038]++;String name = ClassFabUtils.generateClassName("Enhance");

        __CLOVER_367_0.S[8039]++;CtClass newClass = _classSource.newClass(name, inputClass);

        __CLOVER_367_0.S[8040]++;ClassFab classFab = new ClassFabImpl(_classSource, newClass);

        __CLOVER_367_0.S[8041]++;addMissingProperties(classFab, info);

        __CLOVER_367_0.S[8042]++;return classFab.createClass();
    } finally { }}

    public Class getEnhancedClass(Class inputClass)
    {try { __CLOVER_367_0.M[1837]++;
        __CLOVER_367_0.S[8043]++;Class result = (Class) _classes.get(inputClass);

        __CLOVER_367_0.S[8044]++;if ((((result == null) && (++__CLOVER_367_0.CT[1381] != 0)) || (++__CLOVER_367_0.CF[1381] == 0))){
        {
            __CLOVER_367_0.S[8045]++;result = createEnhancedClass(inputClass);

            __CLOVER_367_0.S[8046]++;_classes.put(inputClass, result);
        }}

        __CLOVER_367_0.S[8047]++;return result;
    } finally { }}

    /**
     * Given a particular abstract class; will create an instance of that class. A subclass
     * is created with all abstract properties filled in with ordinary implementations.
     */
    public Object getInstance(Class abstractClass)
    {try { __CLOVER_367_0.M[1838]++;
        __CLOVER_367_0.S[8048]++;Class enhancedClass = getEnhancedClass(abstractClass);

        __CLOVER_367_0.S[8049]++;try
        {
            __CLOVER_367_0.S[8050]++;return enhancedClass.newInstance();
        }
        catch (Exception ex)
        {
            __CLOVER_367_0.S[8051]++;throw new ApplicationRuntimeException(
                ScriptMessages.unableToInstantiate(abstractClass, ex));
        }

    } finally { }}

    private String getMethodName(Method m, String prefix, String propertyName)
    {try { __CLOVER_367_0.M[1839]++;
        __CLOVER_367_0.S[8052]++;if ((((m != null) && (++__CLOVER_367_0.CT[1382] != 0)) || (++__CLOVER_367_0.CF[1382] == 0))){
            __CLOVER_367_0.S[8053]++;return m.getName();}

        __CLOVER_367_0.S[8054]++;StringBuffer buffer = new StringBuffer(prefix);

        __CLOVER_367_0.S[8055]++;buffer.append(propertyName.substring(0, 1).toUpperCase());
        __CLOVER_367_0.S[8056]++;buffer.append(propertyName.substring(1));

        __CLOVER_367_0.S[8057]++;return buffer.toString();
    } finally { }}

    private boolean isAbstract(Method m)
    {try { __CLOVER_367_0.M[1840]++;
        __CLOVER_367_0.S[8058]++;return m == null || Modifier.isAbstract(m.getModifiers());
    } finally { }}
}
TOP

Related Classes of org.apache.tapestry.test.Creator

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.