Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import java.io.*; |
aoqi@0 | 25 | import java.lang.reflect.*; |
aoqi@0 | 26 | |
aoqi@0 | 27 | /** |
aoqi@0 | 28 | * Test MethodParameter attributs by reflection API |
aoqi@0 | 29 | */ |
aoqi@0 | 30 | public class ReflectionVisitor extends Tester.Visitor { |
aoqi@0 | 31 | |
aoqi@0 | 32 | public ReflectionVisitor(Tester tester) { |
aoqi@0 | 33 | super(tester); |
aoqi@0 | 34 | } |
aoqi@0 | 35 | |
aoqi@0 | 36 | public void error(String msg) { |
aoqi@0 | 37 | super.error("reflection: " + msg); |
aoqi@0 | 38 | } |
aoqi@0 | 39 | |
aoqi@0 | 40 | public void warn(String msg) { |
aoqi@0 | 41 | super.warn("reflection: " + msg); |
aoqi@0 | 42 | } |
aoqi@0 | 43 | |
aoqi@0 | 44 | boolean isEnum; |
aoqi@0 | 45 | boolean isInterface; |
aoqi@0 | 46 | boolean isAnon; |
aoqi@0 | 47 | boolean isLocal; |
aoqi@0 | 48 | boolean isMember; |
aoqi@0 | 49 | boolean isStatic; |
aoqi@0 | 50 | boolean isPublic; |
aoqi@0 | 51 | boolean isFinal; |
aoqi@0 | 52 | Class clazz; |
aoqi@0 | 53 | StringBuilder sb; |
aoqi@0 | 54 | |
aoqi@0 | 55 | /** |
aoqi@0 | 56 | * Read class using {@code ClassFile}, and generate a list of methods |
aoqi@0 | 57 | * with parameter names as available in the MethodParameters attribute. |
aoqi@0 | 58 | */ |
aoqi@0 | 59 | void visitClass(final String cl, final File cfile, final StringBuilder sb) |
aoqi@0 | 60 | throws Exception { |
aoqi@0 | 61 | |
aoqi@0 | 62 | this.sb = sb; |
aoqi@0 | 63 | clazz = Class.forName(cl); |
aoqi@0 | 64 | isEnum = clazz.isEnum(); |
aoqi@0 | 65 | isInterface = clazz.isInterface(); |
aoqi@0 | 66 | isAnon = clazz.isAnonymousClass(); |
aoqi@0 | 67 | isLocal = clazz.isLocalClass(); |
aoqi@0 | 68 | isMember = clazz.isMemberClass(); |
aoqi@0 | 69 | isStatic = ((clazz.getModifiers() & Modifier.STATIC) != 0); |
aoqi@0 | 70 | isPublic = ((clazz.getModifiers() & Modifier.PUBLIC) != 0); |
aoqi@0 | 71 | |
aoqi@0 | 72 | sb.append(isStatic ? "static " : "") |
aoqi@0 | 73 | .append(isPublic ? "public " : "") |
aoqi@0 | 74 | .append(isEnum ? "enum " : isInterface ? "interface " : "class ") |
aoqi@0 | 75 | .append(cl).append(" -- ") |
aoqi@0 | 76 | .append(isMember? "inner" : "" ) |
aoqi@0 | 77 | .append(isLocal? "inner" : "" ) |
aoqi@0 | 78 | .append(isAnon ? "anon" : "") |
aoqi@0 | 79 | .append("\n"); |
aoqi@0 | 80 | |
aoqi@0 | 81 | for (Constructor c : clazz.getDeclaredConstructors()) { |
aoqi@0 | 82 | testConstructor(c); |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | for (Method m :clazz.getDeclaredMethods()) { |
aoqi@0 | 86 | testMethod(m); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | void testConstructor(Constructor c) { |
aoqi@0 | 91 | |
aoqi@0 | 92 | String prefix = clazz.getName() + "." + c.getName() + "() - "; |
aoqi@0 | 93 | |
aoqi@0 | 94 | // Parameters must match parameter types |
aoqi@0 | 95 | Parameter params[] = c.getParameters(); |
aoqi@0 | 96 | int paramTypes = c.getParameterTypes().length; |
aoqi@0 | 97 | if (paramTypes != params.length) { |
aoqi@0 | 98 | error(prefix + "number of parameter types (" + paramTypes |
aoqi@0 | 99 | + ") != number of parameters (" + params.length + ")"); |
aoqi@0 | 100 | return; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | sb.append(clazz.getName()).append(".").append("<init>").append("("); |
aoqi@0 | 104 | String sep = ""; |
aoqi@0 | 105 | |
aoqi@0 | 106 | // Some paramters are expected |
aoqi@0 | 107 | if (params.length < 2 && isEnum) { |
aoqi@0 | 108 | error(prefix + "enum constuctor, two arguments expected"); |
aoqi@0 | 109 | } else if (params.length < 1 && (isAnon || isLocal || |
aoqi@0 | 110 | (isMember && !isStatic ))) { |
aoqi@0 | 111 | error(prefix + "class constuctor,expected implicit argument"); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | int i = -1; |
aoqi@0 | 115 | String param = null; |
aoqi@0 | 116 | for (Parameter p : c.getParameters()) { |
aoqi@0 | 117 | i++; |
aoqi@0 | 118 | String pname = p.getName(); |
aoqi@0 | 119 | int pmodifier = p.getModifiers(); |
aoqi@0 | 120 | isFinal = false; |
aoqi@0 | 121 | if (Modifier.isFinal(pmodifier)) { |
aoqi@0 | 122 | isFinal = true; |
aoqi@0 | 123 | pname = "final " + pname; |
aoqi@0 | 124 | } |
aoqi@0 | 125 | sb.append(sep).append(pname); |
aoqi@0 | 126 | if (p.isImplicit()) sb.append("/*implicit*/"); |
aoqi@0 | 127 | if (p.isSynthetic()) sb.append("/*synthetic*/"); |
aoqi@0 | 128 | sep = ", "; |
aoqi@0 | 129 | |
aoqi@0 | 130 | // Set expectations |
aoqi@0 | 131 | String expect = null; |
aoqi@0 | 132 | boolean allowImplicit = false; |
aoqi@0 | 133 | boolean allowSynthetic = false; |
aoqi@0 | 134 | if (isEnum) { |
aoqi@0 | 135 | if (i == 0) { |
aoqi@0 | 136 | expect = "\\$enum\\$name"; |
aoqi@0 | 137 | allowSynthetic = true; |
aoqi@0 | 138 | } else if(i == 1) { |
aoqi@0 | 139 | expect = "\\$enum\\$ordinal"; |
aoqi@0 | 140 | allowSynthetic = true; |
aoqi@0 | 141 | } |
aoqi@0 | 142 | } else if (i == 0) { |
aoqi@0 | 143 | if (isAnon) { |
aoqi@0 | 144 | expect = "this\\$[0-9]+"; |
aoqi@0 | 145 | allowImplicit = true; |
aoqi@0 | 146 | if (isFinal) |
aoqi@0 | 147 | expect = "final this\\$[0-9]+"; |
aoqi@0 | 148 | } else if (isLocal) { |
aoqi@0 | 149 | expect = "this\\$[0-9]+"; |
aoqi@0 | 150 | allowImplicit = true; |
aoqi@0 | 151 | if (isFinal) |
aoqi@0 | 152 | expect = "final this\\$[0-9]+"; |
aoqi@0 | 153 | } else if ((isMember && !isStatic)) { |
aoqi@0 | 154 | expect = "this\\$[0-9]+"; |
aoqi@0 | 155 | allowImplicit = true; |
aoqi@0 | 156 | if (!isPublic) { |
aoqi@0 | 157 | // some but not all non-public inner classes |
aoqi@0 | 158 | // have synthetic argument. For now we give |
aoqi@0 | 159 | // the test a bit of slack and allow either. |
aoqi@0 | 160 | allowSynthetic = true; |
aoqi@0 | 161 | } |
aoqi@0 | 162 | if (isFinal) |
aoqi@0 | 163 | expect = "final this\\$[0-9]+"; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | // Check expected flags |
aoqi@0 | 168 | if (p.isSynthetic() && p.isImplicit()) { |
aoqi@0 | 169 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 170 | "' both isImplicit() and isSynthetic()"); |
aoqi@0 | 171 | break; |
aoqi@0 | 172 | } |
aoqi@0 | 173 | if (allowImplicit && allowSynthetic && |
aoqi@0 | 174 | !(p.isSynthetic() || p.isImplicit())) { |
aoqi@0 | 175 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 176 | "' isImplicit() or isSynthetic() expected"); |
aoqi@0 | 177 | break; |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | if (allowImplicit && !allowSynthetic && !p.isImplicit()) { |
aoqi@0 | 181 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 182 | "' isImplicit() expected"); |
aoqi@0 | 183 | break; |
aoqi@0 | 184 | } |
aoqi@0 | 185 | if (!allowImplicit && allowSynthetic && !p.isSynthetic()) { |
aoqi@0 | 186 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 187 | "' isSynthetic() expected"); |
aoqi@0 | 188 | break; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | if (!allowImplicit && p.isImplicit()) { |
aoqi@0 | 192 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 193 | "' isImplicit() unexpected"); |
aoqi@0 | 194 | break; |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | if (!allowSynthetic && p.isSynthetic()) { |
aoqi@0 | 198 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 199 | "' isSynthetic() unexpected"); |
aoqi@0 | 200 | break; |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | // Check expected names |
aoqi@0 | 204 | if (expect != null) { |
aoqi@0 | 205 | if (pname.matches(expect)) continue; |
aoqi@0 | 206 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 207 | "' expected '" + expect + "'"); |
aoqi@0 | 208 | break; |
aoqi@0 | 209 | } |
aoqi@0 | 210 | |
aoqi@0 | 211 | // Test naming convention for explicit parameters. |
aoqi@0 | 212 | boolean fidelity = !isAnon; |
aoqi@0 | 213 | if (param != null && fidelity) { |
aoqi@0 | 214 | char ch = param.charAt(0); |
aoqi@0 | 215 | expect = (++ch) + param; |
aoqi@0 | 216 | } |
aoqi@0 | 217 | if (isFinal && expect != null) { |
aoqi@0 | 218 | expect = "final " + expect; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | if (pname != null && fidelity) { |
aoqi@0 | 221 | if (isFinal) { |
aoqi@0 | 222 | param = pname.substring(6); |
aoqi@0 | 223 | } else { |
aoqi@0 | 224 | param = pname; |
aoqi@0 | 225 | } |
aoqi@0 | 226 | } |
aoqi@0 | 227 | if (expect != null && !expect.equals(pname)) { |
aoqi@0 | 228 | error(prefix + "param[" + i + "]='" + pname + |
aoqi@0 | 229 | "' expected '" + expect + "'"); |
aoqi@0 | 230 | break; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | } |
aoqi@0 | 233 | if (c.isSynthetic()) { |
aoqi@0 | 234 | sb.append(")/*synthetic*/\n"); |
aoqi@0 | 235 | } else { |
aoqi@0 | 236 | sb.append(")\n"); |
aoqi@0 | 237 | } |
aoqi@0 | 238 | } |
aoqi@0 | 239 | |
aoqi@0 | 240 | void testMethod(Method m) { |
aoqi@0 | 241 | |
aoqi@0 | 242 | String prefix = clazz.getName() + "." + m.getName() + "() - "; |
aoqi@0 | 243 | |
aoqi@0 | 244 | // Parameters must match parameter types |
aoqi@0 | 245 | int paramTypes = m.getParameterTypes().length; |
aoqi@0 | 246 | int params = m.getParameters().length; |
aoqi@0 | 247 | if (paramTypes != params) { |
aoqi@0 | 248 | error(prefix + "number of parameter types (" + paramTypes |
aoqi@0 | 249 | + ") != number of parameters (" + params + ")"); |
aoqi@0 | 250 | return; |
aoqi@0 | 251 | } |
aoqi@0 | 252 | |
aoqi@0 | 253 | sb.append(clazz.getName()).append(".").append(m.getName()).append("("); |
aoqi@0 | 254 | String sep = ""; |
aoqi@0 | 255 | String param = null; |
aoqi@0 | 256 | int i = -1; |
aoqi@0 | 257 | // For methods we expect all parameters to follow |
aoqi@0 | 258 | // the test-case design pattern, except synthetic methods. |
aoqi@0 | 259 | for (Parameter p : m.getParameters()) { |
aoqi@0 | 260 | i++; |
aoqi@0 | 261 | isFinal = false; |
aoqi@0 | 262 | int pmodifier = p.getModifiers(); |
aoqi@0 | 263 | if (param == null) { |
aoqi@0 | 264 | param = p.getName(); |
aoqi@0 | 265 | if (Modifier.isFinal(pmodifier)) { |
aoqi@0 | 266 | isFinal = true; |
aoqi@0 | 267 | param = "final " + param; |
aoqi@0 | 268 | } |
aoqi@0 | 269 | sb.append(sep).append(param); |
aoqi@0 | 270 | } else { |
aoqi@0 | 271 | char c = param.charAt(0); |
aoqi@0 | 272 | String expect = m.isSynthetic() ? ("arg" + i) : ((++c) + param); |
aoqi@0 | 273 | param = p.getName(); |
aoqi@0 | 274 | if (Modifier.isFinal(pmodifier)) { |
aoqi@0 | 275 | isFinal = true; |
aoqi@0 | 276 | expect = "final " + expect; |
aoqi@0 | 277 | param = "final " + param; |
aoqi@0 | 278 | } |
aoqi@0 | 279 | sb.append(sep).append(param); |
jlahoda@2733 | 280 | if (!m.isBridge() && !m.getName().startsWith("lambda$") && !expect.equals(param)) { |
aoqi@0 | 281 | error(prefix + "param[" + i + "]='" |
aoqi@0 | 282 | + param + "' expected '" + expect + "'"); |
aoqi@0 | 283 | break; |
aoqi@0 | 284 | } |
aoqi@0 | 285 | } |
aoqi@0 | 286 | if(isFinal) |
aoqi@0 | 287 | param = param.substring(6); |
aoqi@0 | 288 | if (p.isImplicit()) { |
aoqi@0 | 289 | sb.append("/*implicit*/"); |
aoqi@0 | 290 | } |
aoqi@0 | 291 | if (p.isSynthetic()) { |
aoqi@0 | 292 | sb.append("/*synthetic*/"); |
aoqi@0 | 293 | } |
aoqi@0 | 294 | sep = ", "; |
aoqi@0 | 295 | } |
aoqi@0 | 296 | if (m.isSynthetic()) { |
aoqi@0 | 297 | sb.append(")/*synthetic*/\n"); |
aoqi@0 | 298 | } else { |
aoqi@0 | 299 | sb.append(")\n"); |
aoqi@0 | 300 | } |
aoqi@0 | 301 | } |
aoqi@0 | 302 | } |