You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CtNewNestedClass.java 2.4KB

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