|
| 1 | +/* |
| 2 | + * Copyright 2023 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.googlejavaformat.intellij; |
| 18 | + |
| 19 | +import com.google.common.base.Suppliers; |
| 20 | +import com.intellij.ide.ui.IdeUiService; |
| 21 | +import com.intellij.notification.Notification; |
| 22 | +import com.intellij.notification.NotificationType; |
| 23 | +import com.intellij.openapi.diagnostic.Logger; |
| 24 | +import com.intellij.openapi.project.Project; |
| 25 | +import java.util.function.Supplier; |
| 26 | + |
| 27 | +class JreConfigurationChecker { |
| 28 | + |
| 29 | + private final Supplier<Boolean> hasAccess = Suppliers.memoize(this::checkJreConfiguration); |
| 30 | + |
| 31 | + private final Project project; |
| 32 | + private final Logger logger = Logger.getInstance(JreConfigurationChecker.class); |
| 33 | + |
| 34 | + public JreConfigurationChecker(Project project) { |
| 35 | + this.project = project; |
| 36 | + } |
| 37 | + |
| 38 | + static boolean checkJreConfiguration(Project project) { |
| 39 | + return project.getService(JreConfigurationChecker.class).hasAccess.get(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Determine whether the JRE is configured to work with the google-java-format plugin. If not, |
| 44 | + * display a notification with instructions and return false. |
| 45 | + */ |
| 46 | + private boolean checkJreConfiguration() { |
| 47 | + try { |
| 48 | + boolean hasAccess = |
| 49 | + testClassAccess( |
| 50 | + "com.sun.tools.javac.api.JavacTrees", |
| 51 | + "com.sun.tools.javac.code.Flags", |
| 52 | + "com.sun.tools.javac.file.JavacFileManager", |
| 53 | + "com.sun.tools.javac.parser.JavacParser", |
| 54 | + "com.sun.tools.javac.tree.JCTree", |
| 55 | + "com.sun.tools.javac.util.Log"); |
| 56 | + if (!hasAccess) { |
| 57 | + displayConfigurationErrorNotification(); |
| 58 | + } |
| 59 | + return hasAccess; |
| 60 | + } catch (ClassNotFoundException e) { |
| 61 | + logger.error("Error checking jre configuration for google-java-format", e); |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private boolean testClassAccess(String... classNames) throws ClassNotFoundException { |
| 67 | + for (String className : classNames) { |
| 68 | + if (!testClassAccess(className)) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + private boolean testClassAccess(String className) throws ClassNotFoundException { |
| 76 | + Class<?> klass = Class.forName(className); |
| 77 | + return klass |
| 78 | + .getModule() |
| 79 | + // isExported returns true if the package is either open or exported. Either one is |
| 80 | + // sufficient |
| 81 | + // to run the google-java-format code (even though the documentation specifies --add-opens). |
| 82 | + .isExported(klass.getPackageName(), getClass().getClassLoader().getUnnamedModule()); |
| 83 | + } |
| 84 | + |
| 85 | + private void displayConfigurationErrorNotification() { |
| 86 | + Notification notification = |
| 87 | + new Notification( |
| 88 | + "Configure JRE for google-java-format", |
| 89 | + "Configure the JRE for google-java-format", |
| 90 | + "The google-java-format plugin needs additional configuration before it can be used. " |
| 91 | + + "<a href=\"instructions\">Follow the instructions here</a>.", |
| 92 | + NotificationType.INFORMATION); |
| 93 | + notification.setListener( |
| 94 | + (n, e) -> { |
| 95 | + IdeUiService.getInstance() |
| 96 | + .browse( |
| 97 | + "https://github.com/google/google-java-format/blob/master/README.md#intellij-jre-config"); |
| 98 | + n.expire(); |
| 99 | + }); |
| 100 | + notification.notify(project); |
| 101 | + } |
| 102 | +} |
0 commit comments