Search in sources :

Example 41 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class JavaTreeGenerator method generateTreeFor.

@Override
@Nullable
public TreeElement generateTreeFor(@NotNull PsiElement original, @NotNull final CharTable table, @NotNull final PsiManager manager) {
    if (original instanceof PsiKeyword || original instanceof PsiIdentifier) {
        final String text = original.getText();
        return createLeafFromText(text, table, manager, original, ((PsiJavaToken) original).getTokenType());
    }
    if (original instanceof PsiModifierList) {
        final String text = original.getText();
        assert text != null : "Text is null for " + original + "; " + original.getClass();
        final LanguageLevel level = PsiUtil.getLanguageLevel(original);
        final DummyHolder holder = DummyHolderFactory.createHolder(original.getManager(), new JavaDummyElement(text, MOD_LIST, level), null);
        final TreeElement modifierListElement = holder.getTreeElement().getFirstChildNode();
        if (modifierListElement == null) {
            throw new AssertionError("No modifier list for \"" + text + '\"');
        }
        return markGeneratedIfNeeded(original, modifierListElement);
    }
    if (original instanceof PsiReferenceExpression) {
        TreeElement element = createReferenceExpression(original.getProject(), original.getText(), original);
        PsiElement refElement = ((PsiJavaCodeReferenceElement) original).resolve();
        if (refElement instanceof PsiClass) {
            element.putCopyableUserData(REFERENCED_CLASS_KEY, (PsiClass) refElement);
        }
        return element;
    }
    if (original instanceof PsiJavaCodeReferenceElement) {
        PsiElement refElement = ((PsiJavaCodeReferenceElement) original).resolve();
        final boolean generated = refElement != null && CodeEditUtil.isNodeGenerated(refElement.getNode());
        if (refElement instanceof PsiClass) {
            if (refElement instanceof PsiAnonymousClass) {
                PsiJavaCodeReferenceElement ref = ((PsiAnonymousClass) refElement).getBaseClassReference();
                original = ref;
                refElement = ref.resolve();
            }
            boolean isFQ = false;
            if (original instanceof PsiJavaCodeReferenceElementImpl) {
                int kind = ((PsiJavaCodeReferenceElementImpl) original).getKind(original.getContainingFile());
                switch(kind) {
                    case PsiJavaCodeReferenceElementImpl.CLASS_OR_PACKAGE_NAME_KIND:
                    case PsiJavaCodeReferenceElementImpl.CLASS_NAME_KIND:
                    case PsiJavaCodeReferenceElementImpl.CLASS_IN_QUALIFIED_NEW_KIND:
                        isFQ = false;
                        break;
                    case PsiJavaCodeReferenceElementImpl.CLASS_FQ_NAME_KIND:
                    case PsiJavaCodeReferenceElementImpl.CLASS_FQ_OR_PACKAGE_NAME_KIND:
                        isFQ = true;
                        break;
                    default:
                        LOG.assertTrue(false);
                }
            }
            final String text = isFQ ? ((PsiClass) refElement).getQualifiedName() : original.getText();
            final TreeElement element = createReference(original.getProject(), text, generated);
            element.putCopyableUserData(REFERENCED_CLASS_KEY, (PsiClass) refElement);
            return element;
        }
        return createReference(original.getProject(), original.getText(), generated);
    }
    if (original instanceof PsiCompiledElement) {
        PsiElement sourceVersion = original.getNavigationElement();
        if (sourceVersion != original) {
            return ChangeUtil.generateTreeElement(sourceVersion, table, manager);
        }
        PsiElement mirror = ((PsiCompiledElement) original).getMirror();
        return ChangeUtil.generateTreeElement(mirror, table, manager);
    }
    if (original instanceof PsiTypeElement) {
        PsiTypeElement typeElement = (PsiTypeElement) original;
        PsiType type = typeElement.getType();
        if (type instanceof PsiIntersectionType) {
            type = ((PsiIntersectionType) type).getRepresentative();
        } else if (type instanceof PsiMethodReferenceType || type instanceof PsiLambdaExpressionType) {
            type = PsiType.getJavaLangObject(manager, GlobalSearchScope.projectScope(manager.getProject()));
        }
        String text = type.getCanonicalText(true);
        PsiJavaParserFacade parserFacade = JavaPsiFacade.getInstance(original.getProject()).getParserFacade();
        PsiTypeElement element = parserFacade.createTypeElementFromText(text, original);
        TreeElement result = (TreeElement) element.getNode();
        markGeneratedIfNeeded(original, result);
        encodeInfoInTypeElement(result, type);
        return result;
    }
    return null;
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) Nullable(org.jetbrains.annotations.Nullable)

