169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Array types.
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalfinal class CtArray extends CtClass {
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected ClassPool pool;
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // the name of array type ends with "[]".
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    CtArray(String name, ClassPool cp) {
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(name);
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        pool = cp;
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public ClassPool getClassPool() {
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return pool;
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean isArray() {
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return true;
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private CtClass[] interfaces = null;
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int getModifiers() {
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int mod = Modifier.FINAL;
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            mod |= getComponentType().getModifiers()
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                   & (Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE);
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (NotFoundException e) {}
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return mod;
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtClass[] getInterfaces() throws NotFoundException {
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (interfaces == null) {
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            Class[] intfs = Object[].class.getInterfaces();
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            // java.lang.Cloneable and java.io.Serializable.
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            // If the JVM is CLDC, intfs is empty.
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            interfaces = new CtClass[intfs.length];
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            for (int i = 0; i < intfs.length; i++)
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                interfaces[i] = pool.get(intfs[i].getName());
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return interfaces;
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public boolean subtypeOf(CtClass clazz) throws NotFoundException {
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (super.subtypeOf(clazz))
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return true;
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String cname = clazz.getName();
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (cname.equals(javaLangObject))
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return true;
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CtClass[] intfs = getInterfaces();
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        for (int i = 0; i < intfs.length; i++)
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (intfs[i].subtypeOf(clazz))
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return true;
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return clazz.isArray()
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            && getComponentType().subtypeOf(clazz.getComponentType());
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtClass getComponentType() throws NotFoundException {
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String name = getName();
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return pool.get(name.substring(0, name.length() - 2));
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtClass getSuperclass() throws NotFoundException {
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return pool.get(javaLangObject);
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtMethod[] getMethods() {
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return getSuperclass().getMethods();
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (NotFoundException e) {
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return super.getMethods();
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtMethod getMethod(String name, String desc)
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws NotFoundException
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return getSuperclass().getMethod(name, desc);
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CtConstructor[] getConstructors() {
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return getSuperclass().getConstructors();
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (NotFoundException e) {
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return super.getConstructors();
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
113