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();
}
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;
}
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);
}
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));
}
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;
}
Aggregations