diff --git a/java/java-impl/src/com/intellij/codeInspection/defaultFileTemplateUsage/CatchBodyVisitor.java b/java/java-impl/src/com/intellij/codeInspection/defaultFileTemplateUsage/CatchBodyVisitor.java index 45a2dd29ba60..f75ce730feb9 100644 --- a/java/java-impl/src/com/intellij/codeInspection/defaultFileTemplateUsage/CatchBodyVisitor.java +++ b/java/java-impl/src/com/intellij/codeInspection/defaultFileTemplateUsage/CatchBodyVisitor.java @@ -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); diff --git a/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java b/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java index 9032cbce2d55..eb7183405b56 100644 --- a/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/PsiElementFactoryImpl.java @@ -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); + } + } diff --git a/java/java-impl/src/com/intellij/psi/impl/PsiJavaParserFacadeImpl.java b/java/java-impl/src/com/intellij/psi/impl/PsiJavaParserFacadeImpl.java index 8e1432ac0d36..b16b04177089 100644 --- a/java/java-impl/src/com/intellij/psi/impl/PsiJavaParserFacadeImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/PsiJavaParserFacadeImpl.java @@ -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 PRIMITIVE_TYPES; static { PRIMITIVE_TYPES = new HashMap(); @@ -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; } diff --git a/java/openapi/src/com/intellij/psi/PsiElementFactory.java b/java/openapi/src/com/intellij/psi/PsiElementFactory.java index 00dbbcd9ffa1..73826da8baf5 100644 --- a/java/openapi/src/com/intellij/psi/PsiElementFactory.java +++ b/java/openapi/src/com/intellij/psi/PsiElementFactory.java @@ -512,4 +512,17 @@ public interface PsiElementFactory extends PsiJavaParserFacade, JVMElementFactor */ @NotNull PsiElement createDummyHolder(@NotNull String text, @NotNull IElementType type, @Nullable PsiElement context); + + /** + * Creates a catch 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; } diff --git a/java/openapi/src/com/intellij/psi/PsiJavaParserFacade.java b/java/openapi/src/com/intellij/psi/PsiJavaParserFacade.java index 7e69a1d3f45a..c325fa8d51f0 100644 --- a/java/openapi/src/com/intellij/psi/PsiJavaParserFacade.java +++ b/java/openapi/src/com/intellij/psi/PsiJavaParserFacade.java @@ -220,19 +220,6 @@ public interface PsiJavaParserFacade { @NotNull PsiEnumConstant createEnumConstantFromText(@NotNull String text, @Nullable PsiElement context) throws IncorrectOperationException; - /** - * Creates a catch 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. *