-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run forbidden api checks with runtimeJavaVersion #32947
Merged
alpar-t
merged 8 commits into
elastic:master
from
alpar-t:forbidden-APIs-checks-jvm-31715-part2
Aug 22, 2018
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a960c1f
Add a stand alone task for cli invocation
alpar-t e39bed9
Cut over to cli based implementation
alpar-t 668e8b1
Remove additional configuration
alpar-t 69f6248
remove implemnted todo
alpar-t 479231f
Revert "Remove additional configuration"
alpar-t 5e4506c
Revert to using configuration
alpar-t 9b15b73
Merge remote-tracking branch 'origin/master' into forbidden-APIs-chec…
alpar-t 95c1666
build comments
alpar-t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
buildSrc/src/main/java/org/elasticsearch/gradle/precommit/ForbiddenApisCliTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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. | ||
*/ | ||
package org.elasticsearch.gradle.precommit; | ||
|
||
import de.thetaphi.forbiddenapis.cli.CliMain; | ||
import org.gradle.api.Action; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.JavaVersion; | ||
import org.gradle.api.file.FileCollection; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.InputFiles; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.SkipWhenEmpty; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.process.JavaExecSpec; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class ForbiddenApisCliTask extends DefaultTask { | ||
|
||
private FileCollection signaturesFiles; | ||
private List<String> signatures = new ArrayList<>(); | ||
private Set<String> bundledSignatures = new LinkedHashSet<>(); | ||
private Set<String> suppressAnnotations = new LinkedHashSet<>(); | ||
private JavaVersion targetCompatibility; | ||
private FileCollection classesDirs; | ||
private Action<JavaExecSpec> execAction; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This avoids inheriting from |
||
|
||
public JavaVersion getTargetCompatibility() { | ||
return targetCompatibility; | ||
} | ||
|
||
public void setTargetCompatibility(JavaVersion targetCompatibility) { | ||
this.targetCompatibility = targetCompatibility; | ||
} | ||
|
||
public Action<JavaExecSpec> getExecAction() { | ||
return execAction; | ||
} | ||
|
||
public void setExecAction(Action<JavaExecSpec> execAction) { | ||
this.execAction = execAction; | ||
} | ||
|
||
@OutputFile | ||
public File getMarkerFile() { | ||
return new File( | ||
new File(getProject().getBuildDir(), "precommit"), | ||
getName() | ||
); | ||
} | ||
|
||
@InputFiles | ||
@SkipWhenEmpty | ||
public FileCollection getClassesDirs() { | ||
return classesDirs.filter(File::exists); | ||
} | ||
|
||
public void setClassesDirs(FileCollection classesDirs) { | ||
this.classesDirs = classesDirs; | ||
} | ||
|
||
@InputFiles | ||
public FileCollection getSignaturesFiles() { | ||
return signaturesFiles; | ||
} | ||
|
||
public void setSignaturesFiles(FileCollection signaturesFiles) { | ||
this.signaturesFiles = signaturesFiles; | ||
} | ||
|
||
@Input | ||
public List<String> getSignatures() { | ||
return signatures; | ||
} | ||
|
||
public void setSignatures(List<String> signatures) { | ||
this.signatures = signatures; | ||
} | ||
|
||
@Input | ||
public Set<String> getBundledSignatures() { | ||
return bundledSignatures; | ||
} | ||
|
||
public void setBundledSignatures(Set<String> bundledSignatures) { | ||
this.bundledSignatures = bundledSignatures; | ||
} | ||
|
||
@Input | ||
public Set<String> getSuppressAnnotations() { | ||
return suppressAnnotations; | ||
} | ||
|
||
public void setSuppressAnnotations(Set<String> suppressAnnotations) { | ||
this.suppressAnnotations = suppressAnnotations; | ||
} | ||
|
||
@TaskAction | ||
public void runForbiddenApisAndWriteMarker() throws IOException { | ||
getProject().javaexec((JavaExecSpec spec) -> { | ||
execAction.execute(spec); | ||
spec.setMain(CliMain.class.getName()); | ||
// build the command line | ||
getSignaturesFiles().forEach(file -> spec.args("-f", file.getAbsolutePath())); | ||
getSuppressAnnotations().forEach(annotation -> spec.args("--suppressannotation", annotation)); | ||
getBundledSignatures().forEach(bundled -> { | ||
// there's no option for target compatibility so we have to interpret it | ||
final String prefix; | ||
if (bundled.equals("jdk-system-out") || | ||
bundled.equals("jdk-reflection") || | ||
bundled.equals("jdk-non-portable")) { | ||
prefix = ""; | ||
} else { | ||
prefix = "-" + ( | ||
getTargetCompatibility().compareTo(JavaVersion.VERSION_1_9) >= 0 ? | ||
getTargetCompatibility().getMajorVersion() : | ||
"1." + getTargetCompatibility().getMajorVersion()) | ||
; | ||
} | ||
spec.args("-b", bundled + prefix); | ||
} | ||
); | ||
getClassesDirs().forEach(dir -> | ||
spec.args("-d", dir) | ||
); | ||
}); | ||
Files.write(getMarkerFile().toPath(), Collections.emptyList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we stick with the dsl of forbidden apis, instead of adding these helpers? I would like us to be able to switch back if/when it supports forking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It this is the only concern, we can add these helpers with any implementation of the task.
I don't have a strong preference, but do like how these make the build scripts shorter, without involving build resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, that is fine. Can we please name it
set
instead ofreplace
then, to match common naming with other gradle dsl elements?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjernst
set
will not work, as the task already has that method, so the extension will never be fetched and called, but even with something likesetSignatures
it would be confusing as I don't thinksignatures = ...
would work, as it won't call the closure on task extensions but look for a set method on the task instance.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge and submit a PR for the third party audit as well.
Let me know and I'll make changes to the name if needed,