mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] avoids setting a package name for module-info files
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,13 +18,13 @@ package com.intellij.psi.impl.file;
|
||||
import com.intellij.openapi.roots.JavaProjectRootsUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Maxim.Mossienko
|
||||
* Date: Sep 18, 2008
|
||||
* Time: 3:33:07 PM
|
||||
* @since Sep 18, 2008
|
||||
*/
|
||||
public class JavaUpdateAddedFileProcessor extends UpdateAddedFileProcessor {
|
||||
@Override
|
||||
@@ -34,14 +34,17 @@ public class JavaUpdateAddedFileProcessor extends UpdateAddedFileProcessor {
|
||||
|
||||
@Override
|
||||
public void update(final PsiFile element, PsiFile originalElement) throws IncorrectOperationException {
|
||||
if (element.getViewProvider() instanceof TemplateLanguageFileViewProvider) return;
|
||||
if (element.getViewProvider() instanceof TemplateLanguageFileViewProvider || PsiUtil.isModuleFile(element)) {
|
||||
return;
|
||||
}
|
||||
|
||||
PsiDirectory dir = element.getContainingDirectory();
|
||||
if (dir == null) return;
|
||||
PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(dir);
|
||||
if (aPackage == null) return;
|
||||
String packageName = aPackage.getQualifiedName();
|
||||
|
||||
((PsiClassOwner)element).setPackageName(packageName);
|
||||
if (dir != null) {
|
||||
PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(dir);
|
||||
if (aPackage != null) {
|
||||
String packageName = aPackage.getQualifiedName();
|
||||
((PsiClassOwner)element).setPackageName(packageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-8
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,13 +26,13 @@ import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.FileTypeUtils;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.MoveDestination;
|
||||
import com.intellij.refactoring.PackageWrapper;
|
||||
import com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesUtil;
|
||||
@@ -40,7 +40,6 @@ import com.intellij.refactoring.util.MoveRenameUsageInfo;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.refactoring.util.TextOccurrencesUtil;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -236,16 +235,15 @@ public class MoveClassesOrPackagesUtil {
|
||||
final PsiDirectory containingDirectory = file.getContainingDirectory();
|
||||
if (!Comparing.equal(moveDestination.getVirtualFile(), containingDirectory != null ? containingDirectory.getVirtualFile() : null)) {
|
||||
LOG.assertTrue(file.getVirtualFile() != null, aClass);
|
||||
|
||||
MoveFilesOrDirectoriesUtil.doMoveFile(file, moveDestination);
|
||||
if (file instanceof PsiClassOwner && newPackage != null && !FileTypeUtils.isInServerPageFile(file)) {
|
||||
|
||||
if (newPackage != null && file instanceof PsiClassOwner && !FileTypeUtils.isInServerPageFile(file) && !PsiUtil.isModuleFile(file)) {
|
||||
// Do not rely on class instance identity retention after setPackageName (Scala)
|
||||
String aClassName = aClass.getName();
|
||||
((PsiClassOwner)file).setPackageName(newPackage.getQualifiedName());
|
||||
newClass = findClassByName((PsiClassOwner)file, aClassName);
|
||||
LOG.assertTrue(newClass != null, "name: " + aClassName +
|
||||
", file: " + file +
|
||||
", classes: " + StringUtil.join(((PsiClassOwner)file).getClasses(),
|
||||
psiClass -> psiClass.getName(), " "));
|
||||
LOG.assertTrue(newClass != null, "name:" + aClassName + " file:" + file + " classes:" + Arrays.toString(((PsiClassOwner)file).getClasses()));
|
||||
}
|
||||
}
|
||||
return newClass;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
|
||||
/**
|
||||
* Represents a Java, JSP or class file.
|
||||
* Represents a Java, JSP or .class file.
|
||||
*/
|
||||
public interface PsiJavaFile extends PsiImportHolder, PsiClassOwner {
|
||||
/**
|
||||
@@ -91,4 +91,9 @@ public interface PsiJavaFile extends PsiImportHolder, PsiClassOwner {
|
||||
@Nullable PsiJavaCodeReferenceElement findImportReferenceTo(PsiClass aClass);
|
||||
|
||||
@NotNull LanguageLevel getLanguageLevel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Java module declaration element, or {@code null} if the file is not a module-info one.
|
||||
*/
|
||||
@Nullable PsiJavaModule getModuleDeclaration();
|
||||
}
|
||||
@@ -1281,4 +1281,8 @@ public final class PsiUtil extends PsiUtilCore {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isModuleFile(@NotNull PsiFile file) {
|
||||
return file instanceof PsiJavaFile && ((PsiJavaFile)file).getModuleDeclaration() != null;
|
||||
}
|
||||
}
|
||||
@@ -256,6 +256,12 @@ public class ClsFileImpl extends ClsRepositoryPsiElement<PsiClassHolderFileStub>
|
||||
return level;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiJavaModule getModuleDeclaration() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
|
||||
throw cannotModifyException(this);
|
||||
|
||||
@@ -108,6 +108,10 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
|
||||
|
||||
@Override
|
||||
public void setPackageName(final String packageName) throws IncorrectOperationException {
|
||||
if (PsiUtil.isModuleFile(this)) {
|
||||
throw new IncorrectOperationException("Cannot set package name for module declarations");
|
||||
}
|
||||
|
||||
final PsiPackageStatement packageStatement = getPackageStatement();
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
|
||||
if (packageStatement != null) {
|
||||
@@ -119,10 +123,8 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
|
||||
packageStatement.delete();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!packageName.isEmpty()) {
|
||||
addBefore(factory.createPackageStatement(packageName), getFirstChild());
|
||||
}
|
||||
else if (!packageName.isEmpty()) {
|
||||
addBefore(factory.createPackageStatement(packageName), getFirstChild());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -474,6 +476,12 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
|
||||
return LANGUAGE_LEVEL_KEY.getValue(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiJavaModule getModuleDeclaration() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearCaches() {
|
||||
super.clearCaches();
|
||||
|
||||
@@ -17,21 +17,33 @@ package com.intellij.psi.impl.source;
|
||||
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.psi.FileViewProvider;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.java.stubs.JavaStubElementTypes;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PsiJavaFileImpl extends PsiJavaFileBaseImpl {
|
||||
public PsiJavaFileImpl(FileViewProvider file) {
|
||||
super(JavaStubElementTypes.JAVA_FILE, JavaStubElementTypes.JAVA_FILE, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
@Override
|
||||
public FileType getFileType() {
|
||||
return JavaFileType.INSTANCE;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiJavaModule getModuleDeclaration() {
|
||||
PsiElement element = getFirstChild();
|
||||
if (element instanceof PsiWhiteSpace || element instanceof PsiComment) {
|
||||
element = PsiTreeUtil.skipSiblingsForward(element, PsiWhiteSpace.class, PsiComment.class);
|
||||
}
|
||||
return element instanceof PsiJavaModule ? (PsiJavaModule)element : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PsiJavaFile:" + getName();
|
||||
|
||||
Reference in New Issue
Block a user