Skip to content

Commit 8be7991

Browse files
newtorka-dbot-sdk-js
authored
Lombok use of val (#104)
* Applied on core * Update sdk_specific_pmd_rules.xml * Fix merge conflicts * Formatting --------- Co-authored-by: Alexander Dümont <[email protected]> Co-authored-by: SAP Cloud SDK Bot <[email protected]>
1 parent 61a5fcd commit 8be7991

14 files changed

+145
-135
lines changed

.pipeline/sdk_specific_pmd_rules.xml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@
278278
<rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass" />
279279
<rule ref="category/java/codestyle.xml/FormalParameterNamingConventions" />
280280
<rule ref="category/java/codestyle.xml/IdenticalCatchBranches" />
281-
<rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal" />
282281
<rule ref="category/java/codestyle.xml/LocalVariableNamingConventions" />
283282
<rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal" />
284283
<rule ref="category/java/codestyle.xml/MethodNamingConventions" />
@@ -373,6 +372,39 @@
373372
</example>
374373
</rule>
375374

375+
<!-- Custom rule for LocalVariableCouldBeFinal -->
376+
<!-- <rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal" /> -->
377+
<rule name="LocalVariableShouldBeFinal" language="java"
378+
message="Check for final variable declarations."
379+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
380+
<description>
381+
Mark constant variable declarations as final.
382+
</description>
383+
<priority>2</priority>
384+
<properties>
385+
<property name="xpath">
386+
<value>
387+
<![CDATA[
388+
//LocalVariableDeclaration[
389+
@Final=false() and
390+
not(parent::ForInit) and
391+
not(./ClassType[@SimpleName="val"]) and
392+
not(./VariableDeclarator[@Name = ancestor::Block//AssignmentExpression/VariableAccess/@Name])
393+
]
394+
]]>
395+
</value>
396+
</property>
397+
</properties>
398+
<example>
399+
<![CDATA[
400+
final int x = 5; // good
401+
final var x = 5; // good
402+
val x = 5; // good
403+
var x = 5; // bad
404+
]]>
405+
</example>
406+
</rule>
407+
376408
<rule ref="category/java/errorprone.xml/CloseResource" />
377409
<rule ref="category/java/bestpractices.xml/UseTryWithResources" />
378410
</ruleset>

core/src/main/java/com/sap/ai/sdk/core/AiCoreDestination.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
44
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
55
import javax.annotation.Nonnull;
6+
import lombok.val;
67

78
/** Container for an API client and destination. */
89
@FunctionalInterface
@@ -22,7 +23,7 @@ public interface AiCoreDestination {
2223
*/
2324
@Nonnull
2425
default ApiClient client() {
25-
final var destination = destination();
26+
val destination = destination();
2627
return new ApiClient(destination);
2728
}
2829
}

core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.annotation.Nonnull;
2222
import lombok.RequiredArgsConstructor;
2323
import lombok.extern.slf4j.Slf4j;
24+
import lombok.val;
2425
import org.springframework.http.client.BufferingClientHttpRequestFactory;
2526
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
2627
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
@@ -55,15 +56,15 @@ public AiCoreService() {
5556
@Nonnull
5657
@Override
5758
public ApiClient client() {
58-
final var destination = destination();
59+
val destination = destination();
5960
return clientHandler.apply(this, destination);
6061
}
6162

6263
@Nonnull
6364
@Override
6465
public Destination destination() {
65-
final var dest = baseDestinationHandler.apply(this);
66-
final var builder = builderHandler.apply(this, dest);
66+
val dest = baseDestinationHandler.apply(this);
67+
val builder = builderHandler.apply(this, dest);
6768
if (!deploymentId.isEmpty()) {
6869
destinationSetUrl(builder, dest);
6970
destinationSetHeaders(builder);
@@ -159,7 +160,7 @@ public AiCoreDeployment forDeploymentByScenario(@Nonnull final String scenarioId
159160
@Nonnull
160161
protected Destination getBaseDestination()
161162
throws DestinationAccessException, DestinationNotFoundException {
162-
final var serviceKey = System.getenv("AICORE_SERVICE_KEY");
163+
val serviceKey = System.getenv("AICORE_SERVICE_KEY");
163164
return DestinationResolver.getDestination(serviceKey);
164165
}
165166

@@ -172,7 +173,7 @@ protected Destination getBaseDestination()
172173
@Nonnull
173174
protected DefaultHttpDestination.Builder getDestinationBuilder(
174175
@Nonnull final Destination destination) {
175-
final var builder = DefaultHttpDestination.fromDestination(destination);
176+
val builder = DefaultHttpDestination.fromDestination(destination);
176177
String uri = destination.get(DestinationProperty.URI).get();
177178
if (!uri.endsWith("/")) {
178179
uri = uri + "/";
@@ -191,18 +192,18 @@ protected DefaultHttpDestination.Builder getDestinationBuilder(
191192
@SuppressWarnings("UnstableApiUsage")
192193
@Nonnull
193194
protected ApiClient getApiClient(@Nonnull final Destination destination) {
194-
final var objectMapper =
195+
val objectMapper =
195196
new Jackson2ObjectMapperBuilder()
196197
.modules(new JavaTimeModule())
197198
.visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
198199
.visibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
199200
.serializationInclusion(JsonInclude.Include.NON_NULL) // THIS STOPS `null` serialization
200201
.build();
201202

202-
final var httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
203+
val httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
203204
httpRequestFactory.setHttpClient(ApacheHttpClient5Accessor.getHttpClient(destination));
204205

205-
final var rt = new RestTemplate();
206+
val rt = new RestTemplate();
206207
Iterables.filter(rt.getMessageConverters(), MappingJackson2HttpMessageConverter.class)
207208
.forEach(converter -> converter.setObjectMapper(objectMapper));
208209
rt.setRequestFactory(new BufferingClientHttpRequestFactory(httpRequestFactory));

core/src/main/java/com/sap/ai/sdk/core/DeploymentCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.concurrent.ConcurrentHashMap;
1414
import javax.annotation.Nonnull;
1515
import lombok.extern.slf4j.Slf4j;
16+
import lombok.val;
1617

1718
/**
1819
* Cache for deployment IDs. This class is used to get the deployment id for the orchestration
@@ -35,7 +36,7 @@ class DeploymentCache {
3536
void resetCache(@Nonnull final ApiClient client, @Nonnull final String resourceGroup) {
3637
cache.remove(resourceGroup);
3738
try {
38-
final var deployments =
39+
val deployments =
3940
new HashSet<>(new DeploymentApi(client).query(resourceGroup).getResources());
4041
cache.put(resourceGroup, deployments);
4142
} catch (final OpenApiRequestException e) {
@@ -124,13 +125,13 @@ private Optional<String> getDeploymentIdByScenario(
124125
*/
125126
protected static boolean isDeploymentOfModel(
126127
@Nonnull final AiModel targetModel, @Nonnull final AiDeployment deployment) {
127-
final var deploymentDetails = deployment.getDetails();
128+
val deploymentDetails = deployment.getDetails();
128129
// The AI Core specification doesn't mention that this is nullable, but it can be.
129130
// Remove this check when the specification is fixed.
130131
if (deploymentDetails == null) {
131132
return false;
132133
}
133-
final var resources = deploymentDetails.getResources();
134+
val resources = deploymentDetails.getResources();
134135
if (resources == null) {
135136
return false;
136137
}

core/src/main/java/com/sap/ai/sdk/core/DestinationResolver.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import lombok.AccessLevel;
2424
import lombok.Getter;
2525
import lombok.extern.slf4j.Slf4j;
26+
import lombok.val;
2627

2728
/** Utility class to resolve the destination pointing to the AI Core service. */
2829
@Slf4j
@@ -45,16 +46,16 @@ class DestinationResolver {
4546
@SuppressWarnings("UnstableApiUsage")
4647
static HttpDestination getDestination(@Nullable final String serviceKey) {
4748
final Predicate<Object> aiCore = Optional.of(ServiceIdentifier.AI_CORE)::equals;
48-
final var serviceBindings = accessor.getServiceBindings();
49-
final var aiCoreBinding = tryFind(serviceBindings, b -> aiCore.test(b.getServiceIdentifier()));
49+
val serviceBindings = accessor.getServiceBindings();
50+
val aiCoreBinding = tryFind(serviceBindings, b -> aiCore.test(b.getServiceIdentifier()));
5051

51-
final var serviceKeyPresent = serviceKey != null;
52+
val serviceKeyPresent = serviceKey != null;
5253
if (!aiCoreBinding.isPresent() && serviceKeyPresent) {
5354
addServiceBinding(serviceKey);
5455
}
5556

5657
// get a destination pointing to the AI Core service
57-
final var opts =
58+
val opts =
5859
(aiCoreBinding.isPresent()
5960
? forService(aiCoreBinding.get())
6061
: forService(ServiceIdentifier.AI_CORE))
@@ -85,12 +86,12 @@ private static void addServiceBinding(@Nonnull final String serviceKey) {
8586
"Error in parsing service key from the \"AICORE_SERVICE_KEY\" environment variable.", e);
8687
}
8788

88-
final var binding =
89+
val binding =
8990
new DefaultServiceBindingBuilder()
9091
.withServiceIdentifier(ServiceIdentifier.AI_CORE)
9192
.withCredentials(credentials)
9293
.build();
93-
final var newAccessor =
94+
val newAccessor =
9495
new ServiceBindingMerger(
9596
List.of(accessor, () -> List.of(binding)), ServiceBindingMerger.KEEP_EVERYTHING);
9697
DefaultServiceBindingAccessor.setInstance(newAccessor);

core/src/test/java/com/sap/ai/sdk/core/AiCoreServiceTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import javax.annotation.Nonnull;
24+
import lombok.val;
2425
import org.junit.jupiter.api.AfterEach;
2526
import org.junit.jupiter.api.Test;
2627

@@ -45,7 +46,7 @@ void tearDown() {
4546
@Test
4647
void testLazyEvaluation() {
4748
// setup
48-
final var accessor = mock(ServiceBindingAccessor.class);
49+
val accessor = mock(ServiceBindingAccessor.class);
4950
DestinationResolver.setAccessor(accessor);
5051

5152
// execution without errors
@@ -58,14 +59,14 @@ void testLazyEvaluation() {
5859
@Test
5960
void testDefaultCase() {
6061
// setup
61-
final var accessor = mock(ServiceBindingAccessor.class);
62+
val accessor = mock(ServiceBindingAccessor.class);
6263
DestinationResolver.setAccessor(accessor);
6364
doReturn(List.of(BINDING)).when(accessor).getServiceBindings();
6465

6566
// execution without errors
66-
final var core = new AiCoreService();
67-
final var destination = core.destination();
68-
final var client = core.client();
67+
val core = new AiCoreService();
68+
val destination = core.destination();
69+
val client = core.client();
6970

7071
// verification
7172
assertThat(destination.get(DestinationProperty.URI)).contains("https://srv/v2/");
@@ -82,10 +83,10 @@ void testBaseDestination() {
8283
DestinationResolver.setAccessor(Collections::emptyList);
8384

8485
// execution without errors
85-
final var customDestination = DefaultHttpDestination.builder("https://foo.bar").build();
86-
final var core = new AiCoreService().withDestination(customDestination);
87-
final var destination = core.destination();
88-
final var client = core.client();
86+
val customDestination = DefaultHttpDestination.builder("https://foo.bar").build();
87+
val core = new AiCoreService().withDestination(customDestination);
88+
val destination = core.destination();
89+
val client = core.client();
8990

9091
// verification
9192
assertThat(destination.get(DestinationProperty.URI)).contains("https://foo.bar/v2/");
@@ -97,7 +98,7 @@ void testBaseDestination() {
9798

9899
@Test
99100
void testCustomization() {
100-
final var customService =
101+
val customService =
101102
new AiCoreService() {
102103
@Nonnull
103104
@Override
@@ -112,16 +113,16 @@ protected ApiClient getApiClient(@Nonnull Destination destination) {
112113
}
113114
};
114115

115-
final var customServiceForDeployment =
116+
val customServiceForDeployment =
116117
customService.forDeployment("deployment").withResourceGroup("group");
117118

118-
final var client = customServiceForDeployment.client();
119+
val client = customServiceForDeployment.client();
119120
assertThat(client.getBasePath()).isEqualTo("https://fizz.buzz");
120121

121-
final var destination = customServiceForDeployment.destination().asHttp();
122+
val destination = customServiceForDeployment.destination().asHttp();
122123
assertThat(destination.getUri()).hasToString("https://ai/v2/inference/deployments/deployment/");
123124

124-
final var resourceGroup = customService.resourceGroup;
125+
val resourceGroup = customService.resourceGroup;
125126
assertThat(resourceGroup).isEqualTo("group");
126127
}
127128
}

core/src/test/java/com/sap/ai/sdk/core/CacheTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Map;
1919
import java.util.NoSuchElementException;
2020
import javax.annotation.Nonnull;
21+
import lombok.val;
2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324

@@ -61,10 +62,10 @@ private static void stubEmpty(String resourceGroup) {
6162
*/
6263
@Test
6364
void newDeployment() {
64-
String resourceGroup = "default";
65+
val resourceGroup = "default";
6566
stubGPT4(resourceGroup);
6667

67-
final AiModel gpt4 = createAiModel("gpt-4-32k", null);
68+
val gpt4 = createAiModel("gpt-4-32k", null);
6869

6970
cacheUnderTest.getDeploymentIdByModel(client, resourceGroup, gpt4);
7071
wireMockServer.verify(1, getRequestedFor(urlPathEqualTo("/v2/lm/deployments")));
@@ -84,12 +85,12 @@ void newDeployment() {
8485
*/
8586
@Test
8687
void newDeploymentAfterReset() {
87-
String resourceGroup = "default";
88+
val resourceGroup = "default";
8889
stubEmpty(resourceGroup);
8990
cacheUnderTest.resetCache(client, resourceGroup);
9091
stubGPT4(resourceGroup);
9192

92-
final AiModel gpt4 = createAiModel("gpt-4-32k", null);
93+
val gpt4 = createAiModel("gpt-4-32k", null);
9394

9495
cacheUnderTest.getDeploymentIdByModel(client, resourceGroup, gpt4);
9596
// 1 reset empty and 1 cache miss
@@ -101,12 +102,12 @@ void newDeploymentAfterReset() {
101102

102103
@Test
103104
void resourceGroupIsolation() {
104-
String resourceGroupA = "A";
105-
String resourceGroupB = "B";
105+
val resourceGroupA = "A";
106+
val resourceGroupB = "B";
106107
stubGPT4(resourceGroupA);
107108
stubGPT4(resourceGroupB);
108109

109-
final AiModel gpt4 = createAiModel("gpt-4-32k", null);
110+
val gpt4 = createAiModel("gpt-4-32k", null);
110111

111112
cacheUnderTest.getDeploymentIdByModel(client, resourceGroupA, gpt4);
112113
wireMockServer.verify(
@@ -121,10 +122,10 @@ void resourceGroupIsolation() {
121122

122123
@Test
123124
void exceptionDeploymentNotFound() {
124-
String resourceGroup = "default";
125+
val resourceGroup = "default";
125126
stubEmpty(resourceGroup);
126127

127-
final AiModel gpt4 = createAiModel("gpt-4-32k", null);
128+
val gpt4 = createAiModel("gpt-4-32k", null);
128129

129130
assertThatThrownBy(() -> cacheUnderTest.getDeploymentIdByModel(client, resourceGroup, gpt4))
130131
.isExactlyInstanceOf(NoSuchElementException.class)
@@ -133,26 +134,26 @@ void exceptionDeploymentNotFound() {
133134

134135
@Test
135136
void resetCache() {
136-
String resourceGroup = "default";
137+
val resourceGroup = "default";
137138
stubGPT4(resourceGroup);
138139
cacheUnderTest.resetCache(client, resourceGroup);
139140
wireMockServer.verify(1, getRequestedFor(urlPathEqualTo("/v2/lm/deployments")));
140141

141-
final var destination = DefaultHttpDestination.builder(wireMockServer.baseUrl()).build();
142+
val destination = DefaultHttpDestination.builder(wireMockServer.baseUrl()).build();
142143
new AiCoreService().withDestination(destination).reloadCachedDeployments(resourceGroup);
143144
wireMockServer.verify(2, getRequestedFor(urlPathEqualTo("/v2/lm/deployments")));
144145
}
145146

146147
@Test
147148
public void isDeploymentOfModel() {
148149
// Create a target model
149-
final AiModel gpt4AnyVersion = createAiModel("gpt-4-32k", null);
150-
final AiModel gpt4Version1 = createAiModel("gpt-4-32k", "1.0");
151-
final AiModel gpt4VersionLatest = createAiModel("gpt-4-32k", "latest");
150+
val gpt4AnyVersion = createAiModel("gpt-4-32k", null);
151+
val gpt4Version1 = createAiModel("gpt-4-32k", "1.0");
152+
val gpt4VersionLatest = createAiModel("gpt-4-32k", "latest");
152153

153154
// Create a deployment with a different model by version
154-
final var model = Map.of("model", Map.of("name", "gpt-4-32k", "version", "latest"));
155-
final var deployment =
155+
val model = Map.of("model", Map.of("name", "gpt-4-32k", "version", "latest"));
156+
val deployment =
156157
AiDeployment.create()
157158
.id("test-deployment")
158159
.configurationId("test-configuration")

0 commit comments

Comments
 (0)