Search in sources :

Example 76 with LanguageLevel

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);
            }
        }
    }
}
Also used : RefManager(com.intellij.codeInspection.reference.RefManager) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) THashSet(gnu.trove.THashSet) RefModule(com.intellij.codeInspection.reference.RefModule) UnnecessaryModuleDependencyInspection(com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection) LanguageLevel(com.intellij.pom.java.LanguageLevel) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 77 with LanguageLevel

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() + "'");
    }
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 78 with LanguageLevel

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;
}
Also used : ActionEvent(java.awt.event.ActionEvent) ActionListener(java.awt.event.ActionListener) LanguageLevel(com.intellij.pom.java.LanguageLevel) VerticalFlowLayout(com.intellij.openapi.ui.VerticalFlowLayout)

Example 79 with LanguageLevel

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());
        }
    });
}
Also used : ProjectEx(com.intellij.openapi.project.ex.ProjectEx) ConfigurationException(com.intellij.openapi.options.ConfigurationException) LanguageLevel(com.intellij.pom.java.LanguageLevel) CompilerProjectExtension(com.intellij.openapi.roots.CompilerProjectExtension) IOException(java.io.IOException) LanguageLevelProjectExtension(com.intellij.openapi.roots.LanguageLevelProjectExtension)

Example 80 with LanguageLevel

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());
    }
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) LanguageLevelProjectExtension(com.intellij.openapi.roots.LanguageLevelProjectExtension)

Aggregations

LanguageLevel (com.intellij.pom.java.LanguageLevel)98 LanguageLevelProjectExtension (com.intellij.openapi.roots.LanguageLevelProjectExtension)21 Nullable (org.jetbrains.annotations.Nullable)14 Module (com.intellij.openapi.module.Module)13 NotNull (org.jetbrains.annotations.NotNull)9 Sdk (com.intellij.openapi.projectRoots.Sdk)8 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)7 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 File (java.io.File)6 IOException (java.io.IOException)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 LocalInspectionToolWrapper (com.intellij.codeInspection.ex.LocalInspectionToolWrapper)3 Lexer (com.intellij.lexer.Lexer)3 LanguageLevelModuleExtension (com.intellij.openapi.roots.LanguageLevelModuleExtension)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 JavaProjectData (com.intellij.externalSystem.JavaProjectData)2 DisposeAwareProjectChange (com.intellij.openapi.externalSystem.util.DisposeAwareProjectChange)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2