Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 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.13.4.2</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.24.2</version>
<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,13 +1,18 @@
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 java.io.File;
import java.io.FileInputStream;
Expand All @@ -25,17 +30,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 @@ -132,7 +128,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 @@ -174,11 +172,11 @@ private static void processJavaSourcesJar(File inputFile, APIListing apiListing)
// we eagerly load the apiview_properties.json file into an ApiViewProperties object, so that it can
// be used throughout the analysis process, as required
URL apiViewPropertiesFile = fs.getPath("/META-INF/apiview_properties.json").toUri().toURL();
final ObjectMapper objectMapper = new ObjectMapper();
ApiViewProperties properties = objectMapper.readValue(apiViewPropertiesFile, ApiViewProperties.class);
apiListing.setApiViewProperties(properties);
try (JsonReader reader = JsonProviders.createReader(apiViewPropertiesFile.openStream())) {
apiListing.setApiViewProperties(ApiViewProperties.fromJson(reader));
}
System.out.println(" Found apiview_properties.json file in jar file");
System.out.println(" - Found " + properties.getCrossLanguageDefinitionIds().size() + " cross-language definition IDs");
System.out.println(" - Found " + apiListing.getApiViewProperties().getCrossLanguageDefinitionIds().size() + " cross-language definition IDs");
} 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...");
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import com.azure.tools.apiview.processor.model.TypeKind;
import com.azure.tools.apiview.processor.model.maven.Dependency;
import com.azure.tools.apiview.processor.model.maven.Pom;
import com.github.javaparser.JavaParser;
import com.github.javaparser.JavaParserAdapter;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.TokenRange;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
Expand Down Expand Up @@ -46,13 +47,14 @@
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.TypeParameter;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.printer.configuration.DefaultPrinterConfiguration;
import com.github.javaparser.printer.DefaultPrettyPrinter;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.unbescape.html.HtmlEscape;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand All @@ -67,14 +69,18 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang.StringEscapeUtils;

import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.*;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.attemptToFindJavadocComment;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.getNodeFullyQualifiedName;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.getPackageName;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.isInterfaceType;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.isPrivateOrPackagePrivate;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.isPublicOrProtected;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.isTypeAPublicAPI;
import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.makeId;
import static com.azure.tools.apiview.processor.analysers.util.TokenModifier.INDENT;
import static com.azure.tools.apiview.processor.analysers.util.TokenModifier.NEWLINE;
import static com.azure.tools.apiview.processor.analysers.util.TokenModifier.NOTHING;
Expand All @@ -84,6 +90,8 @@
import static com.azure.tools.apiview.processor.model.TokenKind.DEPRECATED_RANGE_START;
import static com.azure.tools.apiview.processor.model.TokenKind.DOCUMENTATION_RANGE_END;
import static com.azure.tools.apiview.processor.model.TokenKind.DOCUMENTATION_RANGE_START;
import static com.azure.tools.apiview.processor.model.TokenKind.EXTERNAL_LINK_END;
import static com.azure.tools.apiview.processor.model.TokenKind.EXTERNAL_LINK_START;
import static com.azure.tools.apiview.processor.model.TokenKind.KEYWORD;
import static com.azure.tools.apiview.processor.model.TokenKind.MEMBER_NAME;
import static com.azure.tools.apiview.processor.model.TokenKind.NEW_LINE;
Expand All @@ -93,8 +101,6 @@
import static com.azure.tools.apiview.processor.model.TokenKind.TEXT;
import static com.azure.tools.apiview.processor.model.TokenKind.TYPE_NAME;
import static com.azure.tools.apiview.processor.model.TokenKind.WHITESPACE;
import static com.azure.tools.apiview.processor.model.TokenKind.EXTERNAL_LINK_START;
import static com.azure.tools.apiview.processor.model.TokenKind.EXTERNAL_LINK_END;

public class JavaASTAnalyser implements Analyser {
public static final String MAVEN_KEY = "Maven";
Expand All @@ -104,7 +110,23 @@ public class JavaASTAnalyser implements Analyser {
private static final Set<String> BLOCKED_ANNOTATIONS =
new HashSet<>(Arrays.asList("ServiceMethod", "SuppressWarnings"));

private static final Pattern SPLIT_NEWLINE = Pattern.compile(MiscUtils.LINEBREAK);
private static final DefaultPrettyPrinter DEFAULT_PRINTER = new DefaultPrettyPrinter();
private static final JavaParserAdapter PARSER;

static {
// Set up a minimal type solver that only looks at the classes used to run this sample.
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver(false));

ParserConfiguration parserConfiguration = new ParserConfiguration()
.setStoreTokens(true)
.setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver))
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11)
.setDetectOriginalLineSeparator(false);

