Search in sources :

Example 61 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 62 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 63 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)

Example 64 with LanguageLevel

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

the class PsiClassImplUtil method withSubstitutors.

@NotNull
private static <T extends PsiMember> List<Pair<T, PsiSubstitutor>> withSubstitutors(@NotNull final PsiClass psiClass, PsiMember[] members) {
    final ScopedClassHierarchy hierarchy = ScopedClassHierarchy.getHierarchy(psiClass, psiClass.getResolveScope());
    final LanguageLevel level = PsiUtil.getLanguageLevel(psiClass);
    return ContainerUtil.map(members, member -> {
        PsiClass containingClass = member.getContainingClass();
        PsiSubstitutor substitutor = containingClass == null ? null : hierarchy.getSuperMembersSubstitutor(containingClass, level);
        return Pair.create((T) member, substitutor == null ? PsiSubstitutor.EMPTY : substitutor);
    });
}
Also used : LanguageLevel(com.intellij.pom.java.LanguageLevel) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with LanguageLevel

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

the class TypeConversionUtil method isNarrowingReferenceConversionAllowed.

/**
   * see JLS 5.1.5, JLS3 5.1.6
   */
private static boolean isNarrowingReferenceConversionAllowed(@NotNull PsiType fromType, @NotNull PsiType toType) {
    if (toType instanceof PsiPrimitiveType || fromType instanceof PsiPrimitiveType)
        return fromType.equals(toType);
    //Done with primitives
    if (toType instanceof PsiDiamondType || fromType instanceof PsiDiamondType)
        return false;
    if (toType instanceof PsiArrayType && !(fromType instanceof PsiArrayType)) {
        if (fromType instanceof PsiClassType) {
            final PsiClass resolved = ((PsiClassType) fromType).resolve();
            if (resolved instanceof PsiTypeParameter) {
                for (final PsiClassType boundType : resolved.getExtendsListTypes()) {
                    if (!isNarrowingReferenceConversionAllowed(boundType, toType))
                        return false;
                }
                return true;
            }
        }
        if (fromType instanceof PsiCapturedWildcardType) {
            return isNarrowingReferenceConversionAllowed(((PsiCapturedWildcardType) fromType).getUpperBound(), toType);
        }
        return isAssignable(fromType, toType);
    }
    if (fromType instanceof PsiArrayType) {
        if (toType instanceof PsiClassType) {
            final PsiClass resolved = ((PsiClassType) toType).resolve();
            if (resolved instanceof PsiTypeParameter) {
                for (final PsiClassType boundType : resolved.getExtendsListTypes()) {
                    if (!areTypesConvertible(fromType, boundType))
                        return false;
                }
                return true;
            }
        }
        return toType instanceof PsiArrayType && isNarrowingReferenceConversionAllowed(((PsiArrayType) fromType).getComponentType(), ((PsiArrayType) toType).getComponentType());
    }
    if (fromType instanceof PsiIntersectionType) {
        final PsiType[] conjuncts = ((PsiIntersectionType) fromType).getConjuncts();
        for (PsiType conjunct : conjuncts) {
            if (isNarrowingReferenceConversionAllowed(conjunct, toType))
                return true;
        }
        return false;
    } else if (toType instanceof PsiIntersectionType) {
        if (fromType instanceof PsiClassType && ((PsiClassType) fromType).getLanguageLevel().isAtLeast(LanguageLevel.JDK_1_8)) {
            for (PsiType conjunct : ((PsiIntersectionType) toType).getConjuncts()) {
                if (!isNarrowingReferenceConversionAllowed(fromType, conjunct))
                    return false;
            }
            return true;
        }
        return false;
    }
    if (fromType instanceof PsiDisjunctionType) {
        return isNarrowingReferenceConversionAllowed(((PsiDisjunctionType) fromType).getLeastUpperBound(), toType);
    }
    if (toType instanceof PsiDisjunctionType) {
        return false;
    }
    if (fromType instanceof PsiWildcardType) {
        final PsiWildcardType fromWildcard = (PsiWildcardType) fromType;
        final PsiType bound = fromWildcard.getBound();
        if (bound == null)
            return true;
        if (fromWildcard.isSuper()) {
            return isAssignable(toType, bound);
        }
        return isNarrowingReferenceConversionAllowed(bound, toType);
    }
    if (toType instanceof PsiWildcardType) {
        final PsiWildcardType toWildcard = (PsiWildcardType) toType;
        if (toWildcard.isSuper())
            return false;
        final PsiType bound = toWildcard.getBound();
        return bound == null || isNarrowingReferenceConversionAllowed(fromType, bound);
    }
    if (toType instanceof PsiCapturedWildcardType) {
        return isNarrowingReferenceConversionAllowed(fromType, ((PsiCapturedWildcardType) toType).getUpperBound());
    }
    if (fromType instanceof PsiCapturedWildcardType) {
        return isNarrowingReferenceConversionAllowed(((PsiCapturedWildcardType) fromType).getUpperBound(), toType);
    }
    if (isAssignable(fromType, toType))
        return true;
    if (!(fromType instanceof PsiClassType) || !(toType instanceof PsiClassType))
        return false;
    PsiClassType fromClassType = (PsiClassType) fromType;
    PsiClassType toClassType = (PsiClassType) toType;
    PsiClassType.ClassResolveResult fromResult = fromClassType.resolveGenerics();
    final PsiClass fromClass = fromResult.getElement();
    if (fromClass == null)
        return false;
    if (fromClass instanceof PsiTypeParameter)
        return isNarrowingReferenceConversionAllowed(obtainSafeSuperType((PsiTypeParameter) fromClass), toType);
    PsiClassType.ClassResolveResult toResult = toClassType.resolveGenerics();
    final PsiClass toClass = toResult.getElement();
    if (toClass == null)
        return false;
    if (toClass instanceof PsiTypeParameter)
        return isNarrowingReferenceConversionAllowed(fromType, obtainSafeSuperType((PsiTypeParameter) toClass));
    //Done with type parameters
    PsiManager manager = fromClass.getManager();
    final LanguageLevel languageLevel = toClassType.getLanguageLevel();
    if (!fromClass.isInterface()) {
        if (toClass.isInterface()) {
            return (!fromClass.hasModifierProperty(PsiModifier.FINAL) || fromClass.isInheritor(toClass, true)) && checkSuperTypesWithDifferentTypeArguments(toResult, fromClass, manager, fromResult.getSubstitutor(), null, languageLevel);
        } else {
            if (manager.areElementsEquivalent(fromClass, toClass)) {
                return areSameParameterTypes(fromClassType, toClassType);
            }
            if (toClass.isInheritor(fromClass, true)) {
                return checkSuperTypesWithDifferentTypeArguments(fromResult, toClass, manager, toResult.getSubstitutor(), null, languageLevel);
            } else if (fromClass.isInheritor(toClass, true)) {
                return checkSuperTypesWithDifferentTypeArguments(toResult, fromClass, manager, fromResult.getSubstitutor(), null, languageLevel);
            }
            return false;
        }
    } else if (!toClass.isInterface()) {
        if (!toClass.hasModifierProperty(PsiModifier.FINAL)) {
            return checkSuperTypesWithDifferentTypeArguments(fromResult, toClass, manager, toResult.getSubstitutor(), null, languageLevel);
        } else {
            PsiSubstitutor toSubstitutor = getMaybeSuperClassSubstitutor(fromClass, toClass, toResult.getSubstitutor(), null);
            return toSubstitutor != null && areSameArgumentTypes(fromClass, fromResult.getSubstitutor(), toSubstitutor);
        }
    } else if (languageLevel.compareTo(LanguageLevel.JDK_1_5) < 0) {
        //In jls2 check for method in both interfaces with the same signature but different return types.
        Collection<HierarchicalMethodSignature> fromClassMethodSignatures = fromClass.getVisibleSignatures();
        Collection<HierarchicalMethodSignature> toClassMethodSignatures = toClass.getVisibleSignatures();
        for (HierarchicalMethodSignature fromMethodSignature : fromClassMethodSignatures) {
            for (HierarchicalMethodSignature toMethodSignature : toClassMethodSignatures) {
                if (fromMethodSignature.equals(toMethodSignature)) {
                    final PsiType fromClassReturnType = fromMethodSignature.getMethod().getReturnType();
                    final PsiType toClassReturnType = toMethodSignature.getMethod().getReturnType();
                    if (fromClassReturnType != null && toClassReturnType != null && !fromClassReturnType.equals(toClassReturnType)) {
                        return false;
                    }
                }
            }
        }
        return true;
    } else {
        //In jls3 check for super interface with distinct type arguments
        PsiClassType.ClassResolveResult baseResult;
        PsiClass derived;
        PsiSubstitutor derivedSubstitutor;
        if (toClass.isInheritor(fromClass, true)) {
            baseResult = fromResult;
            derived = toClass;
            derivedSubstitutor = toResult.getSubstitutor();
        } else {
            baseResult = toResult;
            derived = fromClass;
            derivedSubstitutor = fromResult.getSubstitutor();
        }
        return checkSuperTypesWithDifferentTypeArguments(baseResult, derived, manager, derivedSubstitutor, null, languageLevel);
    }
}
Also used : 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