1 16 package org.apache.commons.attributes; 17 18 import java.lang.reflect.Field ; 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 22 import java.util.List ; 23 import java.util.Iterator ; 24 import java.util.Set ; 25 import java.util.Map ; 26 import java.util.Collection ; 27 28 class Util { 29 30 public static String getSignature (Method m) { 31 return m.getName () + "(" + getParameterList (m.getParameterTypes ()) + ")"; 32 } 33 34 public static String getSignature (Constructor c) { 35 return "(" + getParameterList (c.getParameterTypes ()) + ")"; 36 } 37 38 public static String decodedClassName (String rawName) throws IllegalArgumentException { 39 if (!rawName.startsWith ("[")) { 40 return rawName; 41 } else { 42 StringBuffer nesting = new StringBuffer (); 43 int i = 0; 44 while (rawName.charAt (i) == '[') { 45 nesting.append ("[]"); 46 i++; 47 } 48 String type = ""; 49 switch (rawName.charAt (i)) { 50 case 'B': type = "byte"; break; 51 case 'C': type = "char"; break; 52 case 'D': type = "double"; break; 53 case 'F': type = "float"; break; 54 case 'I': type = "int"; break; 55 case 'J': type = "long"; break; 56 case 'L': type = rawName.substring (i + 1, rawName.length () - 1); break; 57 case 'S': type = "short"; break; 58 case 'Z': type = "boolean"; break; 59 default: throw new IllegalArgumentException ("Can't decode " + rawName); 60 } 61 62 return type + nesting.toString (); 63 } 64 } 65 66 public static String getParameterList (Class [] params) { 67 StringBuffer sb = new StringBuffer (); 68 for (int i = 0; i < params.length; i++) { 69 if (i > 0) { 70 sb.append (","); 71 } 72 sb.append (decodedClassName (params[i].getName ())); 73 } 74 return sb.toString (); 75 } 76 77 public static void checkTarget (int target, Object attribute, String element) { 78 Target targetAttr = (Target) Attributes.getAttribute (attribute.getClass (), Target.class); 79 if (targetAttr == null) { 80 return; 81 } 82 83 if ((targetAttr.getFlags () & target) == 0) { 84 throw new InvalidAttributeTargetError (attribute.getClass ().getName (), element, targetAttr.getFlags ()); 85 } 86 } 87 88 public static void checkTarget (int target, Set attributes, String element) { 89 Iterator iter = attributes.iterator (); 90 while (iter.hasNext ()) { 91 checkTarget (target, iter.next (), element); 92 } 93 } 94 95 public static void validateRepository (Class owningClass, AttributeRepositoryClass repo) { 96 checkTarget (Target.CLASS, repo.getClassAttributes (), owningClass.getName ()); 97 98 Map fieldAttrs = repo.getFieldAttributes (); 99 for (Iterator iter = fieldAttrs.keySet ().iterator(); iter.hasNext ();) { 100 String fieldName = (String ) iter.next (); 101 checkTarget (Target.FIELD, (Collection ) fieldAttrs.get (fieldName), owningClass.getName () + "." + fieldName); 102 } 103 104 Map ctorAttrs = repo.getConstructorAttributes (); 105 for (Iterator iter = ctorAttrs.keySet ().iterator(); iter.hasNext ();) { 106 String ctorName = (String ) iter.next (); 107 List bundle = (List ) ctorAttrs.get (ctorName); 108 109 for (int i = 0; i < bundle.size (); i++) { 110 switch (i) { 111 case 0: 112 checkTarget (Target.CONSTRUCTOR, (Collection ) bundle.get (0), owningClass.getName () + "." + ctorName); 113 break; 114 115 default: 116 checkTarget (Target.CONSTRUCTOR_PARAMETER, (Collection ) bundle.get (i), "parameter " + (i) + " of " + owningClass.getName () + ctorName); 117 } 118 } 119 } 120 121 Map methodAttrs = repo.getMethodAttributes (); 122 for (Iterator iter = methodAttrs.keySet ().iterator(); iter.hasNext ();) { 123 String methodName = (String ) iter.next (); 124 List bundle = (List ) methodAttrs.get (methodName); 125 126 for (int i = 0; i < bundle.size (); i++) { 127 switch (i) { 128 case 0: 129 checkTarget (Target.METHOD, (Collection ) bundle.get (0), owningClass.getName () + "." + methodName); 130 break; 131 132 case 1: 133 checkTarget (Target.RETURN, (Collection ) bundle.get (1), "return value of " + owningClass.getName () + "." + methodName); 134 break; 135 136 default: 137 checkTarget (Target.METHOD_PARAMETER, (Collection ) bundle.get (i), "parameter " + (i - 1) + " of " + owningClass.getName () + "." + methodName); 138 } 139 } 140 } 141 } 142 } | Popular Tags |