// Configure JavaParser to use type resolution
PARSER = JavaParserAdapter.of(new JavaParser(parserConfiguration));
}

// This is the model that we build up as the AST of all files are analysed. The APIListing is then output as
// JSON that can be understood by APIView.
Expand Down Expand Up @@ -196,19 +218,8 @@ private Optional<ScanClass> scanForTypes(Path path) {
return Optional.of(new ScanClass(path, null));
}
try {
// Set up a minimal type solver that only looks at the classes used to run this sample.
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver(false));

ParserConfiguration parserConfiguration = new ParserConfiguration()
.setStoreTokens(true)
.setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver))
.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_11);

// Configure JavaParser to use type resolution
StaticJavaParser.setConfiguration(parserConfiguration);

CompilationUnit compilationUnit = StaticJavaParser.parse(path);
CompilationUnit compilationUnit = PARSER.parse(Files.newBufferedReader(path));
compilationUnit.setStorage(path, StandardCharsets.UTF_8);
new ScanForClassTypeVisitor().visit(compilationUnit, null);

if (path.endsWith("package-info.java")) {
Expand Down Expand Up @@ -1416,13 +1427,12 @@ private void visitJavaDoc(JavadocComment javadoc) {
// The updated configuration from getDeclarationAsString removes the comment option and hence the toString
// returns an empty string now. So, here we are using the toString overload that takes a PrintConfiguration
// to get the old behavior.
String javaDocText = javadoc.toString(new DefaultPrinterConfiguration());
Arrays.stream(SPLIT_NEWLINE.split(javaDocText)).forEach(line -> {
String javaDocText = DEFAULT_PRINTER.print(javadoc);
splitNewLine(javaDocText).forEach(line -> {
// we want to wrap our javadocs so that they are easier to read, so we wrap at 120 chars
final String wrappedString = MiscUtils.wrap(line, 120);
Arrays.stream(SPLIT_NEWLINE.split(wrappedString)).forEach(line2 -> {
MiscUtils.wrap(line, 120).forEach(line2 -> {
if (line2.contains("&")) {
line2 = StringEscapeUtils.unescapeHtml(line2);
line2 = HtmlEscape.unescapeHtml(line2);
}
addToken(makeWhitespace());

Expand Down Expand Up @@ -1463,11 +1473,10 @@ private void unindent() {
}

private Token makeWhitespace() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append(" ");
}
return new Token(WHITESPACE, sb.toString());
byte[] bytes = new byte[indent];
Arrays.fill(bytes, (byte) ' ');

return new Token(WHITESPACE, new String(bytes, StandardCharsets.UTF_8));
}

private void addComment(String comment) {
Expand Down Expand Up @@ -1507,4 +1516,12 @@ private void handleTokenModifier(TokenModifier modifier) {
break;
}
}

private static Stream<String> splitNewLine(String input) {
if (input == null || input.isEmpty()) {
return Stream.empty();
}

return Stream.of(input.split("\n"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static com.azure.tools.apiview.processor.model.TokenKind.COMMENT;
import static com.azure.tools.apiview.processor.model.TokenKind.KEYWORD;
Expand Down Expand Up @@ -51,7 +51,7 @@ private void processFile(Path file) {

XMLStreamReader reader = null;
try {
reader = factory.createXMLStreamReader(new FileInputStream(file.toFile()));
reader = factory.createXMLStreamReader(Files.newBufferedReader(file));

while (reader.hasNext()) {
final int eventType = reader.next();
Expand Down Expand Up @@ -126,7 +126,7 @@ private void processFile(Path file) {
}
}
}
} catch (XMLStreamException | FileNotFoundException e) {
} catch (XMLStreamException | IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
Expand Down Expand Up @@ -174,7 +174,7 @@ private void startElement(final XMLStreamReader reader) {
if (attributeCount > 0) {
for (int i = 0; i < attributeCount; i++) {
final String prefix = reader.getAttributePrefix(i);
final String key = (prefix == "" ? "" : prefix + ":") + reader.getAttributeLocalName(i);
final String key = (Objects.equals(prefix, "") ? "" : prefix + ":") + reader.getAttributeLocalName(i);
final String value = reader.getAttributeValue(i);

addAttribute(key, value);
Expand Down Expand Up @@ -256,4 +256,4 @@ private void addToken(Token token) {
apiListing.getTokens().add(token);
lastToken = token;
}
}
}
Loading