From 2c34ddf4c9e7d7c04635a1fd21770ac6fc5178aa Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 18 Dec 2019 22:42:45 +0100 Subject: [PATCH] [java] fixing not-null instrumentation of static interface methods (IDEA-229070) GitOrigin-RevId: d419d4a1fcec36a86b1979a2feee50cb1fa70faf --- .../AuxiliaryMethodGenerator.java | 25 ++++--------------- .../InterfaceStaticMethodParameter.java | 11 ++++++++ .../NotNullVerifyingInstrumenterTest.java | 7 ++++++ 3 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 java/java-tests/testData/compiler/notNullVerification/InterfaceStaticMethodParameter.java diff --git a/java/compiler/instrumentation-util/src/com/intellij/compiler/notNullVerification/AuxiliaryMethodGenerator.java b/java/compiler/instrumentation-util/src/com/intellij/compiler/notNullVerification/AuxiliaryMethodGenerator.java index 7f05c787864c..ed017752b8f2 100644 --- a/java/compiler/instrumentation-util/src/com/intellij/compiler/notNullVerification/AuxiliaryMethodGenerator.java +++ b/java/compiler/instrumentation-util/src/com/intellij/compiler/notNullVerification/AuxiliaryMethodGenerator.java @@ -1,24 +1,7 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.compiler.notNullVerification; -import org.jetbrains.org.objectweb.asm.ClassReader; -import org.jetbrains.org.objectweb.asm.ClassVisitor; -import org.jetbrains.org.objectweb.asm.Label; -import org.jetbrains.org.objectweb.asm.MethodVisitor; +import org.jetbrains.org.objectweb.asm.*; import java.util.*; @@ -35,12 +18,14 @@ class AuxiliaryMethodGenerator { private static final String REPORTING_METHOD_DESC = "(I)V"; private final ClassReader myOriginalClass; + private final boolean myIsInterface; private final List myReportingPlaces = new ArrayList(); private String myReportingMethod; private int myMaxArgCount; AuxiliaryMethodGenerator(ClassReader originalClass) { myOriginalClass = originalClass; + myIsInterface = (myOriginalClass.getAccess() & ACC_INTERFACE) == ACC_INTERFACE; } private String getReportingMethodName() { @@ -79,7 +64,7 @@ class AuxiliaryMethodGenerator { myReportingPlaces.add(new ReportingPlace(exceptionClass, descrPattern, args)); pushIntConstant(mv, index); - mv.visitMethodInsn(INVOKESTATIC, className, getReportingMethodName(), REPORTING_METHOD_DESC, false); + mv.visitMethodInsn(INVOKESTATIC, className, getReportingMethodName(), REPORTING_METHOD_DESC, myIsInterface); } private static void pushIntConstant(MethodVisitor mv, int i) { diff --git a/java/java-tests/testData/compiler/notNullVerification/InterfaceStaticMethodParameter.java b/java/java-tests/testData/compiler/notNullVerification/InterfaceStaticMethodParameter.java new file mode 100644 index 000000000000..98aa965c16a9 --- /dev/null +++ b/java/java-tests/testData/compiler/notNullVerification/InterfaceStaticMethodParameter.java @@ -0,0 +1,11 @@ +import org.jetbrains.annotations.NotNull; + +public class InterfaceStaticMethodParameter { + public static void test() { + I.test(null); + } + + interface I { + static void test(@NotNull String s) { } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/compiler/notNullVerification/NotNullVerifyingInstrumenterTest.java b/java/java-tests/testSrc/com/intellij/java/compiler/notNullVerification/NotNullVerifyingInstrumenterTest.java index 601e5f83feec..21c8e140cc36 100644 --- a/java/java-tests/testSrc/com/intellij/java/compiler/notNullVerification/NotNullVerifyingInstrumenterTest.java +++ b/java/java-tests/testSrc/com/intellij/java/compiler/notNullVerification/NotNullVerifyingInstrumenterTest.java @@ -380,6 +380,13 @@ public abstract class NotNullVerifyingInstrumenterTest { verifyNotInstrumented(); } + @Test + public void testInterfaceStaticMethodParameter() throws Exception { + Class testClass = prepareTest(); + Method method = testClass.getMethod("test"); + verifyCallThrowsException("Argument 0 for @NotNull parameter of InterfaceStaticMethodParameter$I.test must not be null", null, method); + } + protected static void verifyCallThrowsException(String expectedError, @Nullable Object instance, Member member, Object... args) throws Exception { String exceptionText = null; try {