Skip to content

Commit

Permalink
Modularize (java 9) the code base
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Neuville committed Sep 27, 2019
1 parent 6671816 commit fa1c9cf
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 28 deletions.
3 changes: 3 additions & 0 deletions annotation/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module derive4j.annotation {
exports org.derive4j;
}
34 changes: 24 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ buildscript {
}

dependencies {
classpath "com.github.ben-manes:gradle-versions-plugin:0.21.0"
classpath "com.github.ben-manes:gradle-versions-plugin:0.25.0"
classpath "gradle.plugin.com.hierynomus.gradle.plugins:license-gradle-plugin:0.15.0"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.7"
classpath "com.diffplug.spotless:spotless-plugin-gradle:3.23.1"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.9"
classpath "com.diffplug.spotless:spotless-plugin-gradle:3.24.2"
classpath "org.javamodularity:moduleplugin:1.6.0"
}

wrapper {
gradleVersion = "5.5"
gradleVersion = "5.6.2"
distributionType = Wrapper.DistributionType.ALL
}
}
Expand All @@ -34,7 +35,6 @@ allprojects {
apply plugin: 'jacoco'
apply plugin: 'idea'


defaultTasks "build"

ext {
Expand All @@ -56,9 +56,9 @@ allprojects {
primaryEmail = "[email protected]"

dependencyJavaPoet = "com.squareup:javapoet:1.11.1"
dependencyPreviousDerive4J = "org.derive4j:derive4j:1.1.0"
dependencyPreviousDerive4J = "org.derive4j:derive4j:1.1.1"
dependencyJunit = "junit:junit:4.12"
dependencyAutoService = "com.google.auto.service:auto-service:1.0-rc4"
dependencyAutoService = "com.google.auto.service:auto-service:1.0-rc6"

gplLicenseName = "The GNU General Public License"
lgplLicenseName = "The GNU Lesser General Public License"
Expand All @@ -84,6 +84,7 @@ subprojects {

apply from: "$rootDir/lib.gradle"
apply plugin: "java"
apply plugin: 'org.javamodularity.moduleplugin'
apply plugin: "com.github.ben-manes.versions"
apply plugin: "com.github.hierynomus.license"
apply plugin: "com.diffplug.gradle.spotless"
Expand All @@ -97,7 +98,6 @@ subprojects {
endWithNewline()
}
}


repositories {
mavenLocal()
Expand Down Expand Up @@ -126,9 +126,23 @@ configure(subprojects) {

apply plugin: "maven"

sourceCompatibility = "1.8"
sourceCompatibility = "1.9"

sourceSets {
generated {
java.srcDir "$buildDir/generated/sources/annotationProcessor/java/main"
}
}

task customJavadoc(type: Javadoc) {
source = sourceSets.main.java + sourceSets.generated.java
classpath = sourceSets.main.compileClasspath
doFirst {
options.addStringOption('-module-path', classpath.asPath)
}
}

task javadocJar(type: Jar, dependsOn: "javadoc") {
task javadocJar(type: Jar, dependsOn: "customJavadoc") {
archiveClassifier = 'javadoc'
from "build/docs/javadoc"
}
Expand Down
4 changes: 4 additions & 0 deletions examples/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module derive4j.examples {
requires derive4j.annotation;
requires functionaljava;
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\:https://services.gradle.org/distributions/gradle-5.5-all.zip
distributionUrl=https\:https://services.gradle.org/distributions/gradle-5.6.2-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
Expand Down
8 changes: 8 additions & 0 deletions processor-api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module derive4j.processor.api {
requires transitive derive4j.annotation;
requires transitive java.compiler;
requires transitive com.squareup.javapoet;

exports org.derive4j.processor.api;
exports org.derive4j.processor.api.model;
}
15 changes: 12 additions & 3 deletions processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ dependencies {
compile project(":annotation")
compile project(":processor-api")
compile dependencyJavaPoet

compileOnly dependencyAutoService

annotationProcessor dependencyAutoService
annotationProcessor dependencyPreviousDerive4J

testCompile dependencyJunit
testCompile 'com.google.testing.compile:compile-testing:0.15'
testCompile "org.functionaljava:functionaljava_1.8:4.8.1"
testCompile('com.google.testing.compile:compile-testing:0.18') {
exclude module: 'auto-value'
}
testCompile "org.functionaljava:functionaljava:4.8.1"
testCompile 'io.atlassian.fugue:fugue:4.7.2'
testCompile 'com.atlassian.fugue:fugue:2.7.0'
testCompile 'com.google.guava:guava:26.0-jre'
testCompile 'io.vavr:vavr:0.9.2'
testCompile "org.derive4j.hkt:hkt:0.9.2"
testCompile "org.derive4j.hkt:hkt:0.10.0-SNAPSHOT"
testCompile "org.derive4j:derive4j-fj:0.2"
testCompile group: 'com.oath.cyclops', name: 'cyclops', version: '10.0.2'

testCompileOnly dependencyAutoService
testCompile project(":annotation")
testRuntime project(":annotation")
}

sourceSets {
test {
resources {
Expand Down
13 changes: 13 additions & 0 deletions processor/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module derive4j.processor {
requires static auto.service.annotations;
requires com.squareup.javapoet;
requires java.compiler;
requires derive4j.processor.api;
requires derive4j.annotation;

provides javax.annotation.processing.Processor
with org.derive4j.processor.DerivingProcessor;

uses org.derive4j.processor.api.ExtensionFactory;
uses org.derive4j.processor.api.DerivatorFactory;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@
*/
package org.derive4j.processor;

import com.google.common.truth.Truth;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.Compiler;
import com.google.testing.compile.JavaFileObjects;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.Test;

import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
import javax.lang.model.SourceVersion;
import javax.tools.*;
import java.io.*;
import java.lang.module.ResolvedModule;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.testing.compile.CompilationSubject.assertThat;

public class CompileExamplesTest {

Expand Down Expand Up @@ -94,14 +105,81 @@ public void compile_extensible_algebras() {
}

private static void checkCompileOf(String... exampleFiles) {
Truth.assert_()
.about(javaSources())
.that(Arrays.asList(exampleFiles)
.stream()
.map(file -> JavaFileObjects.forResource("org/derive4j/example/" + file))
.collect(Collectors.toList()))
.processedWith(new DerivingProcessor())
.compilesWithoutError();
final Compilation compilation = Compiler
.compiler(new ModulePathCompiler())
.withOptions("--release", "9")
.withProcessors(new DerivingProcessor())
.compile(Stream
.concat(Stream.of(getJavaFileObject(Paths.get("../examples/src/main/java/module-info.java")))
, Arrays
.stream(exampleFiles)
.map(file -> getJavaFileObject(Paths.get("../examples/src/main/java/org/derive4j/example/" + file))))
.collect(Collectors.toList()));

assertThat(compilation).succeeded();
}

private static JavaFileObject getJavaFileObject(Path path) {
try {
return JavaFileObjects.forResource(path.toUri().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

}

final class ModulePathCompiler implements JavaCompiler {
private final JavaCompiler compiler;
private final List<File> modulesFiles;

ModulePathCompiler() {
this.compiler = ToolProvider.getSystemJavaCompiler();

final Module module = CompileExamplesTest.class.getModule();
final Set<ResolvedModule> modules = module.getLayer().configuration().modules();
this.modulesFiles = modules
.stream()
.map(rm -> rm.reference().location())
.filter(Optional::isPresent)
.filter(ouri -> ouri.get().getScheme().startsWith("file"))
.map(ouri -> {
final String path = Paths.get(ouri.get()).toString();
return new File(path);
})
.collect(Collectors.toList());
}

@Override
public CompilationTask getTask(Writer writer, JavaFileManager javaFileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> iterable, Iterable<String> iterable1, Iterable<? extends JavaFileObject> iterable2) {
return compiler.getTask(writer, javaFileManager, diagnosticListener, iterable, iterable1, iterable2);
}

@Override
public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticListener, locale, charset);

try {
fileManager.setLocation(StandardLocation.MODULE_PATH, modulesFiles);
} catch (IOException e) {
throw new RuntimeException(e);
}

return fileManager;
}

@Override
public int isSupportedOption(String s) {
return compiler.isSupportedOption(s);
}

@Override
public int run(InputStream inputStream, OutputStream outputStream, OutputStream outputStream1, String... strings) {
return compiler.run(inputStream, outputStream, outputStream1, strings);
}

@Override
public Set<SourceVersion> getSourceVersions() {
return compiler.getSourceVersions();
}
}

0 comments on commit fa1c9cf

Please sign in to comment.