Skip to content

Commit

Permalink
Add support for java 14 language features (#365)
Browse files Browse the repository at this point in the history
Add support for java 14 language features
  • Loading branch information
fawind authored Nov 24, 2020
1 parent ecf9336 commit 828e3d2
Show file tree
Hide file tree
Showing 35 changed files with 775 additions and 266 deletions.
47 changes: 23 additions & 24 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
version: 2.1
jobs:
compile:
docker: [{ image: 'circleci/openjdk:8u222-stretch-node' }]
docker: [{ image: 'circleci/openjdk:15-jdk-buster-node' }]
resource_class: large
environment:
CIRCLE_TEST_REPORTS: /home/circleci/junit
CIRCLE_ARTIFACTS: /home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-Xms6144m -Xmx6144m'
_JAVA_OPTIONS: -XX:ActiveProcessorCount=4 -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-XX:MaxMetaspaceSize=256m' -Dorg.gradle.workers.max=4
_JAVA_OPTIONS: -XX:ActiveProcessorCount=4 -Xmx1177m -XX:MaxMetaspaceSize=512m -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
steps:
- checkout
- run:
Expand Down Expand Up @@ -54,12 +54,13 @@ jobs:
paths: [ project, .gradle/init.gradle ]

check:
docker: [{ image: 'circleci/openjdk:8u222-stretch-node' }]
docker: [{ image: 'circleci/openjdk:15-jdk-buster-node' }]
resource_class: medium
environment:
CIRCLE_TEST_REPORTS: /home/circleci/junit
CIRCLE_ARTIFACTS: /home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-Xms3072m -Xmx3072m'
_JAVA_OPTIONS: -XX:ActiveProcessorCount=2 -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-XX:MaxMetaspaceSize=256m' -Dorg.gradle.workers.max=2
_JAVA_OPTIONS: -XX:ActiveProcessorCount=2 -Xmx938m -XX:MaxMetaspaceSize=512m -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
steps:
- attach_workspace: { at: /home/circleci }
- restore_cache: { key: 'gradle-wrapper-v2-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}' }
Expand All @@ -75,7 +76,7 @@ jobs:
- store_artifacts: { path: ~/artifacts }

unit-test:
docker: [{ image: 'circleci/openjdk:8u222-stretch-node' }]
docker: [{ image: 'circleci/openjdk:15-jdk-buster-node' }]
resource_class: large
environment:
CIRCLE_TEST_REPORTS: /home/circleci/junit
Expand Down Expand Up @@ -119,36 +120,44 @@ jobs:
- store_artifacts: { path: ~/artifacts }

trial-publish:
docker: [{ image: 'circleci/openjdk:8u222-stretch-node' }]
docker: [{ image: 'circleci/openjdk:15-jdk-buster-node' }]
resource_class: medium
environment:
CIRCLE_TEST_REPORTS: /home/circleci/junit
CIRCLE_ARTIFACTS: /home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-Xms3072m -Xmx3072m'
_JAVA_OPTIONS: -XX:ActiveProcessorCount=2 -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-XX:MaxMetaspaceSize=256m' -Dorg.gradle.workers.max=2
_JAVA_OPTIONS: -XX:ActiveProcessorCount=2 -Xmx938m -XX:MaxMetaspaceSize=512m -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
steps:
- attach_workspace: { at: /home/circleci }
- restore_cache: { key: 'gradle-wrapper-v2-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}' }
- restore_cache: { key: 'trial-publish-gradle-cache-v2-{{ checksum "versions.props" }}-{{ checksum "build.gradle" }}' }
- run: ./gradlew --stacktrace publishToMavenLocal
- run:
command: git status --porcelain
when: always
- save_cache:
key: 'trial-publish-gradle-cache-v2-{{ checksum "versions.props" }}-{{ checksum "build.gradle" }}'
paths: [ ~/.gradle/caches ]
- store_test_results: { path: ~/junit }
- store_artifacts: { path: ~/artifacts }

publish:
docker: [{ image: 'circleci/openjdk:8u222-stretch-node' }]
docker: [{ image: 'circleci/openjdk:15-jdk-buster-node' }]
resource_class: medium
environment:
CIRCLE_TEST_REPORTS: /home/circleci/junit
CIRCLE_ARTIFACTS: /home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-Xms3072m -Xmx3072m'
_JAVA_OPTIONS: -XX:ActiveProcessorCount=2 -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
GRADLE_OPTS: -Dorg.gradle.jvmargs='-XX:MaxMetaspaceSize=256m' -Dorg.gradle.workers.max=2
_JAVA_OPTIONS: -XX:ActiveProcessorCount=2 -Xmx938m -XX:MaxMetaspaceSize=512m -XX:ErrorFile=/home/circleci/artifacts/hs_err_pid%p.log -XX:HeapDumpPath=/home/circleci/artifacts
steps:
- attach_workspace: { at: /home/circleci }
- restore_cache: { key: 'gradle-wrapper-v2-{{ checksum "gradle/wrapper/gradle-wrapper.properties" }}' }
- restore_cache: { key: 'publish-gradle-cache-v2-{{ checksum "versions.props" }}-{{ checksum "build.gradle" }}' }
- deploy:
command: ./gradlew --parallel --stacktrace --continue publish
- run:
command: git status --porcelain
when: always
- save_cache:
key: 'publish-gradle-cache-v2-{{ checksum "versions.props" }}-{{ checksum "build.gradle" }}'
paths: [ ~/.gradle/caches ]
Expand All @@ -161,11 +170,6 @@ jobs:
- checkout
- run: /liche -d . -r . -v

circle-all:
docker: [{ image: 'circleci/openjdk:8u222-stretch-node' }]
resource_class: small
steps:
- run: {command: echo "All required jobs finished successfully"}

workflows:
version: 2
Expand All @@ -192,11 +196,6 @@ workflows:
requires: [ compile ]
filters: { branches: { ignore: develop } }

# Catch-all for all required checks
- circle-all:
requires: [ unit-test, unit-test-11, check, trial-publish ]
filters: { tags: { only: /.*/ } }

- publish:
requires: [ circle-all ]
requires: [ unit-test, unit-test-11, check, trial-publish ]
filters: { tags: { only: /.*/ }, branches: { only: develop } }
3 changes: 2 additions & 1 deletion .circleci/template.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
export CIRCLECI_TEMPLATE=java-library-oss
export PRIMARY_BRANCH=develop
export JDK=15
export UNIT_TEST_11=true
34 changes: 32 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ buildscript {

plugins {
id "org.jetbrains.intellij" version "0.4.13" apply false
id 'org.jetbrains.gradle.plugin.idea-ext' version "0.9"
}

apply plugin: 'com.palantir.git-version'
Expand All @@ -45,8 +46,8 @@ subprojects {
apply plugin: 'java-library'
apply plugin: 'org.inferred.processors'

sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceCompatibility = 11
targetCompatibility = 11

tasks.withType(Checkstyle) {
enabled = false
Expand All @@ -62,3 +63,32 @@ subprojects {
systemProperty 'recreate', System.getProperty('recreate', 'false')
}
}

/**
* There is a bunch of truly snowflaky stuff going on this project, because we have this Java14InputAstVisitor class
* which needs to implement some interface methods defined in com.sun.source.util.TreePathScanner, which varies
* depending on what JVM the formatter is running on!
*/
idea.project.ipr.withXml { xml ->
/**
* Even though the p-j-f codebase has sourceCompat=11 and targetCompat=11, we have to convince IntelliJ to let us
* compile against methods from a newer JVM like visitYield, visitSwitchExpression etc. Hence why we turn off the
* --release flag (using USE_RELEASE_OPTION below)
*/
def compilerConfiguration = xml.asNode().component.find({ it.'@name' == 'CompilerConfiguration' })
compilerConfiguration.value().add(new XmlParser().parseText('<option name="USE_RELEASE_OPTION" value="false" />'))
def module = compilerConfiguration.bytecodeTargetLevel.module.find({ it.'@name' == 'palantir-java-format' })
// Module is null on jdk11
if (module != null) {
module.attributes().target = "14"
}

xml.asNode().value().add(new XmlParser().parseText('''
<component name="JavacSettings">
<option name="PREFER_TARGET_JDK_COMPILER" value="false" />
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="palantir-java-format" options="--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" />
</option>
</component>
'''))
}
7 changes: 7 additions & 0 deletions changelog/2.0.0-rc1/pr-365.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: break
break:
description: |
* Add support for java 14 language features.
* **Break:** Increase minimum source compatibility from java 8 to java 11.
links:
- https://github.com/palantir/palantir-java-format/pull/365
9 changes: 9 additions & 0 deletions changelog/@unreleased/pr-365.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type: break
break:
description: |-
* Add support for java 14 language features.
* **Break:** Increase minimum source compatibility from java 8 to java 11.
* To use version 2.x of the formatter, you need to run on java 11 or higher.
* If you require the formatter to run on java 8, you won't be able to upgrade past version 1.x. Note that you can still produce java 8 jars from your repo but need to use at least java 11 to run the formatter.
links:
- https://github.com/palantir/palantir-java-format/pull/365
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,29 @@ class PalantirJavaFormatSpotlessPluginTest extends IntegrationTestKitSpec {
}

def validJavaFile = '''\
package test;
public class Test {
void test() {
int x = 1;
System.out.println("Hello");
Optional.of("hello").orElseGet(() -> {
return "Hello World";
});
}
package test;
public class Test {
void test() {
int x = 1;
System.out.println("Hello");
Optional.of("hello").orElseGet(() -> {
return "Hello World";
});
}
}
'''.stripIndent()

def invalidJavaFile = '''
package test;
import com.java.unused;
public class Test { void test() {int x = 1;
System.out.println(
"Hello"
);
Optional.of("hello").orElseGet(() -> {
return "Hello World";
});
} }
package test;
import com.java.unused;
public class Test { void test() {int x = 1;
System.out.println(
"Hello"
);
Optional.of("hello").orElseGet(() -> {
return "Hello World";
});
} }
'''.stripIndent()
}
1 change: 1 addition & 0 deletions gradle/javadoc.options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
30 changes: 29 additions & 1 deletion palantir-java-format/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ description = 'Palantir Java Format'
dependencies {
api project(':palantir-java-format-spi')
api 'com.google.guava:guava'
implementation 'com.google.errorprone:javac-shaded'
implementation 'org.functionaljava:functionaljava'
implementation 'com.fasterxml.jackson.core:jackson-databind' // purely for the debugger
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8'
Expand All @@ -32,6 +31,30 @@ dependencies {

tasks.withType(JavaCompile).configureEach {
options.errorprone.disable 'StrictUnusedVariable'

options.compilerArgs += [
// Allow access to internal javac apis
'--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
'--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED'
]

if (JavaVersion.current() < JavaVersion.VERSION_14) {
excludes = ['**/Java14InputAstVisitor.java']
}
}

tasks.withType(Javadoc).configureEach {
// Allow access to internal javac apis
options.optionFiles << file('../gradle/javadoc.options')

if (JavaVersion.current() < JavaVersion.VERSION_14) {
exclude '**/Java14InputAstVisitor.java'
}
}

// false positives due to org.junit.runners.* in the test cases
Expand All @@ -42,3 +65,8 @@ tasks.test {
// https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution
systemProperty 'junit.jupiter.execution.parallel.mode.default', 'concurrent'
}

// necessary to compile Java14InputAstVisitor
idea {
module.languageLevel = new org.gradle.plugins.ide.idea.model.IdeaLanguageLevel(14)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
package com.palantir.javaformat.java;

import com.google.common.collect.ImmutableList;
import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ArrayTypeTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.tree.JCTree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import org.openjdk.source.tree.AnnotatedTypeTree;
import org.openjdk.source.tree.AnnotationTree;
import org.openjdk.source.tree.ArrayTypeTree;
import org.openjdk.source.tree.Tree;
import org.openjdk.tools.javac.tree.JCTree;

/**
* Utilities for working with array dimensions.
Expand Down
Loading

0 comments on commit 828e3d2

Please sign in to comment.