Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33312bfe2c152bfdf133b374be088ca9b334923b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
//  ========================================================================
//  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.jsr356.utils;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;

import org.eclipse.jetty.websocket.common.util.ReflectUtils;

public class TypeTree
{
    public static void dumpTree(String indent, Type type)
    {
        if ((type == null) || (type == Object.class))
        {
            return;
        }

        if (type instanceof Class<?>)
        {
            Class<?> ctype = (Class<?>)type;
            System.out.printf("%s (Class) = %s%n",indent,ctype.getName());

            String name = ctype.getName();
            if (name.startsWith("java.lang.") || name.startsWith("java.io."))
            {
                // filter away standard classes from tree (otherwise it will go on infinitely)
                return;
            }

            Type superType = ctype.getGenericSuperclass();
            dumpTree(indent + ".genericSuperClass()",superType);

            Type[] ifaces = ctype.getGenericInterfaces();
            if ((ifaces != null) && (ifaces.length > 0))
            {
                // System.out.printf("%s.genericInterfaces[].length = %d%n",indent,ifaces.length);
                for (int i = 0; i < ifaces.length; i++)
                {
                    Type iface = ifaces[i];
                    dumpTree(indent + ".genericInterfaces[" + i + "]",iface);
                }
            }

            TypeVariable<?>[] typeParams = ctype.getTypeParameters();
            if ((typeParams != null) && (typeParams.length > 0))
            {
                // System.out.printf("%s.typeParameters[].length = %d%n",indent,typeParams.length);
                for (int i = 0; i < typeParams.length; i++)
                {
                    TypeVariable<?> typeParam = typeParams[i];
                    dumpTree(indent + ".typeParameters[" + i + "]",typeParam);
                }
            }
            return;
        }

        if (type instanceof ParameterizedType)
        {
            ParameterizedType ptype = (ParameterizedType)type;
            System.out.printf("%s (ParameterizedType) = %s%n",indent,ReflectUtils.toShortName(ptype));
            // dumpTree(indent + ".ownerType()",ptype.getOwnerType());
            dumpTree(indent + ".rawType(" + ReflectUtils.toShortName(ptype.getRawType()) + ")",ptype.getRawType());
            Type args[] = ptype.getActualTypeArguments();
            if (args != null)
            {
                System.out.printf("%s.actualTypeArguments[].length = %d%n",indent,args.length);
                for (int i = 0; i < args.length; i++)
                {
                    Type arg = args[i];
                    dumpTree(indent + ".actualTypeArguments[" + i + "]",arg);
                }
            }
            return;
        }

        if (type instanceof GenericArrayType)
        {
            GenericArrayType gtype = (GenericArrayType)type;
            System.out.printf("%s (GenericArrayType) = %s%n",indent,gtype);
            return;
        }

        if (type instanceof TypeVariable<?>)
        {
            TypeVariable<?> tvar = (TypeVariable<?>)type;
            System.out.printf("%s (TypeVariable) = %s%n",indent,tvar);
            System.out.printf("%s.getName() = %s%n",indent,tvar.getName());
            System.out.printf("%s.getGenericDeclaration() = %s%n",indent,tvar.getGenericDeclaration());
            return;
        }

        if (type instanceof WildcardType)
        {
            System.out.printf("%s (WildcardType) = %s%n",indent,type);
            return;
        }

        System.out.printf("%s (?) = %s%n",indent,type);
    }
}

Back to the top