001// Copyright 2010, 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.ioc.internal;
016
017import org.apache.tapestry5.ioc.ObjectLocator;
018import org.apache.tapestry5.ioc.internal.util.InternalUtils;
019
020public abstract class AbstractConfigurationImpl<T>
021{
022    private final Class<T> contributionType;
023
024    private final ObjectLocator locator;
025
026    public AbstractConfigurationImpl(Class<T> contributionType, ObjectLocator locator)
027    {
028        this.contributionType = contributionType;
029        this.locator = locator;
030    }
031
032    protected T instantiate(Class<? extends T> clazz)
033    {
034        assert clazz != null;
035
036        // Only attempt to proxy the class if it is the right type for the contribution. Starting
037        // in 5.3, it is allowed to make contributions of different types (as long as they can be
038        // coerced to the right type) ... but this means that sometimes, a class is passed that isn't
039        // assignable to the actual contribution type.
040
041        if (contributionType.isInterface() && InternalUtils.isLocalFile(clazz)
042                && contributionType.isAssignableFrom(clazz))
043            return locator.proxy(contributionType, clazz);
044
045        return locator.autobuild(clazz);
046    }
047}