use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.
the class InconsistentLanguageLevelInspection method runInspection.
@Override
public void runInspection(@NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemProcessor) {
final Set<Module> modules = new THashSet<>();
scope.accept(new PsiElementVisitor() {
@Override
public void visitElement(PsiElement element) {
final Module module = ModuleUtilCore.findModuleForPsiElement(element);
if (module != null) {
modules.add(module);
}
}
});
LanguageLevel projectLanguageLevel = LanguageLevelProjectExtension.getInstance(manager.getProject()).getLanguageLevel();
for (Module module : modules) {
LanguageLevel languageLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
if (languageLevel == null) {
languageLevel = projectLanguageLevel;
}
RefManager refManager = globalContext.getRefManager();
final RefModule refModule = refManager.getRefModule(module);
for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
if (!(entry instanceof ModuleOrderEntry))
continue;
final Module dependantModule = ((ModuleOrderEntry) entry).getModule();
if (dependantModule == null)
continue;
LanguageLevel dependantLanguageLevel = LanguageLevelModuleExtensionImpl.getInstance(dependantModule).getLanguageLevel();
if (dependantLanguageLevel == null) {
dependantLanguageLevel = projectLanguageLevel;
}
if (languageLevel.compareTo(dependantLanguageLevel) < 0) {
final CommonProblemDescriptor problemDescriptor = manager.createProblemDescriptor("Inconsistent language level settings: module " + module.getName() + " with language level " + languageLevel + " depends on module " + dependantModule.getName() + " with language level " + dependantLanguageLevel, new UnnecessaryModuleDependencyInspection.RemoveModuleDependencyFix(module, dependantModule), (QuickFix) QuickFixFactory.getInstance().createShowModulePropertiesFix(module));
problemProcessor.addProblemElement(refModule, problemDescriptor);
}
}
}
}
use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.
the class JavaCreateFromTemplateHandler method createClassOrInterface.
public static PsiClass createClassOrInterface(Project project, PsiDirectory directory, String content, boolean reformat, String extension) throws IncorrectOperationException {
if (extension == null)
extension = StdFileTypes.JAVA.getDefaultExtension();
final String name = "myClass" + "." + extension;
final PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(name, StdFileTypes.JAVA, content);
if (!(psiFile instanceof PsiJavaFile)) {
throw new IncorrectOperationException("This template did not produce a Java class or an interface\n" + psiFile.getText());
}
PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
final PsiClass[] classes = psiJavaFile.getClasses();
if (classes.length == 0) {
throw new IncorrectOperationException("This template did not produce a Java class or an interface\n" + psiFile.getText());
}
PsiClass createdClass = classes[0];
if (reformat) {
CodeStyleManager.getInstance(project).reformat(psiJavaFile);
}
String className = createdClass.getName();
JavaDirectoryServiceImpl.checkCreateClassOrInterface(directory, className);
final LanguageLevel ll = JavaDirectoryService.getInstance().getLanguageLevel(directory);
if (ll.compareTo(LanguageLevel.JDK_1_5) < 0) {
if (createdClass.isAnnotationType()) {
throw new IncorrectOperationException("Annotations only supported at language level 1.5 and higher");
}
if (createdClass.isEnum()) {
throw new IncorrectOperationException("Enums only supported at language level 1.5 and higher");
}
}
psiJavaFile = (PsiJavaFile) psiJavaFile.setName(className + "." + extension);
PsiElement addedElement = directory.add(psiJavaFile);
if (addedElement instanceof PsiJavaFile) {
psiJavaFile = (PsiJavaFile) addedElement;
return psiJavaFile.getClasses()[0];
} else {
PsiFile containingFile = addedElement.getContainingFile();
throw new IncorrectOperationException("Selected class file name '" + containingFile.getName() + "' mapped to not java file type '" + containingFile.getFileType().getDescription() + "'");
}
}
use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.
the class Java15APIUsageInspection method createOptionsPanel.
@Override
public JComponent createOptionsPanel() {
final JPanel panel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 5, true, false));
panel.add(new JLabel("Forbid API usages:"));
final JRadioButton projectRb = new JRadioButton("Respecting to project language level settings");
panel.add(projectRb);
final JRadioButton customRb = new JRadioButton("Higher than:");
panel.add(customRb);
final ButtonGroup gr = new ButtonGroup();
gr.add(projectRb);
gr.add(customRb);
final DefaultComboBoxModel cModel = new DefaultComboBoxModel();
for (LanguageLevel level : LanguageLevel.values()) {
//noinspection unchecked
cModel.addElement(level);
}
@SuppressWarnings("unchecked") final JComboBox llCombo = new JComboBox(cModel) {
@Override
public void setEnabled(boolean b) {
if (b == customRb.isSelected()) {
super.setEnabled(b);
}
}
};
llCombo.setSelectedItem(myEffectiveLanguageLevel != null ? myEffectiveLanguageLevel : LanguageLevel.JDK_1_3);
//noinspection unchecked
llCombo.setRenderer(new ListCellRendererWrapper<LanguageLevel>() {
@Override
public void customize(JList list, LanguageLevel value, int index, boolean selected, boolean hasFocus) {
if (value != null) {
setText(value.getPresentableText());
}
}
});
llCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myEffectiveLanguageLevel = (LanguageLevel) llCombo.getSelectedItem();
}
});
final JPanel comboPanel = new JPanel(new BorderLayout());
comboPanel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
comboPanel.add(llCombo, BorderLayout.WEST);
panel.add(comboPanel);
final ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (projectRb.isSelected()) {
myEffectiveLanguageLevel = null;
} else {
myEffectiveLanguageLevel = (LanguageLevel) llCombo.getSelectedItem();
}
UIUtil.setEnabled(comboPanel, !projectRb.isSelected(), true);
}
};
projectRb.addActionListener(actionListener);
customRb.addActionListener(actionListener);
projectRb.setSelected(myEffectiveLanguageLevel == null);
customRb.setSelected(myEffectiveLanguageLevel != null);
UIUtil.setEnabled(comboPanel, !projectRb.isSelected(), true);
return panel;
}
use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.
the class ProjectConfigurable method apply.
@Override
public void apply() throws ConfigurationException {
final CompilerProjectExtension compilerProjectExtension = CompilerProjectExtension.getInstance(myProject);
assert compilerProjectExtension != null : myProject;
if (myProjectName != null && StringUtil.isEmptyOrSpaces(myProjectName.getText())) {
throw new ConfigurationException("Please, specify project name!");
}
ApplicationManager.getApplication().runWriteAction(() -> {
// set the output path first so that handlers of RootsChanged event sent after JDK is set
// would see the updated path
String canonicalPath = myProjectCompilerOutput.getText();
if (canonicalPath != null && canonicalPath.length() > 0) {
try {
canonicalPath = FileUtil.resolveShortWindowsName(canonicalPath);
} catch (IOException e) {
//file doesn't exist yet
}
canonicalPath = FileUtil.toSystemIndependentName(canonicalPath);
compilerProjectExtension.setCompilerOutputUrl(VfsUtilCore.pathToUrl(canonicalPath));
} else {
compilerProjectExtension.setCompilerOutputPointer(null);
}
LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(myProject);
LanguageLevel level = myLanguageLevelCombo.getSelectedLevel();
if (level != null) {
extension.setLanguageLevel(level);
}
extension.setDefault(myLanguageLevelCombo.isDefault());
myProjectJdkConfigurable.apply();
if (myProjectName != null) {
((ProjectEx) myProject).setProjectName(getProjectName());
if (myDetailsComponent != null)
myDetailsComponent.setText(getBannerSlogan());
}
});
}
use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.
the class LanguageLevelCombo method reset.
public void reset(@NotNull Project project) {
removeAllItems();
for (LanguageLevel level : LanguageLevel.values()) {
addItem(level);
}
Sdk sdk = ProjectRootManagerEx.getInstanceEx(project).getProjectSdk();
sdkUpdated(sdk, project.isDefault());
LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(project);
if (extension.isDefault()) {
setSelectedItem(myDefaultItem);
} else {
setSelectedItem(extension.getLanguageLevel());
}
}
Aggregations