001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005//     http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.internal.genericsresolverguava;
014
015import java.lang.reflect.Field;
016import java.lang.reflect.Method;
017import java.lang.reflect.Type;
018
019import org.apache.tapestry5.services.GenericsResolver;
020
021import com.google.common.reflect.TypeToken;
022
023/**
024 * {@link GuavaGenericsResolver} implementation using Guava.
025 */
026public class GuavaGenericsResolver implements GenericsResolver {
027
028    @Override
029    public Class<?> extractGenericReturnType(Class<?> containingClass, Method method) 
030    {
031        return TypeToken.of(containingClass).resolveType(method.getGenericReturnType()).getRawType();
032    }
033
034    @Override
035    public Class extractGenericFieldType(Class containingClass, Field field) 
036    {
037        return TypeToken.of(containingClass).resolveType(field.getGenericType()).getRawType();
038    }
039
040    @Override
041    public Type extractActualType(Type containingType, Method method) 
042    {
043        return TypeToken.of(containingType).resolveType(method.getGenericReturnType()).getType();
044    }
045
046    @Override
047    public Type extractActualType(Type containingType, Field field) 
048    {
049        return TypeToken.of(containingType).resolveType(field.getGenericType()).getType();
050    }
051
052    @Override
053    public Type resolve(Type type, Type containingType) 
054    {
055        return TypeToken.of(containingType).resolveType(type).getType();
056    }
057
058    @Override
059    public Class asClass(Type actualType) {
060        return TypeToken.of(actualType).getRawType();
061    }
062
063}