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

Polish #34

Merged
merged 1 commit into from
Mar 5, 2023
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 @@ -7,20 +7,19 @@
import lombok.Getter;

import com.navercorp.scavenger.util.HashGenerator;
import com.navercorp.scavenger.util.Signatures;

public class MethodRegistry {
public static final String SYNTHETIC_SIGNATURE_HASH = "";

@Getter
private final Map<String, String> byteBuddySignatureToHash = new ConcurrentHashMap<>();

private static final Pattern SYNTHETIC_SIGNATURE_PATTERN = Pattern.compile(".*\\$\\$(Enhancer|FastClass)BySpringCGLIB\\$\\$.*");

public String getHash(String byteBuddySignature, boolean legacyCompatibilityMode) {
String hash = this.byteBuddySignatureToHash.get(byteBuddySignature);

if (hash == null) {
if (SYNTHETIC_SIGNATURE_PATTERN.matcher(byteBuddySignature).matches()) {
if (Signatures.containsSyntheticPattern(byteBuddySignature)) {
hash = SYNTHETIC_SIGNATURE_HASH;
} else {
String signature = extractSignature(byteBuddySignature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CustomerService(

fun getCustomerByName(groupId: String, customerName: String): CustomerDto {
return customerRepository.findByNameAndGroupId(customerName, groupId)
.orElseThrow { throw EmptyResultDataAccessException(1) }
.orElseThrow { EmptyResultDataAccessException(1) }
.let {
CustomerDto.from(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import com.navercorp.scavenger.dto.CommonImportResultDto
import com.navercorp.scavenger.dto.InvocationImportDto
import com.navercorp.scavenger.exception.UnknownPublicationException
import com.navercorp.scavenger.util.HashGenerator
import com.navercorp.scavenger.util.Signatures
import io.codekvast.javaagent.model.v4.CodeBasePublication4
import io.codekvast.javaagent.model.v4.CommonPublicationData4
import io.codekvast.javaagent.model.v4.InvocationDataPublication4
import java.io.InputStream
import java.io.ObjectInputStream
import java.util.regex.Pattern

sealed interface Publication {
fun getCommonImportDto(customerId: Long): CommonImportDto
Expand Down Expand Up @@ -107,7 +107,7 @@ sealed class LegacyPublication private constructor(val commonData: CommonPublica
override fun getCodeBaseImportDto(commonImportResultDto: CommonImportResultDto): CodeBaseImportDto =
with(commonImportResultDto) {
val entries = pub.entries
.filterNot { syntheticSignaturePattern.matcher(it.signature).matches() }
.filterNot { Signatures.containsSyntheticPattern(it.signature) }
.map {
CodeBaseImportDto.CodeBaseEntry(
declaringType = it.methodSignature.declaringType,
Expand Down Expand Up @@ -140,17 +140,13 @@ sealed class LegacyPublication private constructor(val commonData: CommonPublica
applicationId = applicationId,
environmentId = environmentId,
invocations = pub.invocations
.filterNot { syntheticSignaturePattern.matcher(it).matches() }
.filterNot { Signatures.containsSyntheticPattern(it) }
.map { HashGenerator.Md5.from(it) }
.sorted(),
invokedAtMillis = pub.recordingIntervalStartedAtMillis,
)
}
}

companion object {
val syntheticSignaturePattern: Pattern = Pattern.compile(".*\\.\\.(Enhancer|FastClass)BySpringCGLIB\\.\\..*")
}
}

fun InputStream.toLegacyPublication(): LegacyPublication {
Expand Down
4 changes: 4 additions & 0 deletions scavenger-model/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ dependencies {
implementation("io.grpc:grpc-kotlin-stub:${property("grpcKotlinVersion")}")
implementation("io.grpc:grpc-protobuf:${property("grpcVersion")}")
implementation("javax.annotation:javax.annotation-api:1.3.2")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
testImplementation("org.assertj:assertj-core:3.22.0")
}

tasks.withType<JavaCompile> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.navercorp.scavenger.util;

import java.util.regex.Pattern;

public final class Signatures {

private static final Pattern SYNTHETIC_SIGNATURE_PATTERN = Pattern.compile(".*\\$\\$(Enhancer|FastClass)BySpringCGLIB\\$\\$.*");

private Signatures() {
// utility class
}

public static boolean containsSyntheticPattern(String signature) {
return SYNTHETIC_SIGNATURE_PATTERN.matcher(signature).matches();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.navercorp.scavenger.util;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class SignaturesTest {

@Test
void returnsTrueIfSignatureContainsEnhancerBySpringCGLIB() {
// given
String signature = "TestClass$$EnhancerBySpringCGLIB$$hash.testMethod()";

// when
boolean actual = Signatures.containsSyntheticPattern(signature);

// then
assertThat(actual).isTrue();
}

@Test
void returnsTrueIfSignatureContainsFastClassBySpringCGLIB() {
// given
String signature = "TestClass$$FastClassBySpringCGLIB$$hash.testMethod()";

// when
boolean actual = Signatures.containsSyntheticPattern(signature);

// then
assertThat(actual).isTrue();
}

@Test
void returnsFalseIfDoesNotContainsPattern() {
// given
String signature = "TestClass$hash.testMethod()";

// when
boolean actual = Signatures.containsSyntheticPattern(signature);

// then
assertThat(actual).isFalse();
}
}