Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Util.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.internal.tools.build;
  14. import java.io.*;
  15. import java.io.File;
  16. import java.io.FileFilter;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. /**
  21. * Build-only utilities.
  22. * Many mirror utils module APIs.
  23. */
  24. public class Util {
  25. public static class Constants {
  26. public static final String TESTSRC = "testsrc";
  27. public static final String JAVA5_SRC = "java5-src";
  28. public static final String JAVA5_TESTSRC = "java5-testsrc";
  29. }
  30. // XXX quick hack for Java 5 support
  31. public static final boolean JAVA5_VM;
  32. static {
  33. boolean java5VM = false;
  34. try {
  35. java5VM = (null != Class.forName("java.lang.annotation.Annotation"));
  36. } catch (Throwable t) {
  37. // ignore
  38. }
  39. JAVA5_VM = java5VM;
  40. }
  41. /**
  42. * Map version in long form to short,
  43. * e.g., replacing "alpha" with "a"
  44. */
  45. public static String shortVersion(String version) {
  46. version = Util.replace(version, "alpha", "a");
  47. version = Util.replace(version, "beta", "b");
  48. version = Util.replace(version, "candidate", "rc");
  49. version = Util.replace(version, "development", "d");
  50. version = Util.replace(version, "dev", "d");
  51. return version;
  52. }
  53. /**
  54. * Replace any instances of {replace} in {input} with {with}.
  55. * @param input the String to search/replace
  56. * @param replace the String to search for in input
  57. * @param with the String to replace with in input
  58. * @return input if it has no replace, otherwise a new String
  59. */
  60. public static String replace(String input, String replace, String with) {
  61. int loc = input.indexOf(replace);
  62. if (-1 != loc) {
  63. String result = input.substring(0, loc);
  64. result += with;
  65. int start = loc + replace.length();
  66. if (start < input.length()) {
  67. result += input.substring(start);
  68. }
  69. input = result;
  70. }
  71. return input;
  72. }
  73. /** @return false if filter returned false for any file in baseDir subtree */
  74. public static boolean visitFiles(File baseDir, FileFilter filter) {
  75. Util.iaxIfNotCanReadDir(baseDir, "baseDir");
  76. Util.iaxIfNull(filter, "filter");
  77. File[] files = baseDir.listFiles();
  78. boolean passed = true;
  79. for (int i = 0; passed && (i < files.length); i++) {
  80. passed = files[i].isDirectory()
  81. ? visitFiles(files[i], filter)
  82. : filter.accept(files[i]);
  83. }
  84. return passed;
  85. }
  86. /** @throws IllegalArgumentException if cannot read dir */
  87. public static void iaxIfNotCanReadDir(File dir, String name) {
  88. if (!canReadDir(dir)) {
  89. throw new IllegalArgumentException(name + " dir not readable: " + dir);
  90. }
  91. }
  92. /** @throws IllegalArgumentException if cannot read file */
  93. public static void iaxIfNotCanReadFile(File file, String name) {
  94. if (!canReadFile(file)) {
  95. throw new IllegalArgumentException(name + " file not readable: " + file);
  96. }
  97. }
  98. /** @throws IllegalArgumentException if cannot write dir */
  99. public static void iaxIfNotCanWriteDir(File dir, String name) {
  100. if (!canWriteDir(dir)) {
  101. throw new IllegalArgumentException(name + " dir not writeable: " + dir);
  102. }
  103. }
  104. /** @throws IllegalArgumentException if input is null */
  105. public static void iaxIfNull(Object input, String name) {
  106. if (null == input) {
  107. throw new IllegalArgumentException("null " + name);
  108. }
  109. }
  110. /** render exception to String */
  111. public static String renderException(Throwable thrown) {
  112. if (null == thrown) {
  113. return "(Throwable) null";
  114. }
  115. StringWriter sw = new StringWriter();
  116. PrintWriter pw = new PrintWriter(sw, true);
  117. pw.println(thrown.getMessage());
  118. thrown.printStackTrace(pw);
  119. pw.flush();
  120. return sw.getBuffer().toString();
  121. }
  122. /** @return true if dir is a writable directory */
  123. public static boolean canWriteDir(File dir) {
  124. return (null != dir) && dir.canWrite() && dir.isDirectory();
  125. }
  126. public static String path(String first, String second) {
  127. return first + File.separator + second;
  128. }
  129. public static String path(String[] segments) {
  130. StringBuffer sb = new StringBuffer();
  131. if ((null != segments)) {
  132. for (int i = 0; i < segments.length; i++) {
  133. if (0 < i) {
  134. sb.append(File.separator);
  135. }
  136. sb.append(segments[i]);
  137. }
  138. }
  139. return sb.toString();
  140. }
  141. /** @return true if dir is a readable directory */
  142. public static boolean canReadDir(File dir) {
  143. return (null != dir) && dir.canRead() && dir.isDirectory();
  144. }
  145. /** @return true if dir is a readable file */
  146. public static boolean canReadFile(File file) {
  147. return (null != file) && file.canRead() && file.isFile();
  148. }
  149. /**
  150. * Delete file or directory.
  151. * @param dir the File file or directory to delete.
  152. * @return true if all contents of dir were deleted
  153. */
  154. public static boolean delete(File dir) {
  155. return deleteContents(dir) && dir.delete();
  156. }
  157. /**
  158. * Delete contents of directory.
  159. * The directory itself is not deleted.
  160. * @param dir the File directory whose contents should be deleted.
  161. * @return true if all contents of dir were deleted
  162. */
  163. public static boolean deleteContents(File dir) {
  164. if ((null == dir) || !dir.canWrite()) {
  165. return false;
  166. } else if (dir.isDirectory()) {
  167. File[] files = dir.listFiles();
  168. for (int i = 0; i < files.length; i++) {
  169. if (!deleteContents(files[i]) || !files[i].delete()) {
  170. return false;
  171. }
  172. }
  173. }
  174. return true;
  175. }
  176. /** @return File temporary directory with the given prefix */
  177. public static File makeTempDir(String prefix) {
  178. if (null == prefix) {
  179. prefix = "tempDir";
  180. }
  181. File tempFile = null;
  182. for (int i = 0; i < 10; i++) {
  183. try {
  184. tempFile = File.createTempFile(prefix,"tmp");
  185. tempFile.delete();
  186. if (tempFile.mkdirs()) {
  187. break;
  188. }
  189. tempFile = null;
  190. } catch (IOException e) {
  191. }
  192. }
  193. return tempFile;
  194. }
  195. /**
  196. * Close stream with the usual checks.
  197. * @param stream the InputStream to close - ignored if null
  198. * @return null if closed without IOException, message otherwise
  199. */
  200. public static String close(Writer stream) {
  201. String result = null;
  202. if (null != stream) {
  203. try {
  204. stream.close();
  205. } catch(IOException e) {
  206. result = e.getMessage();
  207. }
  208. }
  209. return result;
  210. }
  211. /**
  212. * @param list the Object[] to test
  213. * @return true if list is null or empty
  214. */
  215. public static boolean isEmpty(Object[] list) {
  216. return ((null == list) || (0 == list.length));
  217. }
  218. }