Skip to content
Open
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 @@ -73,6 +73,9 @@ TypeString parameterTypeString(int paramIndex) {
return null; // don't create type-strings for primitive/array types
}
int end = boundaries[paramIndex] - 1;
if (paramIndex == boundaries.length - 1) {
end--; // move back past ')' to get the end of the last param type
}
return new TypeString(descriptor, start, end, getHash(paramIndex, start, end));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String toString() {
static boolean startsWith(CharSequence cs, String prefix) {
if (cs instanceof TypeString) {
TypeString ts = (TypeString) cs;
return ts.descriptor.startsWith(prefix, ts.offset);
return prefix.length() <= ts.len && ts.descriptor.startsWith(prefix, ts.offset);
} else {
return cs.toString().startsWith(prefix);
}
Expand All @@ -106,7 +106,8 @@ static boolean startsWith(CharSequence cs, String prefix) {
static boolean endsWith(CharSequence cs, String suffix) {
if (cs instanceof TypeString) {
TypeString ts = (TypeString) cs;
return ts.descriptor.startsWith(suffix, (ts.offset + ts.len) - suffix.length());
return suffix.length() <= ts.len
&& ts.descriptor.startsWith(suffix, (ts.offset + ts.len) - suffix.length());
} else {
return cs.toString().endsWith(suffix);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package datadog.instrument.classmatch;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class MethodOutlineTest {
@ParameterizedTest
@ValueSource(strings = {"(LA;)V", "(ILA;)V", "(LA;LA;)V"})
void lastObjectParameterExcludesDescriptorDelimiters(String descriptor) {
MethodOutline method = new MethodOutline(0, "example", descriptor, new String[0]);
int index = method.parameterCount() - 1;

TypeString type = method.parameterTypeString(index);
assertEquals("A", type.toString());
assertEquals("A".hashCode(), type.hashCode());
assertTrue(MethodMatcher.method().parameter(index, TypeMatcher.type("A")).test(method));
}

@ParameterizedTest
@ValueSource(strings = {"(LA;I)V", "(LA;LA;)V"})
void earlierObjectParameterExcludesDescriptorDelimiters(String descriptor) {
MethodOutline method = new MethodOutline(0, "example", descriptor, new String[0]);

assertEquals("A", method.parameterTypeString(0).toString());
assertTrue(MethodMatcher.method().parameter(0, TypeMatcher.type("A")).test(method));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package datadog.instrument.classmatch;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

class TypeStringTest {
@ParameterizedTest
@MethodSource("typeStrings")
void prefixAndSuffixMatchingStayWithinTypeString(TypeString type) {
String name = type.toString();
for (String candidate : new String[] {"", "A", "L", "LA", "A;", "A;I", "()LA", "Longer"}) {
assertEquals(
name.startsWith(candidate),
TypeMatcher.typeStartsWith(candidate).test(type),
"prefix: " + candidate);
assertEquals(
name.endsWith(candidate),
TypeMatcher.typeEndsWith(candidate).test(type),
"suffix: " + candidate);
}
}

static Stream<TypeString> typeStrings() {
return Stream.of(
new FieldOutline(0, "example", "LA;").typeString(),
new MethodOutline(0, "example", "(LA;I)V", new String[0]).parameterTypeString(0),
new MethodOutline(0, "example", "()LA;", new String[0]).returnTypeString());
}
}
Loading