|
| 1 | +/* |
| 2 | + * SonarQube Java |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.java.psi; |
| 18 | + |
| 19 | +import com.intellij.core.CoreApplicationEnvironment; |
| 20 | +import com.intellij.core.CoreJavaDirectoryService; |
| 21 | +import com.intellij.core.CorePsiPackageImplementationHelper; |
| 22 | +import com.intellij.ide.highlighter.JavaClassFileType; |
| 23 | +import com.intellij.ide.highlighter.JavaFileType; |
| 24 | +import com.intellij.java.frontback.psi.impl.syntax.JavaSyntaxDefinitionExtension; |
| 25 | +import com.intellij.lang.LanguageASTFactory; |
| 26 | +import com.intellij.lang.java.JavaLanguage; |
| 27 | +import com.intellij.lang.java.JavaParserDefinition; |
| 28 | +import com.intellij.lang.java.syntax.JavaElementTypeConverterExtension; |
| 29 | +import com.intellij.mock.MockApplication; |
| 30 | +import com.intellij.openapi.Disposable; |
| 31 | +import com.intellij.openapi.application.ApplicationManager; |
| 32 | +import com.intellij.openapi.application.ModalityState; |
| 33 | +import com.intellij.openapi.application.PathManager; |
| 34 | +import com.intellij.openapi.extensions.ExtensionPoint; |
| 35 | +import com.intellij.openapi.extensions.ExtensionsArea; |
| 36 | +import com.intellij.openapi.fileTypes.BinaryFileTypeDecompilers; |
| 37 | +import com.intellij.openapi.projectRoots.JavaVersionService; |
| 38 | +import com.intellij.openapi.util.Condition; |
| 39 | +import com.intellij.openapi.util.Disposer; |
| 40 | +import com.intellij.openapi.util.registry.Registry; |
| 41 | +import com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem; |
| 42 | +import com.intellij.platform.syntax.psi.CommonElementTypeConverterFactory; |
| 43 | +import com.intellij.platform.syntax.psi.ElementTypeConverters; |
| 44 | +import com.intellij.platform.syntax.psi.LanguageSyntaxDefinitions; |
| 45 | +import com.intellij.platform.syntax.psi.PsiSyntaxBuilderFactory; |
| 46 | +import com.intellij.platform.syntax.psi.PsiSyntaxBuilderFactoryImpl; |
| 47 | +import com.intellij.pom.java.LanguageLevel; |
| 48 | +import com.intellij.psi.ClassFileViewProviderFactory; |
| 49 | +import com.intellij.psi.FileTypeFileViewProviders; |
| 50 | +import com.intellij.psi.JavaDirectoryService; |
| 51 | +import com.intellij.psi.JavaModuleGraphHelper; |
| 52 | +import com.intellij.psi.PsiSubstitutorFactory; |
| 53 | +import com.intellij.psi.compiled.ClassFileDecompilers; |
| 54 | +import com.intellij.psi.impl.JavaClassSupersImpl; |
| 55 | +import com.intellij.psi.impl.LanguageConstantExpressionEvaluator; |
| 56 | +import com.intellij.psi.impl.PsiExpressionEvaluator; |
| 57 | +import com.intellij.psi.impl.PsiSubstitutorFactoryImpl; |
| 58 | +import com.intellij.psi.impl.compiled.ClassFileStubBuilder; |
| 59 | +import com.intellij.psi.impl.compiled.ClsDecompilerImpl; |
| 60 | +import com.intellij.psi.impl.file.PsiPackageImplementationHelper; |
| 61 | +import com.intellij.psi.impl.source.tree.JavaASTFactory; |
| 62 | +import com.intellij.psi.stubs.BinaryFileStubBuilders; |
| 63 | +import com.intellij.psi.stubs.StubElementRegistryServiceImplKt; |
| 64 | +import com.intellij.psi.util.JavaClassSupers; |
| 65 | +import java.io.IOException; |
| 66 | +import java.io.UncheckedIOException; |
| 67 | +import java.nio.file.Path; |
| 68 | +import java.util.List; |
| 69 | +import java.util.UUID; |
| 70 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 71 | +import org.w3c.dom.Element; |
| 72 | + |
| 73 | +final class JavaPsiApplication extends CoreApplicationEnvironment { |
| 74 | + |
| 75 | + private static JavaPsiApplication shared; |
| 76 | + private static int users; |
| 77 | + private static final List<String> DESCRIPTORS = List.of( |
| 78 | + "META-INF/Core.xml", "META-INF/Core.analyzer.xml", "META-INF/CoreImpl.xml", "META-INF/CoreImpl.analyzer.xml", |
| 79 | + "intellij.java.frontback.psi.xml", "intellij.java.frontback.psi.impl.xml", |
| 80 | + "intellij.java.psi.xml", "intellij.java.psi.impl.xml"); |
| 81 | + |
| 82 | + private JavaPsiApplication(Disposable owner) { |
| 83 | + super(owner, false); |
| 84 | + registerExtensionPoints(application.getExtensionArea(), false); |
| 85 | + Registry.get("javac.fresh.variables.for.captured.wildcards.only").setValue(true); |
| 86 | + Registry.get("unsound.capture.conversion.java.spec.change").setValue(false); |
| 87 | + Registry.get("java.highest.language.level").setValue(Integer.toString(LanguageLevel.HIGHEST.feature())); |
| 88 | + registerFileType(JavaClassFileType.INSTANCE, "class"); |
| 89 | + registerFileType(JavaFileType.INSTANCE, "java"); |
| 90 | + addExtension(StubElementRegistryServiceImplKt.STUB_REGISTRY_EP, new JavaPsiStubFactories()); |
| 91 | + addExplicitExtension(FileTypeFileViewProviders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileViewProviderFactory()); |
| 92 | + addExplicitExtension(BinaryFileStubBuilders.INSTANCE, JavaClassFileType.INSTANCE, new ClassFileStubBuilder()); |
| 93 | + addExplicitExtension(LanguageASTFactory.INSTANCE, JavaLanguage.INSTANCE, new JavaASTFactory()); |
| 94 | + addExplicitExtension(LanguageSyntaxDefinitions.getINSTANCE(), JavaLanguage.INSTANCE, new JavaSyntaxDefinitionExtension()); |
| 95 | + addExplicitExtension(ElementTypeConverters.getInstance(), JavaLanguage.INSTANCE, new CommonElementTypeConverterFactory()); |
| 96 | + addExplicitExtension(ElementTypeConverters.getInstance(), JavaLanguage.INSTANCE, new JavaElementTypeConverterExtension()); |
| 97 | + registerParserDefinition(new JavaParserDefinition()); |
| 98 | + addExtension(com.intellij.psi.augment.PsiAugmentProvider.EP_NAME, new com.intellij.psi.impl.RecordAugmentProvider()); |
| 99 | + addExplicitExtension(LanguageConstantExpressionEvaluator.INSTANCE, JavaLanguage.INSTANCE, new PsiExpressionEvaluator()); |
| 100 | + registerApplicationService(PsiPackageImplementationHelper.class, new CorePsiPackageImplementationHelper()); |
| 101 | + registerApplicationService(PsiSubstitutorFactory.class, new PsiSubstitutorFactoryImpl()); |
| 102 | + registerApplicationService(JavaClassSupers.class, new JavaClassSupersImpl()); |
| 103 | + registerApplicationService(JavaModuleGraphHelper.class, new JavaPsiModuleGraphHelper()); |
| 104 | + registerApplicationService(JavaDirectoryService.class, new CoreJavaDirectoryService()); |
| 105 | + registerApplicationService(JavaVersionService.class, new JavaVersionService()); |
| 106 | + registerApplicationService(PsiSyntaxBuilderFactory.class, new PsiSyntaxBuilderFactoryImpl()); |
| 107 | + registerApplicationDynamicExtensionPoint("com.intellij.psi.classFileDecompiler", ClassFileDecompilers.Decompiler.class); |
| 108 | + addExtension(ClassFileDecompilers.STATIC_EP_NAME, new ClsDecompilerImpl()); |
| 109 | + BinaryFileTypeDecompilers.getInstance(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected MockApplication createApplication(Disposable owner) { |
| 114 | + return new MockApplication(owner) { |
| 115 | + @Override |
| 116 | + public boolean isUnitTestMode() { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void invokeLater(Runnable action) { |
| 122 | + action.run(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void invokeLater(Runnable action, ModalityState state) { |
| 127 | + action.run(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void invokeLater(Runnable action, Condition<?> expired) { |
| 132 | + if (!expired.value(null)) { |
| 133 | + action.run(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void invokeLater(Runnable action, ModalityState state, Condition<?> expired) { |
| 139 | + invokeLater(action, expired); |
| 140 | + } |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + static synchronized JavaPsiApplication acquire() { |
| 145 | + var current = ApplicationManager.getApplication(); |
| 146 | + if (current != null && (shared == null || current != shared.getApplication())) { |
| 147 | + throw new IllegalStateException("Cannot replace an existing IntelliJ application"); |
| 148 | + } |
| 149 | + if (shared == null) { |
| 150 | + PathManager.setExplicitConfigPath(Path.of(System.getProperty("java.io.tmpdir"), "sonar-java-psi-" + UUID.randomUUID())); |
| 151 | + var owner = Disposer.newDisposable("SonarJava PSI application"); |
| 152 | + try { |
| 153 | + shared = new JavaPsiApplication(owner); |
| 154 | + } catch (RuntimeException | Error failure) { |
| 155 | + try { |
| 156 | + Disposer.dispose(owner); |
| 157 | + } catch (RuntimeException | Error cleanupFailure) { |
| 158 | + failure.addSuppressed(cleanupFailure); |
| 159 | + } finally { |
| 160 | + ApplicationManager.setApplication(null); |
| 161 | + } |
| 162 | + throw failure; |
| 163 | + } |
| 164 | + } |
| 165 | + users++; |
| 166 | + return shared; |
| 167 | + } |
| 168 | + |
| 169 | + static synchronized void release() { |
| 170 | + if (--users == 0) { |
| 171 | + var previous = shared; |
| 172 | + shared = null; |
| 173 | + try { |
| 174 | + ((CoreJarFileSystem) previous.getJarFileSystem()).clearHandlersCache(); |
| 175 | + Disposer.dispose(previous.getParentDisposable()); |
| 176 | + } finally { |
| 177 | + ApplicationManager.setApplication(null); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + static void registerExtensionPoints(ExtensionsArea area, boolean project) { |
| 183 | + for (var resource : DESCRIPTORS) { |
| 184 | + try (var input = JavaPsiApplication.class.getClassLoader().getResourceAsStream(resource)) { |
| 185 | + if (input == null) { |
| 186 | + throw new IllegalStateException("Missing PSI descriptor: " + resource); |
| 187 | + } |
| 188 | + var factory = DocumentBuilderFactory.newInstance(); |
| 189 | + factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 190 | + var document = factory.newDocumentBuilder().parse(input); |
| 191 | + var points = document.getElementsByTagName("extensionPoint"); |
| 192 | + for (int i = 0; i < points.getLength(); i++) { |
| 193 | + var point = (Element) points.item(i); |
| 194 | + if (project != "IDEA_PROJECT".equals(point.getAttribute("area"))) { |
| 195 | + continue; |
| 196 | + } |
| 197 | + var name = point.hasAttribute("qualifiedName") ? point.getAttribute("qualifiedName") : "com.intellij." + point.getAttribute("name"); |
| 198 | + if (!area.hasExtensionPoint(name)) { |
| 199 | + var bean = point.hasAttribute("beanClass"); |
| 200 | + area.registerExtensionPoint(name, point.getAttribute(bean ? "beanClass" : "interface"), |
| 201 | + bean ? ExtensionPoint.Kind.BEAN_CLASS : ExtensionPoint.Kind.INTERFACE); |
| 202 | + } |
| 203 | + } |
| 204 | + } catch (IOException e) { |
| 205 | + throw new UncheckedIOException(e); |
| 206 | + } catch (javax.xml.parsers.ParserConfigurationException | org.xml.sax.SAXException e) { |
| 207 | + throw new IllegalStateException("Cannot register PSI extensions", e); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments