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.

Bindings.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import org.aspectj.bridge.IMessage;
  14. import org.aspectj.weaver.BCException;
  15. import org.aspectj.weaver.UnresolvedType;
  16. public class Bindings {
  17. public static final Bindings NONE = new Bindings(0);
  18. private BindingPattern[] bindings;
  19. public Bindings(BindingPattern[] bindings) {
  20. this.bindings = bindings;
  21. }
  22. public Bindings(int count) {
  23. this(new BindingPattern[count]);
  24. }
  25. public void register(BindingPattern binding, IScope scope) {
  26. int index = binding.getFormalIndex();
  27. BindingPattern existingBinding = bindings[index];
  28. if (existingBinding != null) {
  29. scope.message(IMessage.ERROR, existingBinding, binding, "multiple bindings" + index + ", " + binding);
  30. }
  31. bindings[index] = binding;
  32. }
  33. public void mergeIn(Bindings other, IScope scope) {
  34. for (int i = 0, len = other.bindings.length; i < len; i++) {
  35. if (other.bindings[i] != null) {
  36. register(other.bindings[i], scope);
  37. }
  38. }
  39. }
  40. /**
  41. * signals an error if one has a binding and other doesn't
  42. */
  43. public void checkEquals(Bindings other, IScope scope) {
  44. BindingPattern[] b1 = this.bindings;
  45. BindingPattern[] b2 = other.bindings;
  46. int len = b1.length;
  47. if (len != b2.length) {
  48. throw new BCException("INSANE");
  49. }
  50. for (int i = 0; i < len; i++) {
  51. if (b1[i] == null && b2[i] != null) {
  52. scope.message(IMessage.ERROR, b2[i], "inconsistent binding");
  53. b1[i] = b2[i]; // done just to produce fewer error messages
  54. } else if (b2[i] == null && b1[i] != null) {
  55. scope.message(IMessage.ERROR, b1[i], "inconsistent binding");
  56. b2[i] = b1[i]; // done just to produce fewer error messages
  57. }
  58. }
  59. }
  60. public String toString() {
  61. StringBuilder buf = new StringBuilder("Bindings(");
  62. for (int i = 0, len = bindings.length; i < len; i++) {
  63. if (i > 0)
  64. buf.append(", ");
  65. buf.append(bindings[i]);
  66. }
  67. buf.append(")");
  68. return buf.toString();
  69. }
  70. public int[] getUsedFormals() {
  71. // System.out.println("used: " + this);
  72. int[] ret = new int[bindings.length];
  73. int index = 0;
  74. for (int i = 0, len = bindings.length; i < len; i++) {
  75. if (bindings[i] != null) {
  76. ret[index++] = i;
  77. }
  78. }
  79. int[] newRet = new int[index];
  80. System.arraycopy(ret, 0, newRet, 0, index);
  81. // System.out.println("ret: " + index);
  82. return newRet;
  83. }
  84. public UnresolvedType[] getUsedFormalTypes() {
  85. UnresolvedType[] ret = new UnresolvedType[bindings.length];
  86. int index = 0;
  87. for (BindingPattern binding : bindings) {
  88. if (binding != null) {
  89. ret[index++] = ((BindingTypePattern) binding).getExactType();
  90. }
  91. }
  92. UnresolvedType[] newRet = new UnresolvedType[index];
  93. System.arraycopy(ret, 0, newRet, 0, index);
  94. // System.out.println("ret: " + index);
  95. return newRet;
  96. }
  97. public Bindings copy() {
  98. return new Bindings(bindings.clone());
  99. }
  100. public void checkAllBound(IScope scope) {
  101. for (int i = 0, len = bindings.length; i < len; i++) {
  102. if (bindings[i] == null) {
  103. // ATAJ: avoid warnings for implicit bindings
  104. if (scope.getFormal(i) instanceof FormalBinding.ImplicitFormalBinding) {
  105. bindings[i] = new BindingTypePattern(scope.getFormal(i), false);
  106. } else {
  107. scope.message(IMessage.ERROR, scope.getFormal(i), "formal unbound in pointcut ");
  108. }
  109. }
  110. }
  111. }
  112. public int size() {
  113. return bindings.length;
  114. }
  115. }