Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 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
  // 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.util.List;
 import java.util.Map;
 import  org.apache.hivemind.ApplicationRuntimeException;
 import  org.apache.hivemind.ClassResolver;
 import  org.apache.hivemind.Location;
 import  org.apache.hivemind.Resource;
 import  org.apache.hivemind.impl.DefaultClassResolver;
 import  org.apache.hivemind.service.ClassFactory;
 import  org.apache.hivemind.service.impl.ClassFactoryImpl;
 import  org.apache.hivemind.util.ClasspathResource;
 import  org.apache.hivemind.util.PropertyUtils;
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 (each property complete with an instance variable, an accessor method and a mutator method). This isn't quite the same as how the class is enhanced at runtime (though it does use a subset of the same code), but is sufficient to unit test the class, especially listener methods.

One part of the enhancement is that the specification and messages properties of the page or component class are converted into read/write properties that can be set via reflection (including newInstance(Class, Map).

Howard Lewis Ship
 public class Creator
Keyed on Class, value is an ComponentConstructor.
     private final Map _constructors = new HashMap();
     private final ClassFactory _classFactory = new ClassFactoryImpl();
     private final ClassResolver _classResolver = new DefaultClassResolver();
     private final List _workers = new ArrayList();
     private final Resource _creatorResource = new ClasspathResource(,
     private final Location _creatorLocation = new DescribedLocation(,
             "Creator Location");
     private final ComponentRenderWorker _renderWorker = new MockComponentRenderWorker();
     private final InjectRenderWorker _injectRender = new InjectRenderWorker();
         // Overrride AbstractComponent's implementations of
         // these two properties (making them read/write).
         .add(new CreatePropertyWorker("messages"));
         .add(new CreatePropertyWorker("specification"));
         // Implement any abstract properties.
         // Note that we don't bother setting the errorLog property
         // so failures may turn into NPEs.
         .add(new AbstractPropertyWorker());
        if (inputClass.isInterface() || inputClass.isPrimitive() || inputClass.isArray())
            throw new IllegalArgumentException(ScriptMessages.wrongTypeForEnhancement(inputClass));
                new ComponentSpecification(), inputClassnull);
        IComponentSpecification spec = new ComponentSpecification();
        Iterator i = .iterator();
        while (i.hasNext())
            EnhancementWorker worker = (;
        return op.getConstructor();
        ComponentConstructor result = (ComponentConstructor.get(inputClass);
        if (result == null)
            result = createComponentConstructor(inputClass);
        return result;

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 newInstance(Class abstractClass)
        ComponentConstructor constructor = getComponentConstructor(abstractClass);
            return constructor.newInstance();
        catch (Exception ex)
            throw new ApplicationRuntimeException(ScriptMessages.unableToInstantiate(

Creates a new instance of a given class, and then initializes properties of the instance. The map contains string keys that are property names, and object values.
    public Object newInstance(Class abstractClassMap properties)
        Object result = newInstance(abstractClass);
        if (properties != null)
            Iterator i = properties.entrySet().iterator();
            while (i.hasNext())
                Map.Entry e = (;
                String propertyName = (Stringe.getKey();
        return result;

A convienience (useful in test code) for invoking newInstance(Class, Map). The Map is constructed from the properties array, which consists of alternating keys and values.
    public Object newInstance(Class abstractClassObject[] properties)
        Map propertyMap = Tapestry.convertArrayToMap(properties);
        return newInstance(abstractClasspropertyMap);
New to GrepCode? Check out our FAQ X