Skip to content

Commit 169ed63

Browse files
authored
Merge branch 'master' into joey/aws-sns
2 parents 7c44dfc + 08f3098 commit 169ed63

File tree

12 files changed

+354
-108
lines changed

12 files changed

+354
-108
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/git/GitConfig.java

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package datadog.trace.civisibility.git;
22

3+
import de.thetaphi.forbiddenapis.SuppressForbidden;
34
import java.io.BufferedReader;
45
import java.io.FileReader;
56
import java.io.IOException;
67
import java.util.HashMap;
78
import java.util.Map;
8-
import java.util.regex.Matcher;
9-
import java.util.regex.Pattern;
109

1110
/**
1211
* Represents a .git/config file. It uses a simple algorithm based on regex to parse line by line
1312
* the .git/config file (INI file format).
1413
*/
1514
public class GitConfig {
1615

17-
private final Pattern section = Pattern.compile("\\s*\\[([^]]*)\\]\\s*");
18-
private final Pattern keyValue = Pattern.compile("\\s*([^=]*)=(.*)");
1916
private final Map<String, Map<String, String>> entries = new HashMap<>();
2017

2118
public GitConfig(final String path) {
2219
load(path);
2320
}
2421

22+
@SuppressForbidden // split with one-char String use a fast-path without regex usage
2523
private void load(final String path) {
2624
if (path == null || path.isEmpty()) {
2725
return;
@@ -36,26 +34,33 @@ private void load(final String path) {
3634
String line;
3735
String section = null;
3836
while ((line = br.readLine()) != null) {
37+
3938
// Check if current line matches with the `section` regex:
40-
Matcher m = this.section.matcher(line);
41-
if (m.matches()) {
42-
// Section found: (E.g: remote "origin")
43-
section = m.group(1).trim();
44-
} else if (section != null) {
39+
int sectionStartIdx = line.indexOf('[');
40+
if (sectionStartIdx >= 0) {
41+
int sectionEndIdx = line.indexOf(']', sectionStartIdx + 1);
42+
if (sectionEndIdx >= 0
43+
&& isWhitespace(line, 0, sectionStartIdx)
44+
&& isWhitespace(line, sectionEndIdx + 1, line.length())) {
45+
// Section found: (E.g: remote "origin")
46+
section = line.substring(sectionStartIdx + 1, sectionEndIdx);
47+
continue;
48+
}
49+
}
50+
51+
if (section != null) {
4552
// Locate the concrete `section` in the `entries` map
4653
// and update it with the found key/value.
4754
// E.g: Map({`remote "origin"`: {`url`:`https://some-host/user/repository.git`}}
48-
Map<String, String> kv = this.entries.get(section);
49-
if (kv == null) {
50-
this.entries.put(section, kv = new HashMap<>());
51-
}
52-
// Check if current line is a key/value inside of a certain section.
53-
m = this.keyValue.matcher(line);
54-
if (m.matches()) {
55+
Map<String, String> sectionValues =
56+
this.entries.computeIfAbsent(section, k -> new HashMap<>());
57+
58+
String[] parts = line.split("=");
59+
if (parts.length >= 2) {
5560
// Key/value found: (E.g: key=url, value=https://some-host/user/repository.git)
56-
final String key = m.group(1).trim();
57-
final String value = m.group(2).trim();
58-
kv.put(key, value);
61+
final String key = parts[0].trim();
62+
final String value = join(parts, 1, parts.length).trim();
63+
sectionValues.put(key, value);
5964
}
6065
}
6166
}
@@ -67,6 +72,27 @@ private void load(final String path) {
6772
}
6873
}
6974

75+
private String join(String[] array, int from, int to) {
76+
if (to - from == 1) {
77+
return array[from];
78+
} else {
79+
StringBuilder joined = new StringBuilder();
80+
for (int i = from; i < to; i++) {
81+
joined.append(array[i]);
82+
}
83+
return joined.toString();
84+
}
85+
}
86+
87+
private boolean isWhitespace(String s, int from, int to) {
88+
for (int i = from; i < to; i++) {
89+
if (!Character.isWhitespace(s.charAt(i))) {
90+
return false;
91+
}
92+
}
93+
return true;
94+
}
95+
7096
public String getString(final String section, final String key) {
7197
final Map<String, String> kv = entries.get(section);
7298
if (kv == null) {

dd-java-agent/agent-debugger/build.gradle

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,11 @@ jar {
7575

7676
tasks.withType(Test).configureEach {
7777
onlyIf { !project.rootProject.hasProperty("skipDebuggerTests") }
78-
// DebuggerTransformerTest made some Reflective calls on java.lang package
79-
// needs to open it since jdk16
80-
def matcher = it.name =~ /testJava(\d+)Generated/
81-
if (matcher) {
82-
def javaVersion = matcher.group(1) as int
83-
if (javaVersion >= 16) {
84-
jvmArgs '--add-opens', 'java.base/java.lang=ALL-UNNAMED'
85-
}
86-
}
8778
}
8879

80+
// we want to test with no special reflective access (no --add-opens)
81+
ext.allowReflectiveAccessToJdk = false
82+
8983
subprojects { Project subProj ->
9084
subProj.tasks.withType(Test).configureEach { subTask ->
9185
onlyIf { !project.rootProject.hasProperty("skipDebuggerTests") }

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/CapturedContext.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,18 @@ public Object getMember(Object target, String memberName) {
130130
}
131131
}
132132
} else {
133-
Function<Object, CapturedValue> specialFieldAccess =
134-
WellKnownClasses.getSpecialFieldAccess(target.getClass().getTypeName());
135-
if (specialFieldAccess != null) {
136-
CapturedValue specialField = specialFieldAccess.apply(target);
137-
if (specialField != null && specialField.getName().equals(memberName)) {
138-
return specialField.getValue();
139-
} else {
140-
target = Values.UNDEFINED_OBJECT;
133+
Map<String, Function<Object, CapturedValue>> specialTypeAccess =
134+
WellKnownClasses.getSpecialTypeAccess(target);
135+
if (specialTypeAccess != null) {
136+
Function<Object, CapturedValue> specialFieldAccess = specialTypeAccess.get(memberName);
137+
if (specialFieldAccess != null) {
138+
CapturedValue specialField = specialFieldAccess.apply(target);
139+
if (specialField != null && specialField.getName().equals(memberName)) {
140+
return specialField.getValue();
141+
}
141142
}
142-
} else {
143-
target = ReflectiveFieldValueResolver.resolve(target, target.getClass(), memberName);
144143
}
144+
target = ReflectiveFieldValueResolver.resolve(target, target.getClass(), memberName);
145145
}
146146
checkUndefined(target, memberName, "Cannot dereference to field: ");
147147
return target;

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/el/ReflectiveFieldValueResolver.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,15 @@ private static Field safeGetField(Class<?> container, String name) {
165165

166166
private static Field getField(Class<?> container, String name) {
167167
while (container != null) {
168-
try {
169-
Field fld = container.getDeclaredField(name);
170-
fld.setAccessible(true);
171-
return fld;
172-
} catch (NoSuchFieldException ignored) {
173-
container = container.getSuperclass();
168+
Field[] declaredFields = container.getDeclaredFields();
169+
for (int i = 0; i < declaredFields.length; i++) {
170+
Field declaredField = declaredFields[i];
171+
if (declaredField.getName().equals(name)) {
172+
declaredField.setAccessible(true);
173+
return declaredField;
174+
}
174175
}
176+
container = container.getSuperclass();
175177
}
176178
return null;
177179
}

0 commit comments

Comments
 (0)