Move tool that generates JCL comparison into a separate project. Make this tool compatible with JRE9+

This commit is contained in:
Alexey Andreev 2019-09-05 14:16:11 +03:00
parent 6e6783f93d
commit f47644d890
23 changed files with 221 additions and 94 deletions

View File

@ -360,6 +360,13 @@
</modules> </modules>
</profile> </profile>
<profile>
<id>with-classlib-comparison</id>
<modules>
<module>tools/classlib-comparison-gen</module>
</modules>
</profile>
<profile> <profile>
<id>sign-artifacts</id> <id>sign-artifacts</id>
<build> <build>

View File

@ -0,0 +1,101 @@
<!--
~ Copyright 2019 konsoletyper.
~
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm</artifactId>
<version>0.6.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<artifactId>teavm-classlib-comparison-gen</artifactId>
<name>Tool that generates HTML report about supported classes in TeaVM</name>
<description>Tool that generates HTML report about supported classes in TeaVM</description>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>hppc</artifactId>
<version>0.7.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<configLocation>../../checkstyle.xml</configLocation>
<propertyExpansion>config_loc=${basedir}/../..</propertyExpansion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,19 +13,20 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
class JCLClass { class JCLClass {
public final String name; final String name;
public JCLStatus status; JCLStatus status;
public JCLVisibility visibility = JCLVisibility.PUBLIC; JCLVisibility visibility = JCLVisibility.PUBLIC;
public JCLClassType type; JCLClassType type;
public final List<JCLItem> items = new ArrayList<>(); String outer;
final List<JCLItem> items = new ArrayList<>();
public JCLClass(String name) { JCLClass(String name) {
this.name = name; this.name = name;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
enum JCLClassType { enum JCLClassType {
CLASS, CLASS,

View File

@ -13,14 +13,31 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
import java.io.*; import java.io.File;
import java.net.URL; import java.io.FileOutputStream;
import java.net.URLDecoder; import java.io.IOException;
import java.util.*; import java.io.InputStream;
import java.util.jar.JarEntry; import java.io.InputStreamReader;
import java.util.jar.JarInputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
@ -28,11 +45,8 @@ import org.teavm.model.ReferenceCache;
import org.teavm.parsing.ClasspathClassHolderSource; import org.teavm.parsing.ClasspathClassHolderSource;
public class JCLComparisonBuilder { public class JCLComparisonBuilder {
private static final String JAR_PREFIX = "jar:file:";
private static final String JAR_SUFFIX = "!/java/lang/Object.class";
private static final String CLASS_SUFFIX = ".class"; private static final String CLASS_SUFFIX = ".class";
private static final String TEMPLATE_PLACEHOLDER = "${CONTENT}"; private static final String TEMPLATE_PLACEHOLDER = "${CONTENT}";
private Set<String> packages = new HashSet<>();
private ClassLoader classLoader = JCLComparisonBuilder.class.getClassLoader(); private ClassLoader classLoader = JCLComparisonBuilder.class.getClassLoader();
private JCLComparisonVisitor visitor; private JCLComparisonVisitor visitor;
private String outputDirectory; private String outputDirectory;
@ -45,10 +59,6 @@ public class JCLComparisonBuilder {
this.classLoader = classLoader; this.classLoader = classLoader;
} }
public Set<String> getPackages() {
return packages;
}
public String getOutputDirectory() { public String getOutputDirectory() {
return outputDirectory; return outputDirectory;
} }
@ -59,7 +69,7 @@ public class JCLComparisonBuilder {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
if (args.length == 0) { if (args.length == 0) {
System.err.println("Usage: package.name [package.name2 ...] [-output directory]"); System.err.println("Usage: -output <directory>");
System.exit(1); System.exit(1);
} }
JCLComparisonBuilder builder = new JCLComparisonBuilder(); JCLComparisonBuilder builder = new JCLComparisonBuilder();
@ -67,8 +77,6 @@ public class JCLComparisonBuilder {
for (int i = 0; i < args.length; ++i) { for (int i = 0; i < args.length; ++i) {
if (args[i].equals("-output")) { if (args[i].equals("-output")) {
builder.setOutputDirectory(args[++i]); builder.setOutputDirectory(args[++i]);
} else {
builder.getPackages().add(args[i].replace('.', '/'));
} }
} }
@ -90,7 +98,7 @@ public class JCLComparisonBuilder {
copyResource("html/enum_obj.png"); copyResource("html/enum_obj.png");
copyResource("html/annotation_obj.png"); copyResource("html/annotation_obj.png");
try (Writer out = new OutputStreamWriter(new FileOutputStream(new File( try (Writer out = new OutputStreamWriter(new FileOutputStream(new File(
outputDirectory, "jcl.html")), "UTF-8")) { outputDirectory, "jcl.html")), StandardCharsets.UTF_8)) {
generateHtml(out, packages); generateHtml(out, packages);
} }
File packagesDirectory = new File(outputDirectory, "packages"); File packagesDirectory = new File(outputDirectory, "packages");
@ -115,47 +123,75 @@ public class JCLComparisonBuilder {
private List<JCLPackage> buildModel() throws IOException { private List<JCLPackage> buildModel() throws IOException {
Map<String, JCLPackage> packageMap = new HashMap<>(); Map<String, JCLPackage> packageMap = new HashMap<>();
URL url = classLoader.getResource("java/lang/Object" + CLASS_SUFFIX);
String path = url.toString();
if (!path.startsWith(JAR_PREFIX) || !path.endsWith(JAR_SUFFIX)) {
throw new RuntimeException("Can't find JCL classes");
}
ClasspathClassHolderSource classSource = new ClasspathClassHolderSource(classLoader, new ReferenceCache()); ClasspathClassHolderSource classSource = new ClasspathClassHolderSource(classLoader, new ReferenceCache());
path = path.substring(JAR_PREFIX.length(), path.length() - JAR_SUFFIX.length());
File outDir = new File(outputDirectory).getParentFile();
if (!outDir.exists()) {
outDir.mkdirs();
}
path = URLDecoder.decode(path, "UTF-8");
try (JarInputStream jar = new JarInputStream(new FileInputStream(path))) {
visitor = new JCLComparisonVisitor(classSource, packageMap); visitor = new JCLComparisonVisitor(classSource, packageMap);
while (true) { try {
JarEntry entry = jar.getNextJarEntry(); Path p = Paths.get(URI.create("jrt:/modules/java.base/java/"));
if (entry == null) { Files.walkFileTree(p, new SimpleFileVisitor<>() {
break; @Override
} public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (validateName(entry.getName())) { if (validateName(file.getFileName().toString())) {
compareClass(jar); try (InputStream input = Files.newInputStream(file)) {
} compareClass(input);
jar.closeEntry(); } catch (IOException e) {
throw new RuntimeException(e);
} }
} }
return FileVisitResult.CONTINUE;
}
});
System.out.println();
} catch (FileSystemNotFoundException ex) {
System.out.println("Could not read my modules (perhaps not Java 9?).");
}
for (JCLPackage pkg : packageMap.values()) {
for (JCLClass cls : pkg.classes.toArray(new JCLClass[0])) {
if (cls.outer != null) {
removeInnerPrivateClasses(packageMap, pkg, cls.name);
}
}
}
for (String packageName : packageMap.keySet().toArray(new String[0])) {
JCLPackage pkg = packageMap.get(packageName);
if (pkg.classes.stream().allMatch(cls -> cls.status == JCLStatus.MISSING)) {
packageMap.remove(packageName);
} else if (pkg.classes.stream().anyMatch(cls -> cls.status == JCLStatus.MISSING)) {
pkg.status = JCLStatus.PARTIAL;
}
}
return new ArrayList<>(packageMap.values()); return new ArrayList<>(packageMap.values());
} }
private boolean removeInnerPrivateClasses(Map<String, JCLPackage> packageMap, JCLPackage pkg, String className) {
JCLClass cls = pkg.classes.stream().filter(c -> c.name.equals(className)).findFirst().orElse(null);
if (cls == null) {
return true;
}
if (cls.outer != null) {
String packageName = cls.outer.substring(0, cls.outer.lastIndexOf('.'));
JCLPackage outerPackage = packageMap.get(packageName);
if (outerPackage == null || removeInnerPrivateClasses(packageMap, outerPackage, cls.outer)) {
pkg.classes.remove(cls);
return true;
}
}
return false;
}
private void processModel(List<JCLPackage> packages) { private void processModel(List<JCLPackage> packages) {
Collections.sort(packages, (o1, o2) -> o1.name.compareTo(o2.name)); packages.sort(Comparator.comparing(o -> o.name));
for (JCLPackage pkg : packages) { for (JCLPackage pkg : packages) {
Collections.sort(pkg.classes, (o1, o2) -> o1.name.compareTo(o2.name)); pkg.classes.sort(Comparator.comparing(o -> o.name));
} }
} }
private boolean validateName(String name) { private boolean validateName(String name) {
if (!name.endsWith(CLASS_SUFFIX)) { return name.endsWith(CLASS_SUFFIX);
return false;
}
int slashIndex = name.lastIndexOf('/');
return slashIndex >= 0 && packages.contains(name.substring(0, slashIndex));
} }
private void compareClass(InputStream input) throws IOException { private void compareClass(InputStream input) throws IOException {
@ -174,7 +210,8 @@ public class JCLComparisonBuilder {
private void generateHtml(Writer out, List<JCLPackage> packages) throws IOException { private void generateHtml(Writer out, List<JCLPackage> packages) throws IOException {
String template; String template;
try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream("html/jcl.html"), "UTF-8")) { try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream("html/jcl.html"),
StandardCharsets.UTF_8)) {
template = IOUtils.toString(reader); template = IOUtils.toString(reader);
} }
int placeholderIndex = template.indexOf(TEMPLATE_PLACEHOLDER); int placeholderIndex = template.indexOf(TEMPLATE_PLACEHOLDER);
@ -207,7 +244,8 @@ public class JCLComparisonBuilder {
private void generatePackageHtml(Writer out, JCLPackage pkg) throws IOException { private void generatePackageHtml(Writer out, JCLPackage pkg) throws IOException {
String template; String template;
try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream("html/jcl-class.html"), "UTF-8")) { try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream("html/jcl-class.html"),
StandardCharsets.UTF_8)) {
template = IOUtils.toString(reader); template = IOUtils.toString(reader);
} }
template = template.replace("${CLASSNAME}", pkg.name); template = template.replace("${CLASSNAME}", pkg.name);
@ -242,7 +280,8 @@ public class JCLComparisonBuilder {
private void generateClassHtml(Writer out, JCLPackage pkg, JCLClass cls) throws IOException { private void generateClassHtml(Writer out, JCLPackage pkg, JCLClass cls) throws IOException {
String template; String template;
try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream("html/jcl-class.html"), "UTF-8")) { try (Reader reader = new InputStreamReader(classLoader.getResourceAsStream("html/jcl-class.html"),
StandardCharsets.UTF_8)) {
template = IOUtils.toString(reader); template = IOUtils.toString(reader);
} }
template = template.replace("${CLASSNAME}", pkg.name + "." + cls.name); template = template.replace("${CLASSNAME}", pkg.name + "." + cls.name);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
import java.util.Map; import java.util.Map;
import org.objectweb.asm.*; import org.objectweb.asm.*;
@ -135,30 +135,9 @@ class JCLComparisonVisitor extends ClassVisitor {
} }
@Override @Override
public void visitSource(String source, String debug) { public void visitNestHost(String nestHost) {
} if (jclClass != null) {
jclClass.outer = nestHost.replace('/', '.');
@Override
public void visitOuterClass(String owner, String name, String desc) {
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return null;
}
@Override
public void visitAttribute(Attribute attr) {
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
}
@Override
public void visitEnd() {
if (jclPackage != null && (jclClass == null || jclClass.status != JCLStatus.FOUND)) {
jclPackage.status = JCLStatus.PARTIAL;
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
class JCLItem { class JCLItem {
public final JCLItemType type; public final JCLItemType type;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
enum JCLItemType { enum JCLItemType {
FIELD, FIELD,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
enum JCLStatus { enum JCLStatus {
FOUND, FOUND,

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2014 Alexey Andreev. * Copyright 2019 konsoletyper.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl.report; package org.teavm.tools.classlibcomparison;
enum JCLVisibility { enum JCLVisibility {
PUBLIC, PUBLIC,

View File

Before

Width:  |  Height:  |  Size: 481 B

After

Width:  |  Height:  |  Size: 481 B

View File

Before

Width:  |  Height:  |  Size: 774 B

After

Width:  |  Height:  |  Size: 774 B

View File

Before

Width:  |  Height:  |  Size: 513 B

After

Width:  |  Height:  |  Size: 513 B

View File

Before

Width:  |  Height:  |  Size: 222 B

After

Width:  |  Height:  |  Size: 222 B

View File

Before

Width:  |  Height:  |  Size: 221 B

After

Width:  |  Height:  |  Size: 221 B

View File

Before

Width:  |  Height:  |  Size: 745 B

After

Width:  |  Height:  |  Size: 745 B

View File

Before

Width:  |  Height:  |  Size: 299 B

After

Width:  |  Height:  |  Size: 299 B

View File

Before

Width:  |  Height:  |  Size: 325 B

After

Width:  |  Height:  |  Size: 325 B

View File

Before

Width:  |  Height:  |  Size: 345 B

After

Width:  |  Height:  |  Size: 345 B