001// Copyright 2007 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.services; 016 017import org.apache.tapestry5.ioc.AnnotationProvider; 018import org.apache.tapestry5.ioc.ObjectLocator; 019import org.apache.tapestry5.ioc.ObjectProvider; 020import org.apache.tapestry5.ioc.annotations.IntermediateType; 021import org.apache.tapestry5.ioc.annotations.Symbol; 022import org.apache.tapestry5.ioc.services.Builtin; 023import org.apache.tapestry5.ioc.services.SymbolSource; 024import org.apache.tapestry5.ioc.services.TypeCoercer; 025 026/** 027 * Performs an injection based on a {@link Symbol} annotation. 028 */ 029public class SymbolObjectProvider implements ObjectProvider 030{ 031 private final SymbolSource symbolSource; 032 033 private final TypeCoercer typeCoercer; 034 035 public SymbolObjectProvider(@Builtin SymbolSource symbolSource, 036 037 @Builtin TypeCoercer typeCoercer) 038 { 039 this.symbolSource = symbolSource; 040 this.typeCoercer = typeCoercer; 041 } 042 043 @Override 044 public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) 045 { 046 Symbol annotation = annotationProvider.getAnnotation(Symbol.class); 047 048 if (annotation == null) return null; 049 050 Object value = symbolSource.valueForSymbol(annotation.value()); 051 052 IntermediateType it = annotationProvider.getAnnotation(IntermediateType.class); 053 054 if (it != null) value = typeCoercer.coerce(value, it.value()); 055 056 return typeCoercer.coerce(value, objectType); 057 } 058 059}