1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.attributes;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.Constructor;
  19. import java.lang.reflect.Method;
  20. import java.util.List;
  21. import java.util.Iterator;
  22. import java.util.Set;
  23. import java.util.Map;
  24. import java.util.Collection;
  25. class Util {
  26. public static String getSignature (Method m) {
  27. return m.getName () + "(" + getParameterList (m.getParameterTypes ()) + ")";
  28. }
  29. public static String getSignature (Constructor c) {
  30. return "(" + getParameterList (c.getParameterTypes ()) + ")";
  31. }
  32. public static String decodedClassName (String rawName) throws IllegalArgumentException {
  33. if (!rawName.startsWith ("[")) {
  34. return rawName;
  35. } else {
  36. StringBuffer nesting = new StringBuffer ();
  37. int i = 0;
  38. while (rawName.charAt (i) == '[') {
  39. nesting.append ("[]");
  40. i++;
  41. }
  42. String type = "";
  43. switch (rawName.charAt (i)) {
  44. case 'B': type = "byte"; break;
  45. case 'C': type = "char"; break;
  46. case 'D': type = "double"; break;
  47. case 'F': type = "float"; break;
  48. case 'I': type = "int"; break;
  49. case 'J': type = "long"; break;
  50. case 'L': type = rawName.substring (i + 1, rawName.length () - 1); break;
  51. case 'S': type = "short"; break;
  52. case 'Z': type = "boolean"; break;
  53. default: throw new IllegalArgumentException ("Can't decode " + rawName);
  54. }
  55. return type + nesting.toString ();
  56. }
  57. }
  58. public static String getParameterList (Class[] params) {
  59. StringBuffer sb = new StringBuffer ();
  60. for (int i = 0; i < params.length; i++) {
  61. if (i > 0) {
  62. sb.append (",");
  63. }
  64. sb.append (decodedClassName (params[i].getName ()));
  65. }
  66. return sb.toString ();
  67. }
  68. public static void checkTarget (int target, Object attribute, String element) {
  69. Target targetAttr = (Target) Attributes.getAttribute (attribute.getClass (), Target.class);
  70. if (targetAttr == null) {
  71. return;
  72. }
  73. if ((targetAttr.getFlags () & target) == 0) {
  74. throw new InvalidAttributeTargetError (attribute.getClass ().getName (), element, targetAttr.getFlags ());
  75. }
  76. }
  77. public static void checkTarget (int target, Set attributes, String element) {
  78. Iterator iter = attributes.iterator ();
  79. while (iter.hasNext ()) {
  80. checkTarget (target, iter.next (), element);
  81. }
  82. }
  83. public static void validateRepository (Class owningClass, AttributeRepositoryClass repo) {
  84. checkTarget (Target.CLASS, repo.getClassAttributes (), owningClass.getName ());
  85. Map fieldAttrs = repo.getFieldAttributes ();
  86. for (Iterator iter = fieldAttrs.keySet ().iterator(); iter.hasNext ();) {
  87. String fieldName = (String) iter.next ();
  88. checkTarget (Target.FIELD, (Collection) fieldAttrs.get (fieldName), owningClass.getName () + "." + fieldName);
  89. }
  90. Map ctorAttrs = repo.getConstructorAttributes ();
  91. for (Iterator iter = ctorAttrs.keySet ().iterator(); iter.hasNext ();) {
  92. String ctorName = (String) iter.next ();
  93. List bundle = (List) ctorAttrs.get (ctorName);
  94. for (int i = 0; i < bundle.size (); i++) {
  95. switch (i) {
  96. case 0:
  97. checkTarget (Target.CONSTRUCTOR, (Collection) bundle.get (0), owningClass.getName () + "." + ctorName);
  98. break;
  99. default:
  100. checkTarget (Target.CONSTRUCTOR_PARAMETER, (Collection) bundle.get (i), "parameter " + (i) + " of " + owningClass.getName () + ctorName);
  101. }
  102. }
  103. }
  104. Map methodAttrs = repo.getMethodAttributes ();
  105. for (Iterator iter = methodAttrs.keySet ().iterator(); iter.hasNext ();) {
  106. String methodName = (String) iter.next ();
  107. List bundle = (List) methodAttrs.get (methodName);
  108. for (int i = 0; i < bundle.size (); i++) {
  109. switch (i) {
  110. case 0:
  111. checkTarget (Target.METHOD, (Collection) bundle.get (0), owningClass.getName () + "." + methodName);
  112. break;
  113. case 1:
  114. checkTarget (Target.RETURN, (Collection) bundle.get (1), "return value of " + owningClass.getName () + "." + methodName);
  115. break;
  116. default:
  117. checkTarget (Target.METHOD_PARAMETER, (Collection) bundle.get (i), "parameter " + (i - 1) + " of " + owningClass.getName () + "." + methodName);
  118. }
  119. }
  120. }
  121. }
  122. }