test/tools/javac/MethodParameters/ReflectionVisitor.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 1791
e9855150c5b0
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.net.*;
strarup@1594 27 import java.lang.reflect.*;
strarup@1594 28
strarup@1594 29 /**
strarup@1594 30 * Test MethodParameter attributs by reflection API
strarup@1594 31 */
strarup@1594 32 public class ReflectionVisitor extends Tester.Visitor {
strarup@1594 33
strarup@1594 34 public ReflectionVisitor(Tester tester) {
strarup@1594 35 super(tester);
strarup@1594 36 }
strarup@1594 37
strarup@1594 38 public void error(String msg) {
strarup@1594 39 super.error("reflection: " + msg);
strarup@1594 40 }
strarup@1594 41
strarup@1594 42 public void warn(String msg) {
strarup@1594 43 super.warn("reflection: " + msg);
strarup@1594 44 }
strarup@1594 45
strarup@1594 46 boolean isEnum;
strarup@1594 47 boolean isInterface;
strarup@1594 48 boolean isAnon;
strarup@1594 49 boolean isLocal;
strarup@1594 50 boolean isMember;
strarup@1594 51 boolean isStatic;
strarup@1594 52 boolean isPublic;
strarup@1594 53 Class clazz;
strarup@1594 54 StringBuilder sb;
strarup@1594 55
strarup@1594 56 /**
strarup@1594 57 * Read class using {@code ClassFile}, and generate a list of methods
strarup@1594 58 * with parameter names as available in the MethodParameters attribute.
strarup@1594 59 */
strarup@1594 60 void visitClass(final String cl, final File cfile, final StringBuilder sb)
strarup@1594 61 throws Exception {
strarup@1594 62
strarup@1594 63 this.sb = sb;
strarup@1594 64 clazz = Class.forName(cl);
strarup@1594 65 isEnum = clazz.isEnum();
strarup@1594 66 isInterface = clazz.isInterface();
strarup@1594 67 isAnon = clazz.isAnonymousClass();
strarup@1594 68 isLocal = clazz.isLocalClass();
strarup@1594 69 isMember = clazz.isMemberClass();
strarup@1594 70 isStatic = ((clazz.getModifiers() & Modifier.STATIC) != 0);
strarup@1594 71 isPublic = ((clazz.getModifiers() & Modifier.PUBLIC) != 0);
strarup@1594 72
strarup@1594 73 sb.append(isStatic ? "static " : "")
strarup@1594 74 .append(isPublic ? "public " : "")
strarup@1594 75 .append(isEnum ? "enum " : isInterface ? "interface " : "class ")
strarup@1594 76 .append(cl).append(" -- ")
strarup@1594 77 .append(isMember? "member " : "" )
strarup@1594 78 .append(isLocal? "local " : "" )
strarup@1594 79 .append(isAnon ? "anon" : "")
strarup@1594 80 .append("\n");
strarup@1594 81
strarup@1594 82 for (Constructor c : clazz.getDeclaredConstructors()) {
strarup@1594 83 testConstructor(c);
strarup@1594 84 }
strarup@1594 85
strarup@1594 86 for (Method m :clazz.getDeclaredMethods()) {
strarup@1594 87
strarup@1594 88 testMethod(m);
strarup@1594 89 }
strarup@1594 90 }
strarup@1594 91
strarup@1594 92 void testConstructor(Constructor c) {
strarup@1594 93
strarup@1594 94 String prefix = clazz.getName() + "." + c.getName() + "() - ";
strarup@1594 95
strarup@1594 96 // Parameters must match parameter types
strarup@1594 97 Parameter params[] = c.getParameters();
strarup@1594 98 int paramTypes = c.getParameterTypes().length;
strarup@1594 99 if (paramTypes != params.length) {
strarup@1594 100 error(prefix + "number of parameter types (" + paramTypes
strarup@1594 101 + ") != number of parameters (" + params.length + ")");
strarup@1594 102 return;
strarup@1594 103 }
strarup@1594 104
strarup@1594 105 sb.append(clazz.getName()).append(".").append("<init>").append("(");
strarup@1594 106 String sep = "";
strarup@1594 107
strarup@1594 108 // Some paramters are expected
strarup@1594 109 if (params.length < 2 && isEnum) {
strarup@1594 110 error(prefix + "enum constuctor, two arguments expected");
strarup@1594 111 } else if (params.length < 1 && (isAnon || isLocal ||
strarup@1594 112 (isMember && !isStatic ))) {
strarup@1594 113 error(prefix + "class constuctor,expected implicit argument");
strarup@1594 114 }
strarup@1594 115
strarup@1594 116 int i = -1;
strarup@1594 117 String param = null;
strarup@1594 118 for (Parameter p : c.getParameters()) {
strarup@1594 119 i++;
strarup@1594 120 String pname = p.getName();
strarup@1594 121 sb.append(sep).append(pname);
strarup@1594 122 if (p.isImplicit()) sb.append("!");
strarup@1594 123 if (p.isSynthetic()) sb.append("!!");
strarup@1594 124 sep = ", ";
strarup@1594 125
strarup@1594 126 // Set expectations
strarup@1594 127 String expect = null;
strarup@1594 128 boolean allowImplicit = false;
strarup@1594 129 boolean allowSynthetic = false;
strarup@1594 130 if (isEnum) {
strarup@1594 131 if (i == 0) {
strarup@1594 132 expect = "\\$enum\\$name";
strarup@1594 133 allowSynthetic = true;
strarup@1594 134 } else if(i == 1) {
strarup@1594 135 expect = "\\$enum\\$ordinal";
strarup@1594 136 allowSynthetic = true;
strarup@1594 137 }
strarup@1594 138 } else if (i == 0) {
strarup@1594 139 if (isAnon) {
strarup@1594 140 allowImplicit = true;
strarup@1594 141 } else if (isLocal) {
strarup@1594 142 allowImplicit = true;
strarup@1594 143 expect = "this\\$[0-n]*";
strarup@1594 144 } else if ((isMember && !isStatic)) {
strarup@1594 145 allowImplicit = true;
strarup@1594 146 if (!isPublic) {
strarup@1594 147 // some but not all non-public inner classes
strarup@1594 148 // have synthetic argument. For now we give
strarup@1594 149 // the test a bit of slack and allow either.
strarup@1594 150 allowSynthetic = true;
strarup@1594 151 }
strarup@1594 152 expect = "this\\$[0-n]*";
strarup@1594 153 }
strarup@1594 154 } else if (isAnon) {
strarup@1594 155 // not an implementation gurantee, but okay for now
strarup@1594 156 expect = "x[0-n]*";
strarup@1594 157 }
strarup@1594 158
strarup@1594 159 // Check expected flags
strarup@1594 160 if (p.isSynthetic() && p.isImplicit()) {
strarup@1594 161 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 162 "' both isImplicit() and isSynthetic()");
strarup@1594 163 break;
strarup@1594 164 }
strarup@1594 165 if (allowImplicit && allowSynthetic &&
strarup@1594 166 !(p.isSynthetic() || p.isImplicit())) {
strarup@1594 167 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 168 "' isImplicit() or isSynthetic() expected");
strarup@1594 169 break;
strarup@1594 170 }
strarup@1594 171
strarup@1594 172 if (allowImplicit && !allowSynthetic && !p.isImplicit()) {
strarup@1594 173 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 174 "' isImplicit() expected");
strarup@1594 175 break;
strarup@1594 176 }
strarup@1594 177 if (!allowImplicit && allowSynthetic && !p.isSynthetic()) {
strarup@1594 178 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 179 "' isSynthetic() expected");
strarup@1594 180 break;
strarup@1594 181 }
strarup@1594 182
strarup@1594 183 if (!allowImplicit && p.isImplicit()) {
strarup@1594 184 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 185 "' isImplicit() unexpected");
strarup@1594 186 break;
strarup@1594 187 }
strarup@1594 188
strarup@1594 189 if (!allowSynthetic && p.isSynthetic()) {
strarup@1594 190 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 191 "' isSynthetic() unexpected");
strarup@1594 192 break;
strarup@1594 193 }
strarup@1594 194
strarup@1594 195 // Check expected names
strarup@1594 196 if (expect != null) {
strarup@1594 197 if (pname.matches(expect)) continue;
strarup@1594 198 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 199 "' expected '" + expect + "'");
strarup@1594 200 break;
strarup@1594 201 }
strarup@1594 202
strarup@1594 203 // Test naming convention for explicit parameters.
strarup@1594 204 boolean fidelity = !isAnon;
strarup@1594 205 if (param != null && fidelity) {
strarup@1594 206 char ch = param.charAt(0);
strarup@1594 207 expect = (++ch) + param;
strarup@1594 208 }
strarup@1594 209
strarup@1594 210 if (pname != null && fidelity) {
strarup@1594 211 param = pname;
strarup@1594 212 }
strarup@1594 213
strarup@1594 214 if (expect != null && !expect.equals(pname)) {
strarup@1594 215 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 216 "' expected '" + expect + "'");
strarup@1594 217 break;
strarup@1594 218 }
strarup@1594 219 }
strarup@1594 220 if (c.isSynthetic()) {
strarup@1594 221 sb.append(")!!\n");
strarup@1594 222 } else {
strarup@1594 223 sb.append(")\n");
strarup@1594 224 }
strarup@1594 225 }
strarup@1594 226
strarup@1594 227 void testMethod(Method m) {
strarup@1594 228
strarup@1594 229 String prefix = clazz.getName() + "." + m.getName() + "() - ";
strarup@1594 230
strarup@1594 231 // Parameters must match parameter types
strarup@1594 232 int paramTypes = m.getParameterTypes().length;
strarup@1594 233 int params = m.getParameters().length;
strarup@1594 234 if (paramTypes != params) {
strarup@1594 235 error(prefix + "number of parameter types (" + paramTypes
strarup@1594 236 + ") != number of parameters (" + params + ")");
strarup@1594 237 return;
strarup@1594 238 }
strarup@1594 239
strarup@1594 240 sb.append(clazz.getName()).append(".").append(m.getName()).append("(");
strarup@1594 241 String sep = "";
strarup@1594 242 String param = null;
strarup@1594 243 int i = -1;
strarup@1594 244 // For methods we expect all parameters to follow
strarup@1594 245 // the test-case design pattern, except synthetic methods.
strarup@1594 246 for (Parameter p : m.getParameters()) {
strarup@1594 247 i++;
strarup@1594 248 if (param == null) {
strarup@1594 249 param = p.getName();
strarup@1594 250 sb.append(sep).append(param);
strarup@1594 251 } else {
strarup@1594 252 char c = param.charAt(0);
strarup@1594 253 String expect = m.isSynthetic() ? ("arg" + i) : ((++c) + param);
strarup@1594 254 param = p.getName();
strarup@1594 255 sb.append(sep).append(param);
strarup@1594 256 if (!expect.equals(param)) {
strarup@1594 257 error(prefix + "param[" + i + "]='"
strarup@1594 258 + param + "' expected '" + expect + "'");
strarup@1594 259 break;
strarup@1594 260 }
strarup@1594 261 }
strarup@1594 262 sep = ", ";
strarup@1594 263 }
strarup@1594 264 if (m.isSynthetic()) {
strarup@1594 265 sb.append(")!!\n");
strarup@1594 266 } else {
strarup@1594 267 sb.append(")\n");
strarup@1594 268 }
strarup@1594 269 }
strarup@1594 270 }

mercurial