Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Format java, Fix CI errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mahesh-hegde committed Aug 17, 2022
1 parent 3b17cb8 commit 91533c2
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 100 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
run:
working-directory: ./jni_gen/third_party/ApiSummarizer
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'zulu'
Expand Down
2 changes: 1 addition & 1 deletion jni_gen/test/simple_package/java/dev/dart/pkg2/C2.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dev.dart.pkg2;

public class C2 {
public static int CONSTANT = 12;
public static int CONSTANT = 12;
}
3 changes: 2 additions & 1 deletion jni_gen/test/simple_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ void setupDylibsAndClasses() {
Process.runSync('dart', ['run', 'jni:setup']);
Process.runSync(
'dart', ['run', 'jni:setup', '-S', join(simplePackagePath, 'src')]);
Process.runSync('javac', ['dev/dart/simple_package/Example.java'],
Process.runSync('javac',
['dev/dart/simple_package/Example.java', 'dev/dart/pkg2/C2.java'],
workingDirectory: javaPath);

if (!Platform.isAndroid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
import com.github.hegde.mahesh.apisummarizer.doclet.SummarizerDoclet;
import com.github.hegde.mahesh.apisummarizer.elements.ClassDecl;
import com.github.hegde.mahesh.apisummarizer.util.Log;
import jdk.javadoc.doclet.Doclet;
import org.apache.commons.cli.*;

import javax.tools.DocumentationTool;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.tools.DocumentationTool;
import javax.tools.ToolProvider;
import jdk.javadoc.doclet.Doclet;
import org.apache.commons.cli.*;

public class Main {
private static final CommandLineParser parser = new DefaultParser();
Expand All @@ -41,7 +40,8 @@ public static void runDoclet(List<String> qualifiedNames, SummarizerOptions opti
runDocletWithClass(SummarizerDoclet.class, qualifiedNames, options);
}

public static void runDocletWithClass(Class<? extends Doclet> docletClass, List<String> qualifiedNames, SummarizerOptions options) {
public static void runDocletWithClass(
Class<? extends Doclet> docletClass, List<String> qualifiedNames, SummarizerOptions options) {
List<File> javaFilePaths =
qualifiedNames.stream()
.map(s -> findSourceLocation(s, options.sourcePaths.split(File.pathSeparator)))
Expand Down Expand Up @@ -71,9 +71,7 @@ public static void runDocletWithClass(Class<? extends Doclet> docletClass, List<
cli.addAll(List.of(options.toolOptions.split(" ")));
}

javadoc
.getTask(null, fileManager, System.err::println, docletClass, cli, fileObjects)
.call();
javadoc.getTask(null, fileManager, System.err::println, docletClass, cli, fileObjects).call();
}

public static void main(String[] args) {
Expand Down Expand Up @@ -163,8 +161,10 @@ public static CommandLine parseArgs(String[] args) {
} catch (ParseException e) {
System.out.println(e.getMessage());
help.printHelp(
"java -jar <JAR> [-s <SOURCE_DIR=.>] " + "[-c <CLASSES_JAR>] <CLASS_OR_PACKAGE_NAMES>\n"
+ "Class or package names should be fully qualified.\n\n", options);
"java -jar <JAR> [-s <SOURCE_DIR=.>] "
+ "[-c <CLASSES_JAR>] <CLASS_OR_PACKAGE_NAMES>\n"
+ "Class or package names should be fully qualified.\n\n",
options);
System.exit(1);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.github.hegde.mahesh.apisummarizer.disasm;

import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;

import com.github.hegde.mahesh.apisummarizer.elements.*;
import com.github.hegde.mahesh.apisummarizer.util.SkipException;
import com.github.hegde.mahesh.apisummarizer.util.StreamUtil;
import org.objectweb.asm.*;

import java.util.*;

import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import org.objectweb.asm.*;

public class AsmClassVisitor extends ClassVisitor implements AsmAnnotatedElementVisitor {
private static Param param(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.github.hegde.mahesh.apisummarizer.disasm;

import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Type.ARRAY;
import static org.objectweb.asm.Type.OBJECT;

import com.github.hegde.mahesh.apisummarizer.elements.DeclKind;
import com.github.hegde.mahesh.apisummarizer.elements.TypeUsage;
import com.github.hegde.mahesh.apisummarizer.util.SkipException;
import org.objectweb.asm.Type;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Type.ARRAY;
import static org.objectweb.asm.Type.OBJECT;
import org.objectweb.asm.Type;

class TypeUtils {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.github.hegde.mahesh.apisummarizer.doclet;

import java.util.List;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import java.util.List;
import java.util.stream.Collectors;

// AnnotationVisitor mechanism is not perfect right now. There are edge cases which need to be handled but
// this is not an immediate priority, since the utility of annotations other than Override and NonNull in
// AnnotationVisitor mechanism is not perfect right now. There are edge cases which need to be
// handled but
// this is not an immediate priority, since the utility of annotations other than Override and
// NonNull in
// jni_gen is perhaps limited to custom exclusion filters and rename configuration.
public class AnnotationVisitor implements AnnotationValueVisitor<Object, Void> {

Expand Down Expand Up @@ -78,7 +80,8 @@ public Object visitType(TypeMirror typeMirror, Void unused) {

@Override
public Object visitEnumConstant(VariableElement variableElement, Void unused) {
// TODO: Perhaps simple name is not enough. We need to return qualified name + enum constant name for completeness.
// TODO: Perhaps simple name is not enough. We need to return qualified name + enum constant
// name for completeness.
return variableElement.getSimpleName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import com.github.hegde.mahesh.apisummarizer.elements.*;
import com.github.hegde.mahesh.apisummarizer.util.StreamUtil;
import com.sun.source.doctree.DocCommentTree;

import javax.lang.model.element.*;
import javax.lang.model.type.*;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import javax.lang.model.element.*;
import javax.lang.model.type.*;

public class ElementBuilders {
AstEnv env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import com.github.hegde.mahesh.apisummarizer.elements.Package;
import com.github.hegde.mahesh.apisummarizer.util.Log;
import com.github.hegde.mahesh.apisummarizer.util.SkipException;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;

import java.util.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.util.ElementScanner9;
import java.util.*;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;

class SummarizerDocletBase implements Doclet {
private AstEnv utils;
Expand Down Expand Up @@ -171,13 +170,15 @@ public boolean run(DocletEnvironment docletEnvironment) {
Main.writeAll(types);
return result;
}

public static class TestDoclet extends SummarizerDocletBase {
@Override
public boolean run(DocletEnvironment docletEnvironment) {
return super.run(docletEnvironment);
}

public static List<ClassDecl> getClassDecls() {
return types;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
package com.github.hegde.mahesh.apisummarizer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.github.hegde.mahesh.apisummarizer.doclet.SummarizerDoclet;
import com.github.hegde.mahesh.apisummarizer.elements.ClassDecl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class DocletSummarizerTests {
private List<ClassDecl> parsedDecls;
private final Map<String, ClassDecl> classesByName = new HashMap<>();

@Before
public void setUp() {
var opts = new Main.SummarizerOptions();
opts.sourcePaths = "src/test/resources/";
// javadoc tool API is quite inflexible, in that we cannot pass an doclet object, but a class
// So any state we want to access from it has to be either serialized or saved in static fields.
// This means we lose lot of control over loading of files etc..
// Here, TestDoclet simply stores the result in a static variable which we can get and check later.
Main.runDocletWithClass(SummarizerDoclet.TestDoclet.class, List.of("com.example.Example"), opts);
parsedDecls = SummarizerDoclet.TestDoclet.getClassDecls();
for (var decl : parsedDecls) {
classesByName.put(decl.binaryName, decl);
}
}

@Test
public void checkNumberOfClasses() {
Assert.assertEquals(2, parsedDecls.size());
}

@Test
public void checkNamesOfClasses() {
var names = parsedDecls.stream()
.map(decl -> decl.binaryName)
.collect(Collectors.toSet());
assertTrue(names.contains("com.example.Example"));
assertTrue(names.contains("com.example.Example$Aux"));
}

@Test
public void checkNumberOfFieldsAndMethods() {
var example = classesByName.get("com.example.Example");
assertEquals("Example", example.simpleName);
assertEquals(3, example.fields.size());
assertEquals(3, example.methods.size());
private List<ClassDecl> parsedDecls;
private final Map<String, ClassDecl> classesByName = new HashMap<>();

@Before
public void setUp() {
var opts = new Main.SummarizerOptions();
opts.sourcePaths = "src/test/resources/";
// javadoc tool API is quite inflexible, in that we cannot pass an doclet object, but a class
// So any state we want to access from it has to be either serialized or saved in static fields.
// This means we lose lot of control over loading of files etc..
// Here, TestDoclet simply stores the result in a static variable which we can get and check
// later.
Main.runDocletWithClass(
SummarizerDoclet.TestDoclet.class, List.of("com.example.Example"), opts);
parsedDecls = SummarizerDoclet.TestDoclet.getClassDecls();
for (var decl : parsedDecls) {
classesByName.put(decl.binaryName, decl);
}
}

@Test
public void checkNumberOfClasses() {
Assert.assertEquals(2, parsedDecls.size());
}

@Test
public void checkNamesOfClasses() {
var names = parsedDecls.stream().map(decl -> decl.binaryName).collect(Collectors.toSet());
assertTrue(names.contains("com.example.Example"));
assertTrue(names.contains("com.example.Example$Aux"));
}

@Test
public void checkNumberOfFieldsAndMethods() {
var example = classesByName.get("com.example.Example");
assertEquals("Example", example.simpleName);
assertEquals(3, example.fields.size());
assertEquals(3, example.methods.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package com.example;

public class Example {
static final boolean staticFinalField = true;
static final boolean staticFinalField = true;

Example(int instanceField) {
this.instanceField = instanceField;
}
Example(int instanceField) {
this.instanceField = instanceField;
}

static String staticField = "hello";
static String getStaticField() {
return staticField;
}
static String staticField = "hello";

int instanceField;
int getInstanceField() {
return instanceField;
}
static String getStaticField() {
return staticField;
}

int instanceField;

int getInstanceField() {
return instanceField;
}

public static class Aux extends Example {
static int nothing = 0;

public static class Aux extends Example {
static int nothing = 0;
static Example getAnExample() {
return new Example();
}
static Example getAnExample() {
return new Example();
}
}
}

0 comments on commit 91533c2

Please sign in to comment.