Search in sources :

Example 66 with LanguageLevel

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

the class EffectiveLanguageLevelUtil method getEffectiveLanguageLevel.

@NotNull
public static LanguageLevel getEffectiveLanguageLevel(@NotNull final Module module) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    LanguageLevelModuleExtension moduleLevel = LanguageLevelModuleExtensionImpl.getInstance(module);
    LanguageLevel level = moduleLevel == null ? null : moduleLevel.getLanguageLevel();
    if (level != null)
        return level;
    return LanguageLevelProjectExtension.getInstance(module.getProject()).getLanguageLevel();
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) LanguageLevelModuleExtension(com.intellij.openapi.roots.LanguageLevelModuleExtension) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with LanguageLevel

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

the class AddTypeArgumentsFix method addTypeArguments.

@Nullable
public static PsiExpression addTypeArguments(PsiExpression expression, PsiType toType) {
    if (!PsiUtil.isLanguageLevel5OrHigher(expression))
        return null;
    if (expression instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression methodCall = (PsiMethodCallExpression) expression;
        final PsiReferenceParameterList list = methodCall.getMethodExpression().getParameterList();
        if (list == null || list.getTypeArguments().length > 0)
            return null;
        final JavaResolveResult resolveResult = methodCall.resolveMethodGenerics();
        final PsiElement element = resolveResult.getElement();
        if (element instanceof PsiMethod) {
            final PsiMethod method = (PsiMethod) element;
            final PsiType returnType = method.getReturnType();
            if (returnType == null)
                return null;
            final PsiTypeParameter[] typeParameters = method.getTypeParameters();
            if (typeParameters.length > 0) {
                PsiType[] mappings = PsiType.createArray(typeParameters.length);
                PsiResolveHelper helper = JavaPsiFacade.getInstance(expression.getProject()).getResolveHelper();
                LanguageLevel level = PsiUtil.getLanguageLevel(expression);
                for (int i = 0; i < typeParameters.length; i++) {
                    PsiTypeParameter typeParameter = typeParameters[i];
                    final PsiType substitution = helper.getSubstitutionForTypeParameter(typeParameter, returnType, toType, false, level);
                    if (substitution == null || PsiType.NULL.equals(substitution))
                        return null;
                    mappings[i] = GenericsUtil.eliminateWildcards(substitution, false);
                }
                final PsiElementFactory factory = JavaPsiFacade.getInstance(expression.getProject()).getElementFactory();
                PsiMethodCallExpression copy = (PsiMethodCallExpression) expression.copy();
                final PsiReferenceExpression methodExpression = copy.getMethodExpression();
                final PsiReferenceParameterList parameterList = methodExpression.getParameterList();
                LOG.assertTrue(parameterList != null);
                for (PsiType mapping : mappings) {
                    parameterList.add(factory.createTypeElement(mapping));
                }
                if (methodExpression.getQualifierExpression() == null) {
                    final PsiExpression qualifierExpression;
                    final PsiClass containingClass = method.getContainingClass();
                    LOG.assertTrue(containingClass != null);
                    if (method.hasModifierProperty(PsiModifier.STATIC)) {
                        qualifierExpression = factory.createReferenceExpression(containingClass);
                    } else {
                        qualifierExpression = RefactoringChangeUtil.createThisExpression(method.getManager(), null);
                    }
                    methodExpression.setQualifierExpression(qualifierExpression);
                }
                return (PsiExpression) JavaCodeStyleManager.getInstance(copy.getProject()).shortenClassReferences(copy);
            }
        }
    }
    return null;
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) Nullable(org.jetbrains.annotations.Nullable)

Example 68 with LanguageLevel

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

the class IdeaProjectModelModifier method changeLanguageLevel.

@Override
public Promise<Void> changeLanguageLevel(@NotNull Module module, @NotNull LanguageLevel level) {
    final LanguageLevel moduleLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
    if (moduleLevel != null && JavaSdkUtil.isLanguageLevelAcceptable(myProject, module, level)) {
        final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel();
        rootModel.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(level);
        rootModel.commit();
    } else {
        LanguageLevelProjectExtension.getInstance(myProject).setLanguageLevel(level);
        ProjectRootManagerEx.getInstanceEx(myProject).makeRootsChange(EmptyRunnable.INSTANCE, false, true);
    }
    return Promises.resolvedPromise(null);
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel)

Example 69 with LanguageLevel

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

the class LanguageLevelUsagesCollector method getProjectUsages.

@NotNull
public Set<UsageDescriptor> getProjectUsages(@NotNull Project project) {
    final Set<String> languageLevels = new HashSet<>();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        final LanguageLevelModuleExtension instance = LanguageLevelModuleExtensionImpl.getInstance(module);
        final LanguageLevel languageLevel = instance.getLanguageLevel();
        if (languageLevel != null) {
            languageLevels.add(languageLevel.toString());
        } else {
            languageLevels.add(LanguageLevelProjectExtension.getInstance(project).getLanguageLevel().toString());
        }
    }
    return ContainerUtil.map2Set(languageLevels, languageLevel -> new UsageDescriptor(languageLevel, 1));
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) Module(com.intellij.openapi.module.Module) LanguageLevelModuleExtension(com.intellij.openapi.roots.LanguageLevelModuleExtension) UsageDescriptor(com.intellij.internal.statistic.beans.UsageDescriptor) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 70 with LanguageLevel

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

the class JavaParserDefinition method spaceExistanceTypeBetweenTokens.

@Override
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
    if (right.getElementType() == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN || left.getElementType() == JavaDocTokenType.DOC_TAG_VALUE_SHARP_TOKEN) {
        return SpaceRequirements.MUST_NOT;
    }
    PsiFile containingFile = left.getTreeParent().getPsi().getContainingFile();
    LanguageLevel level = containingFile instanceof PsiJavaFile ? ((PsiJavaFile) containingFile).getLanguageLevel() : LanguageLevel.HIGHEST;
    Lexer lexer = createLexer(level);
    SpaceRequirements spaceRequirements = LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer);
    if (left.getElementType() == JavaTokenType.END_OF_LINE_COMMENT) {
        return SpaceRequirements.MUST_LINE_BREAK;
    }
    if (left.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA) {
        String text = left.getText();
        if (text.length() > 0 && Character.isWhitespace(text.charAt(text.length() - 1))) {
            return SpaceRequirements.MAY;
        }
    }
    if (right.getElementType() == JavaDocTokenType.DOC_COMMENT_DATA) {
        String text = right.getText();
        if (text.length() > 0 && Character.isWhitespace(text.charAt(0))) {
            return SpaceRequirements.MAY;
        }
    } else if (right.getElementType() == JavaDocTokenType.DOC_INLINE_TAG_END) {
        return SpaceRequirements.MAY;
    }
    return spaceRequirements;
}
Also used : Lexer(com.intellij.lexer.Lexer) JavaDocLexer(com.intellij.lang.java.lexer.JavaDocLexer) JavaLexer(com.intellij.lang.java.lexer.JavaLexer) LanguageLevel(com.intellij.pom.java.LanguageLevel)

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