Skip to content
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

service load only once #158

Merged
merged 2 commits into from
Jan 28, 2020
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
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-158.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: fix
fix:
description: 'Only service load the formatter implementation once, rather than once
per gradle project. This avoids gradle potentially exceedding the `MaxMetaspaceSize`
and throwing `java.lang.OutOfMemoryError: Metaspace`.'
links:
- https://github.com/palantir/palantir-java-format/pull/158
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,30 @@

package com.palantir.javaformat.gradle;

import com.google.common.base.Suppliers;
import com.google.common.collect.Iterables;
import com.palantir.javaformat.java.FormatterService;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ServiceLoader;
import java.util.function.Supplier;
import org.gradle.api.artifacts.Configuration;

public class JavaFormatExtension {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 confirmed this is only registered by the PalantirJavaFormatProvider plugin, which only works on the root project:

        Preconditions.checkState(
                rootProject == rootProject.getRootProject(),
                "May only apply com.palantir.java-format-provider to the root project");

private final Configuration configuration;
private final Supplier<FormatterService> memoizedService;

public JavaFormatExtension(Configuration configuration) {
this.configuration = configuration;
this.memoizedService = Suppliers.memoize(this::serviceLoadInternal);
}

public FormatterService serviceLoad() {
return memoizedService.get();
}

private FormatterService serviceLoadInternal() {
URL[] jarUris = configuration.getFiles().stream()
.map(file -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.google.common.base.Suppliers;
import com.palantir.javaformat.gradle.JavaFormatExtension;
import com.palantir.javaformat.java.FormatterService;
import java.io.File;
Expand All @@ -23,7 +22,7 @@ private PalantirJavaFormatStep() {}
/** Creates a step which formats everything - code, import order, and unused imports. */
public static FormatterStep create(Configuration palantirJavaFormat, JavaFormatExtension extension) {
ensureImplementationNotDirectlyLoadable();
Supplier<FormatterService> memoizedService = Suppliers.memoize(extension::serviceLoad);
Supplier<FormatterService> memoizedService = extension::serviceLoad;
return FormatterStep.createLazy(
NAME, () -> new State(palantirJavaFormat.getFiles(), memoizedService), State::createFormat);
}
Expand Down