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

fix bug where origins of synthetic method calls override each other #957

Merged
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 @@ -50,11 +50,12 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.tngtech.archunit.base.Optionals.stream;
import static com.tngtech.archunit.core.importer.JavaClassDescriptorImporter.isLambdaMethodName;
import static com.tngtech.archunit.core.importer.JavaClassDescriptorImporter.isSyntheticAccessMethodName;
import static com.tngtech.archunit.core.importer.JavaClassDescriptorImporter.isSyntheticEnumSwitchMapFieldName;
import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toSet;

class ClassFileImportRecord {
private static final Logger log = LoggerFactory.getLogger(ClassFileImportRecord.class);
Expand Down Expand Up @@ -288,7 +289,7 @@ private <ACCESS extends RawAccessRecord> Stream<ACCESS> fixSyntheticOrigins(

Stream<ACCESS> result = rawAccessRecordsIncludingSyntheticAccesses.stream();
for (SyntheticAccessRecorder syntheticAccessRecorder : syntheticAccessRecorders) {
result = result.flatMap(access -> stream(syntheticAccessRecorder.fixSyntheticAccess(access, createAccessWithNewOrigin)));
result = result.flatMap(access -> syntheticAccessRecorder.fixSyntheticAccess(access, createAccessWithNewOrigin).stream());
}
return result;
}
Expand Down Expand Up @@ -374,7 +375,7 @@ Optional<CodeUnit> getEnclosingCodeUnit(String ownerName) {
}

private static class SyntheticAccessRecorder {
private final Map<String, RawAccessRecord> rawSyntheticMethodInvocationRecordsByTarget = new HashMap<>();
private final SetMultimap<String, RawAccessRecord> rawSyntheticMethodInvocationRecordsByTarget = HashMultimap.create();
private final Predicate<CodeUnit> isSyntheticOrigin;
private final BiConsumer<RawAccessRecord.BaseBuilder<?, ?>, CodeUnit> fixOrigin;

Expand All @@ -390,41 +391,41 @@ void registerSyntheticMethodInvocation(RawAccessRecord record) {
rawSyntheticMethodInvocationRecordsByTarget.put(getMemberKey(record.target), record);
}

<ACCESS extends RawAccessRecord> Optional<ACCESS> fixSyntheticAccess(
<ACCESS extends RawAccessRecord> Set<ACCESS> fixSyntheticAccess(
ACCESS access,
Function<ACCESS, ? extends RawAccessRecord.BaseBuilder<ACCESS, ?>> copyAccess
) {
return isSyntheticOrigin.test(access.caller)
? replaceOriginByFixedOrigin(access, copyAccess)
: Optional.of(access);
: singleton(access);
}

private <ACCESS extends RawAccessRecord> Optional<ACCESS> replaceOriginByFixedOrigin(
private <ACCESS extends RawAccessRecord> Set<ACCESS> replaceOriginByFixedOrigin(
ACCESS accessFromSyntheticMethod,
Function<ACCESS, ? extends RawAccessRecord.BaseBuilder<ACCESS, ?>> copyAccess
) {
RawAccessRecord accessWithCorrectOrigin = findNonSyntheticOriginOf(accessFromSyntheticMethod);

if (accessWithCorrectOrigin != null) {
RawAccessRecord.BaseBuilder<ACCESS, ?> copiedBuilder = copyAccess.apply(accessFromSyntheticMethod);
fixOrigin.accept(copiedBuilder, accessWithCorrectOrigin.caller);
return Optional.of(copiedBuilder.build());
} else {
Set<ACCESS> result = findNonSyntheticOriginOf(accessFromSyntheticMethod)
.map(accessWithCorrectOrigin -> {
RawAccessRecord.BaseBuilder<ACCESS, ?> copiedBuilder = copyAccess.apply(accessFromSyntheticMethod);
fixOrigin.accept(copiedBuilder, accessWithCorrectOrigin.caller);
return copiedBuilder.build();
})
.collect(toSet());

if (result.isEmpty()) {
log.warn("Could not find matching origin for synthetic method {}.{}|{}",
accessFromSyntheticMethod.target.getDeclaringClassName(),
accessFromSyntheticMethod.target.name,
accessFromSyntheticMethod.target.getDescriptor());
return Optional.empty();
}
}

private <ACCESS extends RawAccessRecord> RawAccessRecord findNonSyntheticOriginOf(ACCESS accessFromSyntheticMethod) {
RawAccessRecord result = accessFromSyntheticMethod;
do {
result = rawSyntheticMethodInvocationRecordsByTarget.get(getMemberKey(result.caller));
} while (result != null && isSyntheticOrigin.test(result.caller));

return result;
}

private <ACCESS extends RawAccessRecord> Stream<RawAccessRecord> findNonSyntheticOriginOf(ACCESS access) {
return isSyntheticOrigin.test(access.caller)
? rawSyntheticMethodInvocationRecordsByTarget.get(getMemberKey(access.caller)).stream().flatMap(this::findNonSyntheticOriginOf)
: Stream.of(access);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Supplier;

import com.tngtech.archunit.core.domain.JavaAccess;
import com.tngtech.archunit.core.domain.JavaClass;
import org.junit.Test;

import static com.tngtech.archunit.core.domain.JavaConstructor.CONSTRUCTOR_NAME;
Expand Down Expand Up @@ -334,6 +335,65 @@ Supplier<Runnable> access(Target target) {
);
}

@Test
public void imports_multiple_accesses_to_same_private_field() {
@SuppressWarnings("unused")
class Target {
private String field;
}
@SuppressWarnings("unused")
class Origin {
String first(Target target) {
return target.field;
}

String second(Target target) {
return target.field;
}
}

JavaClass origin = new ClassFileImporter().importClasses(Target.class, Origin.class).get(Origin.class);

assertThatAccesses(origin.getAccessesFromSelf())
.contain(expectedAccess()
.from(Origin.class, "first")
.toField(GET, Target.class, "field")
)
.contain(expectedAccess()
.from(Origin.class, "second")
.toField(GET, Target.class, "field"));
}

@Test
public void imports_multiple_calls_to_same_private_method() {
@SuppressWarnings("unused")
class Target {
private void method() {
}
}
@SuppressWarnings("unused")
class Origin {
void first(Target target) {
target.method();
}

void second(Target target) {
target.method();
}
}

JavaClass origin = new ClassFileImporter().importClasses(Target.class, Origin.class).get(Origin.class);

assertThatAccesses(origin.getAccessesFromSelf())
.contain(expectedAccess()
.from(Origin.class, "first")
.to(Target.class, "method")
)
.contain(expectedAccess()
.from(Origin.class, "second")
.to(Target.class, "method"));
}

private static class Data_of_imports_private_constructor_reference_from_lambda {
@SuppressWarnings("unused")
static class Target {
Expand Down