| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| GenericMethodParameterTypeFactory |
|
| 7.5;7.5 |
| 1 | package org.apache.commons.ognl.internal.entry; | |
| 2 | ||
| 3 | /* | |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one | |
| 5 | * or more contributor license agreements. See the NOTICE file | |
| 6 | * distributed with this work for additional information | |
| 7 | * regarding copyright ownership. The ASF licenses this file | |
| 8 | * to you under the Apache License, Version 2.0 (the | |
| 9 | * "License"); you may not use this file except in compliance | |
| 10 | * with the License. You may obtain a copy of the License at | |
| 11 | * | |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 13 | * | |
| 14 | * Unless required by applicable law or agreed to in writing, | |
| 15 | * software distributed under the License is distributed on an | |
| 16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| 17 | * KIND, either express or implied. See the License for the | |
| 18 | * specific language governing permissions and limitations | |
| 19 | * under the License. | |
| 20 | */ | |
| 21 | ||
| 22 | /* | |
| 23 | * $Id: GenericMethodParameterTypeFactory.java 1194956 2011-10-29 18:02:14Z mcucchiara $ | |
| 24 | */ | |
| 25 | ||
| 26 | import org.apache.commons.ognl.internal.CacheException; | |
| 27 | ||
| 28 | import java.lang.reflect.Array; | |
| 29 | import java.lang.reflect.GenericArrayType; | |
| 30 | import java.lang.reflect.ParameterizedType; | |
| 31 | import java.lang.reflect.Type; | |
| 32 | import java.lang.reflect.TypeVariable; | |
| 33 | ||
| 34 | 23 | public class GenericMethodParameterTypeFactory |
| 35 | implements CacheEntryFactory<GenericMethodParameterTypeCacheEntry, Class<?>[]> | |
| 36 | { | |
| 37 | public Class<?>[] create( GenericMethodParameterTypeCacheEntry entry ) | |
| 38 | throws CacheException | |
| 39 | { | |
| 40 | Class<?>[] types; | |
| 41 | ||
| 42 | 21 | ParameterizedType param = (ParameterizedType) entry.type.getGenericSuperclass(); |
| 43 | 21 | Type[] genTypes = entry.method.getGenericParameterTypes(); |
| 44 | 21 | TypeVariable<?>[] declaredTypes = entry.method.getDeclaringClass().getTypeParameters(); |
| 45 | ||
| 46 | 21 | types = new Class[genTypes.length]; |
| 47 | ||
| 48 | 33 | for ( int i = 0; i < genTypes.length; i++ ) |
| 49 | { | |
| 50 | 12 | TypeVariable<?> paramType = null; |
| 51 | ||
| 52 | 12 | if ( TypeVariable.class.isInstance( genTypes[i] ) ) |
| 53 | { | |
| 54 | 6 | paramType = (TypeVariable<?>) genTypes[i]; |
| 55 | } | |
| 56 | 6 | else if ( GenericArrayType.class.isInstance( genTypes[i] ) ) |
| 57 | { | |
| 58 | 4 | paramType = (TypeVariable<?>) ( (GenericArrayType) genTypes[i] ).getGenericComponentType(); |
| 59 | } | |
| 60 | 2 | else if ( ParameterizedType.class.isInstance( genTypes[i] ) ) |
| 61 | { | |
| 62 | 0 | types[i] = (Class<?>) ( (ParameterizedType) genTypes[i] ).getRawType(); |
| 63 | 0 | continue; |
| 64 | } | |
| 65 | 2 | else if ( Class.class.isInstance( genTypes[i] ) ) |
| 66 | { | |
| 67 | 2 | types[i] = (Class<?>) genTypes[i]; |
| 68 | 2 | continue; |
| 69 | } | |
| 70 | ||
| 71 | 10 | Class<?> resolved = resolveType( param, paramType, declaredTypes ); |
| 72 | ||
| 73 | 10 | if ( resolved != null ) |
| 74 | { | |
| 75 | 4 | if ( GenericArrayType.class.isInstance( genTypes[i] ) ) |
| 76 | { | |
| 77 | 2 | resolved = Array.newInstance( resolved, 0 ).getClass(); |
| 78 | } | |
| 79 | ||
| 80 | 4 | types[i] = resolved; |
| 81 | 4 | continue; |
| 82 | } | |
| 83 | 6 | types[i] = entry.method.getParameterTypes()[i]; |
| 84 | } | |
| 85 | ||
| 86 | 21 | return types; |
| 87 | } | |
| 88 | ||
| 89 | private Class<?> resolveType( ParameterizedType param, TypeVariable<?> var, TypeVariable<?>[] declaredTypes ) | |
| 90 | { | |
| 91 | 10 | if ( param.getActualTypeArguments().length < 1 ) |
| 92 | { | |
| 93 | 0 | return null; |
| 94 | } | |
| 95 | ||
| 96 | 22 | for ( int i = 0; i < declaredTypes.length; i++ ) |
| 97 | { | |
| 98 | 16 | if ( !TypeVariable.class.isInstance( param.getActualTypeArguments()[i] ) |
| 99 | && declaredTypes[i].getName().equals( var.getName() ) ) | |
| 100 | { | |
| 101 | 4 | return (Class<?>) param.getActualTypeArguments()[i]; |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | 6 | return null; |
| 106 | } | |
| 107 | } |