Skip to content

Commit 4076eff

Browse files
Embed the headless PSI environment in java-frontend
1 parent ab3853a commit 4076eff

14 files changed

Lines changed: 2708 additions & 0 deletions
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.CoreJavaFileManager;
20+
import com.intellij.openapi.application.ReadAction;
21+
import com.intellij.openapi.vfs.VirtualFile;
22+
import com.intellij.psi.PsiClass;
23+
import com.intellij.psi.PsiJavaFile;
24+
import com.intellij.psi.PsiJavaModule;
25+
import com.intellij.psi.PsiManager;
26+
import com.intellij.psi.PsiModifierList;
27+
import com.intellij.psi.PsiPackage;
28+
import com.intellij.psi.impl.file.PsiPackageImpl;
29+
import com.intellij.psi.search.GlobalSearchScope;
30+
import java.util.ArrayList;
31+
import java.util.Collection;
32+
import java.util.HashMap;
33+
import java.util.HashSet;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Set;
37+
import java.util.stream.Stream;
38+
39+
final class ExplicitJavaFileManager extends CoreJavaFileManager {
40+
41+
private final PsiManager manager;
42+
private final Map<String, List<PsiClass>> classes = new HashMap<>();
43+
private final JavaPsiModuleGraph moduleGraph;
44+
private final Set<String> packages = new HashSet<>();
45+
private final Map<String, PsiJavaFile> packageInfos = new HashMap<>();
46+
47+
ExplicitJavaFileManager(PsiManager manager, int javaVersion, boolean ignoreUnnamedModuleForSplitPackage) {
48+
super(manager);
49+
this.manager = manager;
50+
moduleGraph = new JavaPsiModuleGraph(javaVersion, ignoreUnnamedModuleForSplitPackage);
51+
}
52+
53+
void index(List<PsiJavaFile> files) {
54+
for (var file : files) {
55+
var name = file.getPackageName();
56+
if (file.getName().equals("package-info.java")) {
57+
packageInfos.putIfAbsent(name, file);
58+
}
59+
moduleGraph.addSourcePackage(name);
60+
while (true) {
61+
packages.add(name);
62+
if (name.isEmpty()) {
63+
break;
64+
}
65+
int separator = name.lastIndexOf('.');
66+
name = separator < 0 ? "" : name.substring(0, separator);
67+
}
68+
for (var declaration : file.getClasses()) {
69+
index(declaration);
70+
}
71+
}
72+
}
73+
74+
void indexClasspathModule(VirtualFile root) {
75+
ReadAction.run(() -> {
76+
PsiJavaModule module = null;
77+
if (root.getFileSystem() instanceof JrtVirtualFileSystem) {
78+
var descriptor = root.findChild("module-info.class");
79+
if (descriptor != null && manager.findFile(descriptor) instanceof PsiJavaFile file) {
80+
module = file.getModuleDeclaration();
81+
}
82+
}
83+
moduleGraph.addRoot(root, module);
84+
});
85+
}
86+
87+
JavaPsiModuleGraph moduleGraph() {
88+
return moduleGraph;
89+
}
90+
91+
@Override
92+
public Collection<PsiJavaModule> findModules(String name, GlobalSearchScope scope) {
93+
return moduleGraph.findModules(name, scope);
94+
}
95+
96+
private void index(PsiClass declaration) {
97+
var name = declaration.getQualifiedName();
98+
if (name != null) {
99+
classes.computeIfAbsent(name, ignored -> new ArrayList<>()).add(declaration);
100+
}
101+
for (var member : declaration.getInnerClasses()) {
102+
index(member);
103+
}
104+
}
105+
106+
@Override
107+
public PsiClass findClass(String name, GlobalSearchScope scope) {
108+
var source = sources(name, scope).findFirst();
109+
return source.orElseGet(() -> super.findClass(name, scope));
110+
}
111+
112+
@Override
113+
public PsiClass[] findClasses(String name, GlobalSearchScope scope) {
114+
return Stream.concat(sources(name, scope), Stream.of(super.findClasses(name, scope))).toArray(PsiClass[]::new);
115+
}
116+
117+
@Override
118+
public PsiPackage findPackage(String name) {
119+
var info = packageInfos.get(name);
120+
if (info != null) {
121+
return new PsiPackageImpl(manager, name) {
122+
@Override
123+
public PsiModifierList getAnnotationList() {
124+
var statement = info.getPackageStatement();
125+
return statement == null ? null : statement.getAnnotationList();
126+
}
127+
};
128+
}
129+
return packages.contains(name) ? new PsiPackageImpl(manager, name) : super.findPackage(name);
130+
}
131+
132+
private Stream<PsiClass> sources(String name, GlobalSearchScope scope) {
133+
return classes.getOrDefault(name, List.of()).stream().filter(declaration -> scope.contains(declaration.getContainingFile().getVirtualFile()));
134+
}
135+
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 java.nio.file.Path;
20+
import java.util.List;
21+
import java.util.Objects;
22+
import java.util.Set;
23+
24+
/**
25+
* Effective frontend inputs. A null runtime selects the running VM only when no
26+
* rt.jar, android.jar or jrt-fs.jar entry suppresses the implicit boot classpath.
27+
* Source strings and charsets belong to the caller's decoded snapshots.
28+
*/
29+
public record JavaPsiConfiguration(Path runtime, List<Path> classpath, int sourceVersion,
30+
boolean previewEnabled, boolean ignoreUnnamedModuleForSplitPackage) {
31+
32+
public JavaPsiConfiguration {
33+
runtime = (runtime == null ? Path.of(System.getProperty("java.home")) : runtime).toAbsolutePath().normalize();
34+
classpath = classpath.stream().map(path -> Objects.requireNonNull(path).toAbsolutePath().normalize()).toList();
35+
}
36+
37+
public List<Path> runtimeArchives() {
38+
return classpath.stream().filter(path -> Set.of("rt.jar", "android.jar", "jrt-fs.jar").contains(path.getFileName().toString())).toList();
39+
}
40+
}

0 commit comments

Comments
 (0)