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