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

Move isSynthetic up to MemberData and implement it for AsmFieldData #3

Merged
merged 4 commits into from
Nov 9, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions hypo-asm/hypo-asm-test-data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ tasks.compileScenario01Java {
tasks.compileScenario02Java {
options.release.set(16)
}

tasks.compileScenario03Java {
options.release.set(16)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scenario03;

import java.util.function.IntSupplier;

// Compiled with JDK 16
public class TestClass {

public IntSupplier intSupplier(Inner inner) {
return () -> inner.notSynthetic;
}

@SuppressWarnings("InnerClassMayBeStatic")
private class Inner {
private int notSynthetic;
}
}
5 changes: 5 additions & 0 deletions hypo-asm/src/main/java/dev/denwav/hypo/asm/AsmFieldData.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public boolean isFinal() {
return (this.node.access & Opcodes.ACC_FINAL) != 0;
}

@Override
public boolean isSynthetic() {
return (this.node.access & Opcodes.ACC_SYNTHETIC) != 0;
}

@Override
public @NotNull String name() {
return this.node.name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Hypo, an extensible and pluggable Java bytecode analytical model.
*
* Copyright (C) 2021 Kyle Wood (DenWav)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, version 3 of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.denwav.hypo.asm.scenarios;

import dev.denwav.hypo.model.data.FieldData;
import dev.denwav.hypo.model.data.MethodData;
import dev.denwav.hypo.model.data.MethodDescriptor;
import dev.denwav.hypo.model.data.types.ClassType;
import dev.denwav.hypo.model.data.types.PrimitiveType;
import dev.denwav.hypo.test.framework.TestScenarioBase;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayName("[asm] Scenario 03 - Synthetic members (Java 16)")
public class Scenario03 extends TestScenarioBase {

@Override
public @NotNull Env env() {
return () -> "scenario-03";
}

@Test
@DisplayName("Test expected synthetic members are synthetic")
public void testSyntheticMembers() throws Exception {
final var testClass = this.context().getProvider().findClass("scenario03/TestClass");
assertNotNull(testClass);
final var inner = this.context().getProvider().findClass("scenario03/TestClass$Inner");
assertNotNull(inner);

final MethodData intSupplierSynth = testClass.method("lambda$intSupplier$0", MethodDescriptor.parseDescriptor("(Lscenario03/TestClass$Inner;)I"));
assertNotNull(intSupplierSynth, "Did not find expected lambda$intSupplier$0 synthetic member in TestClass");
assertTrue(intSupplierSynth.isSynthetic());
jpenilla marked this conversation as resolved.
Show resolved Hide resolved

final FieldData innerSyntheticOuterThisField = inner.field("this$0", new ClassType("scenario03.TestClass"));
assertNotNull(innerSyntheticOuterThisField, "Did not find expected this$0 synthetic member in TestClass$Inner");
assertTrue(innerSyntheticOuterThisField.isSynthetic());
jpenilla marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("Test declared members are not synthetic")
public void testNonSyntheticMembers() throws Exception {
final var testClass = this.context().getProvider().findClass("scenario03/TestClass");
assertNotNull(testClass);
final var inner = this.context().getProvider().findClass("scenario03/TestClass$Inner");
assertNotNull(inner);

final MethodData intSupplier = testClass.method("intSupplier", MethodDescriptor.parseDescriptor("(Lscenario03/TestClass$Inner;)Ljava/util/function/IntSupplier;"));
assertNotNull(intSupplier, "Did not find expected method intSupplier in TestClass");
assertFalse(intSupplier.isSynthetic());
jpenilla marked this conversation as resolved.
Show resolved Hide resolved

final FieldData innerDeclaredField = inner.field("notSynthetic", PrimitiveType.INT);
assertNotNull(innerDeclaredField, "Did not find expected field notSynthetic in TestClass$Inner");
assertFalse(innerDeclaredField.isSynthetic());
jpenilla marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ public interface MemberData extends HypoData {
* @return {@code true} if this member data is a {@code final} member.
*/
boolean isFinal();

/**
* Returns {@code true} if this member data is synthetic.
*
* @return {@code true} if this member data is a synthetic member.
*/
boolean isSynthetic();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public interface MethodData extends MemberData {
*/
boolean isAbstract();

/**
* Returns {@code true} if this method is synthetic.
* @return {@code true} if this method is synthetic.
*/
boolean isSynthetic();

/**
* Returns {@code true} if this is a bridge method.
* @return {@code true} if this is a bridge method.
Expand Down