From d1e5ef2c1102b54e78d02596bf68cd3c619dce6d Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 30 Jan 2009 15:50:53 +0300 Subject: [PATCH] cleanup, performance --- .../intellij/testFramework/InspectionTestCase.java | 5 +- .../emptyMethod/EmptyMethodInspection.java | 109 +++++----- .../codeInspection/reference/RefMethodImpl.java | 20 +- .../intellij/codeInspection/ui/InspectionTree.java | 4 +- .../src/com/intellij/psi/util/PsiFormatUtil.java | 227 +++++++++------------ 5 files changed, 168 insertions(+), 197 deletions(-) diff --git a/ExtendedApi/src/com/intellij/testFramework/InspectionTestCase.java b/ExtendedApi/src/com/intellij/testFramework/InspectionTestCase.java index 6c1c2e9247..46cdb967a1 100644 --- a/ExtendedApi/src/com/intellij/testFramework/InspectionTestCase.java +++ b/ExtendedApi/src/com/intellij/testFramework/InspectionTestCase.java @@ -39,6 +39,7 @@ import java.io.CharArrayReader; import java.io.File; import java.io.StreamTokenizer; import java.util.ArrayList; +import java.util.List; @SuppressWarnings({"HardCodedStringLiteral"}) public abstract class InspectionTestCase extends PsiTestCase { @@ -194,8 +195,8 @@ public abstract class InspectionTestCase extends PsiTestCase { } protected static void compareWithExpected(Document expectedDoc, Document doc, boolean checkRange) throws Exception { - ArrayList expectedProblems = new ArrayList(expectedDoc.getRootElement().getChildren("problem")); - ArrayList reportedProblems = new ArrayList(doc.getRootElement().getChildren("problem")); + List expectedProblems = new ArrayList(expectedDoc.getRootElement().getChildren("problem")); + List reportedProblems = new ArrayList(doc.getRootElement().getChildren("problem")); Element[] expectedArrayed = expectedProblems.toArray(new Element[expectedProblems.size()]); boolean failed = false; diff --git a/inspections/impl/com/intellij/codeInspection/emptyMethod/EmptyMethodInspection.java b/inspections/impl/com/intellij/codeInspection/emptyMethod/EmptyMethodInspection.java index 0238810983..78866a17cc 100644 --- a/inspections/impl/com/intellij/codeInspection/emptyMethod/EmptyMethodInspection.java +++ b/inspections/impl/com/intellij/codeInspection/emptyMethod/EmptyMethodInspection.java @@ -57,72 +57,73 @@ public class EmptyMethodInspection extends GlobalJavaInspectionTool { InspectionManager manager, GlobalInspectionContext globalContext, ProblemDescriptionsProcessor processor) { - if (refEntity instanceof RefMethod) { - final RefMethod refMethod = (RefMethod)refEntity; + if (!(refEntity instanceof RefMethod)) { + return null; + } + final RefMethod refMethod = (RefMethod)refEntity; - if (!isBodyEmpty(refMethod)) return null; - if (refMethod.isConstructor()) return null; - if (refMethod.isSyntheticJSP()) return null; + if (!isBodyEmpty(refMethod)) return null; + if (refMethod.isConstructor()) return null; + if (refMethod.isSyntheticJSP()) return null; - for (RefMethod refSuper : refMethod.getSuperMethods()) { - if (checkElement(refSuper, scope, manager, globalContext, processor) != null) return null; - } + for (RefMethod refSuper : refMethod.getSuperMethods()) { + if (checkElement(refSuper, scope, manager, globalContext, processor) != null) return null; + } - String message = null; - boolean needToDeleteHierarchy = false; - if (refMethod.isOnlyCallsSuper() && !refMethod.isFinal()) { - RefMethod refSuper = findSuperWithBody(refMethod); - final RefJavaUtil refUtil = RefJavaUtil.getInstance(); - if (refSuper != null && Comparing.strEqual(refMethod.getAccessModifier(), refSuper.getAccessModifier())){ - if (Comparing.strEqual(refSuper.getAccessModifier(), PsiModifier.PROTECTED) //protected modificator gives access to method in another package - && !Comparing.strEqual(refUtil.getPackageName(refSuper), refUtil.getPackageName(refMethod))) return null; - } - if (refSuper == null || refUtil.compareAccess(refMethod.getAccessModifier(), refSuper.getAccessModifier()) <= 0) { - message = InspectionsBundle.message("inspection.empty.method.problem.descriptor"); - } + String message = null; + boolean needToDeleteHierarchy = false; + if (refMethod.isOnlyCallsSuper() && !refMethod.isFinal()) { + RefMethod refSuper = findSuperWithBody(refMethod); + final RefJavaUtil refUtil = RefJavaUtil.getInstance(); + if (refSuper != null && Comparing.strEqual(refMethod.getAccessModifier(), refSuper.getAccessModifier())){ + if (Comparing.strEqual(refSuper.getAccessModifier(), PsiModifier.PROTECTED) //protected modificator gives access to method in another package + && !Comparing.strEqual(refUtil.getPackageName(refSuper), refUtil.getPackageName(refMethod))) return null; } - else if (refMethod.hasBody() && hasEmptySuperImplementation(refMethod)) { - - message = InspectionsBundle.message("inspection.empty.method.problem.descriptor1"); + if (refSuper == null || refUtil.compareAccess(refMethod.getAccessModifier(), refSuper.getAccessModifier()) <= 0) { + message = InspectionsBundle.message("inspection.empty.method.problem.descriptor"); } - else if (areAllImplementationsEmpty(refMethod)) { - if (refMethod.hasBody()) { - if (refMethod.getDerivedMethods().isEmpty()) { - if (refMethod.getSuperMethods().isEmpty()) { - message = InspectionsBundle.message("inspection.empty.method.problem.descriptor2"); - } - } - else { - needToDeleteHierarchy = true; - message = InspectionsBundle.message("inspection.empty.method.problem.descriptor3"); + } + else if (refMethod.hasBody() && hasEmptySuperImplementation(refMethod)) { + + message = InspectionsBundle.message("inspection.empty.method.problem.descriptor1"); + } + else if (areAllImplementationsEmpty(refMethod)) { + if (refMethod.hasBody()) { + if (refMethod.getDerivedMethods().isEmpty()) { + if (refMethod.getSuperMethods().isEmpty()) { + message = InspectionsBundle.message("inspection.empty.method.problem.descriptor2"); } } else { - if (!refMethod.getDerivedMethods().isEmpty()) { - needToDeleteHierarchy = true; - message = InspectionsBundle.message("inspection.empty.method.problem.descriptor4"); - } + needToDeleteHierarchy = true; + message = InspectionsBundle.message("inspection.empty.method.problem.descriptor3"); + } + } + else { + if (!refMethod.getDerivedMethods().isEmpty()) { + needToDeleteHierarchy = true; + message = InspectionsBundle.message("inspection.empty.method.problem.descriptor4"); } } + } - if (message != null) { - final ArrayList fixes = new ArrayList(); - fixes.add(getFix(processor, needToDeleteHierarchy)); - SpecialAnnotationsUtil.createAddToSpecialAnnotationFixes(refMethod.getElement(), new Processor() { - public boolean process(final String qualifiedName) { - fixes.add(SpecialAnnotationsUtil.createAddToSpecialAnnotationsListQuickFix( - QuickFixBundle.message("fix.add.special.annotation.text", qualifiedName), - QuickFixBundle.message("fix.add.special.annotation.family"), - EXCLUDE_ANNOS, qualifiedName, refMethod.getElement())); - return true; - } - }); + if (message != null) { + final ArrayList fixes = new ArrayList(); + fixes.add(getFix(processor, needToDeleteHierarchy)); + SpecialAnnotationsUtil.createAddToSpecialAnnotationFixes(refMethod.getElement(), new Processor() { + public boolean process(final String qualifiedName) { + fixes.add(SpecialAnnotationsUtil.createAddToSpecialAnnotationsListQuickFix( + QuickFixBundle.message("fix.add.special.annotation.text", qualifiedName), + QuickFixBundle.message("fix.add.special.annotation.family"), + EXCLUDE_ANNOS, qualifiedName, refMethod.getElement())); + return true; + } + }); - final ProblemDescriptor descriptor = manager.createProblemDescriptor(refMethod.getElement().getNavigationElement(), message, - fixes.toArray(new LocalQuickFix[fixes.size()]), - ProblemHighlightType.GENERIC_ERROR_OR_WARNING); - return new ProblemDescriptor[]{descriptor}; - } + final ProblemDescriptor descriptor = manager.createProblemDescriptor(refMethod.getElement().getNavigationElement(), message, + fixes.toArray(new LocalQuickFix[fixes.size()]), + ProblemHighlightType.GENERIC_ERROR_OR_WARNING); + return new ProblemDescriptor[]{descriptor}; } return null; diff --git a/inspections/impl/com/intellij/codeInspection/reference/RefMethodImpl.java b/inspections/impl/com/intellij/codeInspection/reference/RefMethodImpl.java index e9f02bebda..1c0500338f 100644 --- a/inspections/impl/com/intellij/codeInspection/reference/RefMethodImpl.java +++ b/inspections/impl/com/intellij/codeInspection/reference/RefMethodImpl.java @@ -19,12 +19,10 @@ import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; +import java.util.*; public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { - private static final ArrayList EMPTY_METHOD_LIST = new ArrayList(0); + private static final List EMPTY_METHOD_LIST = Collections.emptyList(); private static final RefParameter[] EMPTY_PARAMS_ARRAY = new RefParameter[0]; private static final int IS_APPMAIN_MASK = 0x10000; @@ -124,10 +122,10 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { private void checkForSuperCall(PsiMethod method) { if (isConstructor()) { - boolean isBaseExplicitlyCalled = false; PsiCodeBlock body = method.getBody(); if (body == null) return; PsiStatement[] statements = body.getStatements(); + boolean isBaseExplicitlyCalled = false; if (statements.length > 0) { PsiStatement first = statements[0]; if (first instanceof PsiExpressionStatement) { @@ -280,7 +278,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { @NonNls final String name = method.getName(); if (getOwnerClass().isTestCase() && name.startsWith("test")) return; - if (getSuperMethods().size() == 0) { + if (getSuperMethods().isEmpty()) { PsiClassType[] throwsList = method.getThrowsList().getReferencedTypes(); if (throwsList.length > 0) { myUnThrownExceptions = new ArrayList(throwsList.length); @@ -345,7 +343,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { } public boolean hasSuperMethods() { - return getSuperMethods().size() > 0 || isLibraryOverride(new HashSet()); + return !getSuperMethods().isEmpty() || isLibraryOverride(new HashSet()); } public boolean isReferenced() { @@ -493,7 +491,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { public void updateReturnValueTemplate(PsiExpression expression) { if (myReturnValueTemplate == null) return; - if (getSuperMethods().size() > 0) { + if (!getSuperMethods().isEmpty()) { for (final RefMethod refMethod : getSuperMethods()) { RefMethodImpl refSuper = (RefMethodImpl)refMethod; refSuper.updateReturnValueTemplate(expression); @@ -529,7 +527,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { public void updateParameterValues(PsiExpression[] args) { if (isExternalOverride()) return; - if (getSuperMethods().size() > 0) { + if (!getSuperMethods().isEmpty()) { for (RefMethod refSuper : getSuperMethods()) { ((RefMethodImpl)refSuper).updateParameterValues(args); } @@ -556,7 +554,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { } public void updateThrowsList(PsiClassType exceptionType) { - if (getSuperMethods().size() > 0) { + if (!getSuperMethods().isEmpty()) { for (RefMethod refSuper : getSuperMethods()) { ((RefMethodImpl)refSuper).updateThrowsList(exceptionType); } @@ -575,7 +573,7 @@ public class RefMethodImpl extends RefJavaElementImpl implements RefMethod { } } - if (myUnThrownExceptions.size() == 0) myUnThrownExceptions = null; + if (myUnThrownExceptions.isEmpty()) myUnThrownExceptions = null; } } diff --git a/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java b/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java index 24a6ac48e3..62403f4b91 100644 --- a/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java +++ b/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java @@ -295,8 +295,8 @@ public class InspectionTree extends Tree { } private class SelectionPath { - private Object[] myPath; - private int[] myIndicies; + private final Object[] myPath; + private final int[] myIndicies; public SelectionPath(TreePath path) { myPath = path.getPath(); diff --git a/openapi/src/com/intellij/psi/util/PsiFormatUtil.java b/openapi/src/com/intellij/psi/util/PsiFormatUtil.java index 24b7988cb3..3613c838f8 100644 --- a/openapi/src/com/intellij/psi/util/PsiFormatUtil.java +++ b/openapi/src/com/intellij/psi/util/PsiFormatUtil.java @@ -16,10 +16,10 @@ package com.intellij.psi.util; import com.intellij.psi.*; -import com.intellij.util.StringBuilderSpinAllocator; +import com.intellij.openapi.util.text.StringUtil; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.annotations.NonNls; /** * @@ -46,23 +46,23 @@ public class PsiFormatUtil { public static String formatVariable(PsiVariable variable, int options, PsiSubstitutor substitutor){ StringBuilder buffer = new StringBuilder(); + formatVariable(variable, options, substitutor,buffer); + return buffer.toString(); + } + private static void formatVariable(PsiVariable variable, int options, PsiSubstitutor substitutor,StringBuilder buffer){ if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) == 0){ - buffer.append(formatModifiers(variable, options)); + formatModifiers(variable, options,buffer); } if ((options & SHOW_TYPE) != 0 && (options & TYPE_AFTER) == 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); buffer.append(formatType(variable.getType(), options, substitutor)); } if (variable instanceof PsiField && (options & SHOW_CONTAINING_CLASS) != 0){ PsiClass aClass = ((PsiField)variable).getContainingClass(); if (aClass != null){ - if (buffer.length() > 0){ - buffer.append(' '); - } String className = aClass.getName(); if (className != null) { + appendSpaceIfNeeded(buffer); if ((options & SHOW_FQ_NAME) != 0){ String qName = aClass.getQualifiedName(); if (qName != null){ @@ -86,9 +86,7 @@ public class PsiFormatUtil { if ((options & SHOW_NAME) != 0){ String name = variable.getName(); if (name != null){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); buffer.append(name); } } @@ -100,13 +98,7 @@ public class PsiFormatUtil { buffer.append(formatType(variable.getType(), options, substitutor)); } if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) != 0){ - String modifiers = formatModifiers(variable, options); - if (modifiers.length() > 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } - buffer.append(modifiers); - } + formatModifiers(variable, options,buffer); } if ((options & SHOW_INITIALIZER) != 0){ PsiExpression initializer = variable.getInitializer(); @@ -124,7 +116,6 @@ public class PsiFormatUtil { } } } - return buffer.toString(); } public static String formatMethod(PsiMethod method, PsiSubstitutor substitutor, int options, int parameterOptions){ @@ -133,24 +124,24 @@ public class PsiFormatUtil { public static String formatMethod(PsiMethod method, PsiSubstitutor substitutor, int options, int parameterOptions, int maxParametersToShow){ StringBuilder buffer = new StringBuilder(); + formatMethod(method, substitutor, options, parameterOptions, maxParametersToShow,buffer); + return buffer.toString(); + } + private static void formatMethod(PsiMethod method, PsiSubstitutor substitutor, int options, int parameterOptions, int maxParametersToShow, StringBuilder buffer){ if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) == 0){ - buffer.append(formatModifiers(method, options)); + formatModifiers(method, options,buffer); } if ((options & SHOW_TYPE) != 0 && (options & TYPE_AFTER) == 0){ PsiType type = method.getReturnType(); if (type != null){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); buffer.append(formatType(type, options, substitutor)); } } if ((options & SHOW_CONTAINING_CLASS) != 0){ PsiClass aClass = method.getContainingClass(); if (aClass != null){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); String name = aClass.getName(); if (name != null) { if ((options & SHOW_FQ_NAME) != 0){ @@ -174,9 +165,7 @@ public class PsiFormatUtil { } else{ if ((options & SHOW_NAME) != 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); buffer.append(method.getName()); } } @@ -205,32 +194,28 @@ public class PsiFormatUtil { } } if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) != 0){ - String modifiers = formatModifiers(method, options); - if (modifiers.length() > 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } - buffer.append(modifiers); - } + formatModifiers(method, options,buffer); } if ((options & SHOW_THROWS) != 0){ String throwsText = formatReferenceList(method.getThrowsList(), options); if (throwsText.length() > 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); //noinspection HardCodedStringLiteral buffer.append("throws "); buffer.append(throwsText); } } - return buffer.toString(); } + private static void appendSpaceIfNeeded(StringBuilder buffer) { + if (buffer.length() != 0 && !StringUtil.endsWithChar(buffer, ' ')) { + buffer.append(' '); + } + } @NotNull public static String formatClass(@NotNull PsiClass aClass, int options){ StringBuilder buffer = new StringBuilder(); if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) == 0){ - buffer.append(formatModifiers(aClass, options)); + formatModifiers(aClass, options,buffer); } if ((options & SHOW_NAME) != 0){ if (aClass instanceof PsiAnonymousClass && (options & SHOW_ANONYMOUS_CLASS_VERBOSE) != 0) { @@ -242,9 +227,7 @@ public class PsiFormatUtil { else { String name = aClass.getName(); if (name != null) { - if (buffer.length() > 0) { - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); if ((options & SHOW_FQ_NAME) != 0) { String qName = aClass.getQualifiedName(); if (qName != null) { @@ -261,29 +244,19 @@ public class PsiFormatUtil { } } if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) != 0){ - String modifiers = formatModifiers(aClass, options); - if (modifiers.length() > 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } - buffer.append(modifiers); - } + formatModifiers(aClass, options,buffer); } if ((options & SHOW_EXTENDS_IMPLEMENTS) != 0){ String extendsText = formatReferenceList(aClass.getExtendsList(), options); if (extendsText.length() > 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); //noinspection HardCodedStringLiteral buffer.append("extends "); buffer.append(extendsText); } String implementsText = formatReferenceList(aClass.getImplementsList(), options); if (implementsText.length() > 0){ - if (buffer.length() > 0){ - buffer.append(' '); - } + appendSpaceIfNeeded(buffer); //noinspection HardCodedStringLiteral buffer.append("implements "); buffer.append(implementsText); @@ -293,6 +266,11 @@ public class PsiFormatUtil { } public static String formatModifiers(PsiElement element, int options) throws IllegalArgumentException{ + StringBuilder buffer = new StringBuilder(); + formatModifiers(element, options,buffer); + return buffer.toString(); + } + private static void formatModifiers(PsiElement element, int options, StringBuilder buffer) throws IllegalArgumentException{ PsiModifierList list; boolean isInterface = false; if (element instanceof PsiVariable){ @@ -304,20 +282,21 @@ public class PsiFormatUtil { else if (element instanceof PsiClass){ isInterface = ((PsiClass)element).isInterface(); list = ((PsiClass)element).getModifierList(); - if (list == null) return ""; + if (list == null) return; } else if (element instanceof PsiClassInitializer){ list = ((PsiClassInitializer)element).getModifierList(); - if (list == null) return ""; + if (list == null) return; } else{ throw new IllegalArgumentException(); } - if (list == null) return ""; - StringBuilder buffer = new StringBuilder(); - if ((options & SHOW_REDUNDANT_MODIFIERS) != 0 - ? list.hasModifierProperty(PsiModifier.PUBLIC) - : list.hasExplicitModifier(PsiModifier.PUBLIC)) appendModifier(buffer, PsiModifier.PUBLIC); + if (list == null) return; + if ((options & SHOW_REDUNDANT_MODIFIERS) == 0 + ? list.hasExplicitModifier(PsiModifier.PUBLIC) + : list.hasModifierProperty(PsiModifier.PUBLIC)) { + appendModifier(buffer, PsiModifier.PUBLIC); + } if (list.hasModifierProperty(PsiModifier.PROTECTED)){ appendModifier(buffer, PsiModifier.PROTECTED); @@ -326,9 +305,9 @@ public class PsiFormatUtil { appendModifier(buffer, PsiModifier.PRIVATE); } - if ((options & SHOW_REDUNDANT_MODIFIERS) != 0 - ? list.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) - : list.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL)) { + if ((options & SHOW_REDUNDANT_MODIFIERS) == 0 + ? list.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL) + : list.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { if (element instanceof PsiClass && element.getParent() instanceof PsiDeclarationStatement) {// local class appendModifier(buffer, PsiBundle.message("local.class.preposition")); } @@ -337,18 +316,18 @@ public class PsiFormatUtil { } } - if ((options & SHOW_REDUNDANT_MODIFIERS) != 0 - ? list.hasModifierProperty(PsiModifier.STATIC) - : list.hasExplicitModifier(PsiModifier.STATIC)) appendModifier(buffer, PsiModifier.STATIC); + if ((options & SHOW_REDUNDANT_MODIFIERS) == 0 + ? list.hasExplicitModifier(PsiModifier.STATIC) + : list.hasModifierProperty(PsiModifier.STATIC)) appendModifier(buffer, PsiModifier.STATIC); if (!isInterface && //cls modifier list - ((options & SHOW_REDUNDANT_MODIFIERS) != 0 - ? list.hasModifierProperty(PsiModifier.ABSTRACT) - : list.hasExplicitModifier(PsiModifier.ABSTRACT))) appendModifier(buffer, PsiModifier.ABSTRACT); + ((options & SHOW_REDUNDANT_MODIFIERS) == 0 + ? list.hasExplicitModifier(PsiModifier.ABSTRACT) + : list.hasModifierProperty(PsiModifier.ABSTRACT))) appendModifier(buffer, PsiModifier.ABSTRACT); - if ((options & SHOW_REDUNDANT_MODIFIERS) != 0 - ? list.hasModifierProperty(PsiModifier.FINAL) - : list.hasExplicitModifier(PsiModifier.FINAL)) appendModifier(buffer, PsiModifier.FINAL); + if ((options & SHOW_REDUNDANT_MODIFIERS) == 0 + ? list.hasExplicitModifier(PsiModifier.FINAL) + : list.hasModifierProperty(PsiModifier.FINAL)) appendModifier(buffer, PsiModifier.FINAL); if (list.hasModifierProperty(PsiModifier.NATIVE) && (options & JAVADOC_MODIFIERS_ONLY) == 0){ appendModifier(buffer, PsiModifier.NATIVE); @@ -367,15 +346,11 @@ public class PsiFormatUtil { if (list.hasModifierProperty(PsiModifier.VOLATILE)){ appendModifier(buffer, PsiModifier.VOLATILE); } - if (buffer.length() > 0){ - buffer.setLength(buffer.length() - 1); - } - return buffer.toString(); } private static void appendModifier(final StringBuilder buffer, final String modifier) { + appendSpaceIfNeeded(buffer); buffer.append(modifier); - buffer.append(' '); } public static String formatReferenceList(PsiReferenceList list, int options){ @@ -396,21 +371,11 @@ public class PsiFormatUtil { if ((options & SHOW_RAW_TYPE) != 0) { type = TypeConversionUtil.erasure(type); } - if ((options & SHOW_FQ_CLASS_NAMES) != 0){ - return type.getInternalCanonicalText(); - } - else{ - return type.getPresentableText(); - } + return (options & SHOW_FQ_CLASS_NAMES) == 0 ? type.getPresentableText() : type.getInternalCanonicalText(); } public static String formatReference(PsiJavaCodeReferenceElement ref, int options){ - if ((options & SHOW_FQ_CLASS_NAMES) != 0){ - return ref.getCanonicalText(); - } - else{ - return ref.getText(); - } + return (options & SHOW_FQ_CLASS_NAMES) == 0 ? ref.getText() : ref.getCanonicalText(); } @Nullable @@ -420,48 +385,54 @@ public class PsiFormatUtil { @Nullable public static String getExternalName(PsiModifierListOwner owner, final boolean showParamName) { - final StringBuilder builder = StringBuilderSpinAllocator.alloc(); - try { - if (owner instanceof PsiClass) { - ClassUtil.formatClassName((PsiClass)owner, builder); - return builder.toString(); - } - final PsiClass psiClass = PsiTreeUtil.getParentOfType(owner, PsiClass.class, false); - assert psiClass != null; - ClassUtil.formatClassName(psiClass, builder); - if (owner instanceof PsiMethod) { - return builder.toString() + " " + formatMethod((PsiMethod)owner, PsiSubstitutor.EMPTY, - SHOW_NAME | SHOW_FQ_NAME | SHOW_TYPE | SHOW_PARAMETERS | SHOW_FQ_CLASS_NAMES, - showParamName ? (SHOW_NAME | SHOW_TYPE | SHOW_FQ_CLASS_NAMES) : (SHOW_TYPE | SHOW_FQ_CLASS_NAMES)); - } - else if (owner instanceof PsiField) { - return builder.toString() + " " + ((PsiField)owner).getName(); - } - else if (owner instanceof PsiParameter) { - final PsiElement declarationScope = ((PsiParameter)owner).getDeclarationScope(); - if (declarationScope instanceof PsiMethod) { - final PsiMethod psiMethod = (PsiMethod)declarationScope; - return builder.toString() + " " + formatMethod(psiMethod, PsiSubstitutor.EMPTY, - SHOW_NAME | SHOW_FQ_NAME | SHOW_TYPE | SHOW_PARAMETERS | SHOW_FQ_CLASS_NAMES, - showParamName ? (SHOW_NAME | SHOW_TYPE | SHOW_FQ_CLASS_NAMES) : (SHOW_TYPE | SHOW_FQ_CLASS_NAMES)) + " " + - (showParamName ? formatVariable((PsiVariable)owner, SHOW_NAME, PsiSubstitutor.EMPTY) : psiMethod.getParameterList().getParameterIndex((PsiParameter)owner)); + final StringBuilder builder = new StringBuilder(); + if (owner instanceof PsiClass) { + ClassUtil.formatClassName((PsiClass)owner, builder); + return builder.toString(); + } + final PsiClass psiClass = PsiTreeUtil.getParentOfType(owner, PsiClass.class, false); + assert psiClass != null; + ClassUtil.formatClassName(psiClass, builder); + if (owner instanceof PsiMethod) { + builder.append(" "); + formatMethod((PsiMethod)owner, PsiSubstitutor.EMPTY, + SHOW_NAME | SHOW_FQ_NAME | SHOW_TYPE | SHOW_PARAMETERS | SHOW_FQ_CLASS_NAMES, + showParamName ? SHOW_NAME | SHOW_TYPE | SHOW_FQ_CLASS_NAMES : SHOW_TYPE | SHOW_FQ_CLASS_NAMES, MAX_PARAMS_TO_SHOW, builder); + } + else if (owner instanceof PsiField) { + builder.append(" ").append(((PsiField)owner).getName()); + } + else if (owner instanceof PsiParameter) { + final PsiElement declarationScope = ((PsiParameter)owner).getDeclarationScope(); + if (declarationScope instanceof PsiMethod) { + final PsiMethod psiMethod = (PsiMethod)declarationScope; + + builder.append(" "); + formatMethod(psiMethod, PsiSubstitutor.EMPTY, + SHOW_NAME | SHOW_FQ_NAME | SHOW_TYPE | SHOW_PARAMETERS | SHOW_FQ_CLASS_NAMES, + showParamName ? SHOW_NAME | SHOW_TYPE | SHOW_FQ_CLASS_NAMES : SHOW_TYPE | SHOW_FQ_CLASS_NAMES, MAX_PARAMS_TO_SHOW, builder); + builder.append(" "); + + if (showParamName) { + formatVariable((PsiVariable)owner, SHOW_NAME, PsiSubstitutor.EMPTY, builder); } + else { + builder.append(psiMethod.getParameterList().getParameterIndex((PsiParameter)owner)); + } + } + else { + return null; } } - finally { - StringBuilderSpinAllocator.dispose(builder); + else { + return null; } - return null; + return builder.toString(); } public static String getPackageDisplayName(@NotNull final PsiClass psiClass) { @NonNls String packageName = psiClass.getQualifiedName(); - if (packageName != null && packageName.lastIndexOf('.') > 0) { - packageName = packageName.substring(0, packageName.lastIndexOf('.')); - } - else { - packageName = ""; - } + packageName = packageName == null || packageName.lastIndexOf('.') <= 0 ? "" : packageName.substring(0, packageName.lastIndexOf('.')); if (packageName.length() == 0) { packageName = "default package"; } -- 2.11.4.GIT