1 22 package org.jboss.aop.pointcut.ast; 23 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 27 33 public class ClassExpression 34 { 35 private String original; 36 private Pattern classPattern; 37 private boolean isAnnotation = false; 38 private boolean isInstanceOf = false; 39 private boolean isTypedef = false; 40 41 private boolean isInstanceOfAnnotated = false; 42 43 public ClassExpression(String expr) 44 { 45 original = expr; 46 if (expr.startsWith("@")) 47 { 48 isAnnotation = true; 49 } 50 else 51 { 52 expr = original; 53 if (expr.startsWith("$instanceof{")) 54 { 55 isInstanceOf = true; 56 expr = expr.substring(12, expr.lastIndexOf("}")); 57 isInstanceOfAnnotated = (expr.startsWith("@")); 58 } 59 else if (expr.startsWith("$typedef{")) 60 { 61 isTypedef = true; 62 expr = expr.substring(9, expr.lastIndexOf("}")); 63 } 64 65 if (!isAnnotation) 66 { 67 expr = expr.replaceAll("\\.", "\\\\."); 68 expr = expr.replaceAll("\\*", ".*"); 69 expr = expr.replaceAll("\\[", "\\\\["); 70 expr = expr.replaceAll("]", "\\\\]"); 71 expr = expr.replaceAll("\\$", "\\\\\\$"); 72 classPattern = Pattern.compile(expr); 73 } 74 } 75 76 } 77 78 public boolean isSimple() 79 { 80 return !(isAnnotation || isInstanceOf || isTypedef || isInstanceOfAnnotated); 81 } 82 83 84 public boolean matches(String classname) 85 { 86 if (isAnnotation) return false; 87 Matcher m = classPattern.matcher(classname); 88 return m.matches(); 89 } 90 91 public boolean matchesAnnotation(String annotation) 92 { 93 if (!isAnnotation) return false; 94 Matcher m = classPattern.matcher(annotation); 95 return m.matches(); 96 } 97 98 public boolean isAnnotation() 99 { 100 return this.isAnnotation; 101 } 102 103 public boolean isInstanceOf() 104 { 105 return this.isInstanceOf; 106 } 107 108 public boolean isTypedef() 109 { 110 return this.isTypedef; 111 } 112 113 public boolean isInstanceOfAnnotated() 114 { 115 return this.isInstanceOfAnnotated; 116 } 117 118 public String getInstanceOfAnnotation() 119 { 120 if (!isInstanceOfAnnotated) 121 { 122 return null; 123 } 124 return original.substring(12, original.lastIndexOf("}")); 125 } 126 127 public String getOriginal() 128 { 129 return original; 130 } 131 132 public static String simpleType(Class type) 133 { 134 Class ret = type; 135 if (ret.isArray()) 136 { 137 Class arr = ret; 138 String array = ""; 139 while (arr.isArray()) 140 { 141 array += "[]"; 142 arr = arr.getComponentType(); 143 } 144 return arr.getName() + array; 145 } 146 return ret.getName(); 147 } 148 149 public static void main(String [] args) 150 { 151 System.out.println(simpleType(Float.TYPE)); 152 } 153 } 154 | Popular Tags |