Skip to content

Commit

Permalink
Version 2.1.0, provide safer copy mappings down option
Browse files Browse the repository at this point in the history
  • Loading branch information
DenWav committed Jul 30, 2023
1 parent 061473c commit 1bc3801
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
group = dev.denwav.hypo
version = 2.0.1-SNAPSHOT
version = 2.1.0

org.gradle.parallel=true
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
// Compiled with JDK 17
public class TestClass {

private long num = 0;

public void test() {
final String s3 = Integer.toString(new Random().nextInt());
String s33 = Integer.toString(new Random().nextInt());
StringBuilder sb = new StringBuilder();
String str = sb.toString();
final Runnable r = () -> {
final String s4 = s3 + s3;
final String s4 = s3 + s3 + this.num;
final String s44 = s33 + s33;
str.length();
final Runnable r1 = () -> {
Expand All @@ -25,4 +27,11 @@ public void test() {
public static Object thing(String in) {
return null;
}

public static void testStatic() {
final String s = Integer.toString(new Random().nextInt());
final Runnable r = () -> {
System.out.println(s);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public void testLambdaCalls() throws Exception {
assertNotNull(methodClosure);
final MethodData call = methodClosure.getClosure();
assertNotNull(call);
assertArrayEquals(new int[] { 1, 2, 4 }, methodClosure.getParamLvtIndices());
assertArrayEquals(new int[] { 0, 1, 2, 4 }, methodClosure.getParamLvtIndices());

assertEquals(testClass, call.parentClass());
assertTrue(call.isSynthetic());
assertTrue(call.isStatic());
assertFalse(call.isStatic());

final List<MethodClosure<MethodData>> nestedCalls = call.get(HypoHydration.LAMBDA_CALLS);
assertNotNull(nestedCalls);
Expand Down Expand Up @@ -112,4 +112,28 @@ public void testLambdaCalls() throws Exception {
.orElse(null);
assertNotNull(outerNestedMethodClosure);
}

@Test
@DisplayName("Test lambda call hydrator on statics")
public void testStaticLambdaCalls() throws Exception {
final var testClass = this.context().getProvider().findClass("scenario05/TestClass");
assertNotNull(testClass);

final MethodData testMethod = testClass.method("testStatic", parseDescriptor("()V"));
assertNotNull(testMethod);

final List<MethodClosure<MethodData>> methodClosures = testMethod.get(HypoHydration.LAMBDA_CALLS);
assertNotNull(methodClosures);
assertEquals(1, methodClosures.size());

final MethodClosure<MethodData> methodClosure = methodClosures.get(0);
assertNotNull(methodClosure);
final MethodData call = methodClosure.getClosure();
assertNotNull(call);
assertArrayEquals(new int[] { 0 }, methodClosure.getParamLvtIndices());

assertEquals(testClass, call.parentClass());
assertTrue(call.isSynthetic());
assertTrue(call.isStatic());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,31 @@
*/
public class CopyMappingsDown implements ChangeContributor {

private CopyMappingsDown() {}
private final boolean stopOnChildMapping;

private CopyMappingsDown(final boolean stopOnChildMapping) {
this.stopOnChildMapping = stopOnChildMapping;
}

/**
* Create a new instance of {@link CopyMappingsDown}.
* Create a new instance of {@link CopyMappingsDown}. This instance will overwrite mappings any child method has
* with mappings provided by the parent method.
* @return A new instance of {@link CopyMappingsDown}.
*/
@Contract(value = "-> new", pure = true)
public static @NotNull CopyMappingsDown create() {
return new CopyMappingsDown();
return new CopyMappingsDown(false);
}

/**
* Create a new instance of {@link CopyMappingsDown}. This instance will not overwrite mappings on any child method.
* If a child method already contains mappings, that method will become the new "root" and the mappings in that
* child method will be propagated to its children instead.
* @return A new instance of {@link CopyMappingsDown}.
*/
@Contract(value = "-> new", pure = true)
public static @NotNull CopyMappingsDown createWithoutOverwrite() {
return new CopyMappingsDown(true);
}

@Override
Expand All @@ -75,36 +91,43 @@ public void contribute(
}

if (!method.isConstructor()) {
walkDown(method, methodMapping, registry);
this.walkDown(method, methodMapping, registry);
} else {
walkConstructor(method, methodMapping, registry, null);
}
}
}

private static void walkDown(
private void walkDown(
final @NotNull MethodData method,
final @NotNull MethodMapping mapping,
final @NotNull ChangeRegistry registry
) {
for (final MethodData childMethod : method.childMethods()) {
setChangeAndWalkDown(childMethod, mapping, registry);
this.setChangeAndWalkDown(childMethod, mapping, registry);
}

final MethodData syntheticTarget = method.get(HypoHydration.SYNTHETIC_TARGET);
if (syntheticTarget != null) {
setChangeAndWalkDown(syntheticTarget, mapping, registry);
this.setChangeAndWalkDown(syntheticTarget, mapping, registry);
}
}

private static void setChangeAndWalkDown(
private void setChangeAndWalkDown(
final @NotNull MethodData method,
final @NotNull MethodMapping mapping,
final @NotNull ChangeRegistry registry
) {
if (this.stopOnChildMapping) {
final ClassMapping<?, ?> classMapping = getClassMapping(mapping.getMappings(), method.parentClass().name());
final MethodMapping methodMapping = getMethodMapping(classMapping, method.name(), method.descriptorText());
if (methodMapping != null) {
return;
}
}
registry.submitChange(CopyMethodMappingChange.of(MemberReference.of(method), mapping));

walkDown(method, mapping, registry);
this.walkDown(method, mapping, registry);
}

private static void walkConstructor(
Expand Down

0 comments on commit 1bc3801

Please sign in to comment.