Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter for Records #990

Merged
merged 4 commits into from Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions org.jacoco.core.test.validation.java14/pom.xml
Expand Up @@ -35,4 +35,25 @@
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
Copy link
Member

@marchof marchof Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: Why does surefire require this flag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marchof enablement of Preview Features by --enable-preview flag is required both at compile and at run time - see Preview Language and VM Features (JEP 12), otherwise we'll get

java.lang.UnsupportedClassVersionError: Preview features are not enabled for org/jacoco/core/test/validation/java14/RecordsTest (class file version 58.65535). Try running with '--enable-preview'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks!

</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2009, 2020 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
*
*******************************************************************************/
package org.jacoco.core.test.validation.java14;

import org.jacoco.core.test.validation.ValidationTestBase;
import org.jacoco.core.test.validation.java14.targets.RecordsTarget;

/**
* Test of code coverage for records.
*/
public class RecordsTest extends ValidationTestBase {

public RecordsTest() {
super(RecordsTarget.class);
}

}
@@ -0,0 +1,52 @@
/*******************************************************************************
* Copyright (c) 2009, 2020 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
*
*******************************************************************************/
package org.jacoco.core.test.validation.java14.targets;

/**
* This target exercises records.
*/
public class RecordsTarget {

private record WithoutFields() { // assertFullyCovered()
}

private record WithFields( // assertPartlyCovered()
int x // assertEmpty()
) {
}

private record WithCustomMethods(int x) { // assertFullyCovered()
public int x() {
return x; // assertNotCovered()
}

public String toString() {
return ""; // assertNotCovered()
}

public int hashCode() {
return 0; // assertNotCovered()
}

public boolean equals(Object object) {
return false; // assertNotCovered()
}
}

public static void main(String[] args) {
new WithoutFields();
new WithFields(42);
new WithCustomMethods(42);
}

}
@@ -0,0 +1,175 @@
/*******************************************************************************
* Copyright (c) 2009, 2020 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
*
*******************************************************************************/
package org.jacoco.core.internal.analysis.filter;

import org.jacoco.core.internal.instr.InstrSupport;
import org.junit.Test;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.MethodNode;

/**
* Unit tests for {@link RecordsFilter}.
*/
public class RecordsFilterTest extends FilterTestBase {

private final RecordsFilter filter = new RecordsFilter();

@Test
public void should_filter_generated_toString_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"toString", "()Ljava/lang/String;", null, null);
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitInvokeDynamicInsn("toString", "(LPoint;)Ljava/lang/String;",
new Handle(Opcodes.H_INVOKESTATIC,
"java/lang/runtime/ObjectMethods", "bootstrap",
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;",
false));
m.visitInsn(Opcodes.ARETURN);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

@Test
public void should_not_filter_custom_toString_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"toString", "()Ljava/lang/String;", null, null);
m.visitLdcInsn("");
m.visitInsn(Opcodes.ARETURN);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_not_filter_non_toString_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"toString", "()V", null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_filter_generated_hashCode_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"hashCode", "()I", null, null);
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitInvokeDynamicInsn("hashCode", "(LPoint;)I", new Handle(
Opcodes.H_INVOKESTATIC, "java/lang/runtime/ObjectMethods",
"bootstrap",
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;",
false));
m.visitInsn(Opcodes.IRETURN);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

@Test
public void should_not_filter_custom_hashCode_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"hashCode", "()I", null, null);
m.visitInsn(Opcodes.ICONST_0);
m.visitInsn(Opcodes.IRETURN);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_not_filter_non_hashCode_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"hashCode", "()V", null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_filter_generated_equals_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"equals", "(Ljava/lang/Object;)Z", null, null);
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitVarInsn(Opcodes.ALOAD, 1);
m.visitInvokeDynamicInsn("equals", "(LPoint;Ljava/lang/Object;)Z",
new Handle(Opcodes.H_INVOKESTATIC,
"java/lang/runtime/ObjectMethods", "bootstrap",
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;",
false));
m.visitInsn(Opcodes.IRETURN);

filter.filter(m, context, output);

assertMethodIgnored(m);
}

@Test
public void should_not_filter_custom_equals_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"equals", "(Ljava/lang/Object;)Z", null, null);
m.visitInsn(Opcodes.ICONST_0);
m.visitInsn(Opcodes.IRETURN);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_not_filter_non_equals_method() {
context.superClassName = "java/lang/Record";
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"equals", "()V", null, null);
m.visitInsn(Opcodes.NOP);

