test/tools/javac/MethodParameters/ReflectionVisitor.java

Mon, 16 Sep 2013 14:13:44 +0200

author
jlahoda
date
Mon, 16 Sep 2013 14:13:44 +0200
changeset 2028
4ce8148ffc4f
parent 1792
ec871c3e8337
child 2137
a48d3b981083
permissions
-rw-r--r--

8021112: Spurious unchecked warning reported by javac
6480588: No way to suppress deprecation warnings when implementing deprecated interface
Summary: Fixing DeferredLintHandler configuration, so lint warnings are reported with correct @SuppressWarnings settings
Reviewed-by: jjg, vromero

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;
strarup@1594 51 Class clazz;
strarup@1594 52 StringBuilder sb;
strarup@1594 53
strarup@1594 54 /**
strarup@1594 55 * Read class using {@code ClassFile}, and generate a list of methods
strarup@1594 56 * with parameter names as available in the MethodParameters attribute.
strarup@1594 57 */
strarup@1594 58 void visitClass(final String cl, final File cfile, final StringBuilder sb)
strarup@1594 59 throws Exception {
strarup@1594 60
strarup@1594 61 this.sb = sb;
strarup@1594 62 clazz = Class.forName(cl);
strarup@1594 63 isEnum = clazz.isEnum();
strarup@1594 64 isInterface = clazz.isInterface();
strarup@1594 65 isAnon = clazz.isAnonymousClass();
strarup@1594 66 isLocal = clazz.isLocalClass();
strarup@1594 67 isMember = clazz.isMemberClass();
strarup@1594 68 isStatic = ((clazz.getModifiers() & Modifier.STATIC) != 0);
strarup@1594 69 isPublic = ((clazz.getModifiers() & Modifier.PUBLIC) != 0);
strarup@1594 70
strarup@1594 71 sb.append(isStatic ? "static " : "")
strarup@1594 72 .append(isPublic ? "public " : "")
strarup@1594 73 .append(isEnum ? "enum " : isInterface ? "interface " : "class ")
strarup@1594 74 .append(cl).append(" -- ")
strarup@1594 75 .append(isMember? "member " : "" )
strarup@1594 76 .append(isLocal? "local " : "" )
strarup@1594 77 .append(isAnon ? "anon" : "")
strarup@1594 78 .append("\n");
strarup@1594 79
strarup@1594 80 for (Constructor c : clazz.getDeclaredConstructors()) {
strarup@1594 81 testConstructor(c);
strarup@1594 82 }
strarup@1594 83
strarup@1594 84 for (Method m :clazz.getDeclaredMethods()) {
strarup@1594 85
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();
strarup@1594 119 sb.append(sep).append(pname);
strarup@1594 120 if (p.isImplicit()) sb.append("!");
strarup@1594 121 if (p.isSynthetic()) sb.append("!!");
strarup@1594 122 sep = ", ";
strarup@1594 123
strarup@1594 124 // Set expectations
strarup@1594 125 String expect = null;
strarup@1594 126 boolean allowImplicit = false;
strarup@1594 127 boolean allowSynthetic = false;
strarup@1594 128 if (isEnum) {
strarup@1594 129 if (i == 0) {
strarup@1594 130 expect = "\\$enum\\$name";
strarup@1594 131 allowSynthetic = true;
strarup@1594 132 } else if(i == 1) {
strarup@1594 133 expect = "\\$enum\\$ordinal";
strarup@1594 134 allowSynthetic = true;
strarup@1594 135 }
strarup@1594 136 } else if (i == 0) {
strarup@1594 137 if (isAnon) {
strarup@1594 138 allowImplicit = true;
strarup@1594 139 } else if (isLocal) {
strarup@1594 140 allowImplicit = true;
strarup@1594 141 expect = "this\\$[0-n]*";
strarup@1594 142 } else if ((isMember && !isStatic)) {
strarup@1594 143 allowImplicit = true;
strarup@1594 144 if (!isPublic) {
strarup@1594 145 // some but not all non-public inner classes
strarup@1594 146 // have synthetic argument. For now we give
strarup@1594 147 // the test a bit of slack and allow either.
strarup@1594 148 allowSynthetic = true;
strarup@1594 149 }
strarup@1594 150 expect = "this\\$[0-n]*";
strarup@1594 151 }
strarup@1594 152 }
strarup@1594 153
strarup@1594 154 // Check expected flags
strarup@1594 155 if (p.isSynthetic() && p.isImplicit()) {
strarup@1594 156 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 157 "' both isImplicit() and isSynthetic()");
strarup@1594 158 break;
strarup@1594 159 }
strarup@1594 160 if (allowImplicit && allowSynthetic &&
strarup@1594 161 !(p.isSynthetic() || p.isImplicit())) {
strarup@1594 162 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 163 "' isImplicit() or isSynthetic() expected");
strarup@1594 164 break;
strarup@1594 165 }
strarup@1594 166
strarup@1594 167 if (allowImplicit && !allowSynthetic && !p.isImplicit()) {
strarup@1594 168 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 169 "' isImplicit() expected");
strarup@1594 170 break;
strarup@1594 171 }
strarup@1594 172 if (!allowImplicit && allowSynthetic && !p.isSynthetic()) {
strarup@1594 173 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 174 "' isSynthetic() expected");
strarup@1594 175 break;
strarup@1594 176 }
strarup@1594 177
strarup@1594 178 if (!allowImplicit && p.isImplicit()) {
strarup@1594 179 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 180 "' isImplicit() unexpected");
strarup@1594 181 break;
strarup@1594 182 }
strarup@1594 183
strarup@1594 184 if (!allowSynthetic && p.isSynthetic()) {
strarup@1594 185 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 186 "' isSynthetic() unexpected");
strarup@1594 187 break;
strarup@1594 188 }
strarup@1594 189
strarup@1594 190 // Check expected names
strarup@1594 191 if (expect != null) {
strarup@1594 192 if (pname.matches(expect)) continue;
strarup@1594 193 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 194 "' expected '" + expect + "'");
strarup@1594 195 break;
strarup@1594 196 }
strarup@1594 197
strarup@1594 198 // Test naming convention for explicit parameters.
strarup@1594 199 boolean fidelity = !isAnon;
strarup@1594 200 if (param != null && fidelity) {
strarup@1594 201 char ch = param.charAt(0);
strarup@1594 202 expect = (++ch) + param;
strarup@1594 203 }
strarup@1594 204
strarup@1594 205 if (pname != null && fidelity) {
strarup@1594 206 param = pname;
strarup@1594 207 }
strarup@1594 208
strarup@1594 209 if (expect != null && !expect.equals(pname)) {
strarup@1594 210 error(prefix + "param[" + i + "]='" + pname +
strarup@1594 211 "' expected '" + expect + "'");
strarup@1594 212 break;
strarup@1594 213 }
strarup@1594 214 }
strarup@1594 215 if (c.isSynthetic()) {
strarup@1594 216 sb.append(")!!\n");
strarup@1594 217 } else {
strarup@1594 218 sb.append(")\n");
strarup@1594 219 }
strarup@1594 220 }
strarup@1594 221
strarup@1594 222 void testMethod(Method m) {
strarup@1594 223
strarup@1594 224 String prefix = clazz.getName() + "." + m.getName() + "() - ";
strarup@1594 225
strarup@1594 226 // Parameters must match parameter types
strarup@1594 227 int paramTypes = m.getParameterTypes().length;
strarup@1594 228 int params = m.getParameters().length;
strarup@1594 229 if (paramTypes != params) {
strarup@1594 230 error(prefix + "number of parameter types (" + paramTypes
strarup@1594 231 + ") != number of parameters (" + params + ")");
strarup@1594 232 return;
strarup@1594 233 }
strarup@1594 234
strarup@1594 235 sb.append(clazz.getName()).append(".").append(m.getName()).append("(");
strarup@1594 236 String sep = "";
strarup@1594 237 String param = null;
strarup@1594 238 int i = -1;
strarup@1594 239 // For methods we expect all parameters to follow
strarup@1594 240 // the test-case design pattern, except synthetic methods.
strarup@1594 241 for (Parameter p : m.getParameters()) {
strarup@1594 242 i++;
strarup@1594 243 if (param == null) {
strarup@1594 244 param = p.getName();
strarup@1594 245 sb.append(sep).append(param);
strarup@1594 246 } else {
strarup@1594 247 char c = param.charAt(0);
strarup@1594 248 String expect = m.isSynthetic() ? ("arg" + i) : ((++c) + param);
strarup@1594 249 param = p.getName();
strarup@1594 250 sb.append(sep).append(param);
vromero@1792 251 if (!m.isBridge() && !expect.equals(param)) {
strarup@1594 252 error(prefix + "param[" + i + "]='"
strarup@1594 253 + param + "' expected '" + expect + "'");
strarup@1594 254 break;
strarup@1594 255 }
strarup@1594 256 }
strarup@1594 257 sep = ", ";
strarup@1594 258 }
strarup@1594 259 if (m.isSynthetic()) {
strarup@1594 260 sb.append(")!!\n");
strarup@1594 261 } else {
strarup@1594 262 sb.append(")\n");
strarup@1594 263 }
strarup@1594 264 }
strarup@1594 265 }

mercurial