From a00d32721fce2daa06c5f5208e92de283aa932b6 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 28 Jan 2010 13:41:53 +0100 Subject: [PATCH] =?UTF-8?q?IDEA-51755=20(Quickfix=20for=20"=E2=80=8BInstan?= =?UTF-8?q?tiating=20object=20to=20get=20class=20object"=20fails=20with=20?= =?UTF-8?q?arrays)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...atingObjectToGetClassObjectInspection.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/InstantiatingObjectToGetClassObjectInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/InstantiatingObjectToGetClassObjectInspection.java index 3c1ff82eeacf..e50855883ca6 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/InstantiatingObjectToGetClassObjectInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/InstantiatingObjectToGetClassObjectInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,22 +29,26 @@ import org.jetbrains.annotations.NotNull; public class InstantiatingObjectToGetClassObjectInspection extends BaseInspection { + @Override @NotNull public String getDisplayName() { return InspectionGadgetsBundle.message( "instantiating.object.to.get.class.object.display.name"); } + @Override public boolean isEnabledByDefault() { return true; } + @Override @NotNull protected String buildErrorString(Object... infos) { return InspectionGadgetsBundle.message( "instantiating.object.to.get.class.object.problem.descriptor"); } + @Override protected InspectionGadgetsFix buildFix(Object... infos) { return new InstantiatingObjectToGetClassObjectFix(); } @@ -58,6 +62,7 @@ public class InstantiatingObjectToGetClassObjectInspection "instantiating.object.to.get.class.object.replace.quickfix"); } + @Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiMethodCallExpression expression = @@ -70,15 +75,27 @@ public class InstantiatingObjectToGetClassObjectInspection return; } final PsiType type = qualifier.getType(); - if (type == null || !(type instanceof PsiClassType)) { + if (type == null) { return; } - final PsiClassType classType = (PsiClassType)type; - final String text = classType.getClassName(); - replaceExpression(expression, text + ".class"); + replaceExpression(expression, + getTypeText(type, new StringBuilder()) + ".class"); + } + + private static StringBuilder getTypeText(PsiType type, + StringBuilder text) { + if (type instanceof PsiArrayType) { + text.append("[]"); + final PsiArrayType arrayType = (PsiArrayType)type; + getTypeText(arrayType.getComponentType(), text); + } else { + text.insert(0, type.getCanonicalText()); + } + return text; } } + @Override public BaseInspectionVisitor buildVisitor() { return new InstantiatingObjectToGetClassObjectVisitor(); }