1 package org.jacorb.test.common; 2 3 23 24 import java.util.*; 25 import java.util.regex.*; 26 import java.io.*; 27 import java.nio.*; 28 import java.nio.channels.*; 29 import java.nio.charset.*; 30 31 41 public class TestAnnotationsParser 42 { 43 47 private static Map instances = new HashMap(); 48 49 private String filename; 50 private ByteBuffer sourceBuffer = null; 51 private CharSequence source = null; 52 53 private TestAnnotations classAnnotations = null; 54 private Map methodAnnotations = null; 55 56 private boolean annotationsLoaded = false; 57 58 61 private TestAnnotationsParser (Class c) 62 { 63 String className = c.getName(); 64 filename = TestUtils.testHome() 65 + "/src/" + className.replace('.', '/') + ".java"; 66 } 67 68 73 public TestAnnotations getClassAnnotations() 74 { 75 if (!annotationsLoaded) 76 if (!containsAnnotations()) 77 return null; 78 else 79 loadAnnotations(); 80 81 return classAnnotations; 82 } 83 84 93 public TestAnnotations getMethodAnnotations (String methodName) 94 { 95 if (!annotationsLoaded) 96 { 97 if (!containsAnnotations()) 98 return null; 99 else 100 loadAnnotations(); 101 } 102 103 if (methodAnnotations == null) 104 return null; 105 else 106 return (TestAnnotations)methodAnnotations.get (methodName); 107 } 108 109 private static Pattern tagPattern = 110 Pattern.compile ("@jacorb-"); 111 112 117 private boolean containsAnnotations() 118 { 119 if (annotationsLoaded) 120 return classAnnotations != null && methodAnnotations != null; 121 else 122 { 123 Matcher m = tagPattern.matcher(getSource()); 128 if (m.find()) 129 return true; 130 else 131 { 132 annotationsLoaded = true; 133 return false; 134 } 135 } 136 } 137 138 private static Pattern javadocPattern = 139 Pattern.compile ("(/\\*\\*\\s.*?\\*/)\\s*([^\n]*)", Pattern.DOTALL); 140 141 private static Pattern classPattern = 142 Pattern.compile ("class\\s.*|.*?\\sclass\\s.*"); 143 144 private static Pattern methodPattern = 145 Pattern.compile ("public void ([^\\s(]+)\\s*\\(.*"); 146 147 151 private void loadAnnotations() 152 { 153 Matcher m = javadocPattern.matcher (getSource()); 154 boolean isFirst = true; 155 while (m.find()) 156 { 157 String javadoc = m.group(1); 158 String item = m.group(2); 159 if (isFirst) 160 { 161 Matcher cm = classPattern.matcher (item); 162 if (cm.matches()) 163 { 164 classAnnotations = createAnnotations (javadoc); 165 isFirst = false; 166 continue; 167 } 168 } 169 Matcher mm = methodPattern.matcher (item); 170 if (mm.matches()) 171 { 172 String methodName = mm.group(1); 173 TestAnnotations ta = createAnnotations (javadoc); 174 if (ta != null) 175 { 176 if (methodAnnotations == null) 177 methodAnnotations = new HashMap(); 178 methodAnnotations.put (methodName, ta); 179 } 180 } 181 isFirst = false; 182 } 183 source = null; 186 sourceBuffer = null; 187 } 188 189 private static Pattern tagValuePattern = 190 Pattern.compile ("@(jacorb-[a-z-]+)\\s+(\\S+)"); 191 192 197 private TestAnnotations createAnnotations (String javadoc) 198 { 199 String clientSince = null; 200 String serverSince = null; 201 if (classAnnotations != null) 202 { 203 clientSince = classAnnotations.getClientSince(); 205 serverSince = classAnnotations.getServerSince(); 206 } 207 Matcher m = tagValuePattern.matcher (javadoc); 208 while (m.find()) 209 { 210 if (m.group(1).equals ("jacorb-since")) 211 { 212 clientSince = m.group(2); 213 serverSince = m.group(2); 214 } 215 else if (m.group(1).equals ("jacorb-client-since")) 216 clientSince = m.group(2); 217 else if (m.group(1).equals ("jacorb-server-since")) 218 serverSince = m.group(2); 219 } 220 if (clientSince != null || serverSince != null) 221 return new TestAnnotations (clientSince, serverSince); 222 else 223 return null; 224 } 225 226 229 private CharSequence getSource() 230 { 231 if (source == null) 232 { 233 source = Charset.forName("ISO-8859-1").decode (getSourceBuffer()); 234 } 235 return source; 236 } 237 238 242 private ByteBuffer getSourceBuffer() 243 { 244 if (sourceBuffer == null) 245 { 246 try 247 { 248 FileInputStream s = new FileInputStream (filename); 249 FileChannel fc = s.getChannel(); 250 sourceBuffer = fc.map (FileChannel.MapMode.READ_ONLY, 0, fc.size()); 251 } 252 catch (IOException ex) 253 { 254 throw new RuntimeException (ex); 255 } 256 } 257 return sourceBuffer; 258 } 259 260 264 public static TestAnnotationsParser getInstance (Class c) 265 { 266 TestAnnotationsParser result = 267 (TestAnnotationsParser)instances.get(c.getName()); 268 if (result == null) 269 { 270 result = new TestAnnotationsParser(c); 271 instances.put (c.getName(), result); 272 } 273 return result; 274 } 275 276 } 277 | Popular Tags |