1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16package javassist;
17
18/**
19 * Array types.
20 */
21final class CtArray extends CtClass {
22    protected ClassPool pool;
23
24    // the name of array type ends with "[]".
25    CtArray(String name, ClassPool cp) {
26        super(name);
27        pool = cp;
28    }
29
30    public ClassPool getClassPool() {
31        return pool;
32    }
33
34    public boolean isArray() {
35        return true;
36    }
37
38    private CtClass[] interfaces = null;
39
40    public int getModifiers() {
41        int mod = Modifier.FINAL;
42        try {
43            mod |= getComponentType().getModifiers()
44                   & (Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE);
45        }
46        catch (NotFoundException e) {}
47        return mod;
48    }
49
50    public CtClass[] getInterfaces() throws NotFoundException {
51        if (interfaces == null) {
52            Class[] intfs = Object[].class.getInterfaces();
53            // java.lang.Cloneable and java.io.Serializable.
54            // If the JVM is CLDC, intfs is empty.
55            interfaces = new CtClass[intfs.length];
56            for (int i = 0; i < intfs.length; i++)
57                interfaces[i] = pool.get(intfs[i].getName());
58        }
59
60        return interfaces;
61    }
62
63    public boolean subtypeOf(CtClass clazz) throws NotFoundException {
64        if (super.subtypeOf(clazz))
65            return true;
66
67        String cname = clazz.getName();
68        if (cname.equals(javaLangObject))
69            return true;
70
71        CtClass[] intfs = getInterfaces();
72        for (int i = 0; i < intfs.length; i++)
73            if (intfs[i].subtypeOf(clazz))
74                return true;
75
76        return clazz.isArray()
77            && getComponentType().subtypeOf(clazz.getComponentType());
78    }
79
80    public CtClass getComponentType() throws NotFoundException {
81        String name = getName();
82        return pool.get(name.substring(0, name.length() - 2));
83    }
84
85    public CtClass getSuperclass() throws NotFoundException {
86        return pool.get(javaLangObject);
87    }
88
89    public CtMethod[] getMethods() {
90        try {
91            return getSuperclass().getMethods();
92        }
93        catch (NotFoundException e) {
94            return super.getMethods();
95        }
96    }
97
98    public CtMethod getMethod(String name, String desc)
99        throws NotFoundException
100    {
101        return getSuperclass().getMethod(name, desc);
102    }
103
104    public CtConstructor[] getConstructors() {
105        try {
106            return getSuperclass().getConstructors();
107        }
108        catch (NotFoundException e) {
109            return super.getConstructors();
110        }
111    }
112}
113