325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.bind.v2.model.nav;
325N/A
325N/Aimport java.lang.reflect.MalformedParameterizedTypeException;
325N/Aimport java.lang.reflect.ParameterizedType;
325N/Aimport java.lang.reflect.Type;
325N/Aimport java.lang.reflect.TypeVariable;
325N/Aimport java.util.Arrays;
325N/A
325N/A
325N/A/**
325N/A * {@link ParameterizedType} implementation.
325N/A */
325N/Aclass ParameterizedTypeImpl implements ParameterizedType {
325N/A private Type[] actualTypeArguments;
325N/A private Class<?> rawType;
325N/A private Type ownerType;
325N/A
325N/A ParameterizedTypeImpl(Class<?> rawType,
325N/A Type[] actualTypeArguments,
325N/A Type ownerType) {
325N/A this.actualTypeArguments = actualTypeArguments;
325N/A this.rawType = rawType;
325N/A if (ownerType != null) {
325N/A this.ownerType = ownerType;
325N/A } else {
325N/A this.ownerType = rawType.getDeclaringClass();
325N/A }
325N/A validateConstructorArguments();
325N/A }
325N/A
325N/A private void validateConstructorArguments() {
325N/A TypeVariable/*<?>*/[] formals = rawType.getTypeParameters();
325N/A // check correct arity of actual type args
325N/A if (formals.length != actualTypeArguments.length) {
325N/A throw new MalformedParameterizedTypeException();
325N/A }
325N/A for (int i = 0; i < actualTypeArguments.length; i++) {
325N/A // check actuals against formals' bounds
325N/A }
325N/A
325N/A }
325N/A
325N/A public Type[] getActualTypeArguments() {
325N/A return actualTypeArguments.clone();
325N/A }
325N/A
325N/A public Class<?> getRawType() {
325N/A return rawType;
325N/A }
325N/A
325N/A public Type getOwnerType() {
325N/A return ownerType;
325N/A }
325N/A
325N/A /*
325N/A * From the JavaDoc for java.lang.reflect.ParameterizedType
325N/A * "Instances of classes that implement this interface must
325N/A * implement an equals() method that equates any two instances
325N/A * that share the same generic type declaration and have equal
325N/A * type parameters."
325N/A */
325N/A @Override
325N/A public boolean equals(Object o) {
325N/A if (o instanceof ParameterizedType) {
325N/A // Check that information is equivalent
325N/A ParameterizedType that = (ParameterizedType) o;
325N/A
325N/A if (this == that)
325N/A return true;
325N/A
325N/A Type thatOwner = that.getOwnerType();
325N/A Type thatRawType = that.getRawType();
325N/A
325N/A if (false) { // Debugging
325N/A boolean ownerEquality = (ownerType == null ?
325N/A thatOwner == null :
325N/A ownerType.equals(thatOwner));
325N/A boolean rawEquality = (rawType == null ?
325N/A thatRawType == null :
325N/A rawType.equals(thatRawType));
325N/A
325N/A boolean typeArgEquality = Arrays.equals(actualTypeArguments, // avoid clone
325N/A that.getActualTypeArguments());
325N/A for (Type t : actualTypeArguments) {
325N/A System.out.printf("\t\t%s%s%n", t, t.getClass());
325N/A }
325N/A
325N/A System.out.printf("\towner %s\traw %s\ttypeArg %s%n",
325N/A ownerEquality, rawEquality, typeArgEquality);
325N/A return ownerEquality && rawEquality && typeArgEquality;
325N/A }
325N/A
325N/A
325N/A return
325N/A (ownerType == null ?
325N/A thatOwner == null :
325N/A ownerType.equals(thatOwner)) &&
325N/A (rawType == null ?
325N/A thatRawType == null :
325N/A rawType.equals(thatRawType)) &&
325N/A Arrays.equals(actualTypeArguments, // avoid clone
325N/A that.getActualTypeArguments());
325N/A } else
325N/A return false;
325N/A }
325N/A
325N/A @Override
325N/A public int hashCode() {
325N/A return Arrays.hashCode(actualTypeArguments) ^
325N/A (ownerType == null ? 0 : ownerType.hashCode()) ^
325N/A (rawType == null ? 0 : rawType.hashCode());
325N/A }
325N/A
325N/A public String toString() {
325N/A StringBuilder sb = new StringBuilder();
325N/A
325N/A if (ownerType != null) {
325N/A if (ownerType instanceof Class)
325N/A sb.append(((Class) ownerType).getName());
325N/A else
325N/A sb.append(ownerType.toString());
325N/A
325N/A sb.append(".");
325N/A
325N/A if (ownerType instanceof ParameterizedTypeImpl) {
325N/A // Find simple name of nested type by removing the
325N/A // shared prefix with owner.
325N/A sb.append(rawType.getName().replace(((ParameterizedTypeImpl) ownerType).rawType.getName() + "$",
325N/A ""));
325N/A } else
325N/A sb.append(rawType.getName());
325N/A } else
325N/A sb.append(rawType.getName());
325N/A
325N/A if (actualTypeArguments != null &&
325N/A actualTypeArguments.length > 0) {
325N/A sb.append("<");
325N/A boolean first = true;
325N/A for (Type t : actualTypeArguments) {
325N/A if (!first)
325N/A sb.append(", ");
325N/A if (t instanceof Class)
325N/A sb.append(((Class) t).getName());
325N/A else
325N/A sb.append(t.toString());
325N/A first = false;
325N/A }
325N/A sb.append(">");
325N/A }
325N/A
325N/A return sb.toString();
325N/A }
325N/A}