Skip to content

Commit 0e0c2ea

Browse files
committed
Use default value when accessing un-initialized Property
Accessing an uninitialized Property is illegal, resulting in errors or warnings when CCUDGP is applied with no value set for 'server'. This is fixed by providing a default value to use in the case of uninitialized Property. Fixes gradle/common-custom-user-data-gradle-plugin#378
1 parent 40efdaa commit 0e0c2ea

File tree

6 files changed

+13
-7
lines changed

6 files changed

+13
-7
lines changed

Diff for: develocity-gradle-plugin-adapters/src/compatibilityApi/java/com/gradle/develocity/agent/gradle/adapters/internal/ReflectionProperty.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ public static <T> ReflectionProperty<T> forGetterAndSetter(Object obj, String ge
5757
}
5858

5959
public static <T> ReflectionProperty<T> forProperty(Object obj, String propertyName) {
60+
return forProperty(obj, propertyName, null);
61+
}
62+
63+
public static <T> ReflectionProperty<T> forProperty(Object obj, String propertyName, T defaultValue) {
6064
return new ReflectionProperty<>(
6165
() -> {
6266
Property<T> prop = (Property<T>) invokeMethod(obj, propertyName);
63-
return prop.get();
67+
return prop.getOrElse(defaultValue);
6468
},
6569
value -> {
6670
Property<T> prop = (Property<T>) invokeMethod(obj, propertyName);

Diff for: develocity-gradle-plugin-adapters/src/develocityCompatibility/java/com/gradle/develocity/agent/gradle/adapters/develocity/BuildScanCaptureConfigurationAdapter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public static BuildScanCaptureAdapter forBuildScanExtension(Object buildScan) {
3737
@VisibleForTesting
3838
static @NotNull ReflectingBuildScanCaptureAdapter forCaptureConfiguration(Object capture) {
3939
return new ReflectingBuildScanCaptureAdapter(
40-
ReflectionProperty.forProperty(capture, "getFileFingerprints"),
41-
ReflectionProperty.forProperty(capture, "getBuildLogging"),
42-
ReflectionProperty.forProperty(capture, "getTestLogging")
40+
ReflectionProperty.forProperty(capture, "getFileFingerprints", true),
41+
ReflectionProperty.forProperty(capture, "getBuildLogging", true),
42+
ReflectionProperty.forProperty(capture, "getTestLogging", true)
4343
);
4444
}
4545
}

Diff for: develocity-gradle-plugin-adapters/src/develocityCompatibility/java/com/gradle/develocity/agent/gradle/adapters/develocity/BuildScanConfigurationAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected ReflectionProperty<String> getTermsOfUseAgreeProperty() {
5454

5555
@Override
5656
protected ReflectionProperty<Boolean> getUploadInBackgroundProperty() {
57-
return ReflectionProperty.forProperty(buildScanExtension, "getUploadInBackground");
57+
return ReflectionProperty.forProperty(buildScanExtension, "getUploadInBackground", true);
5858
}
5959

6060
@Nullable

Diff for: develocity-gradle-plugin-adapters/src/develocityCompatibility/java/com/gradle/develocity/agent/gradle/adapters/develocity/DevelocityConfigurationAdapter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected ReflectionProperty<String> getProjectIdProperty() {
5555

5656
@Override
5757
protected ReflectionProperty<Boolean> getAllowUntrustedServerProperty() {
58-
return ReflectionProperty.forProperty(extension, "getAllowUntrustedServer");
58+
return ReflectionProperty.forProperty(extension, "getAllowUntrustedServer", true);
5959
}
6060

6161
@Override

Diff for: develocity-gradle-plugin-adapters/src/develocityCompatibilityTest/java/com/gradle/develocity/agent/gradle/adapters/develocity/BuildScanConfigurationAdapterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void testUploadInBackground() {
137137
adapter.isUploadInBackground();
138138

139139
// then
140-
verify(configuration.getUploadInBackground()).get();
140+
verify(configuration.getUploadInBackground()).getOrElse(true);
141141
}
142142

143143
@Test

Diff for: develocity-gradle-plugin-adapters/src/testFixtures/java/com/gradle/develocity/agent/gradle/adapters/PropertyMockFixtures.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.gradle.api.provider.Property;
44

5+
import static org.mockito.ArgumentMatchers.any;
56
import static org.mockito.Mockito.lenient;
67
import static org.mockito.Mockito.mock;
78

@@ -20,6 +21,7 @@ public static <T> Property<T> mockPropertyReturning(T value) {
2021
Property<T> prop = (Property<T>) mock(Property.class);
2122
lenient().when(prop.get()).thenReturn(value);
2223
lenient().when(prop.getOrNull()).thenReturn(value);
24+
lenient().when(prop.getOrElse(any())).thenReturn(value);
2325
return prop;
2426
}
2527

0 commit comments

Comments
 (0)