/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/exception/GroovyEmptyFinallyBlockInspection.java

https://bitbucket.org/nbargnesi/idea · Java · 67 lines · 42 code · 10 blank · 15 comment · 4 complexity · 0f69468cf60e17168e8b40240d1ef425 MD5 · raw file

  1. /*
  2. * Copyright 2007-2008 Dave Griffith
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jetbrains.plugins.groovy.codeInspection.exception;
  17. import org.jetbrains.annotations.Nls;
  18. import org.jetbrains.annotations.NotNull;
  19. import org.jetbrains.annotations.Nullable;
  20. import org.jetbrains.plugins.groovy.codeInspection.BaseInspection;
  21. import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
  22. import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrFinallyClause;
  23. import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
  24. import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
  25. public class GroovyEmptyFinallyBlockInspection extends BaseInspection {
  26. @Nls
  27. @NotNull
  28. public String getGroupDisplayName() {
  29. return ERROR_HANDLING;
  30. }
  31. @Nls
  32. @NotNull
  33. public String getDisplayName() {
  34. return "Empty 'finally' block";
  35. }
  36. @Nullable
  37. protected String buildErrorString(Object... args) {
  38. return "Empty '#ref' block #loc";
  39. }
  40. public BaseInspectionVisitor buildVisitor() {
  41. return new Visitor();
  42. }
  43. private static class Visitor extends BaseInspectionVisitor {
  44. public void visitFinallyClause(GrFinallyClause finallyClause) {
  45. super.visitFinallyClause(finallyClause);
  46. final GrOpenBlock body = finallyClause.getBody();
  47. if (body == null || !isEmpty(body)) {
  48. return;
  49. }
  50. registerError(finallyClause.getFirstChild());
  51. }
  52. private static boolean isEmpty(GrOpenBlock body) {
  53. final GrStatement[] statements = body.getStatements();
  54. return statements.length == 0;
  55. }
  56. }
  57. }