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.

StringMaker.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.runtime.reflect;
  14. import java.lang.reflect.Modifier;
  15. class StringMaker {
  16. boolean shortTypeNames = true;
  17. boolean includeArgs = true;
  18. boolean includeThrows = false;
  19. boolean includeModifiers = false;
  20. boolean shortPrimaryTypeNames = false;
  21. boolean includeJoinPointTypeName = true;
  22. boolean includeEnclosingPoint = true;
  23. boolean shortKindName = true;
  24. int cacheOffset;
  25. static StringMaker shortStringMaker;
  26. static {
  27. shortStringMaker = new StringMaker();
  28. shortStringMaker.shortTypeNames = true;
  29. shortStringMaker.includeArgs = false;
  30. shortStringMaker.includeThrows = false;
  31. shortStringMaker.includeModifiers = false;
  32. shortStringMaker.shortPrimaryTypeNames = true;
  33. shortStringMaker.includeJoinPointTypeName = false;
  34. shortStringMaker.includeEnclosingPoint = false;
  35. shortStringMaker.cacheOffset = 0;
  36. }
  37. static StringMaker middleStringMaker;
  38. static {
  39. middleStringMaker = new StringMaker();
  40. middleStringMaker.shortTypeNames = true;
  41. middleStringMaker.includeArgs = true;
  42. middleStringMaker.includeThrows = false;
  43. middleStringMaker.includeModifiers = false;
  44. middleStringMaker.shortPrimaryTypeNames = false;
  45. shortStringMaker.cacheOffset = 1;
  46. }
  47. static StringMaker longStringMaker;
  48. static {
  49. longStringMaker = new StringMaker();
  50. longStringMaker.shortTypeNames = false;
  51. longStringMaker.includeArgs = true;
  52. longStringMaker.includeThrows = false;
  53. longStringMaker.includeModifiers = true;
  54. longStringMaker.shortPrimaryTypeNames = false;
  55. longStringMaker.shortKindName = false;
  56. longStringMaker.cacheOffset = 2;
  57. }
  58. String makeKindName(String name) {
  59. int dash = name.lastIndexOf('-');
  60. if (dash == -1) return name;
  61. return name.substring(dash+1);
  62. }
  63. String makeModifiersString(int modifiers) {
  64. if (!includeModifiers) return "";
  65. String str = Modifier.toString(modifiers);
  66. if (str.length() == 0) return "";
  67. return str + " ";
  68. }
  69. String stripPackageName(String name) {
  70. int dot = name.lastIndexOf('.');
  71. if (dot == -1) return name;
  72. return name.substring(dot+1);
  73. }
  74. String makeTypeName(Class<?> type, String typeName, boolean shortName) {
  75. if (type == null) return "ANONYMOUS";
  76. if (type.isArray()) {
  77. Class<?> componentType = type.getComponentType();
  78. return makeTypeName(componentType, componentType.getName(), shortName) + "[]";
  79. }
  80. if (shortName) {
  81. return stripPackageName(typeName).replace('$', '.');
  82. } else {
  83. return typeName.replace('$', '.');
  84. }
  85. }
  86. public String makeTypeName(Class<?> type) {
  87. return makeTypeName(type, type.getName(),shortTypeNames);
  88. }
  89. public String makePrimaryTypeName(Class<?> type, String typeName) {
  90. return makeTypeName(type, typeName, shortPrimaryTypeNames);
  91. }
  92. public void addTypeNames(StringBuffer buf, Class[] types) {
  93. for (int i = 0; i < types.length; i++) {
  94. if (i > 0) buf.append(", ");
  95. buf.append(makeTypeName(types[i]));
  96. }
  97. }
  98. public void addSignature(StringBuffer buf, Class[] types) {
  99. if (types == null) return;
  100. if (!includeArgs) {
  101. if (types.length == 0) {
  102. buf.append("()");
  103. return;
  104. } else {
  105. buf.append("(..)");
  106. return;
  107. }
  108. }
  109. buf.append("(");
  110. addTypeNames(buf, types);
  111. buf.append(")");
  112. }
  113. public void addThrows(StringBuffer buf, Class[] types) {
  114. if (!includeThrows || types == null || types.length == 0) return;
  115. buf.append(" throws ");
  116. addTypeNames(buf, types);
  117. }
  118. }