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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...o.core.test.validation.java14/src/org/jacoco/core/test/validation/java14/RecordsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...t.validation.java14/src/org/jacoco/core/test/validation/java14/targets/RecordsTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
175 changes: 175 additions & 0 deletions
175
org.jacoco.core.test/src/org/jacoco/core/internal/analysis/filter/RecordsFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/RecordsFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 getThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, thanks!