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.

SimpleScope.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.bridge.IMessageHandler;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.bridge.Message;
  17. import org.aspectj.bridge.SourceLocation;
  18. import org.aspectj.weaver.IHasPosition;
  19. import org.aspectj.weaver.ResolvedType;
  20. import org.aspectj.weaver.UnresolvedType;
  21. import org.aspectj.weaver.World;
  22. public class SimpleScope implements IScope {
  23. private static final String[] NoStrings = new String[0];
  24. private static final String[] javaLangPrefixArray = new String[] { "java.lang.", };
  25. private String[] importedPrefixes = javaLangPrefixArray;
  26. private String[] importedNames = NoStrings;
  27. private World world;
  28. private ResolvedType enclosingType;
  29. protected FormalBinding[] bindings;
  30. public SimpleScope(World world, FormalBinding[] bindings) {
  31. super();
  32. this.world = world;
  33. this.bindings = bindings;
  34. }
  35. public UnresolvedType lookupType(String name, IHasPosition location) {
  36. for (String importedName : importedNames) {
  37. // make sure we're matching against the type name rather than part of it
  38. // if (importedName.endsWith("." + name)) {
  39. if (importedName.endsWith(name)) {
  40. return world.resolve(importedName);
  41. }
  42. }
  43. // Check for a primitive
  44. if (name.length() < 8 && Character.isLowerCase(name.charAt(0))) {
  45. // could be a primitive
  46. int len = name.length();
  47. if (len == 3) {
  48. if (name.equals("int")) {
  49. return UnresolvedType.INT;
  50. }
  51. } else if (len == 4) {
  52. if (name.equals("void")) {
  53. return UnresolvedType.VOID;
  54. } else if (name.equals("byte")) {
  55. return UnresolvedType.BYTE;
  56. } else if (name.equals("char")) {
  57. return UnresolvedType.CHAR;
  58. } else if (name.equals("long")) {
  59. return UnresolvedType.LONG;
  60. }
  61. } else if (len == 5) {
  62. if (name.equals("float")) {
  63. return UnresolvedType.FLOAT;
  64. } else if (name.equals("short")) {
  65. return UnresolvedType.SHORT;
  66. }
  67. } else if (len == 6) {
  68. if (name.equals("double")) {
  69. return UnresolvedType.DOUBLE;
  70. }
  71. } else if (len == 7) {
  72. if (name.equals("boolean")) {
  73. return UnresolvedType.BOOLEAN;
  74. }
  75. }
  76. }
  77. // Is it fully qualified?
  78. if (name.indexOf('.') != -1) {
  79. return world.resolve(UnresolvedType.forName(name), true);
  80. }
  81. for (String importedPrefix : importedPrefixes) {
  82. ResolvedType tryType = world.resolve(UnresolvedType.forName(importedPrefix + name), true);
  83. if (!tryType.isMissing()) {
  84. return tryType;
  85. }
  86. }
  87. return world.resolve(UnresolvedType.forName(name), true);
  88. }
  89. public IMessageHandler getMessageHandler() {
  90. return world.getMessageHandler();
  91. }
  92. public FormalBinding lookupFormal(String name) {
  93. for (FormalBinding binding : bindings) {
  94. if (binding.getName().equals(name)) {
  95. return binding;
  96. }
  97. }
  98. return null;
  99. }
  100. public FormalBinding getFormal(int i) {
  101. return bindings[i];
  102. }
  103. public int getFormalCount() {
  104. return bindings.length;
  105. }
  106. public String[] getImportedNames() {
  107. return importedNames;
  108. }
  109. public String[] getImportedPrefixes() {
  110. return importedPrefixes;
  111. }
  112. public void setImportedNames(String[] importedNames) {
  113. this.importedNames = importedNames;
  114. }
  115. public void setImportedPrefixes(String[] importedPrefixes) {
  116. this.importedPrefixes = importedPrefixes;
  117. }
  118. public static FormalBinding[] makeFormalBindings(UnresolvedType[] types, String[] names) {
  119. int len = types.length;
  120. FormalBinding[] bindings = new FormalBinding[len];
  121. for (int i = 0; i < len; i++) {
  122. bindings[i] = new FormalBinding(types[i], names[i], i);
  123. }
  124. return bindings;
  125. }
  126. public ISourceLocation makeSourceLocation(IHasPosition location) {
  127. return new SourceLocation(ISourceLocation.NO_FILE, 0);
  128. }
  129. public void message(IMessage.Kind kind, IHasPosition location1, IHasPosition location2, String message) {
  130. message(kind, location1, message);
  131. message(kind, location2, message);
  132. }
  133. public void message(IMessage.Kind kind, IHasPosition location, String message) {
  134. getMessageHandler().handleMessage(new Message(message, kind, null, makeSourceLocation(location)));
  135. }
  136. public void message(IMessage aMessage) {
  137. getMessageHandler().handleMessage(aMessage);
  138. }
  139. public World getWorld() {
  140. return world;
  141. }
  142. public ResolvedType getEnclosingType() {
  143. return enclosingType;
  144. }
  145. }