diff --git a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ClasspathEditor.java b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ClasspathEditor.java index 43aeb9aee1af..980d23ae8c1f 100644 --- a/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ClasspathEditor.java +++ b/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ClasspathEditor.java @@ -30,26 +30,26 @@ import com.intellij.openapi.roots.impl.storage.ClasspathStorageProvider; import com.intellij.openapi.roots.ui.configuration.classpath.ClasspathPanelImpl; import com.intellij.openapi.ui.ComboBox; import com.intellij.openapi.util.Disposer; +import com.intellij.ui.ListCellRendererWrapper; +import com.intellij.util.ArrayUtil; import com.intellij.util.ui.JBUI; -import gnu.trove.THashMap; import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.*; +import java.util.LinkedHashMap; import java.util.Map; public class ClasspathEditor extends ModuleElementsEditor implements ModuleRootListener { public static final String NAME = ProjectBundle.message("modules.classpath.title"); private ClasspathPanelImpl myPanel; - private ClasspathFormatPanel myClasspathFormatPanel; public ClasspathEditor(final ModuleConfigurationState state) { super(state); final Disposable disposable = Disposer.newDisposable(); - state.getProject().getMessageBus().connect(disposable).subscribe(ProjectTopics.PROJECT_ROOTS, this); registerDisposable(disposable); } @@ -85,10 +85,7 @@ public class ClasspathEditor extends ModuleElementsEditor implements ModuleRootL public void canApply() throws ConfigurationException { super.canApply(); if (myClasspathFormatPanel != null) { - ClasspathStorageProvider provider = ClasspathStorage.getProvider(myClasspathFormatPanel.getSelectedClasspathFormat()); - if (provider != null) { - provider.assertCompatible(getModel()); - } + myClasspathFormatPanel.canApply(); } } @@ -112,7 +109,7 @@ public class ClasspathEditor extends ModuleElementsEditor implements ModuleRootL ClasspathStorageProvider[] providers = ClasspathStorageProvider.EXTENSION_POINT_NAME.getExtensions(); if (providers.length > 0) { - myClasspathFormatPanel = new ClasspathFormatPanel(providers); + myClasspathFormatPanel = new ClasspathFormatPanel(providers, getModel()); panel.add(myClasspathFormatPanel, BorderLayout.SOUTH); } @@ -151,58 +148,56 @@ public class ClasspathEditor extends ModuleElementsEditor implements ModuleRootL } } - private class ClasspathFormatPanel extends JPanel { - private final JComboBox comboBoxClasspathFormat; + private static class ClasspathFormatPanel extends JPanel { + private final ModifiableRootModel rootModel; + private final JComboBox comboBoxClasspathFormat; - private final Map formatIdToDescription = new THashMap<>(); - - private ClasspathFormatPanel(@NotNull ClasspathStorageProvider[] providers) { + private ClasspathFormatPanel(ClasspathStorageProvider[] providers, ModifiableRootModel model) { super(new GridBagLayout()); + rootModel = model; add(new JLabel(ProjectBundle.message("project.roots.classpath.format.label")), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, JBUI.insets(10, 6, 6, 0), 0, 0)); + Map formatIdToDescription = new LinkedHashMap<>(); formatIdToDescription.put(ClassPathStorageUtil.DEFAULT_STORAGE, ProjectBundle.message("project.roots.classpath.format.default.descr")); for (ClasspathStorageProvider provider : providers) { formatIdToDescription.put(provider.getID(), provider.getDescription()); } - - comboBoxClasspathFormat = new ComboBox(formatIdToDescription.values().toArray()); - updateClasspathFormat(); + comboBoxClasspathFormat = new ComboBox<>(ArrayUtil.toStringArray(formatIdToDescription.keySet())); + comboBoxClasspathFormat.setRenderer(new ListCellRendererWrapper() { + @Override + public void customize(JList list, String value, int index, boolean selected, boolean hasFocus) { + setText(formatIdToDescription.get(value)); + } + }); + comboBoxClasspathFormat.setSelectedItem(getModuleClasspathFormat()); add(comboBoxClasspathFormat, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, JBUI.insets(6, 6, 6, 0), 0, 0)); } - private void updateClasspathFormat() { - comboBoxClasspathFormat.setSelectedItem(formatIdToDescription.get(getModuleClasspathFormat())); - } - private String getSelectedClasspathFormat() { - final String selected = (String)comboBoxClasspathFormat.getSelectedItem(); - for ( Map.Entry entry : formatIdToDescription.entrySet() ) { - if ( entry.getValue().equals(selected)) { - return entry.getKey(); - } - } - throw new IllegalStateException(selected); + return (String)comboBoxClasspathFormat.getSelectedItem(); } - @NotNull private String getModuleClasspathFormat() { - return ClassPathStorageUtil.getStorageType(getModel().getModule()); + return ClassPathStorageUtil.getStorageType(rootModel.getModule()); } - boolean isModified() { - return comboBoxClasspathFormat != null && !getSelectedClasspathFormat().equals(getModuleClasspathFormat()); + private boolean isModified() { + return !getSelectedClasspathFormat().equals(getModuleClasspathFormat()); } - void apply() throws ConfigurationException { - final String storageID = getSelectedClasspathFormat(); - ClasspathStorageProvider provider = ClasspathStorage.getProvider(storageID); + public void canApply() throws ConfigurationException { + ClasspathStorageProvider provider = ClasspathStorage.getProvider(getSelectedClasspathFormat()); if (provider != null) { - provider.assertCompatible(getModel()); + provider.assertCompatible(rootModel); } - ClasspathStorage.setStorageType(getModel(), storageID); + } + + private void apply() throws ConfigurationException { + canApply(); + ClasspathStorage.setStorageType(rootModel, getSelectedClasspathFormat()); } } -} +} \ No newline at end of file diff --git a/java/java-psi-api/src/com/intellij/psi/PsiResolveHelper.java b/java/java-psi-api/src/com/intellij/psi/PsiResolveHelper.java index c35930778906..480fad3e8185 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiResolveHelper.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiResolveHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -173,5 +173,4 @@ public interface PsiResolveHelper { @NotNull LanguageLevel getEffectiveLanguageLevel(@Nullable VirtualFile virtualFile); - -} +} \ No newline at end of file diff --git a/java/java-psi-api/src/com/intellij/psi/infos/CandidateInfo.java b/java/java-psi-api/src/com/intellij/psi/infos/CandidateInfo.java index fb4bbb3cf4c9..e714a144caa5 100644 --- a/java/java-psi-api/src/com/intellij/psi/infos/CandidateInfo.java +++ b/java/java-psi-api/src/com/intellij/psi/infos/CandidateInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,15 +26,17 @@ import org.jetbrains.annotations.NotNull; */ public class CandidateInfo implements JavaResolveResult { public static final CandidateInfo[] EMPTY_ARRAY = new CandidateInfo[0]; + public static final JavaResolveResult[] RESOLVE_RESULT_FOR_PACKAGE_PREFIX_PACKAGE = + {new CandidateInfo(PsiUtilCore.NULL_PSI_ELEMENT, PsiSubstitutor.EMPTY, Boolean.TRUE, false, null, null, null, true)}; private final PsiElement myPlace; private final PsiClass myAccessClass; - @NotNull private final PsiElement myCandidate; + private final PsiElement myCandidate; private final boolean myStaticsProblem; protected final PsiSubstitutor mySubstitutor; private final PsiElement myCurrentFileResolveContext; private final boolean myPackagePrefixPackageReference; - private Boolean myAccessible; // benign datarace + private Boolean myAccessible; // benign data race private CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor, @@ -53,11 +55,12 @@ public class CandidateInfo implements JavaResolveResult { myPlace = place; myPackagePrefixPackageReference = packagePrefixPackageReference; } + public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor, boolean accessProblem, boolean staticsProblem, PsiElement currFileContext) { this(candidate, substitutor, !accessProblem, staticsProblem, currFileContext, null, null, false); } - public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor, boolean accessProblem, boolean staticsProblem){ + public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor, boolean accessProblem, boolean staticsProblem) { this(candidate, substitutor, accessProblem, staticsProblem, null); } @@ -66,25 +69,24 @@ public class CandidateInfo implements JavaResolveResult { PsiElement place, PsiClass accessClass, boolean staticsProblem, - PsiElement currFileContext){ + PsiElement currFileContext) { this(candidate, substitutor, null, staticsProblem, currFileContext, place, accessClass, false); } - public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor, PsiElement place, boolean staticsProblem){ + public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor, PsiElement place, boolean staticsProblem) { this(candidate, substitutor, place, null, staticsProblem, null); } - public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor){ + public CandidateInfo(@NotNull PsiElement candidate, @NotNull PsiSubstitutor substitutor) { this(candidate, substitutor, null, null, false, null); } - public CandidateInfo(@NotNull CandidateInfo candidate, @NotNull PsiSubstitutor newSubstitutor){ - this(candidate.myCandidate, newSubstitutor, candidate.myAccessible, candidate.myStaticsProblem, candidate.myCurrentFileResolveContext, candidate.myPlace, - null, false); + public CandidateInfo(@NotNull CandidateInfo ci, @NotNull PsiSubstitutor newSubstitutor) { + this(ci.myCandidate, newSubstitutor, ci.myAccessible, ci.myStaticsProblem, ci.myCurrentFileResolveContext, ci.myPlace, null, false); } @Override - public boolean isValidResult(){ + public boolean isValidResult() { return isAccessible() && isStaticsScopeCorrect(); } @@ -93,36 +95,44 @@ public class CandidateInfo implements JavaResolveResult { return myPackagePrefixPackageReference; } - @Override @NotNull - public PsiElement getElement(){ + @Override + public PsiElement getElement() { return myCandidate; } @NotNull @Override - public PsiSubstitutor getSubstitutor(){ + public PsiSubstitutor getSubstitutor() { return mySubstitutor; } @Override public boolean isAccessible() { - Boolean Accessible = myAccessible; - boolean accessible = true; - if(Accessible == null) { - if (myPlace != null && myCandidate instanceof PsiMember) { - final PsiMember member = (PsiMember)myCandidate; - accessible = JavaPsiFacade.getInstance(myPlace.getProject()).getResolveHelper() - .isAccessible(member, member.getModifierList(), myPlace, myAccessClass, myCurrentFileResolveContext); - if (accessible && member.hasModifierProperty(PsiModifier.PRIVATE) && myPlace instanceof PsiReferenceExpression && JavaVersionService.getInstance().isAtLeast(myPlace, JavaSdkVersion.JDK_1_7)) { - accessible = !isAccessedThroughTypeParameterBound(); + Boolean accessible = myAccessible; + + if (accessible == null) { + accessible = true; + + if (myPlace != null) { + PsiResolveHelper helper = JavaPsiFacade.getInstance(myPlace.getProject()).getResolveHelper(); + + if (myCandidate instanceof PsiMember) { + PsiMember member = (PsiMember)myCandidate; + accessible = helper.isAccessible(member, member.getModifierList(), myPlace, myAccessClass, myCurrentFileResolveContext); + + if (accessible && + member.hasModifierProperty(PsiModifier.PRIVATE) && + myPlace instanceof PsiReferenceExpression && + JavaVersionService.getInstance().isAtLeast(myPlace, JavaSdkVersion.JDK_1_7)) { + accessible = !isAccessedThroughTypeParameterBound(); + } } } + myAccessible = accessible; } - else { - accessible = Accessible; - } + return accessible; } @@ -143,7 +153,7 @@ public class CandidateInfo implements JavaResolveResult { } @Override - public boolean isStaticsScopeCorrect(){ + public boolean isStaticsScopeCorrect() { return !myStaticsProblem; } @@ -152,6 +162,7 @@ public class CandidateInfo implements JavaResolveResult { return myCurrentFileResolveContext; } + @Override public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; @@ -172,7 +183,7 @@ public class CandidateInfo implements JavaResolveResult { return mySubstitutor.equals(that.mySubstitutor); } - + @Override public int hashCode() { int result = myPlace != null ? myPlace.hashCode() : 0; result = 31 * result + (myAccessClass != null ? myAccessClass.hashCode() : 0); @@ -184,8 +195,4 @@ public class CandidateInfo implements JavaResolveResult { result = 31 * result + (myPackagePrefixPackageReference ? 1 : 0); return result; } - - @NotNull - public static final JavaResolveResult[] RESOLVE_RESULT_FOR_PACKAGE_PREFIX_PACKAGE = - {new CandidateInfo(PsiUtilCore.NULL_PSI_ELEMENT, PsiSubstitutor.EMPTY, Boolean.TRUE, false, null, null, null, true)}; -} +} \ No newline at end of file