Skip to content

Commit

Permalink
Fix bug involving wildcards + fix tests for module path
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Neuville committed Oct 29, 2019
1 parent df21885 commit e45d172
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 90 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ subprojects {
spotless {
java {
eclipse("4.7.2").configFile "$rootDir/etc/eclipse-format.xml"
targetExclude fileTree("$buildDir/generated") { include("**/*.java") }
indentWithSpaces(2)
trimTrailingWhitespace()
removeUnusedImports()
Expand Down Expand Up @@ -126,7 +127,7 @@ configure(subprojects) {

apply plugin: "maven"

sourceCompatibility = "1.9"
sourceCompatibility = JavaVersion.VERSION_1_9

sourceSets {
generated {
Expand Down
16 changes: 13 additions & 3 deletions examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ dependencies {
compileOnly project(":annotation")
annotationProcessor project(":processor")
annotationProcessor project(":processor-api")
annotationProcessor "org.derive4j:derive4j-fj:0.2"
annotationProcessor "org.derive4j:derive4j-fj:0.3-SNAPSHOT"
compileOnly "com.google.auto.value:auto-value-annotations:1.6.2"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"
compile "org.functionaljava:functionaljava_1.8:4.8.1"
compile 'org.functionaljava:functionaljava_1.8:4.9-SNAPSHOT'
compile 'io.atlassian.fugue:fugue:4.7.2'
compile 'com.atlassian.fugue:fugue:2.7.0'
compile 'com.google.guava:guava:26.0-jre'
compile 'io.vavr:vavr:0.9.2'
compile "org.derive4j.hkt:hkt:0.9.2"
compile "org.derive4j.hkt:hkt:0.10.0-SNAPSHOT"
compile group: 'com.oath.cyclops', name: 'cyclops', version: '10.0.2'

testCompile project(":annotation")
testCompile dependencyJunit
}

task compileOne(type: JavaCompile) {
source = sourceSets.main.java.srcDirs
include 'org/derive4j/example/Exists.java'
classpath = compileJava.classpath
destinationDir = compileJava.destinationDir
options.annotationProcessorPath = compileJava.options.annotationProcessorPath
}

configureUpload(bsd3LicenseName)

artifactoryPublish.skip = true
1 change: 1 addition & 0 deletions examples/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module derive4j.examples {
requires derive4j.annotation;
requires functionaljava;
requires org.derive4j.hkt;
}
46 changes: 46 additions & 0 deletions examples/src/main/java/org/derive4j/example/Exists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.derive4j.example;

import org.derive4j.*;
import org.derive4j.hkt.__;

@Data(flavour = Flavour.FJ, value = @Derive(inClass = "_Exists", withVisibility = Visibility.Smart, make = {
Make.constructors, Make.casesMatching, Make.caseOfMatching }))
abstract class Exists<T> implements __<Exists.µ, T> {
public enum µ {
}

interface Cases<R, T> {
R Exists(ExistsImpl<?, T> exists);

R Exists2(ExistsImpl2<T, ?, T> exists2);
}

abstract <R> R match(Cases<R, T> cases);

static final class ExistsImpl<A, B> {
final A a;
final B b;

ExistsImpl(A a, B b) {
this.a = a;
this.b = b;
}
}

static final class ExistsImpl2<A, B, C> {
final A a;
final B b;
final C c;

ExistsImpl2(A a, B b, C c) {
this.a = a;
this.b = b;
this.c = c;
}
}

@ExportAsPublic
static <T> Exists<T> now(T t) {
return _Exists.Exists(new ExistsImpl<>(t, t));
}
}
6 changes: 2 additions & 4 deletions processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ projectTitle = "Derive4J - Annotation Processor"
archivesBaseName = project.projectName

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.18') {
exclude module: 'auto-value'
}
testCompile "org.functionaljava:functionaljava:4.8.1"
testCompile "org.functionaljava:functionaljava_1.8:4.9-SNAPSHOT"
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.10.0-SNAPSHOT"
testCompile "org.derive4j:derive4j-fj:0.2"
testCompile "org.derive4j:derive4j-fj:0.3-SNAPSHOT"
testCompile group: 'com.oath.cyclops', name: 'cyclops', version: '10.0.2'

testCompileOnly dependencyAutoService
Expand Down
3 changes: 0 additions & 3 deletions processor/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public DeriveResult<DerivedCodeSpec> derive(AlgebraicDataType adt) {
private DerivedCodeSpec exportAsPublic(ExecutableElement executableElement) {
MethodSpec.Builder methodBuilder = replicate(executableElement).addModifiers(Modifier.PUBLIC);

TypeName className = ClassName.get(executableElement.getEnclosingElement().asType());
TypeName className = ClassName.get((TypeElement) executableElement.getEnclosingElement());
String methodName = executableElement.getSimpleName().toString();
String parameters = executableElement.getParameters().stream().map(ve -> ve.getSimpleName().toString()).collect(
Collectors.joining(", "));
Expand Down Expand Up @@ -108,7 +108,7 @@ private DerivedCodeSpec exportAsPublic(ExecutableElement executableElement) {
.addStatement("return _$L", methodName)
.build());
} else {
result = methodSpec(replicate(executableElement).addModifiers(Modifier.PUBLIC)
result = methodSpec(methodBuilder
.addStatement("return $L.$L($L)", className, methodName, parameters)
.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -138,7 +140,11 @@ public TypeName mapperTypeName(AlgebraicDataType adt, DataConstructor dc, TypeMi
Map<TypeVariable, TypeMirror> visitorTypeVarSubstitutions = zip(dc.deconstructor().methodType().getParameterTypes(),
dc.deconstructor().visitorMethodType().getParameterTypes())
.stream()
.flatMap(p -> deriveUtils.unify(p._2(), p._1()).get().entrySet().stream())
.flatMap(p -> deriveUtils
.unify(p._2(), p._1())
.orElseGet(Collections::emptyMap)
.entrySet()
.stream())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;

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.*;
Expand Down Expand Up @@ -106,14 +102,13 @@ public void compile_extensible_algebras() {

private static void checkCompileOf(String... exampleFiles) {
final Compilation compilation = Compiler
.compiler(new ModulePathCompiler())
.compiler(new ModulePathCompiler(CompileExamplesTest.class))
.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))))
.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();
Expand All @@ -128,58 +123,3 @@ private static JavaFileObject getJavaFileObject(Path path) {
}

}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,34 @@

import com.google.common.collect.Sets;
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.List;
import java.util.Set;
import org.junit.Test;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.util.ElementFilter;
import org.junit.Test;
import java.net.MalformedURLException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

public class DeriveUtilsImplTest {

@Test
public void allAbstractMethods_should_return_abstract_override() {

Truth.assert_()
.about(javaSource())
.that(JavaFileObjects.forSourceString("org.derive4j.processor.TestF",
"public abstract class TestF<A,B> implements com.google.common.base.Function<A,B>, java.util.function.Function<A,B> {}"))
.processedWith(new AbstractProcessor() {
public void allAbstractMethods_should_return_abstract_override() throws MalformedURLException {
final Compilation compilation = Compiler
.compiler(new ModulePathCompiler(DeriveUtilsImplTest.class))
.withOptions("--release", "9")
.withProcessors(new AbstractProcessor() {
@Override
public Set<String> getSupportedAnnotationTypes() {

Expand All @@ -65,7 +70,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return false;
}
})
.compilesWithoutError();
.compile(Stream
.of(JavaFileObjects.forResource(Paths.get("../examples/src/main/java/module-info.java").toUri().toURL()),
JavaFileObjects.forSourceString("org.derive4j.processor.TestF",
"public abstract class TestF<A,B> implements com.google.common.base.Function<A,B>, java.util.function.Function<A,B> {}"))
.collect(Collectors.toList()));

assertThat(compilation).succeeded();
}

}
Loading

0 comments on commit e45d172

Please sign in to comment.