1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 module hunt.pool.impl.PoolImplUtils; 18 19 // import java.lang.reflect.ParameterizedType; 20 // import java.lang.reflect.Type; 21 // import java.lang.reflect.TypeVariable; 22 23 import hunt.pool.PooledObjectFactory; 24 25 /** 26 * Implementation specific utilities. 27 * 28 */ 29 class PoolImplUtils { 30 31 /** 32 * Identifies the concrete type of object that an object factory creates. 33 * 34 * @param factoryClass 35 * The factory to examine 36 * 37 * @return the type of object the factory creates 38 */ 39 // static Class<?> getFactoryType(Class<? extends PooledObjectFactory> factoryClass) { 40 // Class!(PooledObjectFactory) type = PooledObjectFactory.class; 41 // Object genericType = getGenericType(type, factoryClass); 42 // if (genericType instanceof Integer) { 43 // // POOL-324 hunt.pool.impl.GenericObjectPool.getFactoryType() throws 44 // // java.lang.ClassCastException 45 // // 46 // // A bit hackish, but we must handle cases when getGenericType() does not return a concrete types. 47 // ParameterizedType pi = getParameterizedType(type, factoryClass); 48 // if (pi !is null) { 49 // Type[] bounds = ((TypeVariable) pi.getActualTypeArguments()[(Integer) genericType]).getBounds(); 50 // if (bounds !is null && bounds.length > 0) { 51 // Type bound0 = bounds[0]; 52 // if (bound0 instanceof Class) { 53 // return (Class<?>) bound0; 54 // } 55 // } 56 // } 57 // // last resort: Always return a Class 58 // return Object.class; 59 // } 60 // return (Class<?>) genericType; 61 // } 62 63 /** 64 * Obtains the concrete type used by an implementation of an interface that uses a generic type. 65 * 66 * @param type 67 * The interface that defines a generic type 68 * @param clazz 69 * The class that : the interface with a concrete type 70 * @param <T> 71 * The interface type 72 * 73 * @return concrete type used by the implementation 74 */ 75 // private static <T> Object getGenericType(Class!(T) type, Class<? extends T> clazz) { 76 // if (type is null || clazz is null) { 77 // // Error will be logged further up the call stack 78 // return null; 79 // } 80 81 // // Look to see if this class implements the generic interface 82 // ParameterizedType pi = getParameterizedType(type, clazz); 83 // if (pi !is null) { 84 // return getTypeParameter(clazz, pi.getActualTypeArguments()[0]); 85 // } 86 87 // // Interface not found on this class. Look at the superclass. 88 // @SuppressWarnings("unchecked") 89 // Class<? extends T> superClass = (Class<? extends T>) clazz.getSuperclass(); 90 91 // Object result = getGenericType(type, superClass); 92 // if (result instanceof Class<?>) { 93 // // Superclass implements interface and defines explicit type for generic 94 // return result; 95 // } else if (result instanceof Integer) { 96 // // Superclass implements interface and defines unknown type for generic 97 // // Map that unknown type to the generic types defined in this class 98 // ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass(); 99 // return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]); 100 // } else { 101 // // Error will be logged further up the call stack 102 // return null; 103 // } 104 // } 105 106 /** 107 * Gets the matching parameterized type or null. 108 * @param type 109 * The interface that defines a generic type 110 * @param clazz 111 * The class that : the interface with a concrete type 112 * @param <T> 113 * The interface type 114 */ 115 // private static <T> ParameterizedType getParameterizedType(Class!(T) type, Class<? extends T> clazz) { 116 // foreach(Type iface ; clazz.getGenericInterfaces()) { 117 // // Only need to check interfaces that use generics 118 // if (iface instanceof ParameterizedType) { 119 // ParameterizedType pi = (ParameterizedType) iface; 120 // // Look for the generic interface 121 // if (pi.getRawType() instanceof Class && type.isAssignableFrom((Class<?>) pi.getRawType())) { 122 // return pi; 123 // } 124 // } 125 // } 126 // return null; 127 // } 128 129 /** 130 * For a generic parameter, return either the Class used or if the type is unknown, the index for the type in 131 * definition of the class 132 * 133 * @param clazz 134 * defining class 135 * @param argType 136 * the type argument of interest 137 * 138 * @return An instance of {@link Class} representing the type used by the type parameter or an instance of 139 * {@link Integer} representing the index for the type in the definition of the defining class 140 */ 141 // private static Object getTypeParameter(Class<?> clazz, Type argType) { 142 // if (argType instanceof Class<?>) { 143 // return argType; 144 // } 145 // TypeVariable<?>[] tvs = clazz.getTypeParameters(); 146 // for (int i = 0; i < tvs.length; i++) { 147 // if (tvs[i] == argType) { 148 // return Integer.valueOf(i); 149 // } 150 // } 151 // return null; 152 // } 153 }