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 015 package org.apache.tapestry5.ioc.internal.services; 016 017 import org.apache.tapestry5.ioc.AnnotationProvider; 018 import org.apache.tapestry5.ioc.ObjectLocator; 019 import org.apache.tapestry5.ioc.ObjectProvider; 020 import org.apache.tapestry5.ioc.annotations.IntermediateType; 021 import org.apache.tapestry5.ioc.annotations.Symbol; 022 import org.apache.tapestry5.ioc.services.Builtin; 023 import org.apache.tapestry5.ioc.services.SymbolSource; 024 import org.apache.tapestry5.ioc.services.TypeCoercer; 025 026 /** 027 * Performs an injection based on a {@link Symbol} annotation. 028 */ 029 public 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 public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) 044 { 045 Symbol annotation = annotationProvider.getAnnotation(Symbol.class); 046 047 if (annotation == null) return null; 048 049 Object value = symbolSource.valueForSymbol(annotation.value()); 050 051 IntermediateType it = annotationProvider.getAnnotation(IntermediateType.class); 052 053 if (it != null) value = typeCoercer.coerce(value, it.value()); 054 055 return typeCoercer.coerce(value, objectType); 056 } 057 058 }