Skip to content

Commit

Permalink
feat: add java spotless code formatter (#101)
Browse files Browse the repository at this point in the history
## Description

Introduce Java spotless code formatter ->
https://github.com/diffplug/spotless/tree/main/plugin-maven


## Checklist

- [x] I have followed this repository's contributing guidelines.
- [x] I will adhere to the project's code of conduct.

## Additional information

Check pom.xml and license-header files. Everything else is changed
because I ran `mvn spotless:apply`

> Anything else?

When someone raises a new PR, it will be validated automatically in the
build phase of the github actions for PR.

---------

Signed-off-by: Jude Niroshan <jude.niroshan11@gmail.com>
  • Loading branch information
JudeNiroshan authored Apr 19, 2024
1 parent 1318089 commit 62e4445
Show file tree
Hide file tree
Showing 62 changed files with 6,771 additions and 5,640 deletions.
15 changes: 15 additions & 0 deletions license-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright © 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,43 @@ limitations under the License.]]>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<configuration>
<formats>
<format>
<includes><include>src/*</include></includes>
<trimTrailingWhitespace/>
<endWithNewline/>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<java>
<googleJavaFormat>
<style>GOOGLE</style> <!-- or AOSP (optional) -->
<reflowLongStrings>true</reflowLongStrings> <!-- optional -->
</googleJavaFormat>
<licenseHeader>
<file>${project.basedir}/license-header</file> -->
</licenseHeader>
<removeUnusedImports />
<formatAnnotations />
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/redhat/exhort/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package com.redhat.exhort;

import com.redhat.exhort.api.AnalysisReport;
import com.redhat.exhort.image.ImageRef;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import com.redhat.exhort.api.AnalysisReport;
import com.redhat.exhort.image.ImageRef;

/** The Api interface is used for contracting API implementations. **/
/** The Api interface is used for contracting API implementations. * */
public interface Api {

public static final String CYCLONEDX_MEDIA_TYPE = "application/vnd.cyclonedx+json";
Expand All @@ -43,18 +42,19 @@ public String toString() {

/** POJO class used for aggregating multipart/mixed analysis requests. */
class MixedReport {
final public byte[] html;
final public AnalysisReport json;
public final byte[] html;
public final AnalysisReport json;

public MixedReport(final byte[] html, final AnalysisReport json) {
this.html = html;
this.json = json;
}
public MixedReport()
{

public MixedReport() {
this.html = new byte[0];
this.json = new AnalysisReport();
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
Expand Down Expand Up @@ -104,11 +104,13 @@ public int hashCode() {
* @return the deserialized Json report as an AnalysisReport wrapped in a CompletableFuture
* @throws IOException when failed to load the manifest content
*/
CompletableFuture<AnalysisReport> componentAnalysis(String manifestType, byte[] manifestContent) throws IOException;
CompletableFuture<AnalysisReport> componentAnalysis(String manifestType, byte[] manifestContent)
throws IOException;

CompletableFuture<AnalysisReport> componentAnalysis(String manifestFile) throws IOException;

CompletableFuture<Map<ImageRef, AnalysisReport>> imageAnalysis(Set<ImageRef> imageRefs) throws IOException;
CompletableFuture<Map<ImageRef, AnalysisReport>> imageAnalysis(Set<ImageRef> imageRefs)
throws IOException;

CompletableFuture<byte[]> imageAnalysisHtml(Set<ImageRef> imageRefs) throws IOException;
}
22 changes: 12 additions & 10 deletions src/main/java/com/redhat/exhort/Provider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,33 @@
*/
package com.redhat.exhort;

import java.io.IOException;
import java.nio.file.Path;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.exhort.tools.Ecosystem;
import java.io.IOException;
import java.nio.file.Path;

/**
* The Provider abstraction is used for contracting providers providing a {@link Content}
* per manifest type for constructing backend requests.
**/
* The Provider abstraction is used for contracting providers providing a {@link Content} per
* manifest type for constructing backend requests.
*/
public abstract class Provider {
/**
* Content is used to aggregate a content buffer and a content type.
* These will be used to construct the backend API request.
**/
* Content is used to aggregate a content buffer and a content type. These will be used to
* construct the backend API request.
*/
public static class Content {
public final byte[] buffer;
public final String type;
public Content(byte[] buffer, String type){

public Content(byte[] buffer, String type) {
this.buffer = buffer;
this.type = type;
}
}

/** The ecosystem of this provider, i.e. maven. */
public final Ecosystem.Type ecosystem;

protected final ObjectMapper objectMapper = new ObjectMapper();

protected Provider(Ecosystem.Type ecosystem) {
Expand All @@ -64,5 +65,6 @@ protected Provider(Ecosystem.Type ecosystem) {
* @throws IOException when failed to load the manifest content
*/
public abstract Content provideComponent(byte[] manifestContent) throws IOException;

public abstract Content provideComponent(Path manifestPath) throws IOException;
}
7 changes: 3 additions & 4 deletions src/main/java/com/redhat/exhort/api/PackageRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
*/
package com.redhat.exhort.api;

import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.redhat.exhort.api.serialization.PackageURLSerializer;
import java.util.Objects;

public class PackageRef {

Expand Down Expand Up @@ -55,8 +54,8 @@ public String ref() {

public String name() {
if (purl.getNamespace() == null) {
return purl.getName();
}
return purl.getName();
}
return purl.getNamespace() + ":" + purl.getName();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/redhat/exhort/api/package-info.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/** Package hosting various the Exhort API implementation. **/
/** Package hosting various the Exhort API implementation. * */
package com.redhat.exhort.api;
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,32 @@
*/
package com.redhat.exhort.api.serialization;

import java.io.IOException;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.redhat.exhort.api.PackageRef;
import java.io.IOException;

public class PackageRefDeserializer extends StdDeserializer<PackageRef> {

public PackageRefDeserializer() {
this(null);
}
public PackageRefDeserializer() {
this(null);
}

public PackageRefDeserializer(Class<PackageRef> c) {
super(c);
}
public PackageRefDeserializer(Class<PackageRef> c) {
super(c);
}

@Override
public PackageRef deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
JsonNode n = p.getCodec().readTree(p);
String purl = n.asText();
if (purl == null) {
return null;
}
return new PackageRef(purl);
@Override
public PackageRef deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JacksonException {
JsonNode n = p.getCodec().readTree(p);
String purl = n.asText();
if (purl == null) {
return null;
}

return new PackageRef(purl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.redhat.exhort.api.serialization;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.github.packageurl.PackageURL;
import java.io.IOException;

public class PackageURLSerializer extends StdSerializer<PackageURL> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/** Package hosting various the Exhort API implementation. **/
/** Package hosting various the Exhort API implementation. * */
package com.redhat.exhort.api.serialization;
Loading

0 comments on commit 62e4445

Please sign in to comment.