001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.java.hints;
029:
030: import com.sun.source.tree.BinaryTree;
031: import com.sun.source.tree.Tree;
032: import com.sun.source.tree.Tree.Kind;
033: import com.sun.source.util.TreePath;
034: import com.sun.source.util.Trees;
035: import java.util.Collections;
036: import java.util.EnumSet;
037: import java.util.List;
038: import java.util.Set;
039: import javax.lang.model.element.TypeElement;
040: import javax.lang.model.type.TypeMirror;
041: import org.netbeans.api.java.source.CompilationInfo;
042: import org.netbeans.modules.java.hints.spi.AbstractHint;
043: import org.netbeans.spi.editor.hints.ErrorDescription;
044: import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
045: import org.netbeans.spi.editor.hints.Fix;
046: import org.openide.util.NbBundle;
047:
048: /**
049: *
050: * @author phrebejk
051: */
052: public class WrongStringComparison extends AbstractHint {
053:
054: private static String STRING_TYPE = "java.lang.String"; // NOI18N
055:
056: private static final List<Fix> NO_FIXES = Collections
057: .<Fix> emptyList();
058:
059: private static final Set<Tree.Kind> TREE_KINDS = EnumSet
060: .<Tree.Kind> of(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO);
061:
062: public WrongStringComparison() {
063: super (true, true, AbstractHint.HintSeverity.WARNING);
064: }
065:
066: public Set<Kind> getTreeKinds() {
067: return TREE_KINDS;
068: }
069:
070: public List<ErrorDescription> run(CompilationInfo info,
071: TreePath treePath) {
072:
073: Tree t = treePath.getLeaf();
074:
075: if (t.getKind() != Tree.Kind.EQUAL_TO
076: && t.getKind() != Tree.Kind.NOT_EQUAL_TO) {
077: return null;
078: }
079:
080: BinaryTree bt = (BinaryTree) t;
081:
082: TreePath left = new TreePath(treePath, bt.getLeftOperand());
083: TreePath right = new TreePath(treePath, bt.getRightOperand());
084:
085: Trees trees = info.getTrees();
086: TypeMirror leftType = left == null ? null : trees
087: .getTypeMirror(left);
088: TypeMirror rightType = right == null ? null : trees
089: .getTypeMirror(right);
090:
091: if (leftType != null && rightType != null
092: && STRING_TYPE.equals(leftType.toString())
093: && STRING_TYPE.equals(rightType.toString())) {
094: return Collections
095: .<ErrorDescription> singletonList(ErrorDescriptionFactory
096: .createErrorDescription(
097: getSeverity().toEditorSeverity(),
098: getDisplayName(),
099: // Collections.<Fix>singletonList(new EmptyStatementFix( info.getFileObject(), TreePathHandle.create(tp, info) ) ),
100: NO_FIXES,
101: info.getFileObject(),
102: (int) info
103: .getTrees()
104: .getSourcePositions()
105: .getStartPosition(
106: info
107: .getCompilationUnit(),
108: t),
109: (int) info
110: .getTrees()
111: .getSourcePositions()
112: .getEndPosition(
113: info
114: .getCompilationUnit(),
115: t)));
116:
117: }
118:
119: return null;
120: }
121:
122: public void cancel() {
123: // Does nothing
124: }
125:
126: public String getId() {
127: return "Wrong_String_Comparison"; // NOI18N
128: }
129:
130: public String getDisplayName() {
131: return NbBundle.getMessage(WrongStringComparison.class,
132: "LBL_WrongStringComparison");
133: }
134:
135: public String getDescription() {
136: return NbBundle.getMessage(WrongStringComparison.class,
137: "DSC_WrongStringComparison");
138: }
139:
140: }
|