Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,14 @@ public J visitLambda(J.Lambda lambda, ExecutionContext ctx) {
if (isLambdaInGenericAndOverloadedContext()) {
return l;
}
J.MemberReference updated = newStaticMethodReference(methodType, true, lambda.getType()).withPrefix(lambda.getPrefix());
JavaType.FullyQualified containingType = methodType.getDeclaringType();
if (!methodType.hasFlags(Flag.Static) && select != null) {
JavaType.FullyQualified selectType = TypeUtils.asFullyQualified(select.getType());
if (selectType != null) {
containingType = selectType;
}
}
J.MemberReference updated = newInstanceMethodReference(className(containingType, true), methodType, lambda.getType()).withPrefix(lambda.getPrefix());
doAfterVisit(service(ImportService.class).shortenFullyQualifiedTypeReferencesIn(updated));
return updated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,62 @@ record R(String s) {}
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/906")
@Test
void useReceiverTypeWhenDeclaringTypeIsPackagePrivate() {
rewriteRun(
//language=java
java(
"""
package com.helloworld.internal;

import java.util.Optional;

abstract class Base {
public Optional<String> getValue() {
return Optional.empty();
}
}
"""
),
//language=java
java(
"""
package com.helloworld.internal;

public class Child extends Base {}
"""
),
//language=java
java(
"""
package com.helloworld;

import com.helloworld.internal.Child;
import java.util.Optional;

public class Main {
String get(final Optional<Child> opt) {
return opt.flatMap(s -> s.getValue()).orElse("");
}
}
""",
"""
package com.helloworld;

import com.helloworld.internal.Child;
import java.util.Optional;

public class Main {
String get(final Optional<Child> opt) {
return opt.flatMap(Child::getValue).orElse("");
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/20")
@Test
void castToTypeParameterInLambda() {
Expand Down
Loading