001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 021 package org.sonar.java.ast.visitor; 022 023 import com.puppycrawl.tools.checkstyle.api.AnnotationUtility; 024 import com.puppycrawl.tools.checkstyle.api.DetailAST; 025 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027 /** 028 * Inspired by {@link com.puppycrawl.tools.checkstyle.checks.annotation.SuppressWarningsCheck} 029 */ 030 public final class SuppressWarningsAnnotationUtils { 031 032 private final static String SUPPRESS_WARNINGS_ANNOTATION_NAME = "SuppressWarnings"; 033 private final static String SUPPRESS_WARNINGS_ANNOTATION_FQ_NAME = "java.lang." + SUPPRESS_WARNINGS_ANNOTATION_NAME; 034 private final static String VALUE = "\"all\""; 035 036 public static boolean isSuppressAllWarnings(DetailAST ast) { 037 DetailAST suppressWarningsAnnotation = getSuppressWarningsAnnotation(ast); 038 if (suppressWarningsAnnotation != null) { 039 DetailAST warningHolder = findWarningsHolder(suppressWarningsAnnotation); 040 for (DetailAST warning = warningHolder.findFirstToken(TokenTypes.EXPR); warning != null; warning = warning.getNextSibling()) { 041 if (warning.getType() == TokenTypes.EXPR) { 042 DetailAST fChild = warning.getFirstChild(); 043 if (fChild.getType() == TokenTypes.STRING_LITERAL) { 044 String text = warning.getFirstChild().getText(); 045 if (VALUE.equals(text)) { 046 return true; 047 } 048 } 049 } 050 } 051 } 052 return false; 053 } 054 055 private static DetailAST findWarningsHolder(DetailAST aAnnotation) { 056 DetailAST annValuePair = aAnnotation.findFirstToken(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR); 057 DetailAST annArrayInit; 058 if (annValuePair != null) { 059 annArrayInit = annValuePair.findFirstToken(TokenTypes.ANNOTATION_ARRAY_INIT); 060 } else { 061 annArrayInit = aAnnotation.findFirstToken(TokenTypes.ANNOTATION_ARRAY_INIT); 062 } 063 if (annArrayInit != null) { 064 return annArrayInit; 065 } 066 return aAnnotation; 067 } 068 069 private static DetailAST getSuppressWarningsAnnotation(DetailAST ast) { 070 DetailAST annotation = AnnotationUtility.getAnnotation(ast, SUPPRESS_WARNINGS_ANNOTATION_NAME); 071 if (annotation == null) { 072 annotation = AnnotationUtility.getAnnotation(ast, SUPPRESS_WARNINGS_ANNOTATION_FQ_NAME); 073 } 074 return annotation; 075 } 076 077 private SuppressWarningsAnnotationUtils() { 078 } 079 080 }