Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import java.io.BufferedReader; |
aoqi@0 | 25 | import java.io.File; |
aoqi@0 | 26 | import java.io.FileNotFoundException; |
aoqi@0 | 27 | import java.io.FileWriter; |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.io.InputStreamReader; |
aoqi@0 | 30 | import java.io.PrintWriter; |
aoqi@0 | 31 | import java.io.StringWriter; |
aoqi@0 | 32 | import java.net.URI; |
aoqi@0 | 33 | import java.nio.charset.Charset; |
aoqi@0 | 34 | import java.nio.file.Files; |
aoqi@0 | 35 | import java.nio.file.Path; |
aoqi@0 | 36 | import java.nio.file.Paths; |
aoqi@0 | 37 | import java.nio.file.StandardOpenOption; |
aoqi@0 | 38 | import java.util.ArrayList; |
aoqi@0 | 39 | import java.util.Arrays; |
aoqi@0 | 40 | import java.util.Collection; |
aoqi@0 | 41 | import java.util.Collections; |
aoqi@0 | 42 | import java.util.EnumSet; |
aoqi@0 | 43 | import java.util.List; |
aoqi@0 | 44 | import java.util.Map; |
aoqi@0 | 45 | import java.util.Set; |
aoqi@0 | 46 | import java.util.regex.Matcher; |
aoqi@0 | 47 | import java.util.regex.Pattern; |
aoqi@0 | 48 | |
aoqi@0 | 49 | import javax.tools.JavaCompiler; |
aoqi@0 | 50 | import javax.tools.JavaFileObject; |
aoqi@0 | 51 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 52 | import javax.tools.ToolProvider; |
aoqi@0 | 53 | |
aoqi@0 | 54 | import com.sun.source.util.JavacTask; |
aoqi@0 | 55 | import com.sun.tools.javac.api.JavacTaskImpl; |
aoqi@0 | 56 | |
aoqi@0 | 57 | import sun.tools.jar.Main; |
aoqi@0 | 58 | |
aoqi@0 | 59 | import static java.nio.file.StandardCopyOption.*; |
aoqi@0 | 60 | |
aoqi@0 | 61 | /** |
aoqi@0 | 62 | * Toolbox for jtreg tests. |
aoqi@0 | 63 | */ |
aoqi@0 | 64 | |
aoqi@0 | 65 | public class ToolBox { |
aoqi@0 | 66 | |
aoqi@0 | 67 | public static final String lineSeparator = System.getProperty("line.separator"); |
aoqi@0 | 68 | public static final String jdkUnderTest = System.getProperty("test.jdk"); |
aoqi@0 | 69 | public static final Path javaBinary = Paths.get(jdkUnderTest, "bin", "java"); |
aoqi@0 | 70 | public static final Path javacBinary = Paths.get(jdkUnderTest, "bin", "javac"); |
aoqi@0 | 71 | |
aoqi@0 | 72 | public static final List<String> testToolVMOpts; |
aoqi@0 | 73 | public static final List<String> testVMOpts; |
aoqi@0 | 74 | |
aoqi@0 | 75 | private static final Charset defaultCharset = Charset.defaultCharset(); |
aoqi@0 | 76 | |
aoqi@0 | 77 | static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
aoqi@0 | 78 | |
aoqi@0 | 79 | static { |
aoqi@0 | 80 | String sysProp = System.getProperty("test.tool.vm.opts"); |
aoqi@0 | 81 | if (sysProp != null && sysProp.length() > 0) { |
aoqi@0 | 82 | testToolVMOpts = Arrays.asList(sysProp.split("\\s+")); |
aoqi@0 | 83 | } else { |
aoqi@0 | 84 | testToolVMOpts = Collections.<String>emptyList(); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | sysProp = System.getProperty("test.vm.opts"); |
aoqi@0 | 88 | if (sysProp != null && sysProp.length() > 0) { |
aoqi@0 | 89 | testVMOpts = Arrays.asList(sysProp.split("\\s+")); |
aoqi@0 | 90 | } else { |
aoqi@0 | 91 | testVMOpts = Collections.<String>emptyList(); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * The expected result of command-like method execution. |
aoqi@0 | 97 | */ |
aoqi@0 | 98 | public enum Expect {SUCCESS, FAIL} |
aoqi@0 | 99 | |
aoqi@0 | 100 | enum AcceptedParams { |
aoqi@0 | 101 | EXPECT, |
aoqi@0 | 102 | SOURCES, |
aoqi@0 | 103 | OPTIONS, |
aoqi@0 | 104 | STD_OUTPUT, |
aoqi@0 | 105 | ERR_OUTPUT, |
aoqi@0 | 106 | EXTRA_ENV, |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | enum OutputKind {STD, ERR} |
aoqi@0 | 110 | |
aoqi@0 | 111 | /** |
aoqi@0 | 112 | * Helper class to abstract the processing of command's output. |
aoqi@0 | 113 | */ |
aoqi@0 | 114 | static abstract class WriterHelper { |
aoqi@0 | 115 | OutputKind kind; |
aoqi@0 | 116 | public abstract void pipeOutput(ProcessBuilder pb); |
aoqi@0 | 117 | public abstract void readFromStream(Process p) throws IOException; |
aoqi@0 | 118 | public abstract void addAll(Collection<? extends String> c) throws IOException; |
aoqi@0 | 119 | } |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * Helper class for redirecting command's output to a file. |
aoqi@0 | 123 | */ |
aoqi@0 | 124 | static class FileWriterHelper extends WriterHelper { |
aoqi@0 | 125 | File file; |
aoqi@0 | 126 | |
aoqi@0 | 127 | FileWriterHelper(File file, OutputKind kind) { |
aoqi@0 | 128 | this.file = file; |
aoqi@0 | 129 | this.kind = kind; |
aoqi@0 | 130 | } |
aoqi@0 | 131 | |
aoqi@0 | 132 | @Override |
aoqi@0 | 133 | public void pipeOutput(ProcessBuilder pb) { |
aoqi@0 | 134 | if (file != null) { |
aoqi@0 | 135 | switch (kind) { |
aoqi@0 | 136 | case STD: |
aoqi@0 | 137 | pb.redirectInput(file); |
aoqi@0 | 138 | break; |
aoqi@0 | 139 | case ERR: |
aoqi@0 | 140 | pb.redirectError(file); |
aoqi@0 | 141 | break; |
aoqi@0 | 142 | } |
aoqi@0 | 143 | } |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | @Override |
aoqi@0 | 147 | public void readFromStream(Process p) throws IOException {} |
aoqi@0 | 148 | |
aoqi@0 | 149 | @Override |
aoqi@0 | 150 | public void addAll(Collection<? extends String> c) throws IOException { |
aoqi@0 | 151 | if (file.exists()) |
aoqi@0 | 152 | Files.write(file.toPath(), c, defaultCharset, |
aoqi@0 | 153 | StandardOpenOption.WRITE, StandardOpenOption.APPEND); |
aoqi@0 | 154 | else |
aoqi@0 | 155 | Files.write(file.toPath(), c, defaultCharset); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | /** |
aoqi@0 | 160 | * Helper class for redirecting command's output to a String list. |
aoqi@0 | 161 | */ |
aoqi@0 | 162 | static class ListWriterHelper extends WriterHelper { |
aoqi@0 | 163 | List<String> list; |
aoqi@0 | 164 | |
aoqi@0 | 165 | public ListWriterHelper(List<String> list, OutputKind kind) { |
aoqi@0 | 166 | this.kind = kind; |
aoqi@0 | 167 | this.list = list; |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | @Override |
aoqi@0 | 171 | public void pipeOutput(ProcessBuilder pb) {} |
aoqi@0 | 172 | |
aoqi@0 | 173 | @Override |
aoqi@0 | 174 | public void readFromStream(Process p) throws IOException { |
aoqi@0 | 175 | BufferedReader br = null; |
aoqi@0 | 176 | switch (kind) { |
aoqi@0 | 177 | case STD: |
aoqi@0 | 178 | br = new BufferedReader(new InputStreamReader(p.getInputStream())); |
aoqi@0 | 179 | break; |
aoqi@0 | 180 | case ERR: |
aoqi@0 | 181 | br = new BufferedReader(new InputStreamReader(p.getErrorStream())); |
aoqi@0 | 182 | break; |
aoqi@0 | 183 | } |
aoqi@0 | 184 | String line; |
aoqi@0 | 185 | while ((line = br.readLine()) != null) { |
aoqi@0 | 186 | list.add(line); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | public void addAll(Collection<? extends String> c) { |
aoqi@0 | 191 | list.addAll(c); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | /** |
aoqi@0 | 196 | * Simple factory class for creating a WriterHelper instance. |
aoqi@0 | 197 | */ |
aoqi@0 | 198 | static class WriterHelperFactory { |
aoqi@0 | 199 | static WriterHelper make(File file, OutputKind kind) { |
aoqi@0 | 200 | return new FileWriterHelper(file, kind); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | static WriterHelper make(List<String> list, OutputKind kind) { |
aoqi@0 | 204 | return new ListWriterHelper(list, kind); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | /** |
aoqi@0 | 209 | * A generic class for holding command's arguments. |
aoqi@0 | 210 | */ |
aoqi@0 | 211 | public static abstract class GenericArgs <T extends GenericArgs> { |
aoqi@0 | 212 | protected static List<Set<AcceptedParams>> minAcceptedParams; |
aoqi@0 | 213 | |
aoqi@0 | 214 | protected Set<AcceptedParams> currentParams = |
aoqi@0 | 215 | EnumSet.<AcceptedParams>noneOf(AcceptedParams.class); |
aoqi@0 | 216 | |
aoqi@0 | 217 | protected Expect whatToExpect; |
aoqi@0 | 218 | protected WriterHelper stdOutput; |
aoqi@0 | 219 | protected WriterHelper errOutput; |
aoqi@0 | 220 | protected List<String> args = new ArrayList<>(); |
aoqi@0 | 221 | protected String[] argsArr; |
aoqi@0 | 222 | |
aoqi@0 | 223 | protected GenericArgs() { |
aoqi@0 | 224 | set(Expect.SUCCESS); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | |
aoqi@0 | 227 | public T set(Expect whatToExpt) { |
aoqi@0 | 228 | currentParams.add(AcceptedParams.EXPECT); |
aoqi@0 | 229 | this.whatToExpect = whatToExpt; |
aoqi@0 | 230 | return (T)this; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | public T setStdOutput(List<String> stdOutput) { |
aoqi@0 | 234 | currentParams.add(AcceptedParams.STD_OUTPUT); |
aoqi@0 | 235 | this.stdOutput = WriterHelperFactory.make(stdOutput, OutputKind.STD); |
aoqi@0 | 236 | return (T)this; |
aoqi@0 | 237 | } |
aoqi@0 | 238 | |
aoqi@0 | 239 | public T setStdOutput(File output) { |
aoqi@0 | 240 | currentParams.add(AcceptedParams.STD_OUTPUT); |
aoqi@0 | 241 | this.stdOutput = WriterHelperFactory.make(output, OutputKind.STD); |
aoqi@0 | 242 | return (T)this; |
aoqi@0 | 243 | } |
aoqi@0 | 244 | |
aoqi@0 | 245 | public T setErrOutput(List<String> errOutput) { |
aoqi@0 | 246 | currentParams.add(AcceptedParams.ERR_OUTPUT); |
aoqi@0 | 247 | this.errOutput = WriterHelperFactory.make(errOutput, OutputKind.ERR); |
aoqi@0 | 248 | return (T)this; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | public T setErrOutput(File errOutput) { |
aoqi@0 | 252 | currentParams.add(AcceptedParams.ERR_OUTPUT); |
aoqi@0 | 253 | this.errOutput = WriterHelperFactory.make(errOutput, OutputKind.ERR); |
aoqi@0 | 254 | return (T)this; |
aoqi@0 | 255 | } |
aoqi@0 | 256 | |
aoqi@0 | 257 | public T setAllArgs(String... args) { |
aoqi@0 | 258 | currentParams.add(AcceptedParams.OPTIONS); |
aoqi@0 | 259 | this.argsArr = args; |
aoqi@0 | 260 | return (T) this; |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | |
aoqi@0 | 264 | public T appendArgs(String... args) { |
aoqi@0 | 265 | appendArgs(Arrays.asList(args)); |
aoqi@0 | 266 | return (T)this; |
aoqi@0 | 267 | } |
aoqi@0 | 268 | |
aoqi@0 | 269 | public T appendArgs(Path... args) { |
aoqi@0 | 270 | if (args != null) { |
aoqi@0 | 271 | List<String> list = new ArrayList<>(); |
aoqi@0 | 272 | for (int i = 0; i < args.length; i++) { |
aoqi@0 | 273 | if (args[i] != null) { |
aoqi@0 | 274 | list.add(args[i].toString()); |
aoqi@0 | 275 | } |
aoqi@0 | 276 | } |
aoqi@0 | 277 | appendArgs(list); |
aoqi@0 | 278 | } |
aoqi@0 | 279 | return (T)this; |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | public T appendArgs(List<String> args) { |
aoqi@0 | 283 | if (args != null && args.size() > 0) { |
aoqi@0 | 284 | currentParams.add(AcceptedParams.OPTIONS); |
aoqi@0 | 285 | for (int i = 0; i < args.size(); i++) { |
aoqi@0 | 286 | if (args.get(i) != null) { |
aoqi@0 | 287 | this.args.add(args.get(i)); |
aoqi@0 | 288 | } |
aoqi@0 | 289 | } |
aoqi@0 | 290 | } |
aoqi@0 | 291 | return (T)this; |
aoqi@0 | 292 | } |
aoqi@0 | 293 | |
aoqi@0 | 294 | public T setOptions(List<String> options) { |
aoqi@0 | 295 | currentParams.add(AcceptedParams.OPTIONS); |
aoqi@0 | 296 | this.args = options; |
aoqi@0 | 297 | return (T)this; |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | public T setOptions(String... options) { |
aoqi@0 | 301 | currentParams.add(AcceptedParams.OPTIONS); |
aoqi@0 | 302 | this.args = Arrays.asList(options); |
aoqi@0 | 303 | return (T)this; |
aoqi@0 | 304 | } |
aoqi@0 | 305 | |
aoqi@0 | 306 | public boolean hasMinParams() { |
aoqi@0 | 307 | for (Set<AcceptedParams> minSet : minAcceptedParams) { |
aoqi@0 | 308 | if (currentParams.containsAll(minSet)) { |
aoqi@0 | 309 | return true; |
aoqi@0 | 310 | } |
aoqi@0 | 311 | } |
aoqi@0 | 312 | return false; |
aoqi@0 | 313 | } |
aoqi@0 | 314 | } |
aoqi@0 | 315 | |
aoqi@0 | 316 | /** |
aoqi@0 | 317 | * A more specific class for holding javac-like command's arguments. |
aoqi@0 | 318 | */ |
aoqi@0 | 319 | public static class JavaToolArgs extends GenericArgs<JavaToolArgs> { |
aoqi@0 | 320 | |
aoqi@0 | 321 | static { |
aoqi@0 | 322 | minAcceptedParams = new ArrayList<>(); |
aoqi@0 | 323 | minAcceptedParams.add(EnumSet.<AcceptedParams>of( |
aoqi@0 | 324 | AcceptedParams.EXPECT, AcceptedParams.OPTIONS)); |
aoqi@0 | 325 | minAcceptedParams.add(EnumSet.<AcceptedParams>of( |
aoqi@0 | 326 | AcceptedParams.EXPECT, AcceptedParams.SOURCES)); |
aoqi@0 | 327 | } |
aoqi@0 | 328 | |
aoqi@0 | 329 | protected List<? extends JavaFileObject> sources; |
aoqi@0 | 330 | |
aoqi@0 | 331 | public JavaToolArgs() { |
aoqi@0 | 332 | super(); |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | public JavaToolArgs(Expect whatToExpt) { |
aoqi@0 | 336 | super.set(whatToExpt); |
aoqi@0 | 337 | } |
aoqi@0 | 338 | |
aoqi@0 | 339 | public JavaToolArgs setSources(List<? extends JavaFileObject> sources) { |
aoqi@0 | 340 | currentParams.add(AcceptedParams.SOURCES); |
aoqi@0 | 341 | this.sources = sources; |
aoqi@0 | 342 | return this; |
aoqi@0 | 343 | } |
aoqi@0 | 344 | |
aoqi@0 | 345 | public JavaToolArgs setSources(JavaSource... sources) { |
aoqi@0 | 346 | return setSources(Arrays.asList(sources)); |
aoqi@0 | 347 | } |
aoqi@0 | 348 | |
aoqi@0 | 349 | public JavaToolArgs setSources(String... sources) { |
aoqi@0 | 350 | List<JavaSource> javaSrcs = new ArrayList<>(); |
aoqi@0 | 351 | for (String source : sources) { |
aoqi@0 | 352 | javaSrcs.add(new JavaSource(source)); |
aoqi@0 | 353 | } |
aoqi@0 | 354 | return setSources(javaSrcs); |
aoqi@0 | 355 | } |
aoqi@0 | 356 | } |
aoqi@0 | 357 | |
aoqi@0 | 358 | /** |
aoqi@0 | 359 | * A more specific class for holding any command's arguments. |
aoqi@0 | 360 | */ |
aoqi@0 | 361 | public static class AnyToolArgs extends GenericArgs<AnyToolArgs> { |
aoqi@0 | 362 | |
aoqi@0 | 363 | static { |
aoqi@0 | 364 | minAcceptedParams = new ArrayList<>(); |
aoqi@0 | 365 | minAcceptedParams.add(EnumSet.<AcceptedParams>of( |
aoqi@0 | 366 | AcceptedParams.EXPECT, AcceptedParams.OPTIONS)); |
aoqi@0 | 367 | } |
aoqi@0 | 368 | |
aoqi@0 | 369 | Map<String, String> extraEnv; |
aoqi@0 | 370 | |
aoqi@0 | 371 | public AnyToolArgs() { |
aoqi@0 | 372 | super(); |
aoqi@0 | 373 | } |
aoqi@0 | 374 | |
aoqi@0 | 375 | public AnyToolArgs(Expect whatToExpt) { |
aoqi@0 | 376 | set(whatToExpt); |
aoqi@0 | 377 | } |
aoqi@0 | 378 | |
aoqi@0 | 379 | public AnyToolArgs set(Map<String, String> extraEnv) { |
aoqi@0 | 380 | currentParams.add(AcceptedParams.EXTRA_ENV); |
aoqi@0 | 381 | this.extraEnv = extraEnv; |
aoqi@0 | 382 | return this; |
aoqi@0 | 383 | } |
aoqi@0 | 384 | } |
aoqi@0 | 385 | |
aoqi@0 | 386 | /** |
aoqi@0 | 387 | * Custom exception for bad command execution. |
aoqi@0 | 388 | */ |
aoqi@0 | 389 | public static class CommandExecutionException extends Exception { |
aoqi@0 | 390 | CommandExecutionException(List<String> command, Expect whatToExpt) { |
aoqi@0 | 391 | super(createMessage(command, whatToExpt)); |
aoqi@0 | 392 | } |
aoqi@0 | 393 | |
aoqi@0 | 394 | CommandExecutionException(Expect whatToExpt, String... command) { |
aoqi@0 | 395 | this(Arrays.asList(command), whatToExpt); |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | private static String createMessage(List<String> command, Expect whatToExpt) { |
aoqi@0 | 399 | StringBuilder sb = new StringBuilder().append("Command : "); |
aoqi@0 | 400 | sb.append(command.toString()).append(lineSeparator); |
aoqi@0 | 401 | switch (whatToExpt) { |
aoqi@0 | 402 | case SUCCESS: |
aoqi@0 | 403 | sb.append(" has unexpectedly failed"); |
aoqi@0 | 404 | break; |
aoqi@0 | 405 | case FAIL: |
aoqi@0 | 406 | sb.append(" has been unexpectedly successful"); |
aoqi@0 | 407 | break; |
aoqi@0 | 408 | } |
aoqi@0 | 409 | return sb.toString(); |
aoqi@0 | 410 | } |
aoqi@0 | 411 | } |
aoqi@0 | 412 | |
aoqi@0 | 413 | /** |
aoqi@0 | 414 | * Custom exception for not equal resources. |
aoqi@0 | 415 | */ |
aoqi@0 | 416 | public static class ResourcesNotEqualException extends Exception { |
aoqi@0 | 417 | public ResourcesNotEqualException(List<String> res1, List<String> res2) { |
aoqi@0 | 418 | super(createMessage(res1, res2)); |
aoqi@0 | 419 | } |
aoqi@0 | 420 | |
aoqi@0 | 421 | public ResourcesNotEqualException(String line1, String line2) { |
aoqi@0 | 422 | super(createMessage(line1, line2)); |
aoqi@0 | 423 | } |
aoqi@0 | 424 | |
aoqi@0 | 425 | public ResourcesNotEqualException(Path path1, Path path2) { |
aoqi@0 | 426 | super(createMessage(path1, path2)); |
aoqi@0 | 427 | } |
aoqi@0 | 428 | |
aoqi@0 | 429 | private static String createMessage(Path path1, Path path2) { |
aoqi@0 | 430 | return new StringBuilder() |
aoqi@0 | 431 | .append("The resources provided for comparison in paths \n") |
aoqi@0 | 432 | .append(path1.toString()).append(" and \n") |
aoqi@0 | 433 | .append(path2.toString()).append("are different").toString(); |
aoqi@0 | 434 | } |
aoqi@0 | 435 | |
aoqi@0 | 436 | private static String createMessage(String line1, String line2) { |
aoqi@0 | 437 | return new StringBuilder() |
aoqi@0 | 438 | .append("The resources provided for comparison are different at lines: \n") |
aoqi@0 | 439 | .append(line1).append(" and \n") |
aoqi@0 | 440 | .append(line2).toString(); |
aoqi@0 | 441 | } |
aoqi@0 | 442 | |
aoqi@0 | 443 | private static String createMessage(List<String> res1, List<String> res2) { |
aoqi@0 | 444 | return new StringBuilder() |
aoqi@0 | 445 | .append("The resources provided for comparison are different: \n") |
aoqi@0 | 446 | .append("Resource 1 is: ").append(res1).append("\n and \n") |
aoqi@0 | 447 | .append("Resource 2 is: ").append(res2).append("\n").toString(); |
aoqi@0 | 448 | } |
aoqi@0 | 449 | } |
aoqi@0 | 450 | |
aoqi@0 | 451 | /** |
aoqi@0 | 452 | * A javac compiler caller method. |
aoqi@0 | 453 | */ |
aoqi@0 | 454 | public static int javac(JavaToolArgs params) |
aoqi@0 | 455 | throws CommandExecutionException, IOException { |
aoqi@0 | 456 | if (params.hasMinParams()) { |
aoqi@0 | 457 | if (params.argsArr != null) { |
aoqi@0 | 458 | return genericJavaCMD(JavaCMD.JAVAC, params); |
aoqi@0 | 459 | } else { |
aoqi@0 | 460 | return genericJavaCMD(JavaCMD.JAVAC_API, params); |
aoqi@0 | 461 | } |
aoqi@0 | 462 | } |
aoqi@0 | 463 | throw new AssertionError("javac command has been invoked with less parameters than needed"); |
aoqi@0 | 464 | } |
aoqi@0 | 465 | |
aoqi@0 | 466 | /** |
aoqi@0 | 467 | * A javap calling method. |
aoqi@0 | 468 | */ |
aoqi@0 | 469 | public static String javap(JavaToolArgs params) |
aoqi@0 | 470 | throws CommandExecutionException, IOException { |
aoqi@0 | 471 | if (params.hasMinParams()) { |
aoqi@0 | 472 | List<String> list = new ArrayList<>(); |
aoqi@0 | 473 | params.setErrOutput(list); |
aoqi@0 | 474 | genericJavaCMD(JavaCMD.JAVAP, params); |
aoqi@0 | 475 | return listToString(list); |
aoqi@0 | 476 | } |
aoqi@0 | 477 | throw new AssertionError("javap command has been invoked with less parameters than needed"); |
aoqi@0 | 478 | } |
aoqi@0 | 479 | |
aoqi@0 | 480 | /** |
aoqi@0 | 481 | * A javah calling method. |
aoqi@0 | 482 | */ |
aoqi@0 | 483 | public static int javah(JavaToolArgs params) |
aoqi@0 | 484 | throws CommandExecutionException, IOException { |
aoqi@0 | 485 | if (params.hasMinParams()) { |
aoqi@0 | 486 | return genericJavaCMD(JavaCMD.JAVAH, params); |
aoqi@0 | 487 | } |
aoqi@0 | 488 | throw new AssertionError("javah command has been invoked with less parameters than needed"); |
aoqi@0 | 489 | } |
aoqi@0 | 490 | |
aoqi@0 | 491 | /** |
aoqi@0 | 492 | * A enum class for langtools commands. |
aoqi@0 | 493 | */ |
aoqi@0 | 494 | enum JavaCMD { |
aoqi@0 | 495 | JAVAC { |
aoqi@0 | 496 | @Override |
aoqi@0 | 497 | int run(JavaToolArgs params, PrintWriter pw) { |
aoqi@0 | 498 | return com.sun.tools.javac.Main.compile(params.argsArr, pw); |
aoqi@0 | 499 | } |
aoqi@0 | 500 | }, |
aoqi@0 | 501 | JAVAC_API { |
aoqi@0 | 502 | @Override |
aoqi@0 | 503 | int run(JavaToolArgs params, PrintWriter pw) { |
aoqi@0 | 504 | JavacTask ct = (JavacTask)comp.getTask(pw, null, null, |
aoqi@0 | 505 | params.args, null, params.sources); |
aoqi@0 | 506 | return ((JavacTaskImpl)ct).doCall().exitCode; |
aoqi@0 | 507 | } |
aoqi@0 | 508 | |
aoqi@0 | 509 | @Override |
aoqi@0 | 510 | String getName() { |
aoqi@0 | 511 | return "javac"; |
aoqi@0 | 512 | } |
aoqi@0 | 513 | |
aoqi@0 | 514 | @Override |
aoqi@0 | 515 | List<String> getExceptionMsgContent(JavaToolArgs params) { |
aoqi@0 | 516 | List<String> result = super.getExceptionMsgContent(params); |
aoqi@0 | 517 | for (JavaFileObject source : params.sources) { |
aoqi@0 | 518 | if (source instanceof JavaSource) { |
aoqi@0 | 519 | result.add(((JavaSource)source).name); |
aoqi@0 | 520 | } |
aoqi@0 | 521 | } |
aoqi@0 | 522 | return result; |
aoqi@0 | 523 | } |
aoqi@0 | 524 | }, |
aoqi@0 | 525 | JAVAH { |
aoqi@0 | 526 | @Override |
aoqi@0 | 527 | int run(JavaToolArgs params, PrintWriter pw) { |
aoqi@0 | 528 | return com.sun.tools.javah.Main.run(params.argsArr, pw); |
aoqi@0 | 529 | } |
aoqi@0 | 530 | }, |
aoqi@0 | 531 | JAVAP { |
aoqi@0 | 532 | @Override |
aoqi@0 | 533 | int run(JavaToolArgs params, PrintWriter pw) { |
aoqi@0 | 534 | return com.sun.tools.javap.Main.run(params.argsArr, pw); |
aoqi@0 | 535 | } |
aoqi@0 | 536 | }; |
aoqi@0 | 537 | |
aoqi@0 | 538 | abstract int run(JavaToolArgs params, PrintWriter pw); |
aoqi@0 | 539 | |
aoqi@0 | 540 | String getName() { |
aoqi@0 | 541 | return this.name().toLowerCase(); |
aoqi@0 | 542 | } |
aoqi@0 | 543 | |
aoqi@0 | 544 | List<String> getExceptionMsgContent(JavaToolArgs params) { |
aoqi@0 | 545 | List<String> result = new ArrayList<>(); |
aoqi@0 | 546 | result.add(getName()); |
aoqi@0 | 547 | result.addAll(params.argsArr != null ? |
aoqi@0 | 548 | Arrays.asList(params.argsArr) : |
aoqi@0 | 549 | params.args); |
aoqi@0 | 550 | return result; |
aoqi@0 | 551 | } |
aoqi@0 | 552 | } |
aoqi@0 | 553 | |
aoqi@0 | 554 | /** |
aoqi@0 | 555 | * A helper method for executing langtools commands. |
aoqi@0 | 556 | */ |
aoqi@0 | 557 | private static int genericJavaCMD( |
aoqi@0 | 558 | JavaCMD cmd, |
aoqi@0 | 559 | JavaToolArgs params) |
aoqi@0 | 560 | throws CommandExecutionException, IOException { |
aoqi@0 | 561 | int rc = 0; |
aoqi@0 | 562 | StringWriter sw = null; |
aoqi@0 | 563 | try (PrintWriter pw = (params.errOutput == null) ? |
aoqi@0 | 564 | null : new PrintWriter(sw = new StringWriter())) { |
aoqi@0 | 565 | rc = cmd.run(params, pw); |
aoqi@0 | 566 | } |
aoqi@0 | 567 | String out = (sw == null) ? null : sw.toString(); |
aoqi@0 | 568 | |
aoqi@0 | 569 | if (params.errOutput != null && (out != null) && !out.isEmpty()) { |
aoqi@0 | 570 | params.errOutput.addAll(splitLines(out, lineSeparator)); |
aoqi@0 | 571 | } |
aoqi@0 | 572 | |
aoqi@0 | 573 | if ( (rc == 0 && params.whatToExpect == Expect.SUCCESS) || |
aoqi@0 | 574 | (rc != 0 && params.whatToExpect == Expect.FAIL) ) { |
aoqi@0 | 575 | return rc; |
aoqi@0 | 576 | } |
aoqi@0 | 577 | |
aoqi@0 | 578 | throw new CommandExecutionException(cmd.getExceptionMsgContent(params), |
aoqi@0 | 579 | params.whatToExpect); |
aoqi@0 | 580 | } |
aoqi@0 | 581 | |
aoqi@0 | 582 | /** |
aoqi@0 | 583 | * A jar calling method. |
aoqi@0 | 584 | */ |
aoqi@0 | 585 | public static boolean jar(String... params) throws CommandExecutionException { |
aoqi@0 | 586 | Main jarGenerator = new Main(System.out, System.err, "jar"); |
aoqi@0 | 587 | boolean result = jarGenerator.run(params); |
aoqi@0 | 588 | if (!result) { |
aoqi@0 | 589 | List<String> command = new ArrayList<>(); |
aoqi@0 | 590 | command.add("jar"); |
aoqi@0 | 591 | command.addAll(Arrays.asList(params)); |
aoqi@0 | 592 | throw new CommandExecutionException(command, Expect.SUCCESS); |
aoqi@0 | 593 | } |
aoqi@0 | 594 | return result; |
aoqi@0 | 595 | } |
aoqi@0 | 596 | |
aoqi@0 | 597 | /** |
aoqi@0 | 598 | * A general command calling method. |
aoqi@0 | 599 | */ |
aoqi@0 | 600 | public static int executeCommand(AnyToolArgs params) |
aoqi@0 | 601 | throws CommandExecutionException, IOException, InterruptedException { |
aoqi@0 | 602 | if (params.hasMinParams()) { |
aoqi@0 | 603 | List<String> cmd = (params.args != null) ? |
aoqi@0 | 604 | params.args : |
aoqi@0 | 605 | Arrays.asList(params.argsArr); |
aoqi@0 | 606 | return executeCommand(cmd, params.extraEnv, params.stdOutput, |
aoqi@0 | 607 | params.errOutput, params.whatToExpect); |
aoqi@0 | 608 | } |
aoqi@0 | 609 | throw new AssertionError("command has been invoked with less parameters than needed"); |
aoqi@0 | 610 | } |
aoqi@0 | 611 | |
aoqi@0 | 612 | /** |
aoqi@0 | 613 | * A helper method for calling a general command. |
aoqi@0 | 614 | */ |
aoqi@0 | 615 | private static int executeCommand( |
aoqi@0 | 616 | List<String> command, |
aoqi@0 | 617 | Map<String, String> extraEnv, |
aoqi@0 | 618 | WriterHelper stdOutput, |
aoqi@0 | 619 | WriterHelper errOutput, |
aoqi@0 | 620 | Expect whatToExpt) |
aoqi@0 | 621 | throws IOException, InterruptedException, CommandExecutionException { |
aoqi@0 | 622 | ProcessBuilder pb = new ProcessBuilder(command); |
aoqi@0 | 623 | |
aoqi@0 | 624 | if (stdOutput != null) stdOutput.pipeOutput(pb); |
aoqi@0 | 625 | if (errOutput != null) errOutput.pipeOutput(pb); |
aoqi@0 | 626 | |
aoqi@0 | 627 | if (extraEnv != null) { |
aoqi@0 | 628 | pb.environment().putAll(extraEnv); |
aoqi@0 | 629 | } |
aoqi@0 | 630 | |
aoqi@0 | 631 | Process p = pb.start(); |
aoqi@0 | 632 | |
aoqi@0 | 633 | if (stdOutput != null) stdOutput.readFromStream(p); |
aoqi@0 | 634 | if (errOutput != null) errOutput.readFromStream(p); |
aoqi@0 | 635 | |
aoqi@0 | 636 | int result = p.waitFor(); |
aoqi@0 | 637 | if ( (result == 0 && whatToExpt == Expect.SUCCESS) || |
aoqi@0 | 638 | (result != 0 && whatToExpt == Expect.FAIL) ) { |
aoqi@0 | 639 | return result; |
aoqi@0 | 640 | } |
aoqi@0 | 641 | |
aoqi@0 | 642 | throw new CommandExecutionException(command, whatToExpt); |
aoqi@0 | 643 | } |
aoqi@0 | 644 | |
aoqi@0 | 645 | /** |
aoqi@0 | 646 | * This set of methods can be used instead of diff when the only needed |
aoqi@0 | 647 | * result is the equality or inequality of the two given resources. |
aoqi@0 | 648 | * |
aoqi@0 | 649 | * A resource can be a file or a String list. |
aoqi@0 | 650 | */ |
aoqi@0 | 651 | public static void compareLines(Path aPath, Path otherPath, String encoding) |
aoqi@0 | 652 | throws FileNotFoundException, IOException, ResourcesNotEqualException { |
aoqi@0 | 653 | compareLines(aPath, otherPath, encoding, false); |
aoqi@0 | 654 | } |
aoqi@0 | 655 | |
aoqi@0 | 656 | public static void compareLines( |
aoqi@0 | 657 | Path aPath, Path otherPath, String encoding, boolean trim) |
aoqi@0 | 658 | throws FileNotFoundException, IOException, ResourcesNotEqualException { |
aoqi@0 | 659 | Charset charset = encoding != null ? |
aoqi@0 | 660 | Charset.forName(encoding) : |
aoqi@0 | 661 | defaultCharset; |
aoqi@0 | 662 | List<String> list1 = Files.readAllLines(aPath, charset); |
aoqi@0 | 663 | List<String> list2 = Files.readAllLines(otherPath, charset); |
aoqi@0 | 664 | compareLines(list1, list2, trim); |
aoqi@0 | 665 | } |
aoqi@0 | 666 | |
aoqi@0 | 667 | public static void compareLines(Path path, List<String> strings, String encoding) |
aoqi@0 | 668 | throws FileNotFoundException, IOException, ResourcesNotEqualException { |
aoqi@0 | 669 | compareLines(path, strings, encoding, false); |
aoqi@0 | 670 | } |
aoqi@0 | 671 | |
aoqi@0 | 672 | public static void compareLines(Path path, List<String> strings, |
aoqi@0 | 673 | String encoding, boolean trim) |
aoqi@0 | 674 | throws FileNotFoundException, IOException, ResourcesNotEqualException { |
aoqi@0 | 675 | Charset charset = encoding != null ? |
aoqi@0 | 676 | Charset.forName(encoding) : |
aoqi@0 | 677 | defaultCharset; |
aoqi@0 | 678 | List<String> list = Files.readAllLines(path, charset); |
aoqi@0 | 679 | compareLines(list, strings, trim); |
aoqi@0 | 680 | } |
aoqi@0 | 681 | |
aoqi@0 | 682 | public static void compareLines(List<String> list1, List<String> list2) |
aoqi@0 | 683 | throws ResourcesNotEqualException { |
aoqi@0 | 684 | compareLines(list1, list2, false); |
aoqi@0 | 685 | } |
aoqi@0 | 686 | |
aoqi@0 | 687 | public static void compareLines(List<String> list1, |
aoqi@0 | 688 | List<String> list2, boolean trim) throws ResourcesNotEqualException { |
aoqi@0 | 689 | if ((list1 == list2) || (list1 == null && list2 == null)) return; |
aoqi@0 | 690 | if (list1.size() != list2.size()) |
aoqi@0 | 691 | throw new ResourcesNotEqualException(list1, list2); |
aoqi@0 | 692 | int i = 0; |
aoqi@0 | 693 | int j = 0; |
aoqi@0 | 694 | while (i < list1.size() && |
aoqi@0 | 695 | j < list2.size() && |
aoqi@0 | 696 | equals(list1.get(i), list2.get(j), trim)) { |
aoqi@0 | 697 | i++; j++; |
aoqi@0 | 698 | } |
aoqi@0 | 699 | if (!(i == list1.size() && j == list2.size())) |
aoqi@0 | 700 | throw new ResourcesNotEqualException(list1, list2); |
aoqi@0 | 701 | } |
aoqi@0 | 702 | |
aoqi@0 | 703 | private static boolean equals(String s1, String s2, boolean trim) { |
aoqi@0 | 704 | return (trim ? s1.trim().equals(s2.trim()) : s1.equals(s2)); |
aoqi@0 | 705 | } |
aoqi@0 | 706 | |
aoqi@0 | 707 | /** |
aoqi@0 | 708 | * A set of simple grep-like methods, looks for regExpr in text. |
aoqi@0 | 709 | * The content of text is split using the new line character as a pattern |
aoqi@0 | 710 | * and later the regExpr is seek in every split line. If a match is found, |
aoqi@0 | 711 | * the whole line is added to the result. |
aoqi@0 | 712 | */ |
aoqi@0 | 713 | public static List<String> grep(String regExpr, String text, String sep) { |
aoqi@0 | 714 | return grep(regExpr, splitLines(text, sep)); |
aoqi@0 | 715 | } |
aoqi@0 | 716 | |
aoqi@0 | 717 | public static List<String> grep(String regExpr, List<String> text) { |
aoqi@0 | 718 | List<String> result = new ArrayList<>(); |
aoqi@0 | 719 | Pattern pattern = Pattern.compile(regExpr); |
aoqi@0 | 720 | for (String s : text) { |
aoqi@0 | 721 | if (pattern.matcher(s).find()) { |
aoqi@0 | 722 | result.add(s); |
aoqi@0 | 723 | } |
aoqi@0 | 724 | } |
aoqi@0 | 725 | return result; |
aoqi@0 | 726 | } |
aoqi@0 | 727 | |
aoqi@0 | 728 | public static List<String> grep(String regExpr, File f) |
aoqi@0 | 729 | throws IOException { |
aoqi@0 | 730 | List<String> lines = Files.readAllLines(f.toPath(), defaultCharset); |
aoqi@0 | 731 | return grep(regExpr, lines); |
aoqi@0 | 732 | } |
aoqi@0 | 733 | |
aoqi@0 | 734 | /** |
aoqi@0 | 735 | * A touch-like method. |
aoqi@0 | 736 | */ |
aoqi@0 | 737 | public static boolean touch(String fileName) { |
aoqi@0 | 738 | File file = new File(fileName); |
aoqi@0 | 739 | return touch(file); |
aoqi@0 | 740 | } |
aoqi@0 | 741 | |
aoqi@0 | 742 | public static boolean touch(File file) { |
aoqi@0 | 743 | if (file.exists()) { |
aoqi@0 | 744 | file.setLastModified(System.currentTimeMillis()); |
aoqi@0 | 745 | return true; |
aoqi@0 | 746 | } |
aoqi@0 | 747 | return false; |
aoqi@0 | 748 | } |
aoqi@0 | 749 | |
aoqi@0 | 750 | public static void createJavaFile(File outFile) throws IOException { |
aoqi@0 | 751 | createJavaFile(outFile, null); |
aoqi@0 | 752 | } |
aoqi@0 | 753 | |
aoqi@0 | 754 | /** |
aoqi@0 | 755 | * A method for creating a valid but very simple java file. |
aoqi@0 | 756 | */ |
aoqi@0 | 757 | public static void createJavaFile(File outFile, File superClass) |
aoqi@0 | 758 | throws IOException { |
aoqi@0 | 759 | String srcStr = "public class " + getSimpleName(outFile) + " "; |
aoqi@0 | 760 | if (superClass != null) { |
aoqi@0 | 761 | srcStr = srcStr.concat("extends " + getSimpleName(superClass) + " "); |
aoqi@0 | 762 | } |
aoqi@0 | 763 | srcStr = srcStr.concat("{}"); |
aoqi@0 | 764 | try (PrintWriter ps = new PrintWriter(new FileWriter(outFile))) { |
aoqi@0 | 765 | ps.println(srcStr); |
aoqi@0 | 766 | } |
aoqi@0 | 767 | } |
aoqi@0 | 768 | |
aoqi@0 | 769 | /** |
aoqi@0 | 770 | * Creates a java file name given its source. |
aoqi@0 | 771 | * The file is created in the working directory, creating a directory |
aoqi@0 | 772 | * tree if there is a package declaration. |
aoqi@0 | 773 | */ |
aoqi@0 | 774 | public static void createJavaFileFromSource(String source) throws IOException { |
aoqi@0 | 775 | createJavaFileFromSource(null, source); |
aoqi@0 | 776 | } |
aoqi@0 | 777 | |
aoqi@0 | 778 | /** |
aoqi@0 | 779 | * Creates a java file name given its source. |
aoqi@0 | 780 | * The file is created in the working directory, creating a directory |
aoqi@0 | 781 | * tree if there is a package declaration or the argument initialPath |
aoqi@0 | 782 | * has a valid path. |
aoqi@0 | 783 | * |
aoqi@0 | 784 | * e.i. if initialPath is foo/ and the source is: |
aoqi@0 | 785 | * package bar; |
aoqi@0 | 786 | * |
aoqi@0 | 787 | * public class bazz {} |
aoqi@0 | 788 | * |
aoqi@0 | 789 | * this method will create the file foo/bar/bazz.java in the working |
aoqi@0 | 790 | * directory. |
aoqi@0 | 791 | */ |
aoqi@0 | 792 | public static void createJavaFileFromSource(Path initialPath, |
aoqi@0 | 793 | String source) throws IOException { |
aoqi@0 | 794 | String fileName = getJavaFileNameFromSource(source); |
aoqi@0 | 795 | String dirTree = getDirTreeFromSource(source); |
aoqi@0 | 796 | Path path = (dirTree != null) ? |
aoqi@0 | 797 | Paths.get(dirTree, fileName) : |
aoqi@0 | 798 | Paths.get(fileName); |
aoqi@0 | 799 | path = (initialPath != null) ? |
aoqi@0 | 800 | initialPath.resolve(path): |
aoqi@0 | 801 | path; |
aoqi@0 | 802 | writeFile(path, source); |
aoqi@0 | 803 | } |
aoqi@0 | 804 | |
aoqi@0 | 805 | static Pattern publicClassPattern = |
aoqi@0 | 806 | Pattern.compile("public\\s+(?:class|enum|interface){1}\\s+(\\w+)"); |
aoqi@0 | 807 | static Pattern packageClassPattern = |
aoqi@0 | 808 | Pattern.compile("(?:class|enum|interface){1}\\s+(\\w+)"); |
aoqi@0 | 809 | |
aoqi@0 | 810 | /** |
aoqi@0 | 811 | * Extracts the java file name from the class declaration. |
aoqi@0 | 812 | * This method is intended for simple files and uses regular expressions, |
aoqi@0 | 813 | * so comments matching the pattern can make the method fail. |
aoqi@0 | 814 | */ |
aoqi@0 | 815 | private static String getJavaFileNameFromSource(String source) { |
aoqi@0 | 816 | String className = null; |
aoqi@0 | 817 | Matcher matcher = publicClassPattern.matcher(source); |
aoqi@0 | 818 | if (matcher.find()) { |
aoqi@0 | 819 | className = matcher.group(1) + ".java"; |
aoqi@0 | 820 | } else { |
aoqi@0 | 821 | matcher = packageClassPattern.matcher(source); |
aoqi@0 | 822 | if (matcher.find()) { |
aoqi@0 | 823 | className = matcher.group(1) + ".java"; |
aoqi@0 | 824 | } else { |
aoqi@0 | 825 | throw new AssertionError("Could not extract the java class " + |
aoqi@0 | 826 | "name from the provided source"); |
aoqi@0 | 827 | } |
aoqi@0 | 828 | } |
aoqi@0 | 829 | return className; |
aoqi@0 | 830 | } |
aoqi@0 | 831 | |
aoqi@0 | 832 | static Pattern packagePattern = |
aoqi@0 | 833 | Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))"); |
aoqi@0 | 834 | |
aoqi@0 | 835 | /** |
aoqi@0 | 836 | * Extracts the path from the package declaration if present. |
aoqi@0 | 837 | * This method is intended for simple files and uses regular expressions, |
aoqi@0 | 838 | * so comments matching the pattern can make the method fail. |
aoqi@0 | 839 | */ |
aoqi@0 | 840 | private static String getDirTreeFromSource(String source) { |
aoqi@0 | 841 | Matcher matcher = packagePattern.matcher(source); |
aoqi@0 | 842 | return matcher.find() ? |
aoqi@0 | 843 | matcher.group(1).replace(".", File.separator) : |
aoqi@0 | 844 | null; |
aoqi@0 | 845 | } |
aoqi@0 | 846 | |
aoqi@0 | 847 | /** |
aoqi@0 | 848 | * A method for creating a jar's manifest file with supplied data. |
aoqi@0 | 849 | */ |
aoqi@0 | 850 | public static void mkManifestWithClassPath(String mainClass, |
aoqi@0 | 851 | String... classes) throws IOException { |
aoqi@0 | 852 | List <String> lines = new ArrayList<>(); |
aoqi@0 | 853 | |
aoqi@0 | 854 | StringBuilder sb = new StringBuilder("Class-Path: ".length() + |
aoqi@0 | 855 | classes[0].length()).append("Class-Path: ").append(classes[0]); |
aoqi@0 | 856 | for (int i = 1; i < classes.length; i++) { |
aoqi@0 | 857 | sb.append(" ").append(classes[i]); |
aoqi@0 | 858 | } |
aoqi@0 | 859 | lines.add(sb.toString()); |
aoqi@0 | 860 | if (mainClass != null) { |
aoqi@0 | 861 | lines.add(new StringBuilder("Main-Class: ".length() + |
aoqi@0 | 862 | mainClass.length()) |
aoqi@0 | 863 | .append("Main-Class: ") |
aoqi@0 | 864 | .append(mainClass).toString()); |
aoqi@0 | 865 | } |
aoqi@0 | 866 | Files.write(Paths.get("MANIFEST.MF"), lines, null); |
aoqi@0 | 867 | } |
aoqi@0 | 868 | |
aoqi@0 | 869 | /** |
aoqi@0 | 870 | * A utility method to obtain the file name. |
aoqi@0 | 871 | */ |
aoqi@0 | 872 | static String getSimpleName(File inFile) { |
aoqi@0 | 873 | return inFile.toPath().getFileName().toString(); |
aoqi@0 | 874 | } |
aoqi@0 | 875 | |
aoqi@0 | 876 | /** |
aoqi@0 | 877 | * A method to write to a file, the directory tree is created if needed. |
aoqi@0 | 878 | */ |
aoqi@0 | 879 | public static File writeFile(Path path, String body) throws IOException { |
aoqi@0 | 880 | File result; |
aoqi@0 | 881 | if (path.getParent() != null) { |
aoqi@0 | 882 | Files.createDirectories(path.getParent()); |
aoqi@0 | 883 | } |
aoqi@0 | 884 | try (FileWriter out = new FileWriter(result = path.toAbsolutePath().toFile())) { |
aoqi@0 | 885 | out.write(body); |
aoqi@0 | 886 | } |
aoqi@0 | 887 | return result; |
aoqi@0 | 888 | } |
aoqi@0 | 889 | |
aoqi@0 | 890 | public static File writeFile(String path, String body) throws IOException { |
aoqi@0 | 891 | return writeFile(Paths.get(path), body); |
aoqi@0 | 892 | } |
aoqi@0 | 893 | |
aoqi@0 | 894 | /** |
aoqi@0 | 895 | * A rm-like method, the file is deleted only if it exists. |
aoqi@0 | 896 | */ |
aoqi@0 | 897 | public static void rm(Path path) throws Exception { |
aoqi@0 | 898 | Files.deleteIfExists(path); |
aoqi@0 | 899 | } |
aoqi@0 | 900 | |
aoqi@0 | 901 | public static void rm(String filename) throws Exception { |
aoqi@0 | 902 | rm(Paths.get(filename)); |
aoqi@0 | 903 | } |
aoqi@0 | 904 | |
aoqi@0 | 905 | public static void rm(File f) throws Exception { |
aoqi@0 | 906 | rm(f.toPath()); |
aoqi@0 | 907 | } |
aoqi@0 | 908 | |
aoqi@0 | 909 | /** |
aoqi@0 | 910 | * Copy source file to destination file. |
aoqi@0 | 911 | */ |
aoqi@0 | 912 | public static void copyFile(File destfile, File srcfile) |
aoqi@0 | 913 | throws IOException { |
aoqi@0 | 914 | copyFile(destfile.toPath(), srcfile.toPath()); |
aoqi@0 | 915 | } |
aoqi@0 | 916 | |
aoqi@0 | 917 | public static void copyFile(Path destPath, Path srcPath) |
aoqi@0 | 918 | throws IOException { |
aoqi@0 | 919 | Files.createDirectories(destPath); |
aoqi@0 | 920 | Files.copy(srcPath, destPath, REPLACE_EXISTING); |
aoqi@0 | 921 | } |
aoqi@0 | 922 | |
aoqi@0 | 923 | /** |
aoqi@0 | 924 | * Splits a String using the System's line separator character as splitting point. |
aoqi@0 | 925 | */ |
aoqi@0 | 926 | public static List<String> splitLines(String lines, String sep) { |
aoqi@0 | 927 | return Arrays.asList(lines.split(sep)); |
aoqi@0 | 928 | } |
aoqi@0 | 929 | |
aoqi@0 | 930 | /** |
aoqi@0 | 931 | * Converts a String list into one String by appending the System's line separator |
aoqi@0 | 932 | * character after each component. |
aoqi@0 | 933 | */ |
aoqi@0 | 934 | private static String listToString(List<String> lines) { |
aoqi@0 | 935 | StringBuilder sb = new StringBuilder(); |
aoqi@0 | 936 | for (String s : lines) { |
aoqi@0 | 937 | sb.append(s).append(lineSeparator); |
aoqi@0 | 938 | } |
aoqi@0 | 939 | return sb.toString(); |
aoqi@0 | 940 | } |
aoqi@0 | 941 | |
aoqi@0 | 942 | /** |
aoqi@0 | 943 | * Returns true if the OS is a Windows version. |
aoqi@0 | 944 | */ |
aoqi@0 | 945 | public static boolean isWindows() { |
aoqi@0 | 946 | String osName = System.getProperty("os.name"); |
aoqi@0 | 947 | return osName.toUpperCase().startsWith("WINDOWS"); |
aoqi@0 | 948 | } |
aoqi@0 | 949 | |
aoqi@0 | 950 | /** |
aoqi@0 | 951 | * Class representing an in-memory java source file. It is able to extract |
aoqi@0 | 952 | * the file name from simple source codes using regular expressions. |
aoqi@0 | 953 | */ |
aoqi@0 | 954 | public static class JavaSource extends SimpleJavaFileObject { |
aoqi@0 | 955 | String source; |
aoqi@0 | 956 | String name; |
aoqi@0 | 957 | |
aoqi@0 | 958 | public JavaSource(String className, String source) { |
aoqi@0 | 959 | super(URI.create(className), |
aoqi@0 | 960 | JavaFileObject.Kind.SOURCE); |
aoqi@0 | 961 | this.name = className; |
aoqi@0 | 962 | this.source = source; |
aoqi@0 | 963 | } |
aoqi@0 | 964 | |
aoqi@0 | 965 | public JavaSource(String source) { |
aoqi@0 | 966 | super(URI.create(getJavaFileNameFromSource(source)), |
aoqi@0 | 967 | JavaFileObject.Kind.SOURCE); |
aoqi@0 | 968 | this.name = getJavaFileNameFromSource(source); |
aoqi@0 | 969 | this.source = source; |
aoqi@0 | 970 | } |
aoqi@0 | 971 | |
aoqi@0 | 972 | @Override |
aoqi@0 | 973 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
aoqi@0 | 974 | return source; |
aoqi@0 | 975 | } |
aoqi@0 | 976 | } |
aoqi@0 | 977 | } |