Search in sources :

Example 21 with LanguageLevel

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

the class Java15APIUsageInspectionBase method getLastIncompatibleLanguageLevel.

public static LanguageLevel getLastIncompatibleLanguageLevel(@NotNull PsiMember member, @NotNull LanguageLevel languageLevel) {
    if (member instanceof PsiAnonymousClass)
        return null;
    PsiClass containingClass = member.getContainingClass();
    if (containingClass instanceof PsiAnonymousClass)
        return null;
    if (member instanceof PsiClass && !(member.getParent() instanceof PsiClass || member.getParent() instanceof PsiFile))
        return null;
    Set<String> forbiddenApi = getForbiddenApi(languageLevel);
    String signature = getSignature(member);
    if (forbiddenApi != null && signature != null) {
        LanguageLevel lastIncompatibleLanguageLevel = getLastIncompatibleLanguageLevelForSignature(signature, languageLevel, forbiddenApi);
        if (lastIncompatibleLanguageLevel != null)
            return lastIncompatibleLanguageLevel;
    }
    return containingClass != null ? getLastIncompatibleLanguageLevel(containingClass, languageLevel) : null;
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 22 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 23 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 24 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 25 with LanguageLevel

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

the class ExtractMethodTest method doTestWithJava17.

private void doTestWithJava17() throws Exception {
    LanguageLevelProjectExtension projectExtension = LanguageLevelProjectExtension.getInstance(getProject());
    LanguageLevel oldLevel = projectExtension.getLanguageLevel();
    try {
        projectExtension.setLanguageLevel(LanguageLevel.JDK_1_7);
        doTest();
    } finally {
        projectExtension.setLanguageLevel(oldLevel);
    }
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) 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