diff options
Diffstat (limited to 'org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilter.java')
-rw-r--r-- | org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilter.java | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilter.java index c298e945..82522a8a 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilter.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/KotlinUnsafeCastOperatorFilter.java @@ -1,9 +1,10 @@ /******************************************************************************* - * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 * * Contributors: * Evgeny Mandrikov - initial API and implementation @@ -23,27 +24,29 @@ import org.objectweb.asm.tree.MethodNode; */ public final class KotlinUnsafeCastOperatorFilter implements IFilter { - private static final String KOTLIN_TYPE_CAST_EXCEPTION = "kotlin/TypeCastException"; - public void filter(final MethodNode methodNode, final IFilterContext context, final IFilterOutput output) { + if (!KotlinGeneratedFilter.isKotlinClass(context)) { + return; + } final Matcher matcher = new Matcher(); - for (AbstractInsnNode i = methodNode.instructions - .getFirst(); i != null; i = i.getNext()) { - matcher.match(i, output); + for (final AbstractInsnNode i : methodNode.instructions) { + matcher.match("kotlin/TypeCastException", i, output); + // Since Kotlin 1.4.0: + matcher.match("java/lang/NullPointerException", i, output); } } private static class Matcher extends AbstractMatcher { - public void match(final AbstractInsnNode start, - final IFilterOutput output) { + public void match(final String exceptionType, + final AbstractInsnNode start, final IFilterOutput output) { if (Opcodes.IFNONNULL != start.getOpcode()) { return; } cursor = start; - - nextIsType(Opcodes.NEW, KOTLIN_TYPE_CAST_EXCEPTION); + final JumpInsnNode jumpInsnNode = (JumpInsnNode) cursor; + nextIsType(Opcodes.NEW, exceptionType); nextIs(Opcodes.DUP); nextIs(Opcodes.LDC); if (cursor == null) { @@ -54,13 +57,13 @@ public final class KotlinUnsafeCastOperatorFilter implements IFilter { .startsWith("null cannot be cast to non-null type"))) { return; } - nextIsInvoke(Opcodes.INVOKESPECIAL, KOTLIN_TYPE_CAST_EXCEPTION, - "<init>", "(Ljava/lang/String;)V"); + nextIsInvoke(Opcodes.INVOKESPECIAL, exceptionType, "<init>", + "(Ljava/lang/String;)V"); nextIs(Opcodes.ATHROW); if (cursor == null) { return; } - if (cursor.getNext() != ((JumpInsnNode) start).label) { + if (cursor.getNext() != jumpInsnNode.label) { return; } |