1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.luni.lang.reflect; 18 19import java.lang.reflect.Constructor; 20import java.lang.reflect.GenericDeclaration; 21import java.lang.reflect.Method; 22import java.lang.reflect.Type; 23import java.lang.reflect.TypeVariable; 24 25 26public final class ImplForVariable<D extends GenericDeclaration> 27 implements TypeVariable<D> { 28 private ImplForVariable<D> formalVar; 29 private final GenericDeclaration declOfVarUser; 30 private final String name; 31 private D genericDeclaration; 32 private ListOfTypes bounds; 33 34 35 @Override 36 public boolean equals(Object o) { 37 if(!(o instanceof TypeVariable)) { 38 return false; 39 } 40 TypeVariable<?> that = (TypeVariable<?>) o; 41 return getName().equals(that.getName()) && 42 getGenericDeclaration().equals(that.getGenericDeclaration()); 43 } 44 45 46 @Override 47 public int hashCode() { 48 return 31 * getName().hashCode() + getGenericDeclaration().hashCode(); 49 } 50 51 /** 52 * @param genericDecl declaration where a type variable is declared 53 * @param name type variable name 54 * @param bounds class and interface bounds 55 * @api2vm 56 */ 57 ImplForVariable(D genericDecl, String name, ListOfTypes bounds) { 58 this.genericDeclaration = genericDecl; 59 this.name = name; 60 this.bounds = bounds; 61 this.formalVar = this; 62 this.declOfVarUser = null; 63 } 64 65 /** 66 * @param genericDecl declaration where a type variable is used 67 * @param name type variable name 68 * @api2vm 69 */ 70 ImplForVariable(D genericDecl, String name) { 71 this.name = name; 72 this.declOfVarUser = genericDecl; 73 } 74 75 static TypeVariable findFormalVar(GenericDeclaration layer, String name) { 76 TypeVariable[] formalVars = layer.getTypeParameters(); 77 for (TypeVariable var : formalVars) { 78 if (name.equals(var.getName())) { 79 return var; 80 } 81 } 82 // resolve() looks up the next level only, if null is returned 83 return null; 84 } 85 86 static GenericDeclaration nextLayer(GenericDeclaration decl) { 87 if (decl instanceof Class) { 88 // FIXME: Is the following hierarchy correct?: 89 Class cl = (Class)decl; 90 decl = cl.getEnclosingMethod(); 91 if (decl != null) { 92 return decl; 93 } 94 decl = cl.getEnclosingConstructor(); 95 if (decl != null) { 96 return decl; 97 } 98 return cl.getEnclosingClass(); 99 } else if (decl instanceof Method) { 100 return ((Method)decl).getDeclaringClass(); 101 } else if (decl instanceof Constructor) { 102 return ((Constructor)decl).getDeclaringClass(); 103 } 104 throw new RuntimeException("unknown GenericDeclaration2: " 105 + decl.toString()); 106 } 107 108 void resolve() { 109 if (formalVar == null) { 110 GenericDeclaration curLayer = declOfVarUser; 111 TypeVariable var = null; 112 do { 113 var = findFormalVar(curLayer, name); 114 if (var != null) break; 115 else { 116 curLayer = nextLayer(curLayer); 117 if (curLayer == null) break; // FIXME: SHOULD NEVER HAPPEN! 118 // throw exception: illegal 119 // type variable reference. 120 } 121 } while (true); 122 formalVar = (ImplForVariable<D>)var; 123 this.genericDeclaration = formalVar.genericDeclaration; 124 this.bounds = formalVar.bounds; 125 } 126 } 127 128 public Type[] getBounds() { 129 resolve(); 130 return bounds.getResolvedTypes().clone(); 131 } 132 133 public D getGenericDeclaration() { 134 resolve(); 135 return genericDeclaration; 136 } 137 138 public String getName() { 139 return name; 140 } 141 142 @Override 143 public String toString() { 144 return name; 145 } 146} 147