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