filter.filter(m, context, output);

assertIgnored();
}

@Test
public void should_not_filter_non_records() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"toString", "()Ljava/lang/String;", null, null);
m.visitVarInsn(Opcodes.ALOAD, 0);
m.visitInvokeDynamicInsn("toString", "(LPoint;)Ljava/lang/String;",
new Handle(Opcodes.H_INVOKESTATIC,
"java/lang/runtime/ObjectMethods", "bootstrap",
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/TypeDescriptor;Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/invoke/MethodHandle;)Ljava/lang/Object;",
false));
m.visitInsn(Opcodes.ARETURN);

filter.filter(m, context, output);

assertIgnored();
}

}
Expand Up @@ -38,7 +38,7 @@ public static IFilter all() {
new TryWithResourcesEcjFilter(), new FinallyFilter(),
new PrivateEmptyNoArgConstructorFilter(),
new StringSwitchJavacFilter(), new StringSwitchEcjFilter(),
new EnumEmptyConstructorFilter(),
new EnumEmptyConstructorFilter(), new RecordsFilter(),
new AnnotationGeneratedFilter(), new KotlinGeneratedFilter(),
new KotlinLateinitFilter(), new KotlinWhenFilter(),
new KotlinWhenStringFilter(),
Expand Down
@@ -0,0 +1,89 @@
/*******************************************************************************
* Copyright (c) 2009, 2020 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
*
*******************************************************************************/
package org.jacoco.core.internal.analysis.filter;

import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.MethodNode;

/**
* Filters methods <code>toString</code>, <code>hashCode</code> and
* <code>equals</code> that compiler generates for records.
*/
public final class RecordsFilter implements IFilter {

public void filter(final MethodNode methodNode,
final IFilterContext context, final IFilterOutput output) {
if (!"java/lang/Record".equals(context.getSuperClassName())) {
return;
}
final Matcher matcher = new Matcher();
if (matcher.isEquals(methodNode) || matcher.isHashCode(methodNode)
|| matcher.isToString(methodNode)) {
output.ignore(methodNode.instructions.getFirst(),
methodNode.instructions.getLast());
}
}

private static class Matcher extends AbstractMatcher {
boolean isToString(final MethodNode m) {
if (!"toString".equals(m.name)
|| !"()Ljava/lang/String;".equals(m.desc)) {
return false;
}
firstIsALoad0(m);
nextIsInvokeDynamic("toString");
nextIs(Opcodes.ARETURN);
return cursor != null;
}

boolean isHashCode(final MethodNode m) {
if (!"hashCode".equals(m.name) || !"()I".equals(m.desc)) {
return false;
}
firstIsALoad0(m);
nextIsInvokeDynamic("hashCode");
nextIs(Opcodes.IRETURN);
return cursor != null;
}

boolean isEquals(final MethodNode m) {
if (!"equals".equals(m.name)
|| !"(Ljava/lang/Object;)Z".equals(m.desc)) {
return false;
}
firstIsALoad0(m);
nextIs(Opcodes.ALOAD);
nextIsInvokeDynamic("equals");
nextIs(Opcodes.IRETURN);
return cursor != null;
}

private void nextIsInvokeDynamic(final String name) {
nextIs(Opcodes.INVOKEDYNAMIC);
if (cursor == null) {
return;
}
final InvokeDynamicInsnNode i = (InvokeDynamicInsnNode) cursor;
final Handle bsm = i.bsm;
if (name.equals(i.name)
&& "java/lang/runtime/ObjectMethods".equals(bsm.getOwner())
&& "bootstrap".equals(bsm.getName())) {
return;
}
cursor = null;
}
}

}
7 changes: 7 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -20,6 +20,13 @@ <h1>Change History</h1>

<h2>Snapshot Build @qualified.bundle.version@ (@build.date@)</h2>

<h3>New Features</h3>
<ul>
<li>Methods <code>toString</code>, <code>hashCode</code> and <code>equals</code>
generated by compiler for records are filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/990">#990</a>).</li>
</ul>

<h3>Non-functional Changes</h3>
<ul>
<li>Support for Pack200 was removed in JDK 14. JaCoCo will now throw a detailed
Expand Down