1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.reflect.generics.reflectiveObjects;
27
28 import sun.reflect.generics.tree.FieldTypeSignature;
29
30 import java.lang.reflect.MalformedParameterizedTypeException;
31 import java.lang.reflect.Method;
32 import java.lang.reflect.ParameterizedType;
33 import java.lang.reflect.Type;
34 import java.lang.reflect.TypeVariable;
35 import java.util.Arrays;
36 import java.util.Objects;
37
38 /** Implementing class for ParameterizedType interface. */
39
40 public class ParameterizedTypeImpl implements ParameterizedType {
41 private final Type[] actualTypeArguments;
42 private final Class<?> rawType;
43 private final Type ownerType;
44
45 private ParameterizedTypeImpl(Class<?> rawType,
46 Type[] actualTypeArguments,
47 Type ownerType) {
48 this.actualTypeArguments = actualTypeArguments;
49 this.rawType = rawType;
50 this.ownerType = (ownerType != null) ? ownerType : rawType.getDeclaringClass();
51 validateConstructorArguments();
52 }
53
54 private void validateConstructorArguments() {
55 TypeVariable<?>[] formals = rawType.getTypeParameters();
190 Objects.equals(ownerType, thatOwner) &&
191 Objects.equals(rawType, thatRawType) &&
192 Arrays.equals(actualTypeArguments, // avoid clone
193 that.getActualTypeArguments());
194 } else
195 return false;
196 }
197
198 @Override
199 public int hashCode() {
200 return
201 Arrays.hashCode(actualTypeArguments) ^
202 Objects.hashCode(ownerType) ^
203 Objects.hashCode(rawType);
204 }
205
206 public String toString() {
207 StringBuilder sb = new StringBuilder();
208
209 if (ownerType != null) {
210 if (ownerType instanceof Class)
211 sb.append(((Class)ownerType).getName());
212 else
213 sb.append(ownerType.toString());
214
215 sb.append(".");
216
217 if (ownerType instanceof ParameterizedTypeImpl) {
218 // Find simple name of nested type by removing the
219 // shared prefix with owner.
220 sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
221 ""));
222 } else
223 sb.append(rawType.getName());
224 } else
225 sb.append(rawType.getName());
226
227 if (actualTypeArguments != null &&
228 actualTypeArguments.length > 0) {
229 sb.append("<");
230 boolean first = true;
231 for(Type t: actualTypeArguments) {
232 if (!first)
233 sb.append(", ");
234 sb.append(t.getTypeName());
235 first = false;
236 }
237 sb.append(">");
238 }
239
240 return sb.toString();
241 }
242 }
|
1 /*
2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.reflect.generics.reflectiveObjects;
27
28 import sun.reflect.generics.tree.FieldTypeSignature;
29
30 import java.lang.reflect.MalformedParameterizedTypeException;
31 import java.lang.reflect.Method;
32 import java.lang.reflect.ParameterizedType;
33 import java.lang.reflect.Type;
34 import java.lang.reflect.TypeVariable;
35 import java.util.Arrays;
36 import java.util.StringJoiner;
37 import java.util.Objects;
38
39 /** Implementing class for ParameterizedType interface. */
40
41 public class ParameterizedTypeImpl implements ParameterizedType {
42 private final Type[] actualTypeArguments;
43 private final Class<?> rawType;
44 private final Type ownerType;
45
46 private ParameterizedTypeImpl(Class<?> rawType,
47 Type[] actualTypeArguments,
48 Type ownerType) {
49 this.actualTypeArguments = actualTypeArguments;
50 this.rawType = rawType;
51 this.ownerType = (ownerType != null) ? ownerType : rawType.getDeclaringClass();
52 validateConstructorArguments();
53 }
54
55 private void validateConstructorArguments() {
56 TypeVariable<?>[] formals = rawType.getTypeParameters();
191 Objects.equals(ownerType, thatOwner) &&
192 Objects.equals(rawType, thatRawType) &&
193 Arrays.equals(actualTypeArguments, // avoid clone
194 that.getActualTypeArguments());
195 } else
196 return false;
197 }
198
199 @Override
200 public int hashCode() {
201 return
202 Arrays.hashCode(actualTypeArguments) ^
203 Objects.hashCode(ownerType) ^
204 Objects.hashCode(rawType);
205 }
206
207 public String toString() {
208 StringBuilder sb = new StringBuilder();
209
210 if (ownerType != null) {
211 sb.append(ownerType.getTypeName());
212
213 sb.append(".");
214
215 if (ownerType instanceof ParameterizedTypeImpl) {
216 // Find simple name of nested type by removing the
217 // shared prefix with owner.
218 sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
219 ""));
220 } else
221 sb.append(rawType.getName());
222 } else
223 sb.append(rawType.getName());
224
225 if (actualTypeArguments != null) {
226 StringJoiner sj = new StringJoiner(", ", "<", ">");
227 sj.setEmptyValue("");
228 for(Type t: actualTypeArguments) {
229 sj.add(t.getTypeName());
230 }
231 sb.append(sj.toString());
232 }
233
234 return sb.toString();
235 }
236 }
|