001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.lang.reflect.implementation;
019:
020: import java.lang.reflect.GenericArrayType;
021: import java.lang.reflect.ParameterizedType;
022: import java.lang.reflect.TypeVariable;
023: import java.lang.reflect.WildcardType;
024: import java.lang.reflect.Type;
025:
026: /**
027: * @author Serguei S. Zapreyev
028: * @version $Revision: 1.1.2.2 $
029: */
030: public final class ParameterizedTypeImpl implements ParameterizedType {
031: private final Type[] args;
032: private final Type rawType;
033: private final Type typeOwner;
034:
035: public ParameterizedTypeImpl(Type[] args, Type rawType,
036: Type typeOwner) {
037: this .args = args;
038: this .rawType = rawType;
039: this .typeOwner = typeOwner;
040: }
041:
042: public boolean equals(Object other) {
043: Type[] arr;
044: if (other == null
045: || !(other instanceof ParameterizedType)
046: || args.length != (arr = ((ParameterizedType) other)
047: .getActualTypeArguments()).length) {
048: return false;
049: }
050: for (int i = 0; i < args.length; i++) {
051: if (!args[i].equals(arr[i])) {
052: return false;
053: }
054: }
055: return rawType.equals(((ParameterizedType) other).getRawType())
056: && typeOwner.equals(((ParameterizedType) other)
057: .getOwnerType());
058: }
059:
060: public Type[] getActualTypeArguments() {
061: return (Type[]) args.clone();
062: }
063:
064: public Type getOwnerType() {
065: return typeOwner;
066: }
067:
068: public Type getRawType() {
069: return rawType;
070: }
071:
072: public int hashCode() {
073: //return super.hashCode();
074: int ah = 0;
075: for (int i = 0; i < args.length; i++) {
076: ah += args[i].hashCode();
077: }
078: return ah ^ rawType.hashCode() ^ typeOwner.hashCode();
079: }
080:
081: public String toString() {
082: // TODO: this body should be reimplemented effectively.
083: StringBuffer sb = new StringBuffer();
084: if (typeOwner != null) {
085: sb.append((typeOwner instanceof Class ? ((Class) typeOwner)
086: .getName() : typeOwner.toString())
087: + "." + ((Class) getRawType()).getSimpleName());
088: } else {
089: sb.append(((Class) getRawType()).getName());
090: }
091: if (args.length > 0) {
092: sb.append("<");
093: for (int i = 0; i < args.length; i++) {
094: if (i != 0) {
095: sb.append(", ");
096: }
097: if (args[i] instanceof Class) {
098: sb.append(((Class) args[i]).getName());
099: } else if (args[i] instanceof ParameterizedType) {
100: sb.append(args[i].toString());
101: } else if (args[i] instanceof TypeVariable) {
102: sb.append(args[i].toString());
103: } else if (args[i] instanceof WildcardType) {
104: sb.append(args[i].toString());
105: } else if (args[i] instanceof GenericArrayType) {
106: sb.append(args[i].toString());
107: }
108: }
109: sb.append(">");
110: }
111: return sb.toString();
112: }
113: }
|