Home | History | Annotate | Download | only in reflect
      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 
     17 package org.apache.harmony.luni.lang.reflect;
     18 
     19 import java.lang.reflect.ParameterizedType;
     20 import java.lang.reflect.Type;
     21 
     22 public final class ImplForType implements ParameterizedType {
     23     private final ListOfTypes args;
     24     private final ImplForType ownerType0; // Potentially unresolved.
     25     private Type ownerTypeRes;
     26     private Class rawType; // Already resolved.
     27     private final String rawTypeName;
     28     private ClassLoader loader;
     29 
     30 
     31     public ImplForType(ImplForType ownerType, String rawTypeName,
     32             ListOfTypes args, ClassLoader loader) {
     33         this.ownerType0 = ownerType;
     34         this.rawTypeName = rawTypeName;
     35         this.args = args;
     36         this.loader = loader;
     37     }
     38 
     39 
     40     public Type[] getActualTypeArguments() {
     41         // ASSUMPTION: args is never null!!!
     42         return args.getResolvedTypes().clone();
     43     }
     44 
     45     public Type getOwnerType() {
     46         if (ownerTypeRes == null) {
     47             if (ownerType0 != null) {
     48                 ownerTypeRes = ownerType0.getResolvedType();
     49             } else {
     50                 ownerTypeRes = getRawType().getDeclaringClass();
     51             }
     52         }
     53         return ownerTypeRes;
     54     }
     55 
     56     public Class getRawType() {
     57         if (rawType == null) {
     58             // Here the actual loading of the class has to be performed and the
     59             // Exceptions have to be re-thrown TypeNotPresent...
     60             // How to deal with member (nested) classes?
     61             try {
     62                 rawType = Class.forName(rawTypeName, false, loader);
     63             } catch (ClassNotFoundException e) {
     64                 throw new TypeNotPresentException(rawTypeName, e);
     65             }
     66         }
     67         return rawType;
     68     }
     69 
     70 
     71     Type getResolvedType() {
     72         if (args.getResolvedTypes().length == 0) {
     73             return getRawType();
     74         } else {
     75             return this;
     76         }
     77     }
     78 
     79     @Override
     80     public String toString() {
     81         StringBuilder sb = new StringBuilder();
     82         sb.append(rawTypeName);
     83         if (args.length() > 0) {
     84             sb.append("<").append(args).append(">");
     85         }
     86         return sb.toString();
     87     }
     88 }
     89