diff --git a/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java b/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java index 1b90504e783f4e..8c0a9896885bd7 100644 --- a/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java +++ b/src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps/DepsCheckerClassVisitor.java @@ -337,10 +337,29 @@ public void visitFieldInsn(int opcode, String owner, String name, String desc) { @Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { - checkMember(owner, name, desc); + if (!isMethodHandle(opcode, owner, name)) { + checkMember(owner, name, desc); + } super.visitMethodInsn(opcode, owner, name, desc, itf); } + private boolean isMethodHandle(int opcode, String owner, String name) { + if (opcode != Opcodes.INVOKEVIRTUAL) { + return false; + } + if (!owner.equals("java/lang/invoke/MethodHandle")) { + return false; + } + switch (name) { + case "invoke": + case "invokeExact": + break; + default: + return false; + } + return true; + } + @Override public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { checkDescriptor(desc); diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD index 930ca6699eeb16..6ef72da3f669c4 100644 --- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/BUILD @@ -300,6 +300,32 @@ sh_test( ], ) +sh_test( + name = "invoke_polymorphic_test", + srcs = ["golden_test.sh"], + args = [ + "$(location golden_invoke_polymorphic_test.txt)", + "$(location golden_invoke_polymorphic_test.stderr.txt)", + "199", + "$(location :DumpProto)", + "$(location //src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:ImportDepsChecker)", + "--checking_mode=error", + "--bootclasspath_entry", + "$(location @local_jdk//:jre/lib/rt.jar)", + "--input", + "$(location //src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata:libInvokePolymorphic.jar)", + ], + data = [ + ":DumpProto", + ":libempty.jar", + "//src/java_tools/import_deps_checker/java/com/google/devtools/build/importdeps:ImportDepsChecker", + "//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps:golden_invoke_polymorphic_test.stderr.txt", + "//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps:golden_invoke_polymorphic_test.txt", + "//src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata:libInvokePolymorphic.jar", + "@local_jdk//:jre/lib/rt.jar", + ], +) + java_binary( name = "DumpProto", srcs = ["DumpProto.java"], diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/golden_invoke_polymorphic_test.stderr.txt b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/golden_invoke_polymorphic_test.stderr.txt new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/golden_invoke_polymorphic_test.txt b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/golden_invoke_polymorphic_test.txt new file mode 100644 index 00000000000000..0765b24706f8c4 --- /dev/null +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/golden_invoke_polymorphic_test.txt @@ -0,0 +1,2 @@ +rule_label: "" +success: true diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/BUILD b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/BUILD index f13786e526d721..237b5f0432b8b5 100644 --- a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/BUILD +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/BUILD @@ -69,3 +69,8 @@ java_library( name = "StringAnnotation", srcs = ["StringAnnotation.java"], ) + +java_library( + name = "InvokePolymorphic", + srcs = ["InvokePolymorphic.java"], +) diff --git a/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/InvokePolymorphic.java b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/InvokePolymorphic.java new file mode 100644 index 00000000000000..14665ae64a87b6 --- /dev/null +++ b/src/java_tools/import_deps_checker/javatests/com/google/devtools/build/importdeps/testdata/InvokePolymorphic.java @@ -0,0 +1,23 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.importdeps.testdata; + +import java.lang.invoke.MethodHandle; + +public class InvokePolymorphic { + void f(MethodHandle mh) throws Throwable { + mh.invoke(true, "hello", 42); + } +}