Example 42 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class PsiJavaFileBaseImpl method getLanguageLevelInner.

private LanguageLevel getLanguageLevelInner() {
    if (myOriginalFile instanceof PsiJavaFile) {
        return ((PsiJavaFile) myOriginalFile).getLanguageLevel();
    }
    LanguageLevel forcedLanguageLevel = getUserData(PsiUtil.FILE_LANGUAGE_LEVEL_KEY);
    if (forcedLanguageLevel != null)
        return forcedLanguageLevel;
    VirtualFile virtualFile = getVirtualFile();
    if (virtualFile == null)
        virtualFile = getUserData(IndexingDataKeys.VIRTUAL_FILE);
    final Project project = getProject();
    if (virtualFile == null) {
        final PsiFile originalFile = getOriginalFile();
        if (originalFile instanceof PsiJavaFile && originalFile != this) {
            return ((PsiJavaFile) originalFile).getLanguageLevel();
        }
        return LanguageLevel.HIGHEST;
    }
    return JavaPsiImplementationHelper.getInstance(project).getEffectiveLanguageLevel(virtualFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 43 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class KeywordCompletionTest method testPrivateInJava9Interface.

public void testPrivateInJava9Interface() throws Exception {
    LanguageLevelProjectExtension levelProjectExtension = LanguageLevelProjectExtension.getInstance(getProject());
    LanguageLevel oldLevel = levelProjectExtension.getLanguageLevel();
    try {
        levelProjectExtension.setLanguageLevel(LanguageLevel.JDK_1_9);
        doTest(false);
    } finally {
        levelProjectExtension.setLanguageLevel(oldLevel);
    }
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) LanguageLevelProjectExtension(com.intellij.openapi.roots.LanguageLevelProjectExtension)

Example 44 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class NewProjectUtil method applyJdkToProject.

public static void applyJdkToProject(@NotNull Project project, @NotNull Sdk jdk) {
    ProjectRootManagerEx rootManager = ProjectRootManagerEx.getInstanceEx(project);
    rootManager.setProjectSdk(jdk);
    JavaSdkVersion version = JavaSdk.getInstance().getVersion(jdk);
    if (version != null) {
        LanguageLevel maxLevel = version.getMaxLanguageLevel();
        LanguageLevelProjectExtension extension = LanguageLevelProjectExtension.getInstance(ProjectManager.getInstance().getDefaultProject());
        LanguageLevelProjectExtension ext = LanguageLevelProjectExtension.getInstance(project);
        if (extension.isDefault() || maxLevel.compareTo(ext.getLanguageLevel()) < 0) {
            ext.setLanguageLevel(maxLevel);
        }
    }
}
Also used : ProjectRootManagerEx(com.intellij.openapi.roots.ex.ProjectRootManagerEx) JavaSdkVersion(com.intellij.openapi.projectRoots.JavaSdkVersion) LanguageLevel(com.intellij.pom.java.LanguageLevel) LanguageLevelProjectExtension(com.intellij.openapi.roots.LanguageLevelProjectExtension)

Example 45 with LanguageLevel

use of com.intellij.pom.java.LanguageLevel in project intellij-community by JetBrains.

the class JavaProjectDataService method updateSdk.

private static void updateSdk(@NotNull final Project project, @NotNull final JavaSdkVersion version) {
    final Sdk sdk = findJdk(version);
    if (sdk == null)
        return;
    ExternalSystemApiUtil.executeProjectChangeAction(new DisposeAwareProjectChange(project) {

        @Override
        public void execute() {
            ProjectRootManager.getInstance(project).setProjectSdk(sdk);
            LanguageLevel level = version.getMaxLanguageLevel();
            LanguageLevelProjectExtension languageLevelExtension = LanguageLevelProjectExtension.getInstance(project);
            if (level.compareTo(languageLevelExtension.getLanguageLevel()) < 0) {
                languageLevelExtension.setLanguageLevel(level);
            }
        }
    });
}
Also used : DisposeAwareProjectChange(com.intellij.openapi.externalSystem.util.DisposeAwareProjectChange) 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