annotate graal/com.oracle.jvmci.options.processor/src/com/oracle/jvmci/options/processor/OptionsVerifier.java @ 21709:3c17c0c41a6b

moved com.oracle.asm.**.test to JVMCI namespace (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Wed, 03 Jun 2015 18:33:23 +0200
parents 47bebae7454f
children 5b9adb645217
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16030
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
1 /*
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
4 *
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
8 *
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
13 * accompanied this code).
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
14 *
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
18 *
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
21 * questions.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
22 */
21562
Doug Simon <doug.simon@oracle.com>
parents: 21554
diff changeset
23 package com.oracle.jvmci.options.processor;
16030
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
24
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
25 import static java.lang.String.*;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
26
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
27 import java.io.*;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
28 import java.lang.reflect.*;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
29 import java.util.*;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
30
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
31 import jdk.internal.org.objectweb.asm.*;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
32 import jdk.internal.org.objectweb.asm.Type;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
33
21554
b1530a6cce8c renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents: 18163
diff changeset
34 import com.oracle.jvmci.options.*;
16030
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
35
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
36 /**
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
37 * A {@link ClassVisitor} that verifies a class declaring one or more {@linkplain OptionValue
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
38 * options} has a class initializer that only initializes the option(s). This sanity check mitigates
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
39 * the possibility of an option value being used before the code that sets the value (e.g., from the
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
40 * command line) has been executed.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
41 */
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
42 final class OptionsVerifier extends ClassVisitor {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
43
16623
addc0564e5b5 split com.oracle.graal.truffle.* projects into a separate graal-truffle.jar and added truffle.jar to the boot class path
Doug Simon <doug.simon@oracle.com>
parents: 16030
diff changeset
44 public static void checkClass(Class<?> cls, OptionDescriptor option, Set<Class<?>> checked, GraalJars graalJars) throws IOException {
16030
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
45 if (!checked.contains(cls)) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
46 checked.add(cls);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
47 Class<?> superclass = cls.getSuperclass();
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
48 if (superclass != null && !superclass.equals(Object.class)) {
16623
addc0564e5b5 split com.oracle.graal.truffle.* projects into a separate graal-truffle.jar and added truffle.jar to the boot class path
Doug Simon <doug.simon@oracle.com>
parents: 16030
diff changeset
49 checkClass(superclass, option, checked, graalJars);
16030
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
50 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
51
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
52 String classFilePath = cls.getName().replace('.', '/') + ".class";
16623
addc0564e5b5 split com.oracle.graal.truffle.* projects into a separate graal-truffle.jar and added truffle.jar to the boot class path
Doug Simon <doug.simon@oracle.com>
parents: 16030
diff changeset
53 ClassReader cr = new ClassReader(Objects.requireNonNull(graalJars.getInputStream(classFilePath), "Could not find class file for " + cls.getName()));
16030
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
54
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
55 ClassVisitor cv = new OptionsVerifier(cls, option);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
56 cr.accept(cv, 0);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
57 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
58 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
59
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
60 /**
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
61 * The option field context of the verification.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
62 */
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
63 private final OptionDescriptor option;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
64
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
65 /**
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
66 * The class in which {@link #option} is declared or a super-class of that class. This is the
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
67 * class whose {@code <clinit>} method is being verified.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
68 */
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
69 private final Class<?> cls;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
70
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
71 /**
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
72 * Source file context for error reporting.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
73 */
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
74 String sourceFile = null;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
75
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
76 /**
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
77 * Line number for error reporting.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
78 */
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
79 int lineNo = -1;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
80
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
81 final Class<?>[] boxingTypes = {Boolean.class, Byte.class, Short.class, Character.class, Integer.class, Float.class, Long.class, Double.class};
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
82
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
83 private static Class<?> resolve(String name) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
84 try {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
85 return Class.forName(name.replace('/', '.'));
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
86 } catch (ClassNotFoundException e) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
87 throw new InternalError(e);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
88 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
89 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
90
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
91 OptionsVerifier(Class<?> cls, OptionDescriptor desc) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
92 super(Opcodes.ASM5);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
93 this.cls = cls;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
94 this.option = desc;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
95 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
96
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
97 @Override
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
98 public void visitSource(String source, String debug) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
99 this.sourceFile = source;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
100 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
101
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
102 void verify(boolean condition, String message) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
103 if (!condition) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
104 error(message);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
105 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
106 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
107
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
108 void error(String message) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
109 String errorMessage = format("%s:%d: Illegal code in %s.<clinit> which may be executed when %s.%s is initialized:%n%n %s%n%n" + "The recommended solution is to move " + option.getName() +
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
110 " into a separate class (e.g., %s.Options).%n", sourceFile, lineNo, cls.getSimpleName(), option.getDeclaringClass().getSimpleName(), option.getName(), message,
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
111 option.getDeclaringClass().getSimpleName());
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
112 throw new InternalError(errorMessage);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
113
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
114 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
115
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
116 @Override
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
117 public MethodVisitor visitMethod(int access, String name, String d, String signature, String[] exceptions) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
118 if (name.equals("<clinit>")) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
119 return new MethodVisitor(Opcodes.ASM5) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
120
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
121 @Override
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
122 public void visitLineNumber(int line, Label start) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
123 lineNo = line;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
124 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
125
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
126 @Override
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
127 public void visitFieldInsn(int opcode, String owner, String fieldName, String fieldDesc) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
128 if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
129 verify(resolve(owner).equals(option.getDeclaringClass()), format("store to field %s.%s", resolve(owner).getSimpleName(), fieldName));
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
130 verify(opcode != Opcodes.PUTFIELD, format("store to non-static field %s.%s", resolve(owner).getSimpleName(), fieldName));
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
131 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
132 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
133
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
134 private Executable resolveMethod(String owner, String methodName, String methodDesc) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
135 Class<?> declaringClass = resolve(owner);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
136 if (methodName.equals("<init>")) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
137 for (Constructor<?> c : declaringClass.getDeclaredConstructors()) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
138 if (methodDesc.equals(Type.getConstructorDescriptor(c))) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
139 return c;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
140 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
141 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
142 } else {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
143 Type[] argumentTypes = Type.getArgumentTypes(methodDesc);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
144 for (Method m : declaringClass.getDeclaredMethods()) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
145 if (m.getName().equals(methodName)) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
146 if (Arrays.equals(argumentTypes, Type.getArgumentTypes(m))) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
147 if (Type.getReturnType(methodDesc).equals(Type.getReturnType(m))) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
148 return m;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
149 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
150 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
151 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
152 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
153 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
154 throw new NoSuchMethodError(declaringClass + "." + methodName + methodDesc);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
155 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
156
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
157 /**
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
158 * Checks whether a given method is allowed to be called.
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
159 */
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
160 private boolean checkInvokeTarget(Executable method) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
161 Class<?> holder = method.getDeclaringClass();
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
162 if (method instanceof Constructor) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
163 if (OptionValue.class.isAssignableFrom(holder)) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
164 return true;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
165 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
166 } else if (Arrays.asList(boxingTypes).contains(holder)) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
167 return method.getName().equals("valueOf");
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
168 } else if (method.getDeclaringClass().equals(Class.class)) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
169 return method.getName().equals("desiredAssertionStatus");
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
170 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
171 return false;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
172 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
173
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
174 @Override
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
175 public void visitMethodInsn(int opcode, String owner, String methodName, String methodDesc, boolean itf) {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
176 Executable callee = resolveMethod(owner, methodName, methodDesc);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
177 verify(checkInvokeTarget(callee), "invocation of " + callee);
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
178 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
179 };
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
180 } else {
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
181 return null;
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
182 }
bddb3eb57e90 moved verification of OptionValue declaring classes from run time to build time
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
183 }
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 16623
diff changeset
184 }