test/tools/apt/lib/Tester.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 554
9d9f26857129
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

duke@1 1 /*
ohair@554 2 * Copyright (c) 2004, 2007, 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
duke@1 25 /*
duke@1 26 * A utility used to invoke and test the apt tool.
duke@1 27 * Tests should subclass Tester, and invoke run().
duke@1 28 *
duke@1 29 * @author Scott Seligman
duke@1 30 */
duke@1 31
duke@1 32 import java.io.*;
duke@1 33 import java.util.*;
duke@1 34 import com.sun.mirror.apt.*;
duke@1 35 import com.sun.mirror.declaration.*;
duke@1 36
duke@1 37
duke@1 38 public abstract class Tester {
duke@1 39
duke@1 40 /**
duke@1 41 * The declaration corresponding to this tester's class. Set by
duke@1 42 * TestProcessorFactory after the constructor completes, and
duke@1 43 * before init() is invoked.
duke@1 44 */
duke@1 45 ClassDeclaration thisClassDecl;
duke@1 46
duke@1 47 /**
duke@1 48 * The environment for this apt run. Set by TestProcessorFactory
duke@1 49 * after the constructor completes, and before init() is invoked.
duke@1 50 */
duke@1 51 AnnotationProcessorEnvironment env;
duke@1 52
duke@1 53
duke@1 54 // TestProcessorFactory looks here to find the tester that's running
duke@1 55 // when it's invoked.
duke@1 56 static Tester activeTester;
duke@1 57
duke@1 58 private static final String[] DEFAULT_ARGS = {
duke@1 59 "-nocompile",
duke@1 60 "-XPrintAptRounds",
duke@1 61 "-XListDeclarations",
duke@1 62 };
duke@1 63 private static final String[] NO_STRINGS = {};
duke@1 64
duke@1 65 // Force processor and factory to be compiled
duke@1 66 private static Class dummy = TestProcessorFactory.class;
duke@1 67
duke@1 68 private final String testSrc = System.getProperty("test.src", ".");
duke@1 69 private final String testClasses = System.getProperty("test.classes", ".");
duke@1 70
duke@1 71 // apt command-line args
duke@1 72 private String[] args;
duke@1 73
duke@1 74
duke@1 75 static {
duke@1 76 // Enable assertions in the unnamed package.
duke@1 77 ClassLoader loader = Tester.class.getClassLoader();
duke@1 78 if (loader != null) {
duke@1 79 loader.setPackageAssertionStatus(null, true);
duke@1 80 }
duke@1 81 }
duke@1 82
duke@1 83
duke@1 84 protected Tester(String... additionalArgs) {
duke@1 85 String sourceFile = testSrc + File.separator +
duke@1 86 getClass().getName() + ".java";
duke@1 87
duke@1 88 ArrayList<String> as = new ArrayList<String>();
duke@1 89 Collections.addAll(as, DEFAULT_ARGS);
duke@1 90 as.add("-sourcepath"); as.add(testSrc);
duke@1 91 as.add("-factory"); as.add(TestProcessorFactory.class.getName());
duke@1 92 Collections.addAll(as, additionalArgs);
duke@1 93 as.add(sourceFile);
duke@1 94 args = as.toArray(NO_STRINGS);
duke@1 95 }
duke@1 96
duke@1 97 /**
duke@1 98 * Run apt.
duke@1 99 */
duke@1 100 protected void run() {
duke@1 101 activeTester = this;
duke@1 102 if (com.sun.tools.apt.Main.process(args) != 0) {
duke@1 103 throw new Error("apt errors encountered.");
duke@1 104 }
duke@1 105 }
duke@1 106
duke@1 107 /**
duke@1 108 * Called after thisClassDecl and env have been set, but before any
duke@1 109 * tests are run, to allow the tester subclass to perform any
duke@1 110 * needed initialization.
duke@1 111 */
duke@1 112 protected void init() {
duke@1 113 }
duke@1 114
duke@1 115 /**
duke@1 116 * Returns the declaration of a named method in this class. If this
duke@1 117 * method name is overloaded, one method is chosen arbitrarily.
duke@1 118 * Returns null if no method is found.
duke@1 119 */
duke@1 120 protected MethodDeclaration getMethod(String methodName) {
duke@1 121 for (MethodDeclaration m : thisClassDecl.getMethods()) {
duke@1 122 if (methodName.equals(m.getSimpleName())) {
duke@1 123 return m;
duke@1 124 }
duke@1 125 }
duke@1 126 return null;
duke@1 127 }
duke@1 128
duke@1 129 /**
duke@1 130 * Returns the declaration of a named field in this class.
duke@1 131 * Returns null if no field is found.
duke@1 132 */
duke@1 133 protected FieldDeclaration getField(String fieldName) {
duke@1 134 for (FieldDeclaration f : thisClassDecl.getFields()) {
duke@1 135 if (fieldName.equals(f.getSimpleName())) {
duke@1 136 return f;
duke@1 137 }
duke@1 138 }
duke@1 139 return null;
duke@1 140 }
duke@1 141
duke@1 142 /**
duke@1 143 * Returns the annotation mirror of a given type on a named method
duke@1 144 * in this class. If this method name is overloaded, one method is
duke@1 145 * chosen arbitrarily. Returns null if no appropriate annotation
duke@1 146 * is found.
duke@1 147 */
duke@1 148 protected AnnotationMirror getAnno(String methodName, String annoType) {
duke@1 149 MethodDeclaration m = getMethod(methodName);
duke@1 150 if (m != null) {
duke@1 151 TypeDeclaration at = env.getTypeDeclaration(annoType);
duke@1 152 for (AnnotationMirror a : m.getAnnotationMirrors()) {
duke@1 153 if (at == a.getAnnotationType().getDeclaration()) {
duke@1 154 return a;
duke@1 155 }
duke@1 156 }
duke@1 157 }
duke@1 158 return null;
duke@1 159 }
duke@1 160 }

mercurial