test/tools/javac/MethodParameters/ReflectionVisitor.java

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 2137
a48d3b981083
child 2525
2eb010b6cb22
child 2733
7974f6da2d76
permissions
-rw-r--r--

Merge

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

mercurial