test/tools/apt/lib/Tester.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/apt/lib/Tester.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 + * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +/*
    1.29 + * A utility used to invoke and test the apt tool.
    1.30 + * Tests should subclass Tester, and invoke run().
    1.31 + *
    1.32 + * @author Scott Seligman
    1.33 + */
    1.34 +
    1.35 +import java.io.*;
    1.36 +import java.util.*;
    1.37 +import com.sun.mirror.apt.*;
    1.38 +import com.sun.mirror.declaration.*;
    1.39 +
    1.40 +
    1.41 +public abstract class Tester {
    1.42 +
    1.43 +    /**
    1.44 +     * The declaration corresponding to this tester's class.  Set by
    1.45 +     * TestProcessorFactory after the constructor completes, and
    1.46 +     * before init() is invoked.
    1.47 +     */
    1.48 +    ClassDeclaration thisClassDecl;
    1.49 +
    1.50 +    /**
    1.51 +     * The environment for this apt run.  Set by TestProcessorFactory
    1.52 +     * after the constructor completes, and before init() is invoked.
    1.53 +     */
    1.54 +    AnnotationProcessorEnvironment env;
    1.55 +
    1.56 +
    1.57 +    // TestProcessorFactory looks here to find the tester that's running
    1.58 +    // when it's invoked.
    1.59 +    static Tester activeTester;
    1.60 +
    1.61 +    private static final String[] DEFAULT_ARGS = {
    1.62 +        "-nocompile",
    1.63 +        "-XPrintAptRounds",
    1.64 +        "-XListDeclarations",
    1.65 +    };
    1.66 +    private static final String[] NO_STRINGS = {};
    1.67 +
    1.68 +    // Force processor and factory to be compiled
    1.69 +    private static Class dummy = TestProcessorFactory.class;
    1.70 +
    1.71 +    private final String testSrc =     System.getProperty("test.src",     ".");
    1.72 +    private final String testClasses = System.getProperty("test.classes", ".");
    1.73 +
    1.74 +    // apt command-line args
    1.75 +    private String[] args;
    1.76 +
    1.77 +
    1.78 +    static {
    1.79 +        // Enable assertions in the unnamed package.
    1.80 +        ClassLoader loader = Tester.class.getClassLoader();
    1.81 +        if (loader != null) {
    1.82 +            loader.setPackageAssertionStatus(null, true);
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +
    1.87 +    protected Tester(String... additionalArgs) {
    1.88 +        String sourceFile = testSrc + File.separator +
    1.89 +                            getClass().getName() + ".java";
    1.90 +
    1.91 +        ArrayList<String> as = new ArrayList<String>();
    1.92 +        Collections.addAll(as, DEFAULT_ARGS);
    1.93 +        as.add("-sourcepath");  as.add(testSrc);
    1.94 +        as.add("-factory");     as.add(TestProcessorFactory.class.getName());
    1.95 +        Collections.addAll(as, additionalArgs);
    1.96 +        as.add(sourceFile);
    1.97 +        args = as.toArray(NO_STRINGS);
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Run apt.
   1.102 +     */
   1.103 +    protected void run() {
   1.104 +        activeTester = this;
   1.105 +        if (com.sun.tools.apt.Main.process(args) != 0) {
   1.106 +            throw new Error("apt errors encountered.");
   1.107 +        }
   1.108 +    }
   1.109 +
   1.110 +    /**
   1.111 +     * Called after thisClassDecl and env have been set, but before any
   1.112 +     * tests are run, to allow the tester subclass to perform any
   1.113 +     * needed initialization.
   1.114 +     */
   1.115 +    protected void init() {
   1.116 +    }
   1.117 +
   1.118 +    /**
   1.119 +     * Returns the declaration of a named method in this class.  If this
   1.120 +     * method name is overloaded, one method is chosen arbitrarily.
   1.121 +     * Returns null if no method is found.
   1.122 +     */
   1.123 +    protected MethodDeclaration getMethod(String methodName) {
   1.124 +        for (MethodDeclaration m : thisClassDecl.getMethods()) {
   1.125 +            if (methodName.equals(m.getSimpleName())) {
   1.126 +                return m;
   1.127 +            }
   1.128 +        }
   1.129 +        return null;
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Returns the declaration of a named field in this class.
   1.134 +     * Returns null if no field is found.
   1.135 +     */
   1.136 +    protected FieldDeclaration getField(String fieldName) {
   1.137 +        for (FieldDeclaration f : thisClassDecl.getFields()) {
   1.138 +            if (fieldName.equals(f.getSimpleName())) {
   1.139 +                return f;
   1.140 +            }
   1.141 +        }
   1.142 +        return null;
   1.143 +    }
   1.144 +
   1.145 +    /**
   1.146 +     * Returns the annotation mirror of a given type on a named method
   1.147 +     * in this class.  If this method name is overloaded, one method is
   1.148 +     * chosen arbitrarily.  Returns null if no appropriate annotation
   1.149 +     * is found.
   1.150 +     */
   1.151 +    protected AnnotationMirror getAnno(String methodName, String annoType) {
   1.152 +        MethodDeclaration m = getMethod(methodName);
   1.153 +        if (m != null) {
   1.154 +            TypeDeclaration at = env.getTypeDeclaration(annoType);
   1.155 +            for (AnnotationMirror a : m.getAnnotationMirrors()) {
   1.156 +                if (at == a.getAnnotationType().getDeclaration()) {
   1.157 +                    return a;
   1.158 +                }
   1.159 +            }
   1.160 +        }
   1.161 +        return null;
   1.162 +    }
   1.163 +}

mercurial