diff --git a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/Version.java b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/Version.java index 7cd5c7a79fb2..d68e5c13f014 100644 --- a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/Version.java +++ b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/Version.java @@ -1,9 +1,49 @@ package de.plushnikov.intellij.plugin; +import com.intellij.openapi.roots.OrderEntry; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public interface Version { String PLUGIN_NAME = "Lombok plugin"; /** * Current version of lombok plugin */ String LAST_LOMBOK_VERSION = "1.18.16"; + + + @Nullable + static String parseLombokVersion(@Nullable OrderEntry orderEntry) { + String result = null; + if (orderEntry != null) { + final String presentableName = orderEntry.getPresentableName(); + final Matcher matcher = Pattern.compile("(.*:)([\\d.]+)(.*)").matcher(presentableName); + if (matcher.find()) { + result = matcher.group(2); + } + } + return result; + } + + static int compareVersionString(@NotNull String firstVersionOne, @NotNull String secondVersion) { + String[] firstVersionParts = firstVersionOne.split("\\."); + String[] secondVersionParts = secondVersion.split("\\."); + int length = Math.max(firstVersionParts.length, secondVersionParts.length); + for (int i = 0; i < length; i++) { + int firstPart = i < firstVersionParts.length && !firstVersionParts[i].isEmpty() ? + Integer.parseInt(firstVersionParts[i]) : 0; + int secondPart = i < secondVersionParts.length && !secondVersionParts[i].isEmpty() ? + Integer.parseInt(secondVersionParts[i]) : 0; + if (firstPart < secondPart) { + return -1; + } + if (firstPart > secondPart) { + return 1; + } + } + return 0; + } } diff --git a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivity.java b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivity.java index d7487ee1c03c..8ef0b5c01aba 100644 --- a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivity.java +++ b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivity.java @@ -32,8 +32,6 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jps.model.java.compiler.AnnotationProcessingConfiguration; import javax.swing.event.HyperlinkEvent; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Shows notifications about project setup issues, that make the plugin not working. @@ -41,9 +39,6 @@ import java.util.regex.Pattern; * @author Alexej Kubarev */ public class LombokProjectValidatorActivity implements StartupActivity.DumbAware { - - private static final Pattern LOMBOK_VERSION_PATTERN = Pattern.compile("(.*:)([\\d.]+)(.*)"); - @Override public void runActivity(@NotNull Project project) { // If plugin is not enabled - no point to continue @@ -67,9 +62,9 @@ public class LombokProjectValidatorActivity implements StartupActivity.DumbAware if (hasLombokLibrary && ProjectSettings.isEnabled(project, ProjectSettings.IS_LOMBOK_VERSION_CHECK_ENABLED, false)) { final ModuleManager moduleManager = ModuleManager.getInstance(project); for (Module module : moduleManager.getModules()) { - String lombokVersion = parseLombokVersion(findLombokEntry(ModuleRootManager.getInstance(module))); + String lombokVersion = Version.parseLombokVersion(findLombokEntry(ModuleRootManager.getInstance(module))); - if (null != lombokVersion && compareVersionString(lombokVersion, Version.LAST_LOMBOK_VERSION) < 0) { + if (null != lombokVersion && Version.compareVersionString(lombokVersion, Version.LAST_LOMBOK_VERSION) < 0) { return getNotificationGroup().createNotification(LombokBundle.message("config.warn.dependency.outdated.title"), LombokBundle .message("config.warn.dependency.outdated.message", project.getName(), @@ -156,36 +151,4 @@ public class LombokProjectValidatorActivity implements StartupActivity.DumbAware } return null; } - - @Nullable - String parseLombokVersion(@Nullable OrderEntry orderEntry) { - String result = null; - if (null != orderEntry) { - final String presentableName = orderEntry.getPresentableName(); - final Matcher matcher = LOMBOK_VERSION_PATTERN.matcher(presentableName); - if (matcher.find()) { - result = matcher.group(2); - } - } - return result; - } - - int compareVersionString(@NotNull String firstVersionOne, @NotNull String secondVersion) { - String[] firstVersionParts = firstVersionOne.split("\\."); - String[] secondVersionParts = secondVersion.split("\\."); - int length = Math.max(firstVersionParts.length, secondVersionParts.length); - for (int i = 0; i < length; i++) { - int firstPart = i < firstVersionParts.length && !firstVersionParts[i].isEmpty() ? - Integer.parseInt(firstVersionParts[i]) : 0; - int secondPart = i < secondVersionParts.length && !secondVersionParts[i].isEmpty() ? - Integer.parseInt(secondVersionParts[i]) : 0; - if (firstPart < secondPart) { - return -1; - } - if (firstPart > secondPart) { - return 1; - } - } - return 0; - } } diff --git a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/jps/LombokBuildProcessParametersProvider.java b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/jps/LombokBuildProcessParametersProvider.java new file mode 100644 index 000000000000..be2b80ee6d11 --- /dev/null +++ b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/jps/LombokBuildProcessParametersProvider.java @@ -0,0 +1,63 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package de.plushnikov.intellij.plugin.jps; + +import com.intellij.compiler.server.BuildProcessParametersProvider; +import com.intellij.openapi.application.ReadAction; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.OrderEntry; +import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.JavaPsiFacade; +import com.intellij.psi.PsiClass; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.util.CachedValueProvider; +import com.intellij.psi.util.CachedValuesManager; +import com.intellij.psi.util.PsiUtilCore; +import de.plushnikov.intellij.plugin.LombokClassNames; +import de.plushnikov.intellij.plugin.Version; +import de.plushnikov.intellij.plugin.activity.LombokProjectValidatorActivity; +import de.plushnikov.intellij.plugin.settings.ProjectSettings; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.List; + +public final class LombokBuildProcessParametersProvider extends BuildProcessParametersProvider { + + private final Project myProject; + + public LombokBuildProcessParametersProvider(Project project) { + myProject = project; + } + + @Override + public @NotNull List getVMArguments() { + if (ProjectSettings.isLombokEnabledInProject(myProject) && LombokProjectValidatorActivity.hasLombokLibrary(myProject)) { + return CachedValuesManager.getManager(myProject).getCachedValue(myProject, + () -> { + List disableOptions = + ReadAction.compute(() -> disableOptimisation()) + ? Collections.singletonList("-Djps.track.ap.dependencies=false") + : Collections.emptyList(); + return new CachedValueProvider.Result<>(disableOptions, ProjectRootManager.getInstance(myProject)); + }); + + } + return super.getVMArguments(); + } + + private boolean disableOptimisation() { + PsiClass aClass = JavaPsiFacade.getInstance(myProject).findClass(LombokClassNames.BUILDER, GlobalSearchScope.allScope(myProject)); + VirtualFile virtualFile = PsiUtilCore.getVirtualFile(aClass); + if (virtualFile != null) { + List entries = ProjectRootManager.getInstance(myProject).getFileIndex().getOrderEntriesForFile(virtualFile); + if (!entries.isEmpty()) { + String lombokVersion = Version.parseLombokVersion(entries.get(0)); + if (lombokVersion != null && Version.compareVersionString(lombokVersion, "1.18.16") < 0) { + return true; + } + } + } + return false; + } +} diff --git a/plugins/lombok/src/main/resources/META-INF/plugin.xml b/plugins/lombok/src/main/resources/META-INF/plugin.xml index 6f287a5fa7c1..119bca052481 100644 --- a/plugins/lombok/src/main/resources/META-INF/plugin.xml +++ b/plugins/lombok/src/main/resources/META-INF/plugin.xml @@ -256,6 +256,7 @@ + diff --git a/plugins/lombok/src/test/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivityTest.java b/plugins/lombok/src/test/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivityTest.java index 3c73b8c08695..a41e3d6829fa 100644 --- a/plugins/lombok/src/test/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivityTest.java +++ b/plugins/lombok/src/test/java/de/plushnikov/intellij/plugin/activity/LombokProjectValidatorActivityTest.java @@ -1,6 +1,7 @@ package de.plushnikov.intellij.plugin.activity; import com.intellij.openapi.roots.OrderEntry; +import de.plushnikov.intellij.plugin.Version; import org.junit.Before; import org.junit.Test; @@ -11,65 +12,63 @@ import static org.mockito.Mockito.when; public class LombokProjectValidatorActivityTest { - private LombokProjectValidatorActivity component; private OrderEntry orderEntry; @Before public void setUp() { - component = new LombokProjectValidatorActivity(); orderEntry = mock(OrderEntry.class); } @Test public void parseLombokVersionFromGradle() { when(orderEntry.getPresentableName()).thenReturn("Gradle: org.projectlombok:lombok:1.16.8"); - assertEquals("1.16.8", component.parseLombokVersion(orderEntry)); + assertEquals("1.16.8", Version.parseLombokVersion(orderEntry)); } @Test public void parseLombokVersionFromMaven() { when(orderEntry.getPresentableName()).thenReturn("Maven: org.projectlombok:lombok:1.16.6"); - assertEquals("1.16.6", component.parseLombokVersion(orderEntry)); + assertEquals("1.16.6", Version.parseLombokVersion(orderEntry)); } @Test public void parseLombokVersionFromUnknown() { when(orderEntry.getPresentableName()).thenReturn("lombok"); - assertNull(component.parseLombokVersion(orderEntry)); + assertNull(Version.parseLombokVersion(orderEntry)); } @Test public void compareVersionString1_2() { - assertEquals(-1, component.compareVersionString("1", "2")); + assertEquals(-1, Version.compareVersionString("1", "2")); } @Test public void compareVersionString__2() { - assertEquals(-1, component.compareVersionString("", "2")); + assertEquals(-1, Version.compareVersionString("", "2")); } @Test public void compareVersionString123_121() { - assertEquals(1, component.compareVersionString("1.2.3", "1.2.1")); + assertEquals(1, Version.compareVersionString("1.2.3", "1.2.1")); } @Test public void compareVersionString1166_1168() { - assertEquals(-1, component.compareVersionString("1.16.6", "1.16.8")); + assertEquals(-1, Version.compareVersionString("1.16.6", "1.16.8")); } @Test public void compareVersionString1168_1168() { - assertEquals(0, component.compareVersionString("1.16.8", "1.16.8")); + assertEquals(0, Version.compareVersionString("1.16.8", "1.16.8")); } @Test public void compareVersionString0102_1168() { - assertEquals(-1, component.compareVersionString("0.10.2", "1.16.8")); + assertEquals(-1, Version.compareVersionString("0.10.2", "1.16.8")); } @Test public void compareVersionString1169_1168() { - assertEquals(1, component.compareVersionString("1.16.9", "1.16.8")); + assertEquals(1, Version.compareVersionString("1.16.9", "1.16.8")); } }