|
Java example source code file (T6920317.java)
The T6920317.java Java example source code
/*
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6920317
* @summary package-info.java file has to be specified on the javac cmdline, else it will not be avail
* @library /tools/javac/lib
*/
import java.io.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.lang.model.util.*;
import javax.tools.*;
/**
* The test exercises different ways of providing annotations for a package.
* Each way provides an annotation with a unique argument. For each test
* case, the test verifies that the annotation with the correct argument is
* found by the compiler.
*/
public class T6920317 {
public static void main(String... args) throws Exception {
new T6920317().run(args);
}
// Used to describe properties of files to be put on command line, source path, class path
enum Kind {
/** File is not used. */
NONE,
/** File is used. */
OLD,
/** Only applies to files on classpath/sourcepath, when there is another file on the
* other path of type OLD, in which case, this file must be newer than the other one. */
NEW,
/** Only applies to files on classpath/sourcepath, when there is no file in any other
* location, in which case, this file will be generated by the annotation processor. */
GEN
}
void run(String... args) throws Exception {
// if no args given, all test cases are run
// if args given, they indicate the test cases to be run
for (int i = 0; i < args.length; i++) {
tests.add(Integer.valueOf(args[i]));
}
setup();
// Run tests for all combinations of files on command line, source path and class path.
// Invalid combinations are skipped in the test method
for (Kind cmdLine: EnumSet.of(Kind.NONE, Kind.OLD)) {
for (Kind srcPath: Kind.values()) {
for (Kind clsPath: Kind.values()) {
try {
test(cmdLine, srcPath, clsPath);
} catch (Exception e) {
e.printStackTrace();
error("Exception " + e);
// uncomment to stop on first failed test case
// throw e;
}
}
}
}
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
/** One time setup for files and directories to be used in the various test cases. */
void setup() throws Exception {
// Annotation used in test cases to annotate package. This file is
// given on the command line in test cases.
test_java = writeFile("Test.java", "package p; @interface Test { String value(); }");
// Compile the annotation for use later in setup
File tmpClasses = new File("tmp.classes");
compile(tmpClasses, new String[] { }, test_java);
// package-info file to use on the command line when requied
cl_pkgInfo_java = writeFile("cl/p/package-info.java", "@Test(\"CL\") package p;");
// source path containing package-info
sp_old = new File("src.old");
writeFile("src.old/p/package-info.java", "@Test(\"SP_OLD\") package p;");
// class path containing package-info
cp_old = new File("classes.old");
compile(cp_old, new String[] { "-classpath", tmpClasses.getPath() },
writeFile("tmp.old/p/package-info.java", "@Test(\"CP_OLD\") package p;"));
// source path containing package-info which is newer than the one in cp-old
sp_new = new File("src.new");
File old_class = new File(cp_old, "p/package-info.class");
writeFile("src.new/p/package-info.java", "@Test(\"SP_NEW\") package p;", old_class);
// class path containing package-info which is newer than the one in sp-old
cp_new = new File("classes.new");
File old_java = new File(sp_old, "p/package-info.java");
compile(cp_new, new String[] { "-classpath", tmpClasses.getPath() },
writeFile("tmp.new/p/package-info.java", "@Test(\"CP_NEW\") package p;", old_java));
// directory containing package-info.java to be "generated" later by annotation processor
sp_gen = new File("src.gen");
writeFile("src.gen/p/package-info.java", "@Test(\"SP_GEN\") package p;");
// directory containing package-info.class to be "generated" later by annotation processor
cp_gen = new File("classes.gen");
compile(cp_gen, new String[] { "-classpath", tmpClasses.getPath() },
writeFile("tmp.gen/p/package-info.java", "@Test(\"CP_GEN\") package p;"));
}
void test(Kind cl, Kind sp, Kind cp) throws Exception {
if (skip(cl, sp, cp))
return;
++count;
// if test cases specified, skip this test case if not selected
if (tests.size() > 0 && !tests.contains(count))
return;
System.err.println("Test " + count + " cl:" + cl + " sp:" + sp + " cp:" + cp);
// test specific tmp directory
File test_tmp = new File("tmp.test" + count);
test_tmp.mkdirs();
// build up list of options and files to be compiled
List<String> opts = new ArrayList
Other Java examples (source code examples)Here is a short list of links related to this Java T6920317.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.