View Javadoc
1 /* 2 ContractChecker 3 4 Copyright (C) 2003 Jose San Leandro Armend?riz 5 jsanleandro@yahoo.es 6 chousz@yahoo.com 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public 10 License as published by the Free Software Foundation; either 11 version 2 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-307 USA 21 22 23 Thanks to ACM S.L. for distributing this library under the LGPL license. 24 Contact info: jsr000@terra.es 25 Postal Address: c/Playa de Lagoa, 1 26 Urb. Valdecaba?as 27 Boadilla del monte 28 29 ****************************************************************************** 30 * 31 * Filename: $RCSfile: ContractCheckerUtils.java,v $ 32 * 33 * Author: Jose San Leandro Armend?riz 34 * 35 * Description: Provides some general-purpose methods needed by 36 * ContractChecker. 37 * 38 * File version: $Revision: 1.3 $ 39 * 40 * Project version: $Name: $ 41 * ("Name" means no concrete version has been checked out) 42 * 43 * $Id: ContractCheckerUtils.java,v 1.3 2004/01/11 17:33:05 chous Exp $ 44 * 45 */ 46 package org.acmsl.contractchecker; 47 48 /* 49 * Importing some Ant classes. 50 */ 51 import org.apache.tools.ant.Project; 52 import org.apache.tools.ant.types.FileSet; 53 import org.apache.tools.ant.types.Path; 54 55 /* 56 * Importing some JDK1.3 classes. 57 */ 58 import java.io.BufferedReader; 59 import java.io.File; 60 import java.io.FileWriter; 61 import java.io.FileNotFoundException; 62 import java.io.FileReader; 63 import java.io.IOException; 64 import java.io.PrintWriter; 65 import java.lang.ref.WeakReference; 66 import java.util.ArrayList; 67 import java.util.Collection; 68 69 /*** 70 * Provides some general-purpose methods needed by ContractChecker. 71 * @author <a href="mailto:jsanleandro@yahoo.es" 72 >Jose San Leandro Armend?riz</a> 73 * @version $Revision: 1.3 $ 74 * @testcase test.org.acmsl.contractchecker.TestContractCheckerUtils 75 */ 76 public abstract class ContractCheckerUtils 77 { 78 /*** 79 * Empty fileset array. 80 */ 81 public static final FileSet[] EMPTY_FILESET_ARRAY = new FileSet[0]; 82 83 /*** 84 * Singleton implemented as a weak reference. 85 */ 86 private static WeakReference m__Singleton; 87 88 /*** 89 * Specifies a new weak reference. 90 * @param utils the utils instance to use. 91 */ 92 protected static void setReference(ContractCheckerUtils utils) 93 { 94 m__Singleton = new WeakReference(utils); 95 } 96 97 /*** 98 * Retrieves the weak reference. 99 * @return such reference. 100 */ 101 protected static WeakReference getReference() 102 { 103 return m__Singleton; 104 } 105 106 /*** 107 * Retrieves a ContractCheckerUtils instance. 108 * @return such instance. 109 */ 110 public static ContractCheckerUtils getInstance() 111 { 112 ContractCheckerUtils result = null; 113 114 WeakReference reference = getReference(); 115 116 if (reference != null) 117 { 118 result = (ContractCheckerUtils) reference.get(); 119 } 120 121 if (result == null) 122 { 123 result = new ContractCheckerUtils() {}; 124 125 setReference(result); 126 } 127 128 return result; 129 } 130 131 /*** 132 * Protected constructor to avoid accidental instantiation. 133 */ 134 protected ContractCheckerUtils() {}; 135 136 /*** 137 * Writes a file referred by given path, with given contents. 138 * @param filePath the path of the file. 139 * @param contents the text to write. 140 * @throws FileNotFoundException if the file is not found. 141 * @throws SecurityException if the security manager forbids this 142 operation. 143 * @throws IOException if any other I/O error occurs. 144 */ 145 public void writeFile(String filePath, String contents) 146 throws FileNotFoundException, 147 SecurityException, 148 IOException 149 { 150 writeFile(new File(filePath), contents); 151 } 152 153 /*** 154 * Writes a file with given contents. 155 * @param file the file to write. 156 * @param contents the text to write. 157 * @exception FileNotFoundException if the file is not found. 158 * @exception SecurityException if the security manager forbids this 159 operation. 160 * @exception IOException if any other I/O error occurs. 161 */ 162 public void writeFile(File file, String contents) 163 throws FileNotFoundException, 164 SecurityException, 165 IOException 166 { 167 if ( (file != null) 168 && (contents != null)) 169 { 170 FileWriter t_fwWriter = new FileWriter(file); 171 172 PrintWriter t_pwWriter = new PrintWriter(t_fwWriter); 173 174 t_pwWriter.println(contents); 175 176 t_pwWriter.close(); 177 178 t_fwWriter.close(); 179 } 180 } 181 182 /*** 183 * Retrieves the package name of the aspects associated to 184 * given package. 185 * @param packageName the original package. 186 * @return the package for the associated aspects. 187 */ 188 public String retrieveAspectPackage(String packageName) 189 { 190 return "aspects." + packageName; 191 } 192 193 /*** 194 * Retrieves the folder for the aspects associated to given 195 * package. 196 * @param parentFolder the parent folder. 197 * @param packageName the original package. 198 * @return the folder in which the associated aspects should be 199 * generated. 200 */ 201 public File retrieveAspectFolder(File parentFolder, String packageName) 202 { 203 File result = parentFolder; 204 205 if (result != null) 206 { 207 result = 208 new File( 209 parentFolder.getAbsolutePath() 210 + File.separator 211 + packageToFilePath(packageName)); 212 } 213 214 return result; 215 } 216 217 /*** 218 * Translates given package name to a relative file path. 219 * @param packageName the pacjage name. 220 * @return such path 221 */ 222 public String packageToFilePath(String packageName) 223 { 224 String result = packageName; 225 226 if (result != null) 227 { 228 result = result.replaceAll("//.", File.separator); 229 } 230 231 return result; 232 } 233 234 /*** 235 * Removes the exception declaration from given method signature. 236 * @param methodSignature the method signature. 237 * @return the updated signature. 238 */ 239 public String removeExceptions(String methodSignature) 240 { 241 String result = methodSignature; 242 243 if (result != null) 244 { 245 // Should be done with regexps, but it would 246 // involve adding dependency on acmsl-commons. 247 int t_iThrowsPosition = result.indexOf(" throws "); 248 249 if (t_iThrowsPosition > -1) 250 { 251 result = 252 result.substring( 253 0, t_iThrowsPosition); 254 } 255 } 256 257 return result; 258 } 259 260 /*** 261 * Removes the return declaration from given method signature. 262 * @param methodSignature the method signature. 263 * @return the updated signature. 264 */ 265 public String removeReturn(String methodSignature) 266 { 267 String result = methodSignature; 268 269 if (result != null) 270 { 271 result = result.trim(); 272 273 // Should be done with regexps, but it would 274 // involve adding dependency on acmsl-commons. 275 int t_iReturnPosition = result.indexOf(" "); 276 277 if (t_iReturnPosition > -1) 278 { 279 result = 280 result.substring( 281 t_iReturnPosition 282 + 1); // " ".length()); 283 } 284 } 285 286 return result; 287 } 288 289 /*** 290 * Translates given path to a collection of filesets. 291 * @param path the path to translate. 292 * @return the filesets. 293 */ 294 public FileSet[] toFileSets(Path path) 295 { 296 Collection t_cResult = new ArrayList(); 297 298 if (path != null) 299 { 300 String[] t_astrLocations = path.list(); 301 302 if (t_astrLocations != null) 303 { 304 for (int t_iLocationIndex = 0; 305 t_iLocationIndex < t_astrLocations.length; 306 t_iLocationIndex++) 307 { 308 t_cResult.add( 309 toFileSet( 310 t_astrLocations[t_iLocationIndex], 311 path.getProject())); 312 } 313 } 314 } 315 316 return (FileSet[]) t_cResult.toArray(EMPTY_FILESET_ARRAY); 317 } 318 319 /*** 320 * BUilds a FileSet for given location. 321 * @param location the location. 322 * @param project the project. 323 * @return the fileset. 324 */ 325 public FileSet toFileSet(String location, Project project) 326 { 327 FileSet result = null; 328 329 if (location != null) 330 { 331 File t_Location = new File(location); 332 333 result = new FileSet(); 334 result.setProject(project); 335 336 if (t_Location.isDirectory()) 337 { 338 result.setDir(t_Location); 339 result.setIncludes("**/*.java"); 340 } 341 else if (t_Location.isFile()) 342 { 343 result.setIncludesfile(t_Location); 344 } 345 } 346 347 return result; 348 } 349 }

This page was automatically generated by Maven