mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
push down createCatchSection() from PsiJavaParserFacade to PsiElementFactory
This commit is contained in:
+2
-2
@@ -69,7 +69,7 @@ class CatchBodyVisitor extends JavaRecursiveElementWalkingVisitor {
|
||||
PsiCodeBlock templateCatchBlock;
|
||||
final PsiParameter templateParameter;
|
||||
try {
|
||||
final PsiJavaParserFacade elementFactory = JavaPsiFacade.getInstance(section.getProject()).getParserFacade();
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(section.getProject()).getElementFactory();
|
||||
PsiCatchSection sectionTemplate = elementFactory.createCatchSection(type, parameter.getName(), parameter);
|
||||
templateCatchBlock = sectionTemplate.getCatchBlock();
|
||||
|
||||
@@ -124,7 +124,7 @@ class CatchBodyVisitor extends JavaRecursiveElementWalkingVisitor {
|
||||
if (catchBlock == null) return;
|
||||
PsiType type = parameter.getType();
|
||||
if (!(type instanceof PsiClassType)) return;
|
||||
final PsiJavaParserFacade elementFactory = JavaPsiFacade.getInstance(section.getProject()).getParserFacade();
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(section.getProject()).getElementFactory();
|
||||
try {
|
||||
PsiCatchSection sectionTemplate = elementFactory.createCatchSection(type, parameter.getName(), parameter);
|
||||
section.replace(sectionTemplate);
|
||||
|
||||
@@ -15,9 +15,17 @@
|
||||
*/
|
||||
package com.intellij.psi.impl;
|
||||
|
||||
import com.intellij.ide.fileTemplates.FileTemplate;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.ide.fileTemplates.JavaTemplateUtil;
|
||||
import com.intellij.lang.*;
|
||||
import com.intellij.lang.java.parser.JavaParserUtil;
|
||||
import com.intellij.lang.java.parser.StatementParser;
|
||||
import com.intellij.lexer.Lexer;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
@@ -41,10 +49,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static com.intellij.openapi.util.text.StringUtil.join;
|
||||
|
||||
public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements PsiElementFactory {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.PsiElementFactoryImpl");
|
||||
|
||||
private PsiClass myArrayClass;
|
||||
private PsiClass myArrayClass15;
|
||||
|
||||
@@ -703,16 +714,6 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
|
||||
return method;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiCatchSection createCatchSection(@NotNull final PsiType exceptionType,
|
||||
@NotNull final String exceptionName,
|
||||
final PsiElement context) throws IncorrectOperationException {
|
||||
final PsiCatchSection psiCatchSection = super.createCatchSection(exceptionType, exceptionName, context);
|
||||
GeneratedMarkerVisitor.markGenerated(psiCatchSection);
|
||||
return psiCatchSection;
|
||||
}
|
||||
|
||||
private static PsiImportStatementBase extractImport(final PsiJavaFile aFile, final boolean isStatic) {
|
||||
final PsiImportList importList = aFile.getImportList();
|
||||
assert importList != null : aFile;
|
||||
@@ -725,4 +726,59 @@ public class PsiElementFactoryImpl extends PsiJavaParserFacadeImpl implements Ps
|
||||
assert original != null : message;
|
||||
original.replace(replacement);
|
||||
}
|
||||
|
||||
private static final JavaParserUtil.ParserWrapper CATCH_SECTION = new JavaParserUtil.ParserWrapper() {
|
||||
@Override
|
||||
public void parse(final PsiBuilder builder) {
|
||||
StatementParser.parseCatchBlock(builder);
|
||||
}
|
||||
};
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiCatchSection createCatchSection(@NotNull final PsiType exceptionType,
|
||||
@NotNull final String exceptionName,
|
||||
@Nullable final PsiElement context) throws IncorrectOperationException {
|
||||
if (!(exceptionType instanceof PsiClassType || exceptionType instanceof PsiDisjunctionType)) {
|
||||
throw new IncorrectOperationException("Unexpected type:" + exceptionType);
|
||||
}
|
||||
final String text = StringUtil.join("catch (", exceptionType.getCanonicalText(), " ", exceptionName, ") {}");
|
||||
final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context);
|
||||
final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
|
||||
if (!(element instanceof PsiCatchSection)) {
|
||||
throw new IncorrectOperationException("Incorrect catch section '" + text + "'. Parsed element: " + element);
|
||||
}
|
||||
setupCatchBlock(exceptionName, context, (PsiCatchSection)element);
|
||||
final PsiCatchSection catchSection = (PsiCatchSection)CodeStyleManager.getInstance(myManager.getProject()).reformat(element);
|
||||
GeneratedMarkerVisitor.markGenerated(catchSection);
|
||||
return catchSection;
|
||||
}
|
||||
|
||||
private void setupCatchBlock(final String exceptionName, @Nullable final PsiElement context, final PsiCatchSection psiCatchSection)
|
||||
throws IncorrectOperationException {
|
||||
final FileTemplate catchBodyTemplate = FileTemplateManager.getInstance().getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY);
|
||||
LOG.assertTrue(catchBodyTemplate != null);
|
||||
|
||||
final Properties props = new Properties();
|
||||
props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName);
|
||||
if (context != null && context.isPhysical()) {
|
||||
final PsiDirectory directory = context.getContainingFile().getContainingDirectory();
|
||||
if (directory != null) {
|
||||
JavaTemplateUtil.setPackageNameAttribute(props, directory);
|
||||
}
|
||||
}
|
||||
|
||||
final PsiCodeBlock codeBlockFromText;
|
||||
try {
|
||||
codeBlockFromText = createCodeBlockFromText("{\n" + catchBodyTemplate.getText(props) + "\n}", null);
|
||||
}
|
||||
catch (ProcessCanceledException ce) {
|
||||
throw ce;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IncorrectOperationException("Incorrect file template", e);
|
||||
}
|
||||
psiCatchSection.getCatchBlock().replace(codeBlockFromText);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,20 +15,14 @@
|
||||
*/
|
||||
package com.intellij.psi.impl;
|
||||
|
||||
import com.intellij.ide.fileTemplates.FileTemplate;
|
||||
import com.intellij.ide.fileTemplates.FileTemplateManager;
|
||||
import com.intellij.ide.fileTemplates.JavaTemplateUtil;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.lang.PsiBuilder;
|
||||
import com.intellij.lang.java.parser.*;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.progress.ProcessCanceledException;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.impl.source.DummyHolder;
|
||||
import com.intellij.psi.impl.source.DummyHolderFactory;
|
||||
import com.intellij.psi.impl.source.JavaDummyElement;
|
||||
@@ -44,7 +38,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author max
|
||||
@@ -57,8 +50,6 @@ public class PsiJavaParserFacadeImpl implements PsiJavaParserFacade {
|
||||
myManager = manager;
|
||||
}
|
||||
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.PsiJavaParserFacadeImpl");
|
||||
|
||||
private static final JavaParserUtil.ParserWrapper ANNOTATION = new JavaParserUtil.ParserWrapper() {
|
||||
@Override
|
||||
public void parse(final PsiBuilder builder) {
|
||||
@@ -151,13 +142,6 @@ public class PsiJavaParserFacadeImpl implements PsiJavaParserFacade {
|
||||
}
|
||||
};
|
||||
|
||||
private static final JavaParserUtil.ParserWrapper CATCH_SECTION = new JavaParserUtil.ParserWrapper() {
|
||||
@Override
|
||||
public void parse(final PsiBuilder builder) {
|
||||
StatementParser.parseCatchBlock(builder);
|
||||
}
|
||||
};
|
||||
|
||||
private static final Map<String, PsiPrimitiveType> PRIMITIVE_TYPES;
|
||||
static {
|
||||
PRIMITIVE_TYPES = new HashMap<String, PsiPrimitiveType>();
|
||||
@@ -385,51 +369,6 @@ public class PsiJavaParserFacadeImpl implements PsiJavaParserFacade {
|
||||
return (PsiEnumConstant)element;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiCatchSection createCatchSection(@NotNull final PsiType exceptionType,
|
||||
@NotNull final String exceptionName,
|
||||
@Nullable final PsiElement context) throws IncorrectOperationException {
|
||||
if (!(exceptionType instanceof PsiClassType || exceptionType instanceof PsiDisjunctionType)) {
|
||||
throw new IncorrectOperationException("Unexpected type:" + exceptionType);
|
||||
}
|
||||
final String text = StringUtil.join("catch (", exceptionType.getCanonicalText(), " ", exceptionName, ") {}");
|
||||
final DummyHolder holder = DummyHolderFactory.createHolder(myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context);
|
||||
final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode());
|
||||
if (!(element instanceof PsiCatchSection)) {
|
||||
throw new IncorrectOperationException("Incorrect catch section '" + text + "'. Parsed element: " + element);
|
||||
}
|
||||
setupCatchBlock(exceptionName, context, (PsiCatchSection)element);
|
||||
return (PsiCatchSection)CodeStyleManager.getInstance(myManager.getProject()).reformat(element);
|
||||
}
|
||||
|
||||
private void setupCatchBlock(final String exceptionName, @Nullable final PsiElement context, final PsiCatchSection psiCatchSection)
|
||||
throws IncorrectOperationException {
|
||||
final FileTemplate catchBodyTemplate = FileTemplateManager.getInstance().getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY);
|
||||
LOG.assertTrue(catchBodyTemplate != null);
|
||||
|
||||
final Properties props = new Properties();
|
||||
props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName);
|
||||
if (context != null && context.isPhysical()) {
|
||||
final PsiDirectory directory = context.getContainingFile().getContainingDirectory();
|
||||
if (directory != null) {
|
||||
JavaTemplateUtil.setPackageNameAttribute(props, directory);
|
||||
}
|
||||
}
|
||||
|
||||
final PsiCodeBlock codeBlockFromText;
|
||||
try {
|
||||
codeBlockFromText = createCodeBlockFromText("{\n" + catchBodyTemplate.getText(props) + "\n}", null);
|
||||
}
|
||||
catch (ProcessCanceledException ce) {
|
||||
throw ce;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IncorrectOperationException("Incorrect file template", e);
|
||||
}
|
||||
psiCatchSection.getCatchBlock().replace(codeBlockFromText);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiType createPrimitiveType(@NotNull final String text, @NotNull final PsiAnnotation[] annotations) throws IncorrectOperationException {
|
||||
@@ -452,7 +391,7 @@ public class PsiJavaParserFacadeImpl implements PsiJavaParserFacade {
|
||||
return PRIMITIVE_TYPES.get(text);
|
||||
}
|
||||
|
||||
private static LanguageLevel level(@Nullable final PsiElement context) {
|
||||
protected static LanguageLevel level(@Nullable final PsiElement context) {
|
||||
return context != null ? PsiUtil.getLanguageLevel(context) : LanguageLevel.HIGHEST;
|
||||
}
|
||||
|
||||
|
||||
@@ -512,4 +512,17 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor
|
||||
*/
|
||||
@NotNull
|
||||
PsiElement createDummyHolder(@NotNull String text, @NotNull IElementType type, @Nullable PsiElement context);
|
||||
|
||||
/**
|
||||
* Creates a <code>catch</code> section for catching an exception of the specified
|
||||
* type and name.
|
||||
*
|
||||
* @param exceptionType the type of the exception to catch (either {@linkplain PsiClassType} or {@linkplain PsiDisjunctionType}).
|
||||
* @param exceptionName the name of the variable in which the caught exception is stored (may be an empty string).
|
||||
* @param context the context for resolving references.
|
||||
* @return the created catch section instance.
|
||||
* @throws IncorrectOperationException if some of the parameters are not valid.
|
||||
*/
|
||||
@NotNull
|
||||
PsiCatchSection createCatchSection(@NotNull PsiType exceptionType, @NotNull String exceptionName, @Nullable PsiElement context) throws IncorrectOperationException;
|
||||
}
|
||||
|
||||
@@ -220,19 +220,6 @@ public interface PsiJavaParserFacade {
|
||||
@NotNull
|
||||
PsiEnumConstant createEnumConstantFromText(@NotNull String text, @Nullable PsiElement context) throws IncorrectOperationException;
|
||||
|
||||
/**
|
||||
* Creates a <code>catch</code> section for catching an exception of the specified
|
||||
* type and name.
|
||||
*
|
||||
* @param exceptionType the type of the exception to catch (either {@linkplain PsiClassType} or {@linkplain PsiDisjunctionType}).
|
||||
* @param exceptionName the name of the variable in which the caught exception is stored (may be an empty string).
|
||||
* @param context the context for resolving references.
|
||||
* @return the created catch section instance.
|
||||
* @throws IncorrectOperationException if some of the parameters are not valid.
|
||||
*/
|
||||
@NotNull
|
||||
PsiCatchSection createCatchSection(@NotNull PsiType exceptionType, @NotNull String exceptionName, @Nullable PsiElement context) throws IncorrectOperationException;
|
||||
|
||||
/**
|
||||
* Creates a Java type from the specified text.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user