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

Get vanilla tests passing in Java 9 #210

Merged
merged 3 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# sudo/dist/group must be required/trusty/edge to access oraclejdk9 (2016-11-14)
sudo: required
dist: trusty
group: edge

language: java
install: ./gradlew clean jar
script: ./gradlew check
jdk:
- oraclejdk8

install: ./gradlew clean jar
script: ./gradlew check

env:
global:
- JAVA_9_HOME=$(jdk_switcher home oraclejdk9)
- PGP_KEY_ID="2887CE9B"
- PGP_PASSWORD=""
- PGP_SECRET_KEY_RING_FILE=~/.gnupg/secring.gpg
Expand Down
29 changes: 28 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ task shadowTest {
}
}

//// Java 9 support (for tests) //////////////////////////////////
if (!hasProperty('java9Home') && System.env.containsKey('JAVA_9_HOME')) {
project.ext.java9Home = System.env['JAVA_9_HOME']
}
assert hasProperty('java9Home') : "Set the property 'java9Home' in your your gradle.properties pointing to a Java 9 installation"
def java9Path = new File(java9Home, 'bin')
def java9 = [:].withDefault { execName ->
def executable = new File(java9Path, execName)
assert executable.exists() : "There is no ${execName} executable in ${java9Path}"
executable
}

afterEvaluate {
tasks.withType(AbstractCompile) {
if (targetCompatibility == "1.9") {
options.with {
fork = true
forkOptions.executable = java9.javac
}
}
}
}

//// Vanilla integration tests ///////////////////////////////////
configurations {
vanillaCompile
Expand All @@ -206,7 +229,7 @@ task vanillaTest {
check.dependsOn it
}

(6..8).each { jdk ->
(6..9).each { jdk ->
def sourceSet = sourceSets.create("vanilla${jdk}") {
java {
srcDir file('src/it/vanilla/src/main/java')
Expand Down Expand Up @@ -243,6 +266,10 @@ task vanillaTest {
vanillaTest.dependsOn it
testClassesDir = sourceSets["vanilla${jdk}Test"].output.classesDir
classpath = sourceSets["vanilla${jdk}Test"].runtimeClasspath
if (jdk == 9) {
executable java9.java
scanForTestClasses = false
}
reports {
html {
destination "$reportsDir/vanilla${jdk}"
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat May 07 11:46:53 BST 2016
#Mon Nov 14 21:56:03 GMT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2-bin.zip
23 changes: 15 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

##############################################################################
##
Expand Down Expand Up @@ -154,11 +154,18 @@ if $cygwin ; then
esac
fi

# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
# Escape application args
for s in "${@}" ; do
s=\"$s\"
APP_ARGS=$APP_ARGS" "$s
done

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- "$DEFAULT_JVM_OPTS" "$JAVA_OPTS" "$GRADLE_OPTS" "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
exec "$JAVACMD" "$@"
6 changes: 0 additions & 6 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ goto fail
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args

:win9xME_args
@rem Slurp the command line arguments.
Expand All @@ -60,11 +59,6 @@ set _SKIP=2
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*
goto execute

:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$

:execute
@rem Setup the command line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
import java.util.SortedSet;
import java.util.TreeSet;

import javax.annotation.Generated;

/**
* Code generation for the @{@link FreeBuilder} annotation.
*/
Expand Down Expand Up @@ -96,7 +94,7 @@ private void addBuilderTypeDeclaration(SourceBuilder code, Metadata metadata) {
.addLine(" * Auto-generated superclass of %s,", metadata.getBuilder().javadocLink())
.addLine(" * derived from the API of %s.", metadata.getType().javadocLink())
.addLine(" */")
.addLine("@%s(\"%s\")", Generated.class, this.getClass().getName());
.add(Excerpts.generated(getClass()));
for (Excerpt annotation : metadata.getGeneratedBuilderAnnotations()) {
code.add(annotation);
}
Expand Down Expand Up @@ -744,7 +742,7 @@ private void writeStubSource(SourceBuilder code, Metadata metadata) {
.addLine(" * Placeholder. Create {@code %s.Builder} and subclass this type.",
metadata.getType())
.addLine(" */")
.addLine("@%s(\"%s\")", Generated.class, this.getClass().getName())
.add(Excerpts.generated(getClass()))
.addLine("abstract class %s {}", metadata.getGeneratedBuilder().declaration());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.google.common.collect.ImmutableList;

import org.inferred.freebuilder.processor.util.feature.JavaxPackage;

import java.util.List;

import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -103,6 +105,35 @@ public static Excerpt forEach(TypeMirror elementType, String iterable, String me
return new ForEachExcerpt(elementType, iterable, method);
}

private static final class GeneratedAnnotationExcerpt extends Excerpt {
private final Class<?> generator;

GeneratedAnnotationExcerpt(Class<?> generator) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably have a test that the Generated annotation doesn't show up in JDK9. Or, is that way more work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impossible, really—we don't run unit tests in JDK 9.

this.generator = generator;
}

@Override
public void addTo(SourceBuilder code) {
QualifiedName generated = code.feature(JavaxPackage.JAVAX).generated().orNull();
if (generated != null) {
code.addLine("@%s(\"%s\")", generated, generator.getName());
}
}

@Override
protected void addFields(FieldReceiver fields) {
fields.add("generator", generator);
}
}

/**
* Returns an excerpt of the {@link javax.annotation.Generated} annotation, if available,
* with value set to the full name of the {@code generator} class as recommended.
*/
public static Excerpt generated(Class<?> generator) {
return new GeneratedAnnotationExcerpt(generator);
}

private static final class JoiningExcerpt extends Excerpt {
private final String separator;
private final List<?> excerpts;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.inferred.freebuilder.processor.util.feature;

import com.google.common.base.Optional;

import org.inferred.freebuilder.processor.util.QualifiedName;
import org.inferred.freebuilder.processor.util.SourceBuilder;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.util.Elements;

/**
* Types in the javax package, if available. Defaults to {@link #AVAILABLE} in tests.
*/
public enum JavaxPackage implements Feature<JavaxPackage> {

AVAILABLE("javax"), UNAVAILABLE("No javax");

/**
* Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current status of
* {@link JavaxPackage}.
*/
public static final FeatureType<JavaxPackage> JAVAX =
new FeatureType<JavaxPackage>() {

@Override
protected JavaxPackage testDefault() {
return AVAILABLE;
}

@Override
protected JavaxPackage forEnvironment(ProcessingEnvironment env) {
return hasType(env.getElementUtils(), GENERATED) ? AVAILABLE : UNAVAILABLE;
}
};

private static final QualifiedName GENERATED = QualifiedName.of("javax.annotation", "Generated");

private final String humanReadableFormat;

JavaxPackage(String humanReadableFormat) {
this.humanReadableFormat = humanReadableFormat;
}

/**
* Parameterized type for {@code java.util.function.Consumer<T>}, if available.
*/
public Optional<QualifiedName> generated() {
return ifAvailable(GENERATED);
}

@Override
public String toString() {
return humanReadableFormat;
}

private static boolean hasType(Elements elements, QualifiedName type) {
return elements.getTypeElement(type.toString()) != null;
}

private <T> Optional<T> ifAvailable(T value) {
return (this == AVAILABLE) ? Optional.of(value) : Optional.<T>absent();
}
}