001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 the ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package eu.cqse.check.base; 018 019import java.util.List; 020import java.util.Set; 021 022import eu.cqse.check.framework.core.CheckException; 023import eu.cqse.check.framework.scanner.IToken; 024import eu.cqse.check.java.StaticMethodCallRecognizer; 025 026/** 027 * Parent class that provides the basic functionality for detecting 028 * language-specific unwanted calls. 029 */ 030public abstract class UnwantedMethodCallsCheckBase extends EntityTokenCheckBase { 031 032 /** A description for this check applicable to all extenders. */ 033 public static final String CHECK_DESCRIPTION = "Certain methods like console or debugging methods should be avoided in code" 034 + " because they could introduce unwanted side effects or reduce performance."; 035 036 /** Template method for obtaining the list of unwanted methods. */ 037 protected abstract Set<String> getUnwantedMethods(); 038 039 /** {@inheritDoc} */ 040 @Override 041 protected void processTokens(List<IToken> tokens) throws CheckException { 042 for (String unwantedMethod : getUnwantedMethods()) { 043 createFindingForIndices(new StaticMethodCallRecognizer(unwantedMethod), tokens); 044 } 045 } 046 047 /** 048 * Creates findings for all calls found in the indices. 049 */ 050 private void createFindingForIndices(StaticMethodCallRecognizer recognizer, List<IToken> tokens) 051 throws CheckException { 052 for (int callIndex : recognizer.findCallsInTokens(tokens)) { 053 createFinding("Method `" + recognizer.getFullQualifiedMethodName() + "` should not be called", 054 tokens.get(callIndex), tokens.get(callIndex + recognizer.getTokenLength())); 055 } 056 } 057 058 /** {@inheritDoc} */ 059 @Override 060 protected String getXPathSelectionString() { 061 return "//STATEMENT"; 062 } 063 064}