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.5KB

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