use of com.intellij.openapi.util.NotNullLazyValue in project intellij-community by JetBrains.
the class GuavaFluentIterableConversionRule method buildCompoundDescriptor.
@Nullable
public static GuavaChainedConversionDescriptor buildCompoundDescriptor(PsiMethodCallExpression expression, PsiType to, TypeMigrationLabeler labeler) {
List<TypeConversionDescriptorBase> methodDescriptors = new SmartList<>();
NotNullLazyValue<TypeConversionRule> optionalDescriptor = new NotNullLazyValue<TypeConversionRule>() {
@NotNull
@Override
protected TypeConversionRule compute() {
for (TypeConversionRule rule : TypeConversionRule.EP_NAME.getExtensions()) {
if (rule instanceof GuavaOptionalConversionRule) {
return rule;
}
}
throw new RuntimeException("GuavaOptionalConversionRule extension is not found");
}
};
PsiMethodCallExpression current = expression;
while (true) {
final PsiMethod method = current.resolveMethod();
if (method == null) {
break;
}
final String methodName = method.getName();
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) {
break;
}
TypeConversionDescriptorBase descriptor = null;
if (FLUENT_ITERABLE.equals(containingClass.getQualifiedName())) {
descriptor = getOneMethodDescriptor(methodName, method, current.getType(), null, current);
if (descriptor == null) {
return null;
}
} else if (GuavaOptionalConversionRule.GUAVA_OPTIONAL.equals(containingClass.getQualifiedName())) {
descriptor = optionalDescriptor.getValue().findConversion(null, null, method, current.getMethodExpression(), labeler);
if (descriptor == null) {
return null;
}
}
if (descriptor == null) {
addToMigrateChainQualifier(labeler, current);
break;
}
methodDescriptors.add(descriptor);
final PsiExpression qualifier = current.getMethodExpression().getQualifierExpression();
if (qualifier instanceof PsiMethodCallExpression) {
current = (PsiMethodCallExpression) qualifier;
} else if (method.hasModifierProperty(PsiModifier.STATIC)) {
if (!CHAIN_HEAD_METHODS.contains(methodName)) {
return null;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null || !(FLUENT_ITERABLE.equals(aClass.getQualifiedName()) || GuavaOptionalConversionRule.GUAVA_OPTIONAL.equals(aClass.getQualifiedName()))) {
return null;
}
break;
} else if (qualifier instanceof PsiReferenceExpression && ((PsiReferenceExpression) qualifier).resolve() instanceof PsiVariable) {
addToMigrateChainQualifier(labeler, qualifier);
break;
} else {
return null;
}
}
return new GuavaChainedConversionDescriptor(methodDescriptors, to);
}
use of com.intellij.openapi.util.NotNullLazyValue in project intellij-community by JetBrains.
the class JavaOverrideImplementMemberChooser method create.
@Nullable
public static JavaOverrideImplementMemberChooser create(final PsiElement aClass, final boolean toImplement, final Collection<CandidateInfo> candidates, final Collection<CandidateInfo> secondary) {
final Project project = aClass.getProject();
if (candidates.isEmpty() && secondary.isEmpty())
return null;
final PsiMethodMember[] onlyPrimary = convertToMethodMembers(candidates);
final LinkedHashSet<CandidateInfo> allCandidates = new LinkedHashSet<>(candidates);
allCandidates.addAll(secondary);
final PsiMethodMember[] all = convertToMethodMembers(allCandidates);
final NotNullLazyValue<PsiMethodWithOverridingPercentMember[]> lazyElementsWithPercent = new NotNullLazyValue<PsiMethodWithOverridingPercentMember[]>() {
@NotNull
@Override
protected PsiMethodWithOverridingPercentMember[] compute() {
final PsiMethodWithOverridingPercentMember[] elements = PsiMethodWithOverridingPercentMember.calculateOverridingPercents(candidates);
Arrays.sort(elements, PsiMethodWithOverridingPercentMember.COMPARATOR);
return elements;
}
};
final boolean merge = PropertiesComponent.getInstance(project).getBoolean(PROP_COMBINED_OVERRIDE_IMPLEMENT, true);
final LanguageLevel languageLevel = PsiUtil.getLanguageLevel(aClass);
//hide option if implement interface for 1.5 language level
final boolean overrideVisible = languageLevel.isAtLeast(LanguageLevel.JDK_1_6) || languageLevel.equals(LanguageLevel.JDK_1_5) && !toImplement;
final JavaOverrideImplementMemberChooser javaOverrideImplementMemberChooser = new JavaOverrideImplementMemberChooser(all, onlyPrimary, lazyElementsWithPercent, project, overrideVisible, merge, toImplement, PropertiesComponent.getInstance(project).getBoolean(PROP_OVERRIDING_SORTED_OVERRIDE_IMPLEMENT));
javaOverrideImplementMemberChooser.setTitle(getChooserTitle(toImplement, merge));
javaOverrideImplementMemberChooser.setCopyJavadocVisible(true);
if (toImplement) {
if (onlyPrimary.length == 0) {
javaOverrideImplementMemberChooser.selectElements(new ClassMember[] { all[0] });
} else {
javaOverrideImplementMemberChooser.selectElements(onlyPrimary);
}
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (!toImplement || onlyPrimary.length == 0) {
javaOverrideImplementMemberChooser.selectElements(all);
}
javaOverrideImplementMemberChooser.close(DialogWrapper.OK_EXIT_CODE);
return javaOverrideImplementMemberChooser;
}
return javaOverrideImplementMemberChooser;
}
use of com.intellij.openapi.util.NotNullLazyValue in project intellij-community by JetBrains.
the class JavaEEGradleProjectResolverExtension method populateModuleExtraModels.
@Override
public void populateModuleExtraModels(@NotNull IdeaModule gradleModule, @NotNull final DataNode<ModuleData> ideModule) {
final WebConfiguration webConfiguration = resolverCtx.getExtraProject(gradleModule, WebConfiguration.class);
final NotNullLazyValue<DataNode<? extends ModuleData>> findTargetModuleNode = new NotNullLazyValue<DataNode<? extends ModuleData>>() {
@NotNull
@Override
protected DataNode<? extends ModuleData> compute() {
final String mainSourceSetModuleId = ideModule.getData().getId() + ":main";
DataNode<? extends ModuleData> targetModuleNode = ExternalSystemApiUtil.find(ideModule, GradleSourceSetData.KEY, node -> mainSourceSetModuleId.equals(node.getData().getId()));
if (targetModuleNode == null) {
targetModuleNode = ideModule;
}
return targetModuleNode;
}
};
if (webConfiguration != null) {
final List<War> warModels = ContainerUtil.map(webConfiguration.getWarModels(), new Function<WebConfiguration.WarModel, War>() {
@Override
public War fun(WebConfiguration.WarModel model) {
War war = new War(model.getWarName(), model.getWebAppDirName(), model.getWebAppDir());
war.setWebXml(model.getWebXml());
war.setWebResources(mapWebResources(model.getWebResources()));
war.setClasspath(model.getClasspath());
war.setManifestContent(model.getManifestContent());
war.setArchivePath(model.getArchivePath());
return war;
}
});
findTargetModuleNode.getValue().createChild(WebConfigurationModelData.KEY, new WebConfigurationModelData(GradleConstants.SYSTEM_ID, warModels));
}
final EarConfiguration earConfiguration = resolverCtx.getExtraProject(gradleModule, EarConfiguration.class);
if (earConfiguration != null) {
final List<Ear> warModels = ContainerUtil.map(earConfiguration.getEarModels(), new Function<EarConfiguration.EarModel, Ear>() {
@Override
public Ear fun(EarConfiguration.EarModel model) {
Ear ear = new Ear(model.getEarName(), model.getAppDirName(), model.getLibDirName());
ear.setManifestContent(model.getManifestContent());
ear.setDeploymentDescriptor(model.getDeploymentDescriptor());
ear.setResources(mapEarResources(model.getResources()));
ear.setArchivePath(model.getArchivePath());
return ear;
}
});
final Collection<DependencyData> deployDependencies = GradleProjectResolverUtil.getIdeDependencies(resolverCtx, findTargetModuleNode.getValue(), earConfiguration.getDeployDependencies());
final Collection<DependencyData> earlibDependencies = GradleProjectResolverUtil.getIdeDependencies(resolverCtx, findTargetModuleNode.getValue(), earConfiguration.getEarlibDependencies());
findTargetModuleNode.getValue().createChild(EarConfigurationModelData.KEY, new EarConfigurationModelData(GradleConstants.SYSTEM_ID, warModels, deployDependencies, earlibDependencies));
}
nextResolver.populateModuleExtraModels(gradleModule, ideModule);
}
use of com.intellij.openapi.util.NotNullLazyValue in project intellij-community by JetBrains.
the class BuildClasspathModuleGradleDataService method importData.
@Override
public void importData(@NotNull final Collection<DataNode<BuildScriptClasspathData>> toImport, @Nullable final ProjectData projectData, @NotNull final Project project, @NotNull final IdeModifiableModelsProvider modelsProvider) {
if (projectData == null || toImport.isEmpty()) {
return;
}
final GradleInstallationManager gradleInstallationManager = ServiceManager.getService(GradleInstallationManager.class);
ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(GradleConstants.SYSTEM_ID);
assert manager != null;
AbstractExternalSystemLocalSettings localSettings = manager.getLocalSettingsProvider().fun(project);
final String linkedExternalProjectPath = projectData.getLinkedExternalProjectPath();
final File gradleHomeDir = toImport.iterator().next().getData().getGradleHomeDir();
final GradleLocalSettings gradleLocalSettings = GradleLocalSettings.getInstance(project);
if (gradleHomeDir != null) {
gradleLocalSettings.setGradleHome(linkedExternalProjectPath, gradleHomeDir.getPath());
}
final GradleProjectSettings settings = GradleSettings.getInstance(project).getLinkedProjectSettings(linkedExternalProjectPath);
final NotNullLazyValue<Set<String>> externalProjectGradleSdkLibs = new NotNullLazyValue<Set<String>>() {
@NotNull
@Override
protected Set<String> compute() {
final Set<String> gradleSdkLibraries = ContainerUtil.newLinkedHashSet();
File gradleHome = gradleInstallationManager.getGradleHome(project, linkedExternalProjectPath);
if (gradleHome != null && gradleHome.isDirectory()) {
final Collection<File> libraries = gradleInstallationManager.getClassRoots(project, linkedExternalProjectPath);
if (libraries != null) {
for (File library : libraries) {
gradleSdkLibraries.add(FileUtil.toCanonicalPath(library.getPath()));
}
}
}
return gradleSdkLibraries;
}
};
final NotNullLazyValue<Set<String>> buildSrcProjectsRoots = new NotNullLazyValue<Set<String>>() {
@NotNull
@Override
protected Set<String> compute() {
Set<String> result = new LinkedHashSet<>();
//// add main java root of buildSrc project
result.add(linkedExternalProjectPath + "/buildSrc/src/main/java");
//// add main groovy root of buildSrc project
result.add(linkedExternalProjectPath + "/buildSrc/src/main/groovy");
for (Module module : modelsProvider.getModules(projectData)) {
final String projectPath = ExternalSystemApiUtil.getExternalProjectPath(module);
if (projectPath != null && StringUtil.startsWith(projectPath, linkedExternalProjectPath + "/buildSrc")) {
final List<String> sourceRoots = ContainerUtil.map(modelsProvider.getSourceRoots(module, false), VirtualFile::getPath);
result.addAll(sourceRoots);
}
}
return result;
}
};
final Map<String, ExternalProjectBuildClasspathPojo> localProjectBuildClasspath = ContainerUtil.newHashMap(localSettings.getProjectBuildClasspath());
for (final DataNode<BuildScriptClasspathData> node : toImport) {
if (GradleConstants.SYSTEM_ID.equals(node.getData().getOwner())) {
DataNode<ModuleData> moduleDataNode = ExternalSystemApiUtil.findParent(node, ProjectKeys.MODULE);
if (moduleDataNode == null)
continue;
String externalModulePath = moduleDataNode.getData().getLinkedExternalProjectPath();
if (settings == null || settings.getDistributionType() == null) {
LOG.warn("Gradle SDK distribution type was not configured for the project at " + linkedExternalProjectPath);
}
final Set<String> buildClasspath = ContainerUtil.newLinkedHashSet();
BuildScriptClasspathData buildScriptClasspathData = node.getData();
for (BuildScriptClasspathData.ClasspathEntry classpathEntry : buildScriptClasspathData.getClasspathEntries()) {
for (String path : classpathEntry.getSourcesFile()) {
buildClasspath.add(FileUtil.toCanonicalPath(path));
}
for (String path : classpathEntry.getClassesFile()) {
buildClasspath.add(FileUtil.toCanonicalPath(path));
}
}
ExternalProjectBuildClasspathPojo projectBuildClasspathPojo = localProjectBuildClasspath.get(linkedExternalProjectPath);
if (projectBuildClasspathPojo == null) {
projectBuildClasspathPojo = new ExternalProjectBuildClasspathPojo(moduleDataNode.getData().getExternalName(), ContainerUtil.newArrayList(), ContainerUtil.newHashMap());
localProjectBuildClasspath.put(linkedExternalProjectPath, projectBuildClasspathPojo);
}
List<String> projectBuildClasspath = ContainerUtil.newArrayList(externalProjectGradleSdkLibs.getValue());
projectBuildClasspath.addAll(buildSrcProjectsRoots.getValue());
projectBuildClasspathPojo.setProjectBuildClasspath(projectBuildClasspath);
projectBuildClasspathPojo.getModulesBuildClasspath().put(externalModulePath, new ExternalModuleBuildClasspathPojo(externalModulePath, ContainerUtil.newArrayList(buildClasspath)));
}
}
localSettings.setProjectBuildClasspath(localProjectBuildClasspath);
if (!project.isDisposed()) {
GradleBuildClasspathManager.getInstance(project).reload();
}
}
use of com.intellij.openapi.util.NotNullLazyValue in project intellij-community by JetBrains.
the class JavaClassListReferenceProvider method getReferencesByString.
@Override
@NotNull
public PsiReference[] getReferencesByString(String str, @NotNull final PsiElement position, int offsetInPosition) {
if (position instanceof XmlTag && ((XmlTag) position).getValue().getTextElements().length == 0) {
return PsiReference.EMPTY_ARRAY;
}
if (str.length() < 2) {
return PsiReference.EMPTY_ARRAY;
}
int offset = position.getTextRange().getStartOffset() + offsetInPosition;
for (PsiElement child = position.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child instanceof OuterLanguageElement && child.getTextRange().contains(offset)) {
return PsiReference.EMPTY_ARRAY;
}
}
NotNullLazyValue<Set<String>> topLevelPackages = new NotNullLazyValue<Set<String>>() {
@NotNull
@Override
protected Set<String> compute() {
final Set<String> knownTopLevelPackages = new HashSet<>();
final List<PsiElement> defaultPackages = getDefaultPackages(position.getProject());
for (final PsiElement pack : defaultPackages) {
if (pack instanceof PsiPackage) {
knownTopLevelPackages.add(((PsiPackage) pack).getName());
}
}
return knownTopLevelPackages;
}
};
final List<PsiReference> results = new ArrayList<>();
for (int dot = str.indexOf('.'); dot > 0; dot = str.indexOf('.', dot + 1)) {
int start = dot;
while (start > 0 && Character.isLetterOrDigit(str.charAt(start - 1))) start--;
if (dot == start) {
continue;
}
String candidate = str.substring(start, dot);
if (topLevelPackages.getValue().contains(candidate)) {
int end = dot;
while (end < str.length() - 1) {
end++;
char ch = str.charAt(end);
if (ch != '.' && !Character.isJavaIdentifierPart(ch)) {
break;
}
}
String s = str.substring(start, end + 1);
ContainerUtil.addAll(results, new JavaClassReferenceSet(s, position, offsetInPosition + start, false, this) {
@Override
public boolean isSoft() {
return true;
}
@Override
public boolean isAllowDollarInNames() {
return true;
}
}.getAllReferences());
ProgressManager.checkCanceled();
}
}
return ContainerUtil.toArray(results, new PsiReference[results.size()]);
}
Aggregations