001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program 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
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi.inspectors;
024:
025: import java.util.List;
026:
027: import org.hammurapi.InspectorBase;
028:
029: import com.pavelvlasov.jsel.JselException;
030: import com.pavelvlasov.jsel.LanguageElement;
031: import com.pavelvlasov.jsel.TypeSpecification;
032: import com.pavelvlasov.jsel.VariableDefinition;
033: import com.pavelvlasov.jsel.expressions.Dot;
034: import com.pavelvlasov.jsel.expressions.Expression;
035: import com.pavelvlasov.jsel.expressions.Ident;
036: import com.pavelvlasov.jsel.expressions.MethodCall;
037: import com.pavelvlasov.jsel.expressions.PrimaryExpression;
038: import com.pavelvlasov.jsel.expressions.StringConstant;
039: import com.pavelvlasov.review.SourceMarker;
040:
041: /**
042: * ER-123
043: * If you have to compare with a string do not use degree.equals("1")) but "1".equals(degree)
044: * @author Janos Czako
045: * @version $Revision: 1.2 $
046: */
047: public class StringLiteralEqualsRule extends InspectorBase {
048:
049: /**
050: * The name of the operation, we are checking.
051: */
052: private static final String EQUALS = "equals";
053:
054: /**
055: * Reviews the methodcalls if they violate against the rule.
056: *
057: * @param element the methodcall to be reviewed.
058: */
059: public void visit(MethodCall element) {
060: PrimaryExpression methodName = element.getName();
061: if (methodName instanceof Dot) {
062: List flatOperands = ((Dot) methodName).getFlatOperands();
063: methodName = (PrimaryExpression) flatOperands
064: .get(flatOperands.size() - 1);
065: }
066:
067: List parameters = element.getParameters();
068: if (methodName instanceof Ident
069: && EQUALS.equals(((Ident) methodName).getText())
070: && parameters.size() == 1) {
071: Expression parameter = (Expression) parameters.get(0);
072: if (parameter instanceof StringConstant) {
073: context.reportViolation((SourceMarker) parameter);
074: } else if (parameter instanceof Ident) {
075: try {
076: // TODO analyze if parameter is a final string constant from another type like MyClass.MY_CONSTANT
077: Object r = ((LanguageElement) parameter)
078: .getEnclosingScope().getVariableNamespace()
079: .find(((Ident) parameter).getText());
080: if (r instanceof VariableDefinition) {
081: VariableDefinition vd = (VariableDefinition) r;
082: TypeSpecification typeSpecification = vd
083: .getTypeSpecification();
084: try {
085: if (vd.getModifiers().contains("final")
086: && vd.getInitializer() != null
087: && typeSpecification
088: .getDimensions() == 0
089: && typeSpecification.getType()
090: .isKindOf(
091: "java.lang.String")) {
092: context
093: .reportViolation((SourceMarker) parameter);
094: }
095: } catch (JselException e) {
096: context
097: .warn(
098: (SourceMarker) typeSpecification,
099: e);
100: }
101: }
102: } catch (JselException e) {
103: context.warn((SourceMarker) parameter, e);
104: }
105: }
106: }
107: }
108:
109: }
|