Skip to content

Commit 0e76f04

Browse files
authored
chore: checkstyle formater & linter (#1316)
* add checkstyle to the project * apply checkstyle and add copyright headers when missing * only check style on JDK 11 (fix jdk 8 build) * notice in the contribution guide
1 parent baa3592 commit 0e76f04

File tree

266 files changed

+6810
-3722
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+6810
-3722
lines changed

CONTRIBUTING.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ To send us a pull request, please:
3131

3232
1. Fork the repository.
3333
2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change.
34-
3. Ensure local tests pass.
35-
4. Commit to your fork using clear commit messages.
36-
5. Send us a pull request, answering any default questions in the pull request interface.
37-
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.
34+
3. Ensure local tests pass: `mvn clean test`
35+
4. Ensure your code is formatted with the provided [checkstyle.xml](https://github.com/aws-powertools/powertools-lambda-java/blob/main/checkstyle.xml): `mvn clean verify`
36+
5. Commit to your fork using clear commit messages.
37+
6. Send us a pull request, answering any default questions in the pull request interface.
38+
7. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.
3839

3940
GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
4041
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).

checkstyle.xml

+427
Large diffs are not rendered by default.

examples/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2023 Amazon.com, Inc. or its affiliates.
4+
~ Licensed under the Apache License, Version 2.0 (the
5+
~ "License"); you may not use this file except in compliance
6+
~ with the License. You may obtain a copy of the License at
7+
~ http://www.apache.org/licenses/LICENSE-2.0
8+
~ Unless required by applicable law or agreed to in writing, software
9+
~ distributed under the License is distributed on an "AS IS" BASIS,
10+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
~ See the License for the specific language governing permissions and
12+
~ limitations under the License.
13+
~
14+
-->
15+
216
<project xmlns="http://maven.apache.org/POM/4.0.0"
317
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
418
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

examples/powertools-examples-cloudformation/infra/cdk/src/main/java/com/myorg/PowertoolsExamplesCloudformationCdkApp.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ public class PowertoolsExamplesCloudformationCdkApp {
77
public static void main(final String[] args) {
88
App app = new App();
99

10-
new PowertoolsExamplesCloudformationCdkStack(app, "PowertoolsExamplesCloudformationCdkStack", StackProps.builder()
11-
.build());
10+
new PowertoolsExamplesCloudformationCdkStack(app, "PowertoolsExamplesCloudformationCdkStack",
11+
StackProps.builder()
12+
.build());
1213

1314
app.synth();
1415
}

examples/powertools-examples-cloudformation/src/main/java/helloworld/App.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public App() {
4141
protected Response create(CloudFormationCustomResourceEvent cloudFormationCustomResourceEvent, Context context) {
4242
// Validate the CloudFormation Custom Resource event
4343
Objects.requireNonNull(cloudFormationCustomResourceEvent, "cloudFormationCustomResourceEvent cannot be null.");
44-
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"), "BucketName cannot be null.");
44+
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"),
45+
"BucketName cannot be null.");
4546

4647
log.info(cloudFormationCustomResourceEvent);
4748
String bucketName = (String) cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName");
@@ -70,7 +71,8 @@ protected Response create(CloudFormationCustomResourceEvent cloudFormationCustom
7071
protected Response update(CloudFormationCustomResourceEvent cloudFormationCustomResourceEvent, Context context) {
7172
// Validate the CloudFormation Custom Resource event
7273
Objects.requireNonNull(cloudFormationCustomResourceEvent, "cloudFormationCustomResourceEvent cannot be null.");
73-
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"), "BucketName cannot be null.");
74+
Objects.requireNonNull(cloudFormationCustomResourceEvent.getResourceProperties().get("BucketName"),
75+
"BucketName cannot be null.");
7476

7577
log.info(cloudFormationCustomResourceEvent);
7678
// Get the physicalResourceId. physicalResourceId is the value returned to CloudFormation in the Create request, and passed in on subsequent requests (e.g. UPDATE or DELETE)
@@ -112,7 +114,8 @@ protected Response update(CloudFormationCustomResourceEvent cloudFormationCustom
112114
protected Response delete(CloudFormationCustomResourceEvent cloudFormationCustomResourceEvent, Context context) {
113115
// Validate the CloudFormation Custom Resource event
114116
Objects.requireNonNull(cloudFormationCustomResourceEvent, "cloudFormationCustomResourceEvent cannot be null.");
115-
Objects.requireNonNull(cloudFormationCustomResourceEvent.getPhysicalResourceId(), "PhysicalResourceId cannot be null.");
117+
Objects.requireNonNull(cloudFormationCustomResourceEvent.getPhysicalResourceId(),
118+
"PhysicalResourceId cannot be null.");
116119

117120
log.info(cloudFormationCustomResourceEvent);
118121
// Get the physicalResourceId. physicalResourceId is the value provided to CloudFormation in the Create request.
@@ -142,7 +145,8 @@ protected Response delete(CloudFormationCustomResourceEvent cloudFormationCustom
142145

143146
private boolean bucketExists(String bucketName) {
144147
try {
145-
HeadBucketResponse headBucketResponse = s3Client.headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
148+
HeadBucketResponse headBucketResponse =
149+
s3Client.headBucket(HeadBucketRequest.builder().bucket(bucketName).build());
146150
if (headBucketResponse.sdkHttpResponse().isSuccessful()) {
147151
return true;
148152
}
@@ -157,7 +161,8 @@ private void createBucket(String bucketName) {
157161
S3Waiter waiter = s3Client.waiter();
158162
CreateBucketRequest createBucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
159163
s3Client.createBucket(createBucketRequest);
160-
WaiterResponse<HeadBucketResponse> waiterResponse = waiter.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build());
164+
WaiterResponse<HeadBucketResponse> waiterResponse =
165+
waiter.waitUntilBucketExists(HeadBucketRequest.builder().bucket(bucketName).build());
161166
waiterResponse.matched().response().ifPresent(log::info);
162167
log.info("Bucket Created {}", bucketName);
163168
}

examples/powertools-examples-core/src/main/java/helloworld/App.java

+43-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,47 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
115
package helloworld;
216

3-
import java.io.BufferedReader;
4-
import java.io.IOException;
5-
import java.io.InputStreamReader;
6-
import java.net.URL;
7-
import java.util.HashMap;
8-
import java.util.Map;
9-
import java.util.stream.Collectors;
17+
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
18+
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
19+
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
20+
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
1021

1122
import com.amazonaws.services.lambda.runtime.Context;
1223
import com.amazonaws.services.lambda.runtime.RequestHandler;
1324
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
1425
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
1526
import com.amazonaws.xray.AWSXRay;
1627
import com.amazonaws.xray.entities.Entity;
28+
import java.io.BufferedReader;
29+
import java.io.IOException;
30+
import java.io.InputStreamReader;
31+
import java.net.URL;
32+
import java.util.HashMap;
33+
import java.util.Map;
34+
import java.util.stream.Collectors;
1735
import org.apache.logging.log4j.LogManager;
1836
import org.apache.logging.log4j.Logger;
1937
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
2038
import software.amazon.cloudwatchlogs.emf.model.Unit;
21-
import software.amazon.lambda.powertools.logging.LoggingUtils;
2239
import software.amazon.lambda.powertools.logging.Logging;
40+
import software.amazon.lambda.powertools.logging.LoggingUtils;
2341
import software.amazon.lambda.powertools.metrics.Metrics;
2442
import software.amazon.lambda.powertools.tracing.CaptureMode;
25-
import software.amazon.lambda.powertools.tracing.TracingUtils;
2643
import software.amazon.lambda.powertools.tracing.Tracing;
27-
28-
import static software.amazon.lambda.powertools.metrics.MetricsUtils.metricsLogger;
29-
import static software.amazon.lambda.powertools.metrics.MetricsUtils.withSingleMetric;
30-
import static software.amazon.lambda.powertools.tracing.TracingUtils.putMetadata;
31-
import static software.amazon.lambda.powertools.tracing.TracingUtils.withEntitySubsegment;
44+
import software.amazon.lambda.powertools.tracing.TracingUtils;
3245

3346
/**
3447
* Handler for requests to Lambda function.
@@ -47,10 +60,11 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
4760

4861
metricsLogger().putMetric("CustomMetric1", 1, Unit.COUNT);
4962

50-
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) -> {
51-
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
52-
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
53-
});
63+
withSingleMetric("CustomMetrics2", 1, Unit.COUNT, "Another", (metric) ->
64+
{
65+
metric.setDimensions(DimensionSet.of("AnotherService", "CustomService"));
66+
metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1"));
67+
});
5468

5569
LoggingUtils.appendKey("test", "willBeLogged");
5670

@@ -62,11 +76,12 @@ public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEv
6276
TracingUtils.putAnnotation("Test", "New");
6377
String output = String.format("{ \"message\": \"hello world\", \"location\": \"%s\" }", pageContents);
6478

65-
TracingUtils.withSubsegment("loggingResponse", subsegment -> {
66-
String sampled = "log something out";
67-
log.info(sampled);
68-
log.info(output);
69-
});
79+
TracingUtils.withSubsegment("loggingResponse", subsegment ->
80+
{
81+
String sampled = "log something out";
82+
log.info(sampled);
83+
log.info(output);
84+
});
7085

7186
threadOption1();
7287

@@ -91,10 +106,11 @@ private void threadOption1() throws InterruptedException {
91106

92107
private void threadOption2() throws InterruptedException {
93108
Entity traceEntity = AWSXRay.getTraceEntity();
94-
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment -> {
95-
String var = "somethingToProcess";
96-
log.info("inside threaded logging inline {}", var);
97-
}));
109+
Thread anotherThread = new Thread(() -> withEntitySubsegment("inlineLog", traceEntity, subsegment ->
110+
{
111+
String var = "somethingToProcess";
112+
log.info("inside threaded logging inline {}", var);
113+
}));
98114
anotherThread.start();
99115
anotherThread.join();
100116
}

examples/powertools-examples-core/src/main/java/helloworld/AppStream.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
115
package helloworld;
216

17+
import com.amazonaws.services.lambda.runtime.Context;
18+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
320
import java.io.IOException;
421
import java.io.InputStream;
522
import java.io.OutputStream;
623
import java.util.Map;
7-
8-
import com.amazonaws.services.lambda.runtime.Context;
9-
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
10-
import com.fasterxml.jackson.databind.ObjectMapper;
1124
import software.amazon.lambda.powertools.logging.Logging;
1225
import software.amazon.lambda.powertools.metrics.Metrics;
1326

Original file line numberDiff line numberDiff line change
@@ -1,43 +1,59 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
115
package helloworld;
216

17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.Assert.assertTrue;
20+
321
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
422
import com.amazonaws.xray.AWSXRay;
523
import org.junit.After;
624
import org.junit.Before;
725
import org.junit.Test;
826

9-
import static org.junit.Assert.*;
10-
1127
public class AppTest {
1228

13-
@Before
14-
public void setup() {
15-
if(null == System.getenv("LAMBDA_TASK_ROOT")) {
16-
AWSXRay.beginSegment("test");
29+
@Before
30+
public void setup() {
31+
if (null == System.getenv("LAMBDA_TASK_ROOT")) {
32+
AWSXRay.beginSegment("test");
33+
}
1734
}
18-
}
1935

20-
@After
21-
public void tearDown() {
22-
if (AWSXRay.getCurrentSubsegmentOptional().isPresent()) {
23-
AWSXRay.endSubsegment();
36+
@After
37+
public void tearDown() {
38+
if (AWSXRay.getCurrentSubsegmentOptional().isPresent()) {
39+
AWSXRay.endSubsegment();
40+
}
41+
42+
if (null == System.getenv("LAMBDA_TASK_ROOT")) {
43+
AWSXRay.endSegment();
44+
}
2445
}
2546

26-
if(null == System.getenv("LAMBDA_TASK_ROOT")) {
27-
AWSXRay.endSegment();
47+
@Test
48+
public void successfulResponse() {
49+
App app = new App();
50+
APIGatewayProxyResponseEvent result = app.handleRequest(null, null);
51+
assertEquals(result.getStatusCode().intValue(), 200);
52+
assertEquals(result.getHeaders().get("Content-Type"), "application/json");
53+
String content = result.getBody();
54+
assertNotNull(content);
55+
assertTrue(content.contains("\"message\""));
56+
assertTrue(content.contains("\"hello world\""));
57+
assertTrue(content.contains("\"location\""));
2858
}
29-
}
30-
31-
@Test
32-
public void successfulResponse() {
33-
App app = new App();
34-
APIGatewayProxyResponseEvent result = app.handleRequest(null, null);
35-
assertEquals(result.getStatusCode().intValue(), 200);
36-
assertEquals(result.getHeaders().get("Content-Type"), "application/json");
37-
String content = result.getBody();
38-
assertNotNull(content);
39-
assertTrue(content.contains("\"message\""));
40-
assertTrue(content.contains("\"hello world\""));
41-
assertTrue(content.contains("\"location\""));
42-
}
4359
}

examples/powertools-examples-idempotency/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
<!--
2+
~ Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
~ Licensed under the Apache License, Version 2.0 (the
4+
~ "License"); you may not use this file except in compliance
5+
~ with the License. You may obtain a copy of the License at
6+
~ http://www.apache.org/licenses/LICENSE-2.0
7+
~ Unless required by applicable law or agreed to in writing, software
8+
~ distributed under the License is distributed on an "AS IS" BASIS,
9+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
~ See the License for the specific language governing permissions and
11+
~ limitations under the License.
12+
~
13+
-->
14+
115
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
216
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
317
<modelVersion>4.0.0</modelVersion>

0 commit comments

Comments
 (0)