test/tools/javac/api/lib/ToolTester.java

Sun, 12 May 2013 18:18:54 -0700

author
jjg
date
Sun, 12 May 2013 18:18:54 -0700
changeset 1733
e39669aea0bd
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8014363: javac test class ToolTester handles classpath incorrectly
Reviewed-by: ksrini

duke@1 1 /*
jjg@1733 2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
duke@1 22 */
duke@1 23
duke@1 24 import java.io.File;
duke@1 25 import java.io.IOException;
duke@1 26 import java.nio.charset.Charset;
jjg@1733 27 import java.util.*;
duke@1 28 import javax.tools.*;
duke@1 29
duke@1 30 import static javax.tools.StandardLocation.CLASS_PATH;
duke@1 31 import static javax.tools.StandardLocation.SOURCE_PATH;
duke@1 32 import static javax.tools.StandardLocation.CLASS_OUTPUT;
duke@1 33
duke@1 34 public class ToolTester {
duke@1 35 public final File test_src = new File(System.getProperty("test.src", "."));
duke@1 36 public final File test_classes = new File(System.getProperty("test.classes", "."));
jjg@1733 37 public final List<File> test_class_path = pathToFiles(System.getProperty("test.class.path"),
jjg@1733 38 Arrays.asList(test_classes));
duke@1 39 public final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
duke@1 40 public final StandardJavaFileManager fm = getFileManager(tool, null, null);
duke@1 41 public JavaCompiler.CompilationTask task = null;
duke@1 42 public final StandardJavaFileManager getFileManager(JavaCompiler tool,
duke@1 43 DiagnosticListener<JavaFileObject> dl,
duke@1 44 Charset encoding) {
duke@1 45 StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding);
duke@1 46 try {
duke@1 47 fm.setLocation(SOURCE_PATH, Arrays.asList(test_src));
jjg@1733 48 fm.setLocation(CLASS_PATH, test_class_path);
duke@1 49 fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes));
duke@1 50 } catch (IOException e) {
duke@1 51 throw new AssertionError(e);
duke@1 52 }
duke@1 53 return fm;
duke@1 54 }
jjg@1733 55
jjg@1733 56 protected List<File> pathToFiles(String path, List<File> defaultPath) {
jjg@1733 57 List<File> files = new ArrayList<>();
jjg@1733 58 for (String f: path.split(File.pathSeparator)) {
jjg@1733 59 if (f.isEmpty())
jjg@1733 60 continue;
jjg@1733 61 File file = new File(f);
jjg@1733 62 if (file.exists())
jjg@1733 63 files.add(file);
jjg@1733 64 }
jjg@1733 65 if (files.isEmpty())
jjg@1733 66 files.addAll(defaultPath);
jjg@1733 67 return files;
jjg@1733 68 }
jjg@1733 69
jjg@1733 70 protected <T> List<T> join(List<T> a, List<T> b) {
jjg@1733 71 if (a.isEmpty())
jjg@1733 72 return b;
jjg@1733 73 if (b.isEmpty())
jjg@1733 74 return a;
jjg@1733 75 List<T> result = new ArrayList<>();
jjg@1733 76 result.addAll(a);
jjg@1733 77 result.addAll(b);
jjg@1733 78 return result;
jjg@1733 79 }
duke@1 80 }

mercurial