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.genericsresolverguava.internal;
014
015import java.lang.reflect.Field;
016import java.lang.reflect.Method;
017import java.lang.reflect.Type;
018
019import org.apache.tapestry5.commons.services.GenericsResolver;
020import org.apache.tapestry5.genericsresolverguava.internal.GuavaGenericsResolver;
021
022import com.google.common.reflect.TypeToken;
023
024/**
025 * {@link GuavaGenericsResolver} implementation using Guava.
026 */
027public class GuavaGenericsResolver implements GenericsResolver {
028
029    @Override
030    public Class<?> extractGenericReturnType(Class<?> containingClass, Method method) 
031    {
032        return TypeToken.of(containingClass).resolveType(method.getGenericReturnType()).getRawType();
033    }
034
035    @Override
036    public Class extractGenericFieldType(Class containingClass, Field field) 
037    {
038        return TypeToken.of(containingClass).resolveType(field.getGenericType()).getRawType();
039    }
040
041    @Override
042    public Type extractActualType(Type containingType, Method method) 
043    {
044        return TypeToken.of(containingType).resolveType(method.getGenericReturnType()).getType();
045    }
046
047    @Override
048    public Type extractActualType(Type containingType, Field field) 
049    {
050        return TypeToken.of(containingType).resolveType(field.getGenericType()).getType();
051    }
052
053    @Override
054    public Type resolve(Type type, Type containingType) 
055    {
056        return TypeToken.of(containingType).resolveType(type).getType();
057    }
058
059    @Override
060    public Class asClass(Type actualType) {
061        return TypeToken.of(actualType).getRawType();
062    }
063
064}