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.

NameMangler.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.lang.reflect.Modifier;
  14. public class NameMangler {
  15. // public static final char[] AJC_DOLLAR_PREFIX = { 'a', 'j', 'c', '$' };
  16. // public static final char[] CLINIT = { '<', 'c', 'l', 'i', 'n', 'i', 't', '>' };
  17. public static final String PREFIX = "ajc$";
  18. public static final char[] PREFIX_CHARS = "ajc$".toCharArray();
  19. // public static final char[] INIT = { '<', 'i', 'n', 'i', 't', '>' };
  20. public static final String ITD_PREFIX = PREFIX + "interType$";
  21. // public static final char[] METHOD_ASPECTOF = {'a', 's', 'p','e','c','t','O','f'};
  22. // public static final char[] METHOD_HASASPECT = { 'h', 'a', 's', 'A', 's', 'p', 'e', 'c', 't' };
  23. // public static final char[] STATIC_INITIALIZER = { '<', 'c', 'l', 'i', 'n', 'i', 't', '>' };
  24. public static final String CFLOW_STACK_TYPE = "org.aspectj.runtime.internal.CFlowStack";
  25. public static final String CFLOW_COUNTER_TYPE = "org.aspectj.runtime.internal.CFlowCounter";
  26. public static final UnresolvedType CFLOW_STACK_UNRESOLVEDTYPE = UnresolvedType
  27. .forSignature("Lorg/aspectj/runtime/internal/CFlowStack;");
  28. public static final UnresolvedType CFLOW_COUNTER_UNRESOLVEDTYPE = UnresolvedType
  29. .forSignature("Lorg/aspectj/runtime/internal/CFlowCounter;");
  30. public static final String SOFT_EXCEPTION_TYPE = "org.aspectj.lang.SoftException";
  31. public static final String PERSINGLETON_FIELD_NAME = PREFIX + "perSingletonInstance";
  32. public static final String PERCFLOW_FIELD_NAME = PREFIX + "perCflowStack";
  33. // public static final String PERTHIS_FIELD_NAME = PREFIX + "perSingletonInstance";
  34. // -----
  35. public static final String PERCFLOW_PUSH_METHOD = PREFIX + "perCflowPush";
  36. public static final String PEROBJECT_BIND_METHOD = PREFIX + "perObjectBind";
  37. // PTWIMPL Method and field names
  38. public static final String PERTYPEWITHIN_GETINSTANCE_METHOD = PREFIX + "getInstance";
  39. public static final String PERTYPEWITHIN_CREATEASPECTINSTANCE_METHOD = PREFIX + "createAspectInstance";
  40. public static final String PERTYPEWITHIN_WITHINTYPEFIELD = PREFIX + "withinType";
  41. public static final String PERTYPEWITHIN_GETWITHINTYPENAME_METHOD = "getWithinTypeName";
  42. public static final String AJC_PRE_CLINIT_NAME = PREFIX + "preClinit";
  43. public static final String AJC_POST_CLINIT_NAME = PREFIX + "postClinit";
  44. public static final String INITFAILURECAUSE_FIELD_NAME = PREFIX + "initFailureCause";
  45. public static final String ANNOTATION_CACHE_FIELD_NAME = PREFIX + "anno$";
  46. public static boolean isSyntheticMethod(String methodName, boolean declaredInAspect) {
  47. if (methodName.startsWith(PREFIX)) {
  48. // it's synthetic unless it is an advice method
  49. if (methodName.startsWith("ajc$before") || methodName.startsWith("ajc$after")) {
  50. return false;
  51. } else if (methodName.startsWith("ajc$around")) {
  52. // around advice method is not synthetic, but generated proceed is...
  53. return (methodName.endsWith("proceed"));
  54. } else if (methodName.startsWith("ajc$interMethod$")) {
  55. return false; // body of an itd-m
  56. }
  57. return true;
  58. } else if (methodName.contains("_aroundBody")) {
  59. return true;
  60. }
  61. // these aren't the droids you're looking for...move along...... pr148727
  62. // else if (declaredInAspect) {
  63. // if (methodName.equals("aspectOf") || methodName.equals("hasAspect")) {
  64. // return true;
  65. // }
  66. // }
  67. return false;
  68. }
  69. public static String perObjectInterfaceGet(UnresolvedType aspectType) {
  70. return makeName(aspectType.getNameAsIdentifier(), "perObjectGet");
  71. }
  72. public static String perObjectInterfaceSet(UnresolvedType aspectType) {
  73. return makeName(aspectType.getNameAsIdentifier(), "perObjectSet");
  74. }
  75. public static String perObjectInterfaceField(UnresolvedType aspectType) {
  76. return makeName(aspectType.getNameAsIdentifier(), "perObjectField");
  77. }
  78. // PTWIMPL method names that must include aspect type
  79. public static String perTypeWithinFieldForTarget(UnresolvedType aspectType) {
  80. return makeName(aspectType.getNameAsIdentifier(), "ptwAspectInstance");
  81. }
  82. public static String perTypeWithinLocalAspectOf(UnresolvedType aspectType) {
  83. return makeName(aspectType.getNameAsIdentifier(), "localAspectOf");
  84. }
  85. public static String itdAtDeclareParentsField(UnresolvedType aspectType, UnresolvedType itdType) {
  86. return makeName("instance", aspectType.getNameAsIdentifier(), itdType.getNameAsIdentifier());
  87. }
  88. public static String privilegedAccessMethodForMethod(String name, UnresolvedType objectType, UnresolvedType aspectType) {
  89. return makeName("privMethod", aspectType.getNameAsIdentifier(), objectType.getNameAsIdentifier(), name);
  90. }
  91. /**
  92. * Create the old style (<1.6.9) format getter name which includes the aspect requesting access and the type containing the
  93. * field in the name of the type. At 1.6.9 and above the name is simply ajc$get$<fieldname>
  94. */
  95. public static String privilegedAccessMethodForFieldGet(String name, UnresolvedType objectType, UnresolvedType aspectType) {
  96. StringBuilder nameBuilder = new StringBuilder();
  97. nameBuilder.append(makeName("privFieldGet", aspectType.getNameAsIdentifier(), objectType.getNameAsIdentifier(), name));
  98. return nameBuilder.toString();
  99. }
  100. /**
  101. * Create the old style (<1.6.9) format setter name which includes the aspect requesting access and the type containing the
  102. * field in the name of the type. At 1.6.9 and above the name is simply ajc$set$<fieldname>
  103. */
  104. public static String privilegedAccessMethodForFieldSet(String name, UnresolvedType objectType, UnresolvedType aspectType) {
  105. return makeName("privFieldSet", aspectType.getNameAsIdentifier(), objectType.getNameAsIdentifier(), name);
  106. }
  107. public static String inlineAccessMethodForMethod(String name, UnresolvedType objectType, UnresolvedType aspectType) {
  108. return makeName("inlineAccessMethod", aspectType.getNameAsIdentifier(), objectType.getNameAsIdentifier(), name);
  109. }
  110. public static String inlineAccessMethodForFieldGet(String name, UnresolvedType objectType, UnresolvedType aspectType) {
  111. return makeName("inlineAccessFieldGet", aspectType.getNameAsIdentifier(), objectType.getNameAsIdentifier(), name);
  112. }
  113. public static String inlineAccessMethodForFieldSet(String name, UnresolvedType objectType, UnresolvedType aspectType) {
  114. return makeName("inlineAccessFieldSet", aspectType.getNameAsIdentifier(), objectType.getNameAsIdentifier(), name);
  115. }
  116. /**
  117. * The name of methods corresponding to advice declarations Of the form:
  118. * "ajc$[AdviceKind]$[AspectName]$[NumberOfAdviceInAspect]$[PointcutHash]"
  119. */
  120. public static String adviceName(String nameAsIdentifier, AdviceKind kind, int adviceSeqNumber, int pcdHash) {
  121. String newname = makeName(kind.getName(), nameAsIdentifier, Integer.toString(adviceSeqNumber), Integer.toHexString(pcdHash));
  122. return newname;
  123. }
  124. /**
  125. * This field goes on top-most implementers of the interface the field is declared onto
  126. */
  127. public static String interFieldInterfaceField(UnresolvedType aspectType, UnresolvedType interfaceType, String name) {
  128. return makeName("interField", aspectType.getNameAsIdentifier(), interfaceType.getNameAsIdentifier(), name);
  129. }
  130. /**
  131. * This instance method goes on the interface the field is declared onto as well as its top-most implementors
  132. */
  133. public static String interFieldInterfaceSetter(UnresolvedType aspectType, UnresolvedType interfaceType, String name) {
  134. return makeName("interFieldSet", aspectType.getNameAsIdentifier(), interfaceType.getNameAsIdentifier(), name);
  135. }
  136. /**
  137. * This instance method goes on the interface the field is declared onto as well as its top-most implementors
  138. */
  139. public static String interFieldInterfaceGetter(UnresolvedType aspectType, UnresolvedType interfaceType, String name) {
  140. return makeName("interFieldGet", aspectType.getNameAsIdentifier(), interfaceType.getNameAsIdentifier(), name);
  141. }
  142. /**
  143. * This static method goes on the aspect that declares the inter-type field
  144. */
  145. public static String interFieldSetDispatcher(UnresolvedType aspectType, UnresolvedType onType, String name) {
  146. return makeName("interFieldSetDispatch", aspectType.getNameAsIdentifier(), onType.getNameAsIdentifier(), name);
  147. }
  148. /**
  149. * This static method goes on the aspect that declares the inter-type field
  150. */
  151. public static String interFieldGetDispatcher(UnresolvedType aspectType, UnresolvedType onType, String name) {
  152. return makeName("interFieldGetDispatch", aspectType.getNameAsIdentifier(), onType.getNameAsIdentifier(), name);
  153. }
  154. /**
  155. * This field goes on the class the field is declared onto
  156. */
  157. public static String interFieldClassField(int modifiers, UnresolvedType aspectType, UnresolvedType classType, String name) {
  158. // return name;
  159. if (Modifier.isPublic(modifiers)) {
  160. return name;
  161. }
  162. // // ??? might want to handle case where aspect and class are in same package similar to public
  163. return makeName("interField", makeVisibilityName(modifiers, aspectType), name);
  164. }
  165. // /**
  166. // * This static method goes on the aspect that declares the inter-type field
  167. // */
  168. // public static String classFieldSetDispatcher(UnresolvedType aspectType, UnresolvedType classType, String name) {
  169. // return makeName("interFieldSetDispatch", aspectType.getNameAsIdentifier(),
  170. // classType.getNameAsIdentifier(), name);
  171. // }
  172. //
  173. // /**
  174. // * This static method goes on the aspect that declares the inter-type field
  175. // */
  176. // public static String classFieldGetDispatcher(UnresolvedType aspectType, UnresolvedType classType, String name)
  177. // {
  178. // return makeName(
  179. // "interFieldGetDispatch",
  180. // aspectType.getNameAsIdentifier(),
  181. // classType.getNameAsIdentifier(),
  182. // name);
  183. // }
  184. /**
  185. * This static void method goes on the aspect that declares the inter-type field and is called from the appropriate place
  186. * (target's initializer, or clinit, or topmost implementer's inits), to initialize the field;
  187. */
  188. public static String interFieldInitializer(UnresolvedType aspectType, UnresolvedType classType, String name) {
  189. return makeName("interFieldInit", aspectType.getNameAsIdentifier(), classType.getNameAsIdentifier(), name);
  190. }
  191. // ----
  192. /**
  193. * This method goes on the target type of the inter-type method. (and possibly the topmost-implemeters, if the target type is an
  194. * interface)
  195. */
  196. public static String interMethod(int modifiers, UnresolvedType aspectType, UnresolvedType classType, String name) {
  197. if (Modifier.isPublic(modifiers)) {
  198. return name;
  199. }
  200. // ??? might want to handle case where aspect and class are in same package similar to public
  201. return makeName("interMethodDispatch2", makeVisibilityName(modifiers, aspectType), name);
  202. }
  203. /**
  204. * This static method goes on the declaring aspect of the inter-type method.
  205. */
  206. public static String interMethodDispatcher(UnresolvedType aspectType, UnresolvedType classType, String name) {
  207. return makeName("interMethodDispatch1", aspectType.getNameAsIdentifier(), classType.getNameAsIdentifier(), name);
  208. }
  209. /**
  210. * This static method goes on the declaring aspect of the inter-type method.
  211. */
  212. public static String interMethodBody(UnresolvedType aspectType, UnresolvedType classType, String name) {
  213. return makeName("interMethod", aspectType.getNameAsIdentifier(), classType.getNameAsIdentifier(), name);
  214. }
  215. // ----
  216. /**
  217. * This static method goes on the declaring aspect of the inter-type constructor.
  218. */
  219. public static String preIntroducedConstructor(UnresolvedType aspectType, UnresolvedType targetType) {
  220. return makeName("preInterConstructor", aspectType.getNameAsIdentifier(), targetType.getNameAsIdentifier());
  221. }
  222. /**
  223. * This static method goes on the declaring aspect of the inter-type constructor.
  224. */
  225. public static String postIntroducedConstructor(UnresolvedType aspectType, UnresolvedType targetType) {
  226. return makeName("postInterConstructor", aspectType.getNameAsIdentifier(), targetType.getNameAsIdentifier());
  227. }
  228. // ----
  229. /**
  230. * This static method goes on the target class of the inter-type method.
  231. */
  232. public static String superDispatchMethod(UnresolvedType classType, String name) {
  233. return makeName("superDispatch", classType.getNameAsIdentifier(), name);
  234. }
  235. /**
  236. * This static method goes on the target class of the inter-type method.
  237. */
  238. public static String protectedDispatchMethod(UnresolvedType classType, String name) {
  239. return makeName("protectedDispatch", classType.getNameAsIdentifier(), name);
  240. }
  241. // ----
  242. private static String makeVisibilityName(int modifiers, UnresolvedType aspectType) {
  243. if (Modifier.isPrivate(modifiers)) {
  244. return aspectType.getOutermostType().getNameAsIdentifier();
  245. } else if (Modifier.isProtected(modifiers)) {
  246. throw new RuntimeException("protected inter-types not allowed");
  247. } else if (Modifier.isPublic(modifiers)) {
  248. return "";
  249. } else {
  250. return aspectType.getPackageNameAsIdentifier();
  251. }
  252. }
  253. private static String makeName(String s1, String s2) {
  254. return "ajc$" + s1 + "$" + s2;
  255. }
  256. public static String makeName(String s1, String s2, String s3) {
  257. return "ajc$" + s1 + "$" + s2 + "$" + s3;
  258. }
  259. public static String makeName(String s1, String s2, String s3, String s4) {
  260. return "ajc$" + s1 + "$" + s2 + "$" + s3 + "$" + s4;
  261. }
  262. public static String cflowStack(CrosscuttingMembers xcut) {
  263. return makeName("cflowStack", Integer.toHexString(xcut.getCflowEntries().size()));
  264. }
  265. public static String cflowCounter(CrosscuttingMembers xcut) {
  266. return makeName("cflowCounter", Integer.toHexString(xcut.getCflowEntries().size()));
  267. }
  268. public static String makeClosureClassName(UnresolvedType enclosingType, String suffix) {
  269. return enclosingType.getName() + "$AjcClosure" + suffix;
  270. }
  271. public static String aroundShadowMethodName(Member shadowSig, String suffixTag) {
  272. StringBuffer ret = new StringBuffer();
  273. ret.append(getExtractableName(shadowSig)).append("_aroundBody").append(suffixTag);
  274. return ret.toString();
  275. }
  276. public static String aroundAdviceMethodName(Member shadowSig, String suffixTag) {
  277. StringBuffer ret = new StringBuffer();
  278. ret.append(getExtractableName(shadowSig)).append("_aroundBody").append(suffixTag).append("$advice");
  279. return ret.toString();
  280. }
  281. public static String getExtractableName(Member shadowSignature) {
  282. String name = shadowSignature.getName();
  283. MemberKind kind = shadowSignature.getKind();
  284. if (kind == Member.CONSTRUCTOR) {
  285. return "init$";
  286. } else if (kind == Member.STATIC_INITIALIZATION) {
  287. return "clinit$";
  288. } else {
  289. return name;
  290. }
  291. }
  292. public static String proceedMethodName(String adviceMethodName) {
  293. return adviceMethodName + "proceed";
  294. }
  295. }