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
18import javassist.bytecode.ClassFile;
19import javassist.bytecode.AccessFlag;
20import javassist.bytecode.InnerClassesAttribute;
21
22/**
23 * A newly created public nested class.
24 */
25class CtNewNestedClass extends CtNewClass {
26    CtNewNestedClass(String realName, ClassPool cp, boolean isInterface,
27                     CtClass superclass) {
28        super(realName, cp, isInterface, superclass);
29    }
30
31    /**
32     * This method does not change the STATIC bit.  The original value is kept.
33     */
34    public void setModifiers(int mod) {
35        mod = mod & ~Modifier.STATIC;
36        super.setModifiers(mod);
37        updateInnerEntry(mod, getName(), this, true);
38    }
39
40    private static void updateInnerEntry(int mod, String name, CtClass clazz, boolean outer) {
41        ClassFile cf = clazz.getClassFile2();
42        InnerClassesAttribute ica = (InnerClassesAttribute)cf.getAttribute(
43                                                InnerClassesAttribute.tag);
44        if (ica == null)
45            return;
46
47        int n = ica.tableLength();
48        for (int i = 0; i < n; i++)
49            if (name.equals(ica.innerClass(i))) {
50                int acc = ica.accessFlags(i) & AccessFlag.STATIC;
51                ica.setAccessFlags(i, mod | acc);
52                String outName = ica.outerClass(i);
53                if (outName != null && outer)
54                    try {
55                        CtClass parent = clazz.getClassPool().get(outName);
56                        updateInnerEntry(mod, name, parent, false);
57                    }
58                    catch (NotFoundException e) {
59                        throw new RuntimeException("cannot find the declaring class: "
60                                                   + outName);
61                    }
62
63                break;
64            }
65    }
66}
67