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.

MultiArrayType.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.bytecode.analysis;
  17. import javassist.ClassPool;
  18. import javassist.CtClass;
  19. import javassist.NotFoundException;
  20. /**
  21. * Represents an array of {@link MultiType} instances.
  22. *
  23. * @author Jason T. Greene
  24. */
  25. public class MultiArrayType extends Type {
  26. private MultiType component;
  27. private int dims;
  28. public MultiArrayType(MultiType component, int dims) {
  29. super(null);
  30. this.component = component;
  31. this.dims = dims;
  32. }
  33. public CtClass getCtClass() {
  34. CtClass clazz = component.getCtClass();
  35. if (clazz == null)
  36. return null;
  37. ClassPool pool = clazz.getClassPool();
  38. if (pool == null)
  39. pool = ClassPool.getDefault();
  40. String name = arrayName(clazz.getName(), dims);
  41. try {
  42. return pool.get(name);
  43. } catch (NotFoundException e) {
  44. throw new RuntimeException(e);
  45. }
  46. }
  47. boolean popChanged() {
  48. return component.popChanged();
  49. }
  50. public int getDimensions() {
  51. return dims;
  52. }
  53. public Type getComponent() {
  54. return dims == 1 ? (Type)component : new MultiArrayType(component, dims - 1);
  55. }
  56. public int getSize() {
  57. return 1;
  58. }
  59. public boolean isArray() {
  60. return true;
  61. }
  62. public boolean isAssignableFrom(Type type) {
  63. throw new UnsupportedOperationException("Not implemented");
  64. }
  65. public boolean isReference() {
  66. return true;
  67. }
  68. public boolean isAssignableTo(Type type) {
  69. if (eq(type.getCtClass(), Type.OBJECT.getCtClass()))
  70. return true;
  71. if (eq(type.getCtClass(), Type.CLONEABLE.getCtClass()))
  72. return true;
  73. if (eq(type.getCtClass(), Type.SERIALIZABLE.getCtClass()))
  74. return true;
  75. if (! type.isArray())
  76. return false;
  77. Type typeRoot = getRootComponent(type);
  78. int typeDims = type.getDimensions();
  79. if (typeDims > dims)
  80. return false;
  81. if (typeDims < dims) {
  82. if (eq(typeRoot.getCtClass(), Type.OBJECT.getCtClass()))
  83. return true;
  84. if (eq(typeRoot.getCtClass(), Type.CLONEABLE.getCtClass()))
  85. return true;
  86. if (eq(typeRoot.getCtClass(), Type.SERIALIZABLE.getCtClass()))
  87. return true;
  88. return false;
  89. }
  90. return component.isAssignableTo(typeRoot);
  91. }
  92. public boolean equals(Object o) {
  93. if (! (o instanceof MultiArrayType))
  94. return false;
  95. MultiArrayType multi = (MultiArrayType)o;
  96. return component.equals(multi.component) && dims == multi.dims;
  97. }
  98. public String toString() {
  99. // follows the same detailed formating scheme as component
  100. return arrayName(component.toString(), dims);
  101. }
  102. }