Skip to content

Commit

Permalink
Use azure-json in ApiView Generator (#8033)
Browse files Browse the repository at this point in the history
Use azure-json in ApiView Generator
  • Loading branch information
alzimmermsft authored Apr 15, 2024
1 parent f494b72 commit f37db1a
Show file tree
Hide file tree
Showing 21 changed files with 586 additions and 259 deletions.
26 changes: 13 additions & 13 deletions src/java/apiview-java-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
<groupId>com.azure</groupId>
<artifactId>azure-json</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-core</artifactId>
<version>3.25.9</version>
<artifactId>javaparser-core</artifactId>
<version>3.25.10</version>
</dependency>

<dependency>
Expand All @@ -50,14 +50,14 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.0.0</version> <!-- {x-version-update;org.mockito:mockito-core;external_dependency} -->
<version>4.11.0</version> <!-- {x-version-update;org.mockito:mockito-core;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.unbescape</groupId>
<artifactId>unbescape</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
</dependencies>

<build>
Expand All @@ -66,7 +66,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.12.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -79,7 +79,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.azure.tools.apiview.processor;

import com.azure.tools.apiview.processor.analysers.JavaASTAnalyser;
import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.json.JsonWriter;
import com.azure.tools.apiview.processor.analysers.Analyser;
import com.azure.tools.apiview.processor.analysers.JavaASTAnalyser;
import com.azure.tools.apiview.processor.analysers.XMLASTAnalyser;
import com.azure.tools.apiview.processor.model.*;
import com.azure.tools.apiview.processor.model.APIListing;
import com.azure.tools.apiview.processor.model.ApiViewProperties;
import com.azure.tools.apiview.processor.model.Diagnostic;
import com.azure.tools.apiview.processor.model.DiagnosticKind;
import com.azure.tools.apiview.processor.model.LanguageVariant;
import com.azure.tools.apiview.processor.model.Token;
import com.azure.tools.apiview.processor.model.maven.Pom;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand All @@ -26,17 +29,8 @@
import java.util.stream.Stream;

import static com.azure.tools.apiview.processor.model.TokenKind.LINE_ID_MARKER;
import static com.fasterxml.jackson.databind.MapperFeature.AUTO_DETECT_CREATORS;
import static com.fasterxml.jackson.databind.MapperFeature.AUTO_DETECT_FIELDS;
import static com.fasterxml.jackson.databind.MapperFeature.AUTO_DETECT_GETTERS;
import static com.fasterxml.jackson.databind.MapperFeature.AUTO_DETECT_IS_GETTERS;

public class Main {
private static final ObjectWriter WRITER = new ObjectMapper()
.disable(AUTO_DETECT_CREATORS, AUTO_DETECT_FIELDS, AUTO_DETECT_GETTERS, AUTO_DETECT_IS_GETTERS)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.writerWithDefaultPrettyPrinter();

// expected argument order:
// [inputFiles] <outputDirectory>
public static void main(String[] args) throws IOException {
Expand Down Expand Up @@ -133,7 +127,9 @@ private static void processFile(final File inputFile, final File outputFile) thr
}

// Write out to the filesystem
WRITER.writeValue(outputFile, apiListing);
try (JsonWriter jsonWriter = JsonProviders.createWriter(Files.newBufferedWriter(outputFile.toPath()))) {
apiListing.toJson(jsonWriter);
}
}

private static void processJavaSourcesJar(File inputFile, APIListing apiListing) throws IOException {
Expand Down Expand Up @@ -168,25 +164,7 @@ private static void processJavaSourcesJar(File inputFile, APIListing apiListing)
// Read all files within the jar file so that we can create a list of files to analyse
final List<Path> allFiles = new ArrayList<>();
try (FileSystem fs = FileSystems.newFileSystem(inputFile.toPath(), Main.class.getClassLoader())) {

try {
// we eagerly load the apiview_properties.json file into an ApiViewProperties object, so that it can
// be used throughout the analysis process, as required
// the filename is [<artifactid>_]apiview_properties.json
String filename = (artifactId != null && !artifactId.isEmpty() ? (artifactId + "_") : "") + "apiview_properties.json";
URL apiViewPropertiesFile = fs.getPath("/META-INF/" + filename).toUri().toURL();
final ObjectMapper objectMapper = new ObjectMapper();
ApiViewProperties properties = objectMapper.readValue(apiViewPropertiesFile, ApiViewProperties.class);
apiListing.setApiViewProperties(properties);
System.out.println(" Found apiview_properties.json file in jar file");
System.out.println(" - Found " + properties.getCrossLanguageDefinitionIds().size() + " cross-language definition IDs");
} catch (InvalidFormatException e) {
System.out.println(" ERROR: Unable to parse apiview_properties.json file in jar file");
e.printStackTrace();
} catch (Exception e) {
// this is fine, we just won't have any APIView properties to read in
System.out.println(" No apiview_properties.json file found in jar file - continuing...");
}
tryParseApiViewProperties(fs, apiListing, artifactId);

fs.getRootDirectories().forEach(root -> {
try (Stream<Path> paths = Files.walk(root)) {
Expand All @@ -205,6 +183,42 @@ private static void processJavaSourcesJar(File inputFile, APIListing apiListing)
}
}

/**
* Attempts to process the {@code apiview_properties.json} file in the jar file, if it exists.
* <p>
* If the file was found and successfully parsed as {@link ApiViewProperties}, it is set on the {@link APIListing}
* object.
*
* @param fs the {@link FileSystem} representing the jar file
* @param apiListing the {@link APIListing} object to set the {@link ApiViewProperties} on
* @param artifactId the artifact ID of the jar file
*/
private static void tryParseApiViewProperties(FileSystem fs, APIListing apiListing, String artifactId) {
// the filename is [<artifactid>_]apiview_properties.json
String artifactName = (artifactId != null && !artifactId.isEmpty()) ? (artifactId + "_") : "";
String filePath = "/META-INF/" + artifactName + "apiview_properties.json";
Path apiviewPropertiesPath = fs.getPath(filePath);
if (!Files.exists(apiviewPropertiesPath)) {
System.out.println(" No apiview_properties.json file found in jar file - continuing...");
return;
}

try {
// we eagerly load the apiview_properties.json file into an ApiViewProperties object, so that it can
// be used throughout the analysis process, as required
try (JsonReader reader = JsonProviders.createReader(Files.readAllBytes(apiviewPropertiesPath))) {
ApiViewProperties properties = ApiViewProperties.fromJson(reader);
apiListing.setApiViewProperties(properties);
System.out.println(" Found apiview_properties.json file in jar file");
System.out.println(" - Found " + properties.getCrossLanguageDefinitionIds().size()
+ " cross-language definition IDs");
}
} catch (IOException e) {
System.out.println(" ERROR: Unable to parse apiview_properties.json file in jar file - continuing...");
e.printStackTrace();
}
}

private static void processXmlFile(File inputFile, APIListing apiListing) {
final ReviewProperties reviewProperties = new ReviewProperties();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.azure.tools.apiview.processor.analysers;


import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -25,6 +24,6 @@ public interface Analyser {
void analyse(List<Path> allFiles);

default void analyse(Path file) {
analyse(Arrays.asList(file));
analyse(Collections.singletonList(file));
}
}
Loading

0 comments on commit f37db1a

Please sign in to comment.