test/tools/javac/MethodParameters/Tester.java

Thu, 21 Feb 2013 15:26:46 +0000

author
mcimadamore
date
Thu, 21 Feb 2013 15:26:46 +0000
changeset 1599
9f0ec00514b6
parent 1594
267225edc1fe
child 1792
ec871c3e8337
permissions
-rw-r--r--

8007461: Regression: bad overload resolution when inner class and outer class have method with same name
Summary: Fix regression in varargs method resolution introduced by bad refactoring
Reviewed-by: jjg

strarup@1594 1 /*
strarup@1594 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
strarup@1594 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
strarup@1594 4 *
strarup@1594 5 * This code is free software; you can redistribute it and/or modify it
strarup@1594 6 * under the terms of the GNU General Public License version 2 only, as
strarup@1594 7 * published by the Free Software Foundation.
strarup@1594 8 *
strarup@1594 9 * This code is distributed in the hope that it will be useful, but WITHOUT
strarup@1594 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
strarup@1594 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
strarup@1594 12 * version 2 for more details (a copy is included in the LICENSE file that
strarup@1594 13 * accompanied this code).
strarup@1594 14 *
strarup@1594 15 * You should have received a copy of the GNU General Public License version
strarup@1594 16 * 2 along with this work; if not, write to the Free Software Foundation,
strarup@1594 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
strarup@1594 18 *
strarup@1594 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
strarup@1594 20 * or visit www.oracle.com if you need additional information or have any
strarup@1594 21 * questions.
strarup@1594 22 */
strarup@1594 23
strarup@1594 24 import java.io.*;
strarup@1594 25 import java.util.*;
strarup@1594 26 import java.lang.reflect.Constructor;
strarup@1594 27
strarup@1594 28 /**
strarup@1594 29 * Test driver for MethodParameters testing.
strarup@1594 30 * <p>
strarup@1594 31 * The intended use of this driver is to run it, giving the name of
strarup@1594 32 * a single class compiled with -parameters as argument. The driver
strarup@1594 33 * will test the specified class, and any nested classes it finds.
strarup@1594 34 * <p>
strarup@1594 35 * Each class is tested in two way. By refelction, and by directly
strarup@1594 36 * checking MethodParameters attributes in the classfile. The checking
strarup@1594 37 * is done using two visitor classes {@link ClassFileVisitor} and
strarup@1594 38 * {@link ReflectionVisitor}.
strarup@1594 39 * <p>
strarup@1594 40 * The {@code ReflectionVisitor} test logically belongs with library tests.
strarup@1594 41 * we wish to reuse the same test-cases, so both test are committed together,
strarup@1594 42 * under langtools. The tests, may be duplicated in the jdk repository.
strarup@1594 43 */
strarup@1594 44 public class Tester {
strarup@1594 45
strarup@1594 46 final static File classesdir = new File(System.getProperty("test.classes", "."));
strarup@1594 47
strarup@1594 48 /**
strarup@1594 49 * The visitor classes that does the actual checking are referenced
strarup@1594 50 * statically, to force compilations, without having to reference
strarup@1594 51 * them in individual test cases.
strarup@1594 52 * <p>
strarup@1594 53 * This makes it easy to change the set of visitors, without
strarup@1594 54 * complicating the design with dynamic discovery and compilation
strarup@1594 55 * of visitor classes.
strarup@1594 56 */
strarup@1594 57 static final Class visitors[] = {
strarup@1594 58 ClassFileVisitor.class,
strarup@1594 59 ReflectionVisitor.class
strarup@1594 60 };
strarup@1594 61
strarup@1594 62 /**
strarup@1594 63 * Test-driver expect a single classname as argument.
strarup@1594 64 */
strarup@1594 65 public static void main(String... args) throws Exception {
strarup@1594 66 if (args.length != 1) {
strarup@1594 67 throw new Error("A single class name is expected as argument");
strarup@1594 68 }
strarup@1594 69 final String pattern = args[0] + ".*\\.class";
strarup@1594 70 File files[] = classesdir.listFiles(new FileFilter() {
strarup@1594 71 public boolean accept(File f) {
strarup@1594 72 return f.getName().matches(pattern);
strarup@1594 73 }
strarup@1594 74 });
strarup@1594 75 if (files.length == 0) {
strarup@1594 76 File file = new File(classesdir, args[0] + ".class");
strarup@1594 77 throw new Error(file.getPath() + " not found");
strarup@1594 78 }
strarup@1594 79
strarup@1594 80 new Tester(args[0], files).run();
strarup@1594 81 }
strarup@1594 82
strarup@1594 83 public Tester(String name, File files[]) {
strarup@1594 84 this.classname = name;
strarup@1594 85 this.files = files;
strarup@1594 86 }
strarup@1594 87
strarup@1594 88 void run() throws Exception {
strarup@1594 89
strarup@1594 90 // Test with each visitor
strarup@1594 91 for (Class<Visitor> vclass : visitors) {
strarup@1594 92 try {
strarup@1594 93 String vname = vclass.getName();
strarup@1594 94 Constructor c = vclass.getConstructor(Tester.class);
strarup@1594 95
strarup@1594 96 info("\nRun " + vname + " for " + classname + "\n");
strarup@1594 97 StringBuilder sb = new StringBuilder();
strarup@1594 98 for (File f : files) {
strarup@1594 99 String fname = f.getName();
strarup@1594 100 fname = fname.substring(0, fname.length() - 6);
strarup@1594 101 Visitor v = (Visitor) c.newInstance(this);
strarup@1594 102 try {
strarup@1594 103 v.visitClass(fname, f, sb);
strarup@1594 104 } catch(Exception e) {
strarup@1594 105 error("Uncaught exception in visitClass()");
strarup@1594 106 e.printStackTrace();
strarup@1594 107 }
strarup@1594 108 }
strarup@1594 109 info(sb.toString());
strarup@1594 110 } catch(ReflectiveOperationException e) {
strarup@1594 111 warn("Class " + vclass.getName() + " ignored, not a Visitor");
strarup@1594 112 continue;
strarup@1594 113 }
strarup@1594 114 }
strarup@1594 115
strarup@1594 116 if(0 != warnings)
strarup@1594 117 System.err.println("Test generated " + warnings + " warnings");
strarup@1594 118
strarup@1594 119 if(0 != errors)
strarup@1594 120 throw new Exception("Tester test failed with " +
strarup@1594 121 errors + " errors");
strarup@1594 122 }
strarup@1594 123
strarup@1594 124 abstract static class Visitor {
strarup@1594 125 Tester tester;
strarup@1594 126 File classesdir;
strarup@1594 127
strarup@1594 128 public Visitor(Tester tester) {
strarup@1594 129 this.tester = tester;
strarup@1594 130 }
strarup@1594 131
strarup@1594 132 abstract void visitClass(final String classname, final File cfile,
strarup@1594 133 final StringBuilder sb) throws Exception;
strarup@1594 134
strarup@1594 135 public void error(String msg) {
strarup@1594 136 tester.error(msg);
strarup@1594 137 }
strarup@1594 138
strarup@1594 139 public void warn(String msg) {
strarup@1594 140 tester.warn(msg);
strarup@1594 141 }
strarup@1594 142 }
strarup@1594 143
strarup@1594 144 void error(String msg) {
strarup@1594 145 System.err.println("Error: " + msg);
strarup@1594 146 errors++;
strarup@1594 147 }
strarup@1594 148
strarup@1594 149 void warn(String msg) {
strarup@1594 150 System.err.println("Warning: " + msg);
strarup@1594 151 warnings++;
strarup@1594 152 }
strarup@1594 153
strarup@1594 154 void info(String msg) {
strarup@1594 155 System.out.println(msg);
strarup@1594 156 }
strarup@1594 157
strarup@1594 158 int errors;
strarup@1594 159 int warnings;
strarup@1594 160 String classname;
strarup@1594 161 File files[];
strarup@1594 162 }

mercurial