Moved a few hints to std. distro.
1.1 --- a/javahints/nbproject/project.properties Thu Jun 14 15:02:44 2012 +0200
1.2 +++ b/javahints/nbproject/project.properties Tue Jun 26 16:30:27 2012 +0200
1.3 @@ -50,7 +50,7 @@
1.4 auxiliary.org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width=80
1.5 javac.compilerargs=-Xlint:unchecked
1.6 javac.source=1.6
1.7 -spec.version.base=2.56.0
1.8 +spec.version.base=2.57.0
1.9
1.10 nbm.needs.restart=true
1.11 requires.nb.javac=true
2.1 --- a/javahints/src/org/netbeans/modules/javahints/Bundle.properties Thu Jun 14 15:02:44 2012 +0200
2.2 +++ b/javahints/src/org/netbeans/modules/javahints/Bundle.properties Tue Jun 26 16:30:27 2012 +0200
2.3 @@ -130,16 +130,6 @@
2.4 FIX_ConvertAnonymousToInner=Convert Anonymous to Inner
2.5
2.6
2.7 -DN_NPECheck=Null Dereference
2.8 -DESC_NPECheck=Checks various problems related to dereferencing nulls
2.9 -ERR_DereferencingNull=Dereferencing null
2.10 -ERR_PossiblyDereferencingNull=Possibly Dereferencing null
2.11 -ERR_AssigningNullToNotNull=Assigning Null to NotNull Variable
2.12 -ERR_PossibleAssigingNullToNotNull=Possibly Assigning Null to NotNull Variable
2.13 -ERR_NULL_TO_NON_NULL_ARG=Passing null to not-null argument
2.14 -ERR_POSSIBLENULL_TO_NON_NULL_ARG=Passing possible null to not-null argument
2.15 -
2.16 -
2.17 DN_NegateCondition=Negate if condition
2.18 DESC_NegateCondition=Negate if condition
2.19 FIX_NegateCondition=Negate if condition
3.1 --- a/javahints/src/org/netbeans/modules/javahints/InvertIf.java Thu Jun 14 15:02:44 2012 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,70 +0,0 @@
3.4 -/*
3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.6 - *
3.7 - * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
3.8 - *
3.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
3.10 - * Other names may be trademarks of their respective owners.
3.11 - *
3.12 - * The contents of this file are subject to the terms of either the GNU
3.13 - * General Public License Version 2 only ("GPL") or the Common
3.14 - * Development and Distribution License("CDDL") (collectively, the
3.15 - * "License"). You may not use this file except in compliance with the
3.16 - * License. You can obtain a copy of the License at
3.17 - * http://www.netbeans.org/cddl-gplv2.html
3.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.19 - * specific language governing permissions and limitations under the
3.20 - * License. When distributing the software, include this License Header
3.21 - * Notice in each file and include the License file at
3.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
3.23 - * particular file as subject to the "Classpath" exception as provided
3.24 - * by Oracle in the GPL Version 2 section of the License file that
3.25 - * accompanied this code. If applicable, add the following below the
3.26 - * License Header, with the fields enclosed by brackets [] replaced by
3.27 - * your own identifying information:
3.28 - * "Portions Copyrighted [year] [name of copyright owner]"
3.29 - *
3.30 - * If you wish your version of this file to be governed by only the CDDL
3.31 - * or only the GPL Version 2, indicate your decision by adding
3.32 - * "[Contributor] elects to include this software in this distribution
3.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
3.34 - * single choice of license, a recipient has the option to distribute
3.35 - * your version of this file under either the CDDL, the GPL Version 2 or
3.36 - * to extend the choice of license to its licensees as provided above.
3.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
3.38 - * Version 2 license, then the option applies only if the new code is
3.39 - * made subject to such option by the copyright holder.
3.40 - *
3.41 - * Contributor(s):
3.42 - *
3.43 - * Portions Copyrighted 2012 Sun Microsystems, Inc.
3.44 - */
3.45 -package org.netbeans.modules.javahints;
3.46 -
3.47 -import com.sun.source.util.TreePath;
3.48 -import org.netbeans.spi.editor.hints.ErrorDescription;
3.49 -import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
3.50 -import org.netbeans.spi.java.hints.Hint;
3.51 -import org.netbeans.spi.java.hints.HintContext;
3.52 -import org.netbeans.spi.java.hints.JavaFixUtilities;
3.53 -import org.netbeans.spi.java.hints.TriggerPattern;
3.54 -import org.openide.util.NbBundle.Messages;
3.55 -
3.56 -@Hint(displayName = "#DN_InvertIf", description = "#DESC_InvertIf", category = "general", hintKind= Hint.Kind.ACTION)
3.57 -@Messages({
3.58 - "DN_InvertIf=Invert If",
3.59 - "DESC_InvertIf=Will invert an if statement; negate the condition and switch the statements from the then and else sections."
3.60 -})
3.61 -public class InvertIf {
3.62 -
3.63 - @TriggerPattern(value = "if ($cond) $then; else $else;")
3.64 - @Messages({"ERR_InvertIf=Invert If",
3.65 - "FIX_InvertIf=Invert If"})
3.66 - public static ErrorDescription computeWarning(HintContext ctx) {
3.67 - TreePath cond = ctx.getVariables().get("$cond");
3.68 - long conditionEnd = ctx.getInfo().getTrees().getSourcePositions().getEndPosition(cond.getCompilationUnit(), cond.getParentPath().getLeaf());
3.69 - if (ctx.getCaretLocation() > conditionEnd) return null;
3.70 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_InvertIf(), JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_InvertIf(), ctx.getPath(), "if (!$cond) $else; else $then;"));
3.71 - }
3.72 -
3.73 -}
4.1 --- a/javahints/src/org/netbeans/modules/javahints/NPECheck.java Thu Jun 14 15:02:44 2012 +0200
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,691 +0,0 @@
4.4 -/*
4.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4.6 - *
4.7 - * Copyright 1997-2012 Sun Microsystems, Inc. All rights reserved.
4.8 - *
4.9 - * The contents of this file are subject to the terms of either the GNU
4.10 - * General Public License Version 2 only ("GPL") or the Common
4.11 - * Development and Distribution License("CDDL") (collectively, the
4.12 - * "License"). You may not use this file except in compliance with the
4.13 - * License. You can obtain a copy of the License at
4.14 - * http://www.netbeans.org/cddl-gplv2.html
4.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
4.16 - * specific language governing permissions and limitations under the
4.17 - * License. When distributing the software, include this License Header
4.18 - * Notice in each file and include the License file at
4.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
4.20 - * particular file as subject to the "Classpath" exception as provided
4.21 - * by Sun in the GPL Version 2 section of the License file that
4.22 - * accompanied this code. If applicable, add the following below the
4.23 - * License Header, with the fields enclosed by brackets [] replaced by
4.24 - * your own identifying information:
4.25 - * "Portions Copyrighted [year] [name of copyright owner]"
4.26 - *
4.27 - * Contributor(s):
4.28 - *
4.29 - * Portions Copyrighted 2007-2012 Sun Microsystems, Inc.
4.30 - */
4.31 -
4.32 -package org.netbeans.modules.javahints;
4.33 -
4.34 -import com.sun.source.tree.*;
4.35 -import com.sun.source.tree.Tree.Kind;
4.36 -import com.sun.source.util.TreePath;
4.37 -import com.sun.source.util.TreePathScanner;
4.38 -import java.util.ArrayList;
4.39 -import java.util.EnumSet;
4.40 -import java.util.HashMap;
4.41 -import java.util.HashSet;
4.42 -import java.util.IdentityHashMap;
4.43 -import java.util.List;
4.44 -import java.util.Map;
4.45 -import java.util.Map.Entry;
4.46 -import java.util.Set;
4.47 -import javax.lang.model.element.AnnotationMirror;
4.48 -import javax.lang.model.element.Element;
4.49 -import javax.lang.model.element.ElementKind;
4.50 -import javax.lang.model.element.ExecutableElement;
4.51 -import javax.lang.model.element.TypeElement;
4.52 -import javax.lang.model.element.VariableElement;
4.53 -import org.netbeans.api.java.source.CompilationInfo;
4.54 -import org.netbeans.spi.editor.hints.ErrorDescription;
4.55 -import org.openide.util.NbBundle;
4.56 -
4.57 -import static org.netbeans.modules.javahints.NPECheck.State.*;
4.58 -import org.netbeans.spi.java.hints.*;
4.59 -
4.60 -/**XXX: null initializer to a non-null variable!
4.61 - *
4.62 - * @author lahvac
4.63 - */
4.64 -@Hint(displayName="#DN_NPECheck", description="#DESC_NPECheck", category="bugs", enabled=false)
4.65 -public class NPECheck {
4.66 -
4.67 - @TriggerPattern("$var = $expr")
4.68 - public static ErrorDescription assignment(HintContext ctx) {
4.69 - Element e = ctx.getInfo().getTrees().getElement(ctx.getVariables().get("$var"));
4.70 -
4.71 - if (e == null || !VARIABLE_ELEMENT.contains(e.getKind())) {
4.72 - return null;
4.73 - }
4.74 -
4.75 - TreePath expr = ctx.getVariables().get("$expr");
4.76 - State r = computeExpressionsState(ctx.getInfo()).get(expr.getLeaf());
4.77 -
4.78 - State elementState = getStateFromAnnotations(e);
4.79 -
4.80 - if (elementState != null && elementState.isNotNull()) {
4.81 - String key = null;
4.82 -
4.83 - if (r == NULL) {
4.84 - key = "ERR_AssigningNullToNotNull";
4.85 - }
4.86 -
4.87 - if (r == POSSIBLE_NULL_REPORT) {
4.88 - key = "ERR_PossibleAssigingNullToNotNull";
4.89 - }
4.90 -
4.91 - if (key != null) {
4.92 - return ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), NbBundle.getMessage(NPECheck.class, key));
4.93 - }
4.94 - }
4.95 -
4.96 - return null;
4.97 - }
4.98 -
4.99 - @TriggerPattern("$select.$variable")
4.100 - public static ErrorDescription memberSelect(HintContext ctx) {
4.101 - TreePath select = ctx.getVariables().get("$select");
4.102 - State r = computeExpressionsState(ctx.getInfo()).get(select.getLeaf());
4.103 -
4.104 - if (r == State.NULL) {
4.105 - String displayName = NbBundle.getMessage(NPECheck.class, "ERR_DereferencingNull");
4.106 -
4.107 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
4.108 - }
4.109 -
4.110 - if (r == State.POSSIBLE_NULL_REPORT) {
4.111 - String displayName = NbBundle.getMessage(NPECheck.class, "ERR_PossiblyDereferencingNull");
4.112 -
4.113 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
4.114 - }
4.115 -
4.116 - return null;
4.117 - }
4.118 -
4.119 - @TriggerTreeKind(Kind.METHOD_INVOCATION)
4.120 - public static List<ErrorDescription> methodInvocation(HintContext ctx) {
4.121 - MethodInvocationTree mit = (MethodInvocationTree) ctx.getPath().getLeaf();
4.122 - List<State> paramStates = new ArrayList<State>(mit.getArguments().size());
4.123 - Map<Tree, State> expressionsState = computeExpressionsState(ctx.getInfo());
4.124 -
4.125 - for (Tree param : mit.getArguments()) {
4.126 - State r = expressionsState.get(param);
4.127 - paramStates.add(r != null ? r : State.POSSIBLE_NULL);
4.128 - }
4.129 -
4.130 - Element e = ctx.getInfo().getTrees().getElement(ctx.getPath());
4.131 -
4.132 - if (e == null || e.getKind() != ElementKind.METHOD) {
4.133 - return null;
4.134 - }
4.135 -
4.136 - ExecutableElement ee = (ExecutableElement) e;
4.137 - int index = 0;
4.138 - List<ErrorDescription> result = new ArrayList<ErrorDescription>();
4.139 -
4.140 - for (VariableElement param : ee.getParameters()) {
4.141 - if (getStateFromAnnotations(param) == NOT_NULL) {
4.142 - switch (paramStates.get(index)) {
4.143 - case NULL:
4.144 - result.add(ErrorDescriptionFactory.forTree(ctx, mit.getArguments().get(index), NbBundle.getMessage(NPECheck.class, "ERR_NULL_TO_NON_NULL_ARG")));
4.145 - break;
4.146 - case POSSIBLE_NULL_REPORT:
4.147 - result.add(ErrorDescriptionFactory.forTree(ctx, mit.getArguments().get(index), NbBundle.getMessage(NPECheck.class, "ERR_POSSIBLENULL_TO_NON_NULL_ARG")));
4.148 - break;
4.149 - }
4.150 - }
4.151 - }
4.152 -
4.153 - return result;
4.154 - }
4.155 -
4.156 - private static final Object KEY_EXPRESSION_STATE = new Object();
4.157 - //Cancelling:
4.158 - private static Map<Tree, State> computeExpressionsState(CompilationInfo info) {
4.159 - Map<Tree, State> result = (Map<Tree, State>) info.getCachedValue(KEY_EXPRESSION_STATE);
4.160 -
4.161 - if (result != null) {
4.162 - return result;
4.163 - }
4.164 -
4.165 - VisitorImpl v = new VisitorImpl(info);
4.166 -
4.167 - v.scan(info.getCompilationUnit(), null);
4.168 -
4.169 - info.putCachedValue(KEY_EXPRESSION_STATE, result = v.expressionState, CompilationInfo.CacheClearPolicy.ON_TASK_END);
4.170 -
4.171 - return result;
4.172 - }
4.173 -
4.174 - private static State getStateFromAnnotations(Element e) {
4.175 - return getStateFromAnnotations(e, State.POSSIBLE_NULL);
4.176 - }
4.177 -
4.178 - private static State getStateFromAnnotations(Element e, State def) {
4.179 - for (AnnotationMirror am : e.getAnnotationMirrors()) {
4.180 - String simpleName = ((TypeElement) am.getAnnotationType().asElement()).getSimpleName().toString();
4.181 -
4.182 - if ("Nullable".equals(simpleName) || "NullAllowed".equals(simpleName)) {
4.183 - return State.POSSIBLE_NULL_REPORT;
4.184 - }
4.185 -
4.186 - if ("CheckForNull".equals(simpleName)) {
4.187 - return State.POSSIBLE_NULL_REPORT;
4.188 - }
4.189 -
4.190 - if ("NotNull".equals(simpleName) || "NonNull".equals(simpleName)) {
4.191 - return State.NOT_NULL;
4.192 - }
4.193 - }
4.194 -
4.195 - return def;
4.196 - }
4.197 -
4.198 - private static final class VisitorImpl extends TreePathScanner<State, Void> {
4.199 -
4.200 - private final CompilationInfo info;
4.201 - private Map<VariableElement, State> variable2State = new HashMap<VariableElement, NPECheck.State>();
4.202 - private Map<VariableElement, State> testedTo = new HashMap<VariableElement, NPECheck.State>();
4.203 - private final Map<Tree, State> expressionState = new IdentityHashMap<Tree, State>();
4.204 -
4.205 - public VisitorImpl(CompilationInfo info) {
4.206 - this.info = info;
4.207 - }
4.208 -
4.209 - @Override
4.210 - public State scan(Tree tree, Void p) {
4.211 - State r = super.scan(tree, p);
4.212 -
4.213 - if (r != null) {
4.214 - expressionState.put(tree, r);
4.215 - }
4.216 -
4.217 - return r;
4.218 - }
4.219 -
4.220 - @Override
4.221 - public State visitAssignment(AssignmentTree node, Void p) {
4.222 - Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getVariable()));
4.223 -
4.224 - if (e == null || !VARIABLE_ELEMENT.contains(e.getKind())) {
4.225 - return super.visitAssignment(node, p);
4.226 - }
4.227 -
4.228 - State r = scan(node.getExpression(), p);
4.229 -
4.230 - variable2State.put((VariableElement) e, r);
4.231 -
4.232 - scan(node.getVariable(), p);
4.233 -
4.234 - return r;
4.235 - }
4.236 -
4.237 - @Override
4.238 - public State visitVariable(VariableTree node, Void p) {
4.239 - Element e = info.getTrees().getElement(getCurrentPath());
4.240 -
4.241 - if (e == null) {
4.242 - return super.visitVariable(node, p);
4.243 - }
4.244 -
4.245 - State r = scan(node.getInitializer(), p);
4.246 -
4.247 - variable2State.put((VariableElement) e, r);
4.248 -
4.249 - return r;
4.250 - }
4.251 -
4.252 - @Override
4.253 - public State visitMemberSelect(MemberSelectTree node, Void p) {
4.254 - State expr = scan(node.getExpression(), p);
4.255 - boolean wasNPE = false;
4.256 -
4.257 - if (expr == State.NULL) {
4.258 - wasNPE = true;
4.259 - }
4.260 -
4.261 - if (expr == State.POSSIBLE_NULL_REPORT) {
4.262 - wasNPE = true;
4.263 - }
4.264 -
4.265 - Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getExpression()));
4.266 -
4.267 - if (isVariableElement(e)) {
4.268 - State r = getStateFromAnnotations(e);
4.269 -
4.270 - if (wasNPE) {
4.271 - variable2State.put((VariableElement) e, NOT_NULL_BE_NPE);
4.272 - }
4.273 -
4.274 - return r;
4.275 - }
4.276 -
4.277 - return State.POSSIBLE_NULL;
4.278 - }
4.279 -
4.280 - @Override
4.281 - public State visitLiteral(LiteralTree node, Void p) {
4.282 - if (node.getValue() == null) {
4.283 - return State.NULL;
4.284 - } else {
4.285 - return State.NOT_NULL;
4.286 - }
4.287 - }
4.288 -
4.289 - @Override
4.290 - public State visitIf(IfTree node, Void p) {
4.291 - Map<VariableElement, State> oldTestedTo = testedTo;
4.292 -
4.293 - testedTo = new HashMap<VariableElement, NPECheck.State>();
4.294 -
4.295 - scan(node.getCondition(), p);
4.296 -
4.297 - Map<VariableElement, State> oldVariable2State = variable2State;
4.298 -
4.299 - variable2State = new HashMap<VariableElement, NPECheck.State>(oldVariable2State);
4.300 - variable2State.putAll(testedTo);
4.301 -
4.302 - scan(node.getThenStatement(), null);
4.303 -
4.304 - Map<VariableElement, State> variableStatesAfterThen = new HashMap<VariableElement, NPECheck.State>(variable2State);
4.305 -
4.306 - variable2State = new HashMap<VariableElement, NPECheck.State>(oldVariable2State);
4.307 -
4.308 - for (Map.Entry<VariableElement, State> entry : testedTo.entrySet()) {
4.309 - variable2State.put(entry.getKey(), entry.getValue().reverse());
4.310 - }
4.311 -
4.312 - scan(node.getElseStatement(), null);
4.313 -
4.314 - Map<VariableElement, State> variableStatesAfterElse = new HashMap<VariableElement, NPECheck.State>(variable2State);
4.315 -
4.316 - variable2State = oldVariable2State;
4.317 -
4.318 - Set<VariableElement> uncertain = new HashSet<VariableElement>();
4.319 -
4.320 - if (node.getElseStatement() == null) {
4.321 - for (Entry<VariableElement, State> e : variableStatesAfterThen.entrySet()) {
4.322 - if (testedTo.get(e.getKey()) == State.NULL) {
4.323 - if (e.getValue() != null) {
4.324 - switch (e.getValue()) {
4.325 - case NOT_NULL:
4.326 - case NOT_NULL_BE_NPE:
4.327 - variable2State.put(e.getKey(), e.getValue());
4.328 - break;
4.329 - case POSSIBLE_NULL:
4.330 - variable2State.put(e.getKey(), POSSIBLE_NULL);
4.331 - uncertain.add(e.getKey());
4.332 - break;
4.333 - }
4.334 - }
4.335 - } else {
4.336 - State c = collect(variable2State.get(e.getKey()), e.getValue());
4.337 -
4.338 - variable2State.put(e.getKey(), c);
4.339 - }
4.340 - }
4.341 - } else {
4.342 - for (Entry<VariableElement, State> e : variableStatesAfterThen.entrySet()) {
4.343 - State t = e.getValue();
4.344 - State el = variableStatesAfterElse.get(e.getKey());
4.345 -
4.346 - if (t == el) {
4.347 - variable2State.put(e.getKey(), t);
4.348 - } else {
4.349 - if (t == State.NULL && el == State.NOT_NULL) {
4.350 - variable2State.put(e.getKey(), State.POSSIBLE_NULL_REPORT);
4.351 - }
4.352 - if (el == State.NULL && t == State.NOT_NULL) {
4.353 - variable2State.put(e.getKey(), State.POSSIBLE_NULL_REPORT);
4.354 - }
4.355 - }
4.356 - }
4.357 - }
4.358 -
4.359 - boolean thenExitsFromAllBranches = new ExitsFromAllBranches(info).scan(new TreePath(getCurrentPath(), node.getThenStatement()), null) == Boolean.TRUE;
4.360 -
4.361 - if (!thenExitsFromAllBranches) {
4.362 - for (Entry<VariableElement, State> test : testedTo.entrySet()) {
4.363 - if ((variable2State.get(test.getKey()) == POSSIBLE_NULL || variable2State.get(test.getKey()) == null) && !uncertain.contains(test.getKey())) {
4.364 - variable2State.put(test.getKey(), POSSIBLE_NULL_REPORT);
4.365 - }
4.366 - }
4.367 - } else {
4.368 - for (Entry<VariableElement, State> test : testedTo.entrySet()) {
4.369 - variable2State.put(test.getKey(), test.getValue().reverse());
4.370 - }
4.371 - }
4.372 -
4.373 - testedTo = oldTestedTo;
4.374 -
4.375 - return null;
4.376 - }
4.377 -
4.378 - @Override
4.379 - public State visitBinary(BinaryTree node, Void p) {
4.380 - State left = scan(node.getLeftOperand(), p);
4.381 - boolean rightAlreadyProcessed = false;
4.382 -
4.383 - if (node.getKind() == Kind.CONDITIONAL_AND) {
4.384 - boolean isParentAnd = getCurrentPath().getLeaf().getKind() == Kind.CONDITIONAL_AND;
4.385 - Map<VariableElement, State> oldVariable2State = variable2State;
4.386 - Map<VariableElement, State> oldTestedTo = testedTo;
4.387 -
4.388 - variable2State = new HashMap<VariableElement, NPECheck.State>(variable2State);
4.389 - variable2State.putAll(testedTo);
4.390 -
4.391 - testedTo = new HashMap<VariableElement, State>();
4.392 -
4.393 - scan(node.getRightOperand(), p);
4.394 - variable2State = oldVariable2State;
4.395 -
4.396 - if (isParentAnd) {
4.397 - Map<VariableElement, State> o = new HashMap<VariableElement, NPECheck.State>(oldTestedTo);
4.398 -
4.399 - o.putAll(testedTo);
4.400 -
4.401 - testedTo = o;
4.402 - }
4.403 -
4.404 - rightAlreadyProcessed = true;
4.405 - }
4.406 -
4.407 - if (node.getKind() == Kind.CONDITIONAL_OR) {
4.408 - boolean isParentOr = getCurrentPath().getLeaf().getKind() == Kind.CONDITIONAL_OR;
4.409 - Map<VariableElement, State> oldVariable2State = variable2State;
4.410 - Map<VariableElement, State> oldTestedTo = testedTo;
4.411 -
4.412 - variable2State = new HashMap<VariableElement, NPECheck.State>(variable2State);
4.413 -
4.414 - for (Entry<VariableElement, State> e : testedTo.entrySet()) {
4.415 - variable2State.put(e.getKey(), e.getValue().reverse());
4.416 - }
4.417 -
4.418 - testedTo = new HashMap<VariableElement, State>();
4.419 -
4.420 - scan(node.getRightOperand(), p);
4.421 - variable2State = oldVariable2State;
4.422 -
4.423 - if (isParentOr) {
4.424 - Map<VariableElement, State> o = new HashMap<VariableElement, NPECheck.State>(oldTestedTo);
4.425 -
4.426 - o.putAll(testedTo);
4.427 -
4.428 - testedTo = o;
4.429 - }
4.430 -
4.431 - rightAlreadyProcessed = true;
4.432 - }
4.433 -
4.434 - State right = null;
4.435 -
4.436 - if (!rightAlreadyProcessed) {
4.437 - right = scan(node.getRightOperand(), p);
4.438 - }
4.439 -
4.440 - if (node.getKind() == Kind.EQUAL_TO) {
4.441 - if (right == State.NULL) {
4.442 - Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getLeftOperand()));
4.443 -
4.444 - if (isVariableElement(e)) {
4.445 - testedTo.put((VariableElement) e, State.NULL);
4.446 -
4.447 - return null;
4.448 - }
4.449 - }
4.450 - if (left == State.NULL) {
4.451 - Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getRightOperand()));
4.452 -
4.453 - if (isVariableElement(e)) {
4.454 - testedTo.put((VariableElement) e, State.NULL);
4.455 -
4.456 - return null;
4.457 - }
4.458 - }
4.459 - }
4.460 -
4.461 - if (node.getKind() == Kind.NOT_EQUAL_TO) {
4.462 - if (right == State.NULL) {
4.463 - Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getLeftOperand()));
4.464 -
4.465 - if (isVariableElement(e)) {
4.466 - testedTo.put((VariableElement) e, State.NOT_NULL);
4.467 -
4.468 - return null;
4.469 - }
4.470 - }
4.471 - if (left == State.NULL) {
4.472 - Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getRightOperand()));
4.473 -
4.474 - if (isVariableElement(e)) {
4.475 - testedTo.put((VariableElement) e, State.NOT_NULL);
4.476 -
4.477 - return null;
4.478 - }
4.479 - }
4.480 - }
4.481 -
4.482 - return null;
4.483 - }
4.484 -
4.485 - @Override
4.486 - public State visitConditionalExpression(ConditionalExpressionTree node, Void p) {
4.487 - //TODO: handle the condition similarly to visitIf
4.488 - scan(node.getCondition(), p);
4.489 -
4.490 - Map<VariableElement, State> oldVariable2State = variable2State;
4.491 -
4.492 - variable2State = new HashMap<VariableElement, State>(variable2State);
4.493 -
4.494 - variable2State.putAll(testedTo);
4.495 -
4.496 - State thenSection = scan(node.getTrueExpression(), p);
4.497 -
4.498 - variable2State = new HashMap<VariableElement, State>(variable2State);
4.499 -
4.500 - for (Entry<VariableElement, State> e : testedTo.entrySet()) {
4.501 - variable2State.put(e.getKey(), e.getValue().reverse());
4.502 - }
4.503 -
4.504 - State elseSection = scan(node.getFalseExpression(), p);
4.505 -
4.506 - variable2State = oldVariable2State;
4.507 -
4.508 - State result;
4.509 -
4.510 - if (thenSection == elseSection) {
4.511 - result = thenSection;
4.512 - } else {
4.513 - result = NPECheck.State.NOT_NULL.POSSIBLE_NULL;
4.514 - }
4.515 -
4.516 - for (Entry<VariableElement, State> test : testedTo.entrySet()) {
4.517 - if (variable2State.get(test.getKey()) == POSSIBLE_NULL || variable2State.get(test.getKey()) == null) {
4.518 - variable2State.put(test.getKey(), POSSIBLE_NULL_REPORT);
4.519 - }
4.520 - }
4.521 -
4.522 - return result;
4.523 - }
4.524 -
4.525 - @Override
4.526 - public State visitNewClass(NewClassTree node, Void p) {
4.527 - super.visitNewClass(node, p);
4.528 -
4.529 - return State.NOT_NULL;
4.530 - }
4.531 -
4.532 - @Override
4.533 - public State visitMethodInvocation(MethodInvocationTree node, Void p) {
4.534 - scan(node.getTypeArguments(), p);
4.535 - scan(node.getMethodSelect(), p);
4.536 -
4.537 - for (Tree param : node.getArguments()) {
4.538 - scan(param, p);
4.539 - }
4.540 -
4.541 - Element e = info.getTrees().getElement(getCurrentPath());
4.542 -
4.543 - if (e == null || e.getKind() != ElementKind.METHOD) {
4.544 - return State.POSSIBLE_NULL;
4.545 - }
4.546 -
4.547 - return getStateFromAnnotations(e);
4.548 - }
4.549 -
4.550 - @Override
4.551 - public State visitIdentifier(IdentifierTree node, Void p) {
4.552 - super.visitIdentifier(node, p);
4.553 -
4.554 - Element e = info.getTrees().getElement(getCurrentPath());
4.555 -
4.556 - if (e == null) {
4.557 - return State.POSSIBLE_NULL;
4.558 - }
4.559 -
4.560 - if (e != null) {
4.561 - State s = variable2State.get(e);
4.562 -
4.563 - if (s != null) {
4.564 - return s;
4.565 - }
4.566 - }
4.567 -
4.568 - return getStateFromAnnotations(e);
4.569 - }
4.570 -
4.571 - @Override
4.572 - public State visitWhileLoop(WhileLoopTree node, Void p) {
4.573 - Map<VariableElement, State> oldTestedTo = testedTo;
4.574 -
4.575 - testedTo = new HashMap<VariableElement, State>();
4.576 -
4.577 - Map<VariableElement, State> oldVariable2State = variable2State;
4.578 -
4.579 - variable2State = new HashMap<VariableElement, State>(variable2State);
4.580 -
4.581 - scan(node.getCondition(), p);
4.582 -
4.583 - variable2State.putAll(testedTo);
4.584 -
4.585 - scan(node.getStatement(), p);
4.586 -
4.587 - variable2State = new HashMap<VariableElement, State>(oldVariable2State);
4.588 -
4.589 - for (Entry<VariableElement, State> e : testedTo.entrySet()) {
4.590 - State o = variable2State.get(e.getKey());
4.591 -
4.592 - if (e.getValue() == NOT_NULL && (o == POSSIBLE_NULL || o == null)) {
4.593 - variable2State.put(e.getKey(), POSSIBLE_NULL_REPORT);
4.594 - }
4.595 - }
4.596 -
4.597 - testedTo = oldTestedTo;
4.598 -
4.599 - return null;
4.600 - }
4.601 -
4.602 - }
4.603 -
4.604 - static enum State {
4.605 - NULL,
4.606 - POSSIBLE_NULL,
4.607 - POSSIBLE_NULL_REPORT,
4.608 - NOT_NULL,
4.609 - NOT_NULL_BE_NPE;
4.610 -
4.611 - public State reverse() {
4.612 - switch (this) {
4.613 - case NULL:
4.614 - return NOT_NULL;
4.615 - case POSSIBLE_NULL:
4.616 - case POSSIBLE_NULL_REPORT:
4.617 - return this;
4.618 - case NOT_NULL:
4.619 - case NOT_NULL_BE_NPE:
4.620 - return NULL;
4.621 - default: throw new IllegalStateException();
4.622 - }
4.623 - }
4.624 -
4.625 - public boolean isNotNull() {
4.626 - return this == NOT_NULL || this == NOT_NULL_BE_NPE;
4.627 - }
4.628 -
4.629 - public static State collect(State s1, State s2) {
4.630 - if (s1 == s2) return s1;
4.631 - if (s1 == NULL && s2 != null && s2.isNotNull()) return POSSIBLE_NULL_REPORT;
4.632 - if (s1 != null && s1.isNotNull() && s2 == NULL) return POSSIBLE_NULL_REPORT;
4.633 - if (s1 == POSSIBLE_NULL_REPORT && s2 != POSSIBLE_NULL) return POSSIBLE_NULL_REPORT;
4.634 - if (s1 != POSSIBLE_NULL && s2 == POSSIBLE_NULL_REPORT) return POSSIBLE_NULL_REPORT;
4.635 -
4.636 - return POSSIBLE_NULL;
4.637 - }
4.638 - }
4.639 -
4.640 - //XXX copied from IntroduceHint:
4.641 - private static final class ExitsFromAllBranches extends TreePathScanner<Boolean, Void> {
4.642 -
4.643 - private CompilationInfo info;
4.644 - private Set<Tree> seenTrees = new HashSet<Tree>();
4.645 -
4.646 - public ExitsFromAllBranches(CompilationInfo info) {
4.647 - this.info = info;
4.648 - }
4.649 -
4.650 - @Override
4.651 - public Boolean scan(Tree tree, Void p) {
4.652 - seenTrees.add(tree);
4.653 - return super.scan(tree, p);
4.654 - }
4.655 -
4.656 - @Override
4.657 - public Boolean visitIf(IfTree node, Void p) {
4.658 - return scan(node.getThenStatement(), null) == Boolean.TRUE && scan(node.getElseStatement(), null) == Boolean.TRUE;
4.659 - }
4.660 -
4.661 - @Override
4.662 - public Boolean visitReturn(ReturnTree node, Void p) {
4.663 - return true;
4.664 - }
4.665 -
4.666 - @Override
4.667 - public Boolean visitBreak(BreakTree node, Void p) {
4.668 - return !seenTrees.contains(info.getTreeUtilities().getBreakContinueTarget(getCurrentPath()));
4.669 - }
4.670 -
4.671 - @Override
4.672 - public Boolean visitContinue(ContinueTree node, Void p) {
4.673 - return !seenTrees.contains(info.getTreeUtilities().getBreakContinueTarget(getCurrentPath()));
4.674 - }
4.675 -
4.676 - @Override
4.677 - public Boolean visitClass(ClassTree node, Void p) {
4.678 - return false;
4.679 - }
4.680 -
4.681 - @Override
4.682 - public Boolean visitThrow(ThrowTree node, Void p) {
4.683 - return true; //XXX: simplification
4.684 - }
4.685 -
4.686 - }
4.687 -
4.688 - private static boolean isVariableElement(Element ve) {
4.689 - return ve != null && VARIABLE_ELEMENT.contains(ve.getKind());
4.690 - }
4.691 -
4.692 - private static final Set<ElementKind> VARIABLE_ELEMENT = EnumSet.of(ElementKind.EXCEPTION_PARAMETER, ElementKind.FIELD, ElementKind.LOCAL_VARIABLE, ElementKind.PARAMETER);
4.693 -
4.694 -}
5.1 --- a/javahints/src/org/netbeans/modules/javahints/jdk5/IteratorToFor.java Thu Jun 14 15:02:44 2012 +0200
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,134 +0,0 @@
5.4 -/*
5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5.6 - *
5.7 - * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5.8 - *
5.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
5.10 - * Other names may be trademarks of their respective owners.
5.11 - *
5.12 - * The contents of this file are subject to the terms of either the GNU
5.13 - * General Public License Version 2 only ("GPL") or the Common
5.14 - * Development and Distribution License("CDDL") (collectively, the
5.15 - * "License"). You may not use this file except in compliance with the
5.16 - * License. You can obtain a copy of the License at
5.17 - * http://www.netbeans.org/cddl-gplv2.html
5.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
5.19 - * specific language governing permissions and limitations under the
5.20 - * License. When distributing the software, include this License Header
5.21 - * Notice in each file and include the License file at
5.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
5.23 - * particular file as subject to the "Classpath" exception as provided
5.24 - * by Oracle in the GPL Version 2 section of the License file that
5.25 - * accompanied this code. If applicable, add the following below the
5.26 - * License Header, with the fields enclosed by brackets [] replaced by
5.27 - * your own identifying information:
5.28 - * "Portions Copyrighted [year] [name of copyright owner]"
5.29 - *
5.30 - * If you wish your version of this file to be governed by only the CDDL
5.31 - * or only the GPL Version 2, indicate your decision by adding
5.32 - * "[Contributor] elects to include this software in this distribution
5.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
5.34 - * single choice of license, a recipient has the option to distribute
5.35 - * your version of this file under either the CDDL, the GPL Version 2 or
5.36 - * to extend the choice of license to its licensees as provided above.
5.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
5.38 - * Version 2 license, then the option applies only if the new code is
5.39 - * made subject to such option by the copyright holder.
5.40 - *
5.41 - * Contributor(s):
5.42 - *
5.43 - * Portions Copyrighted 2012 Sun Microsystems, Inc.
5.44 - */
5.45 -
5.46 -package org.netbeans.modules.javahints.jdk5;
5.47 -
5.48 -import com.sun.source.tree.Tree;
5.49 -import com.sun.source.util.TreePath;
5.50 -import com.sun.source.util.TreePathScanner;
5.51 -import java.util.Collection;
5.52 -import javax.lang.model.element.Element;
5.53 -import javax.lang.model.element.TypeElement;
5.54 -import javax.lang.model.type.TypeMirror;
5.55 -import org.netbeans.spi.editor.hints.ErrorDescription;
5.56 -import org.netbeans.spi.java.hints.ErrorDescriptionFactory;
5.57 -import org.netbeans.spi.java.hints.Hint;
5.58 -import org.netbeans.spi.java.hints.HintContext;
5.59 -import org.netbeans.spi.java.hints.JavaFixUtilities;
5.60 -import org.netbeans.spi.java.hints.TriggerPattern;
5.61 -import org.openide.util.NbBundle.Messages;
5.62 -
5.63 -@Hint(displayName="#DN_IteratorToFor", description="#DESC_IteratorToFor", category="rules15")
5.64 -@Messages({
5.65 - "DN_IteratorToFor=Use JDK 5 for-loop",
5.66 - "DESC_IteratorToFor=Replaces simple uses of Iterator with a corresponding for-loop.",
5.67 - "ERR_IteratorToFor=Use of Iterator for simple loop",
5.68 - "FIX_IteratorToFor=Convert to for-loop"
5.69 -})
5.70 -public class IteratorToFor {
5.71 -
5.72 - @TriggerPattern("java.util.Iterator $it = $coll.iterator(); while ($it.hasNext()) {$type $elem = ($type) $it.next(); $rest$;}")
5.73 - public static ErrorDescription whileIdiom(HintContext ctx) {
5.74 - if (uses(ctx, ctx.getMultiVariables().get("$rest$"), ctx.getVariables().get("$it"))) {
5.75 - return null;
5.76 - }
5.77 - if (!iterable(ctx, ctx.getVariables().get("$coll"), ctx.getVariables().get("$type"))) {
5.78 - return null;
5.79 - }
5.80 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToFor(),
5.81 - JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_IteratorToFor(), ctx.getPath(), "for ($type $elem : $coll) {$rest$;}"));
5.82 - }
5.83 -
5.84 - @TriggerPattern("for (java.util.Iterator $it = $coll.iterator(); $it.hasNext(); ) {$type $elem = ($type) $it.next(); $rest$;}")
5.85 - public static ErrorDescription forIdiom(HintContext ctx) {
5.86 - if (uses(ctx, ctx.getMultiVariables().get("$rest$"), ctx.getVariables().get("$it"))) {
5.87 - return null;
5.88 - }
5.89 - if (!iterable(ctx, ctx.getVariables().get("$coll"), ctx.getVariables().get("$type"))) {
5.90 - return null;
5.91 - }
5.92 - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_IteratorToFor(),
5.93 - JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_IteratorToFor(), ctx.getPath(), "for ($type $elem : $coll) {$rest$;}"));
5.94 - }
5.95 -
5.96 - // adapted from org.netbeans.modules.java.hints.declarative.conditionapi.Matcher.referencedIn
5.97 - private static boolean uses(final HintContext ctx, Collection<? extends TreePath> statements, TreePath var) {
5.98 - final Element e = ctx.getInfo().getTrees().getElement(var);
5.99 - for (TreePath tp : statements) {
5.100 - boolean occurs = Boolean.TRUE.equals(new TreePathScanner<Boolean, Void>() {
5.101 - @Override public Boolean scan(Tree tree, Void p) {
5.102 - if (tree == null) {
5.103 - return false;
5.104 - }
5.105 - TreePath currentPath = new TreePath(getCurrentPath(), tree);
5.106 - Element currentElement = ctx.getInfo().getTrees().getElement(currentPath);
5.107 - if (e.equals(currentElement)) {
5.108 - return true;
5.109 - }
5.110 - return super.scan(tree, p);
5.111 - }
5.112 - @Override public Boolean reduce(Boolean r1, Boolean r2) {
5.113 - if (r1 == null) {
5.114 - return r2;
5.115 - }
5.116 - if (r2 == null) {
5.117 - return r1;
5.118 - }
5.119 - return r1 || r2;
5.120 - }
5.121 - }.scan(tp, null));
5.122 - if (occurs) {
5.123 - return true;
5.124 - }
5.125 - }
5.126 - return false;
5.127 - }
5.128 -
5.129 - private static boolean iterable(HintContext ctx, TreePath collection, TreePath type) {
5.130 - TypeMirror collectionType = ctx.getInfo().getTrees().getTypeMirror(collection);
5.131 - TypeElement iterable = ctx.getInfo().getElements().getTypeElement("java.lang.Iterable");
5.132 - TypeMirror iterableType = ctx.getInfo().getTypes().getDeclaredType(iterable, ctx.getInfo().getTypes().getWildcardType(ctx.getInfo().getTrees().getTypeMirror(type), null));
5.133 - TypeMirror bogusIterableType = ctx.getInfo().getTypes().getDeclaredType(iterable, ctx.getInfo().getTypes().getNullType());
5.134 - return ctx.getInfo().getTypes().isAssignable(collectionType, iterableType) && !ctx.getInfo().getTypes().isAssignable(collectionType, bogusIterableType);
5.135 - }
5.136 -
5.137 -}
6.1 --- a/javahints/test/unit/src/org/netbeans/modules/javahints/Bundle_test.properties Thu Jun 14 15:02:44 2012 +0200
6.2 +++ b/javahints/test/unit/src/org/netbeans/modules/javahints/Bundle_test.properties Tue Jun 26 16:30:27 2012 +0200
6.3 @@ -14,11 +14,5 @@
6.4 #The Original Software is NetBeans. The Initial Developer of the Original
6.5 # Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
6.6 #Microsystems, Inc. All Rights Reserved.
6.7 -ERR_DereferencingNull=DN
6.8 -ERR_PossiblyDereferencingNull=Possibly Dereferencing null
6.9 -ERR_AssigningNullToNotNull=ANNNV
6.10 -ERR_PossibleAssigingNullToNotNull=PANNNV
6.11 -ERR_NULL_TO_NON_NULL_ARG=ERR_NULL_TO_NON_NULL_ARG
6.12 -ERR_POSSIBLENULL_TO_NON_NULL_ARG=ERR_POSSIBLENULL_TO_NON_NULL_ARG
6.13 DN_NegateCondition=Negate condition
6.14 FIX_AddReturn=FIX_AddReturn
7.1 --- a/javahints/test/unit/src/org/netbeans/modules/javahints/InvertIfTest.java Thu Jun 14 15:02:44 2012 +0200
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,120 +0,0 @@
7.4 -/*
7.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7.6 - *
7.7 - * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
7.8 - *
7.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7.10 - * Other names may be trademarks of their respective owners.
7.11 - *
7.12 - * The contents of this file are subject to the terms of either the GNU
7.13 - * General Public License Version 2 only ("GPL") or the Common
7.14 - * Development and Distribution License("CDDL") (collectively, the
7.15 - * "License"). You may not use this file except in compliance with the
7.16 - * License. You can obtain a copy of the License at
7.17 - * http://www.netbeans.org/cddl-gplv2.html
7.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
7.19 - * specific language governing permissions and limitations under the
7.20 - * License. When distributing the software, include this License Header
7.21 - * Notice in each file and include the License file at
7.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
7.23 - * particular file as subject to the "Classpath" exception as provided
7.24 - * by Oracle in the GPL Version 2 section of the License file that
7.25 - * accompanied this code. If applicable, add the following below the
7.26 - * License Header, with the fields enclosed by brackets [] replaced by
7.27 - * your own identifying information:
7.28 - * "Portions Copyrighted [year] [name of copyright owner]"
7.29 - *
7.30 - * If you wish your version of this file to be governed by only the CDDL
7.31 - * or only the GPL Version 2, indicate your decision by adding
7.32 - * "[Contributor] elects to include this software in this distribution
7.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
7.34 - * single choice of license, a recipient has the option to distribute
7.35 - * your version of this file under either the CDDL, the GPL Version 2 or
7.36 - * to extend the choice of license to its licensees as provided above.
7.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
7.38 - * Version 2 license, then the option applies only if the new code is
7.39 - * made subject to such option by the copyright holder.
7.40 - *
7.41 - * Contributor(s):
7.42 - *
7.43 - * Portions Copyrighted 2012 Sun Microsystems, Inc.
7.44 - */
7.45 -package org.netbeans.modules.javahints;
7.46 -
7.47 -import org.junit.Test;
7.48 -import org.netbeans.modules.java.hints.test.api.HintTest;
7.49 -
7.50 -public class InvertIfTest {
7.51 -
7.52 - @Test
7.53 - public void testComments() throws Exception {
7.54 - HintTest.create()
7.55 - .setCaretMarker('|')
7.56 - .input("package test;\n" +
7.57 - "public class Test {\n" +
7.58 - " public static void main(String[] args) {\n" +
7.59 - " i|f\n" +
7.60 - " // A\n" +
7.61 - " (args[0].isEmpty())\n" +
7.62 - " // B\n" +
7.63 - " { //1\n" +
7.64 - " //2\n" +
7.65 - " System.err.println(\"1\");\n" +
7.66 - " //3\n" +
7.67 - " } \n" +
7.68 - " // C\n" +
7.69 - " else\n" +
7.70 - " // D\n" +
7.71 - " {//4\n" +
7.72 - " //5\n" +
7.73 - " System.err.println(\"2\");\n" +
7.74 - " //6\n" +
7.75 - " } //E\n" +
7.76 - " }\n" +
7.77 - "}\n")
7.78 - .run(InvertIf.class)
7.79 - .findWarning("3:9-3:9:verifier:" + Bundle.ERR_InvertIf())
7.80 - .applyFix()
7.81 - .assertCompilable()
7.82 - .assertOutput("package test;\n" +
7.83 - "public class Test {\n" +
7.84 - " public static void main(String[] args) {\n" +
7.85 - " if\n" +
7.86 - " // A\n" +
7.87 - " (!args[0].isEmpty())\n" +
7.88 - " // B\n" +
7.89 - " {//4\n" +
7.90 - " //5\n" +
7.91 - " System.err.println(\"2\");\n" +
7.92 - " //6\n" +
7.93 - " } \n" +
7.94 - " // C\n" +
7.95 - " else\n" +
7.96 - " // D\n" +
7.97 - " { //1\n" +
7.98 - " //2\n" +
7.99 - " System.err.println(\"1\");\n" +
7.100 - " //3\n" +
7.101 - " } //E\n" +
7.102 - " }\n" +
7.103 - "}\n");
7.104 - }
7.105 -
7.106 - @Test
7.107 - public void testCaretPosition() throws Exception {
7.108 - HintTest.create()
7.109 - .setCaretMarker('|')
7.110 - .input("package test;\n" +
7.111 - "public class Test {\n" +
7.112 - " public static void main(String[] args) {\n" +
7.113 - " if (args[0].isEmpty()) |{\n" +
7.114 - " System.err.println(\"1\");\n" +
7.115 - " } else {\n" +
7.116 - " System.err.println(\"2\");\n" +
7.117 - " }\n" +
7.118 - " }\n" +
7.119 - "}\n")
7.120 - .run(InvertIf.class)
7.121 - .assertWarnings();
7.122 - }
7.123 -}
8.1 --- a/javahints/test/unit/src/org/netbeans/modules/javahints/NPECheckTest.java Thu Jun 14 15:02:44 2012 +0200
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,326 +0,0 @@
8.4 -/*
8.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8.6 - *
8.7 - * Copyright 1997-2012 Sun Microsystems, Inc. All rights reserved.
8.8 - *
8.9 - * The contents of this file are subject to the terms of either the GNU
8.10 - * General Public License Version 2 only ("GPL") or the Common
8.11 - * Development and Distribution License("CDDL") (collectively, the
8.12 - * "License"). You may not use this file except in compliance with the
8.13 - * License. You can obtain a copy of the License at
8.14 - * http://www.netbeans.org/cddl-gplv2.html
8.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
8.16 - * specific language governing permissions and limitations under the
8.17 - * License. When distributing the software, include this License Header
8.18 - * Notice in each file and include the License file at
8.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
8.20 - * particular file as subject to the "Classpath" exception as provided
8.21 - * by Sun in the GPL Version 2 section of the License file that
8.22 - * accompanied this code. If applicable, add the following below the
8.23 - * License Header, with the fields enclosed by brackets [] replaced by
8.24 - * your own identifying information:
8.25 - * "Portions Copyrighted [year] [name of copyright owner]"
8.26 - *
8.27 - * Contributor(s):
8.28 - *
8.29 - * Portions Copyrighted 2007-2012 Sun Microsystems, Inc.
8.30 - */
8.31 -
8.32 -package org.netbeans.modules.javahints;
8.33 -
8.34 -import org.netbeans.junit.NbTestCase;
8.35 -import org.netbeans.modules.java.hints.test.api.HintTest;
8.36 -
8.37 -/**
8.38 - *
8.39 - * @author lahvac
8.40 - */
8.41 -public class NPECheckTest extends NbTestCase {
8.42 -
8.43 - public NPECheckTest(String testName) {
8.44 - super(testName);
8.45 - }
8.46 -
8.47 - public void testSimpleNull1() throws Exception {
8.48 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {Object o; o = null; o.toString();}}", "0:69-0:77:verifier:DN");
8.49 - }
8.50 -
8.51 - public void testSimpleNull2() throws Exception {
8.52 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {Object o = null; o.toString();}}", "0:66-0:74:verifier:DN");
8.53 - }
8.54 -
8.55 - public void testIf1() throws Exception {
8.56 - performAnalysisTest("test/Test.java", "package test; class Test {private void test(Object o) {if (o == null) {o.toString();}}}", "0:73-0:81:verifier:DN");
8.57 - }
8.58 -
8.59 - public void testIf2() throws Exception {
8.60 - HintTest.create()
8.61 - .input("package test; class Test {private void test(Object o) {if (o == null) {o = \"\";} o.length();}}", false)
8.62 - .run(NPECheck.class)
8.63 - .assertWarnings();
8.64 - }
8.65 -
8.66 - public void testIf3() throws Exception {
8.67 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s == null) {s = \"\";} s.length();}}");
8.68 - }
8.69 -
8.70 - public void testIf4() throws Exception {
8.71 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s == null) {s = \"\";} else {s = \"\";} s.length();}}");
8.72 - }
8.73 -
8.74 - public void testIf5() throws Exception {
8.75 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s == null) {s = \"\";} else {s = null;} s.length();}}", "0:108-0:114:verifier:Possibly Dereferencing null");
8.76 - }
8.77 -
8.78 - public void testIf6() throws Exception {
8.79 - performAnalysisTest("test/Test.java", "package test; class Test {private void test(Object o) {if (null == o) {o.toString();}}}", "0:73-0:81:verifier:DN");
8.80 - }
8.81 -
8.82 - public void testIf7() throws Exception {
8.83 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String o = null; if (o != null) {o.toString();}}}");
8.84 - }
8.85 -
8.86 - public void testIf8() throws Exception {
8.87 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String o = null; if (null != o) {o.toString();}}}");
8.88 - }
8.89 -
8.90 - public void testIf9() throws Exception {
8.91 - HintTest.create()
8.92 - .input("package test; class Test {private void test(String s) {if (s == null) {} s.length();}}")
8.93 - .run(NPECheck.class)
8.94 - .assertWarnings("0:75-0:81:verifier:Possibly Dereferencing null");
8.95 - }
8.96 -
8.97 - public void testIfa() throws Exception {
8.98 - HintTest.create()
8.99 - .input("package test; class Test {private void test(String s1, String s2) {if (s1 == null) {s1 = s2;} s1.length();}}")
8.100 - .run(NPECheck.class)
8.101 - .assertWarnings();
8.102 - }
8.103 -
8.104 - public void testIfb() throws Exception {
8.105 - HintTest.create()
8.106 - .input("package test; class Test {private void test(@Null String s) {if (s == null) {throw new UnsupportedOperationException();} s.length();}} @interface Null {}")
8.107 - .run(NPECheck.class)
8.108 - .assertWarnings();
8.109 - }
8.110 -
8.111 - public void testIfc() throws Exception {
8.112 - performAnalysisTest("test/Test.java",
8.113 - "package test; class Test {\n"+
8.114 - " private void test(int i, @CheckForNull String o) {\n" +
8.115 - " if (i > 2 && o != null && o.length() > 2) {\n" +
8.116 - " }\n" +
8.117 - " }\n" +
8.118 - " @interface CheckForNull{}\n" +
8.119 - "}\n");
8.120 - }
8.121 -
8.122 - public void testIfd() throws Exception {
8.123 - performAnalysisTest("test/Test.java",
8.124 - "package test; class Test { private void test(int i, @CheckForNull String o) {\n" +
8.125 - " if (i > 2 || o == null || o.length() > 2) {\n" +
8.126 - " }\n" +
8.127 - " }\n" +
8.128 - " @interface CheckForNull{}\n" +
8.129 - "}\n");
8.130 - }
8.131 -
8.132 - public void testTernary1() throws Exception {
8.133 - performAnalysisTest("test/Test.java", "package test; class Test {private void test(int i) {String s = i == 0 ? \"\" : null; s.length();}}");
8.134 - }
8.135 -
8.136 - public void testTernary2() throws Exception {
8.137 - performAnalysisTest("test/Test.java",
8.138 - "package test; class Test {private void test(String s) {Object o = s == null ? \"\" : s; s = s.toString();}}",
8.139 - "0:92-0:100:verifier:Possibly Dereferencing null");
8.140 - }
8.141 -
8.142 - public void testTernary3() throws Exception {
8.143 - performAnalysisTest("test/Test.java",
8.144 - "package test;\n" +
8.145 - "class Test {\n" +
8.146 - " public void test() {" +
8.147 - " String e = null;" +
8.148 - " String f = e == null ? \"\" : e.trim();" +
8.149 - " }" +
8.150 - "}");
8.151 - }
8.152 -
8.153 - public void testNewClass() throws Exception {
8.154 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s == null) {s = new String(\"\");} s.length();}}");
8.155 - }
8.156 -
8.157 - public void testCheckForNull1() throws Exception {
8.158 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = get(); s.length();} @Nullable private String get() {return \"\";} @interface Nullable {}}", "0:67-0:73:verifier:Possibly Dereferencing null");
8.159 - }
8.160 -
8.161 - public void testCheckForNull2() throws Exception {
8.162 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {s.length();} @Nullable private String s; @interface Nullable {}}", "0:49-0:55:verifier:Possibly Dereferencing null");
8.163 - }
8.164 -
8.165 - public void testAssignNullToNotNull() throws Exception {
8.166 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {s = null;} @NotNull private String s; @interface NotNull {}}", "0:47-0:55:verifier:ANNNV");
8.167 - }
8.168 -
8.169 - public void testPossibleAssignNullToNotNull() throws Exception {
8.170 - performAnalysisTest("test/Test.java", "package test; class Test {private void test(int i) {String s2 = null; if (i == 0) {s2 = \"\";} s = s2;} @NotNull private String s; @interface NotNull {}}", "0:93-0:99:verifier:PANNNV");
8.171 - }
8.172 -
8.173 - public void testNullCheckAnd1() throws Exception {
8.174 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s != null && s.length() > 0) {}}}");
8.175 - }
8.176 -
8.177 - public void testNullCheckAnd2() throws Exception {
8.178 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s == null && s.length() > 0) {}}}", "0:83-0:89:verifier:DN");
8.179 - }
8.180 -
8.181 - public void testNullCheckOr1() throws Exception {
8.182 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s != null || s.length() > 0) {}}}", "0:83-0:89:verifier:DN");
8.183 - }
8.184 -
8.185 - public void testNullCheckOr2() throws Exception {
8.186 - performAnalysisTest("test/Test.java", "package test; class Test {private void test() {String s = null; if (s == null || s.length() > 0) {}}}");
8.187 - }
8.188 -
8.189 - public void testContinue1() throws Exception {
8.190 - performAnalysisTest("test/Test.java",
8.191 - "package test; class Test {private void test(String[] sa) {for (String s : sa) {if (s == null) continue; s.length();}}}");
8.192 - }
8.193 -
8.194 - public void testCondition1() throws Exception {
8.195 - performAnalysisTest("test/Test.java",
8.196 - "package test; class Test {private void test(String sa) {boolean b = sa != null && (sa.length() == 0 || sa.length() == 1);}}");
8.197 - }
8.198 -
8.199 - public void testCondition2() throws Exception {
8.200 - performAnalysisTest("test/Test.java",
8.201 - "package test; class Test {private void test(String sa) {boolean b = sa == null || (sa.length() == 0 && sa.length() == 1);}}");
8.202 - }
8.203 -
8.204 - public void testCondition3() throws Exception {
8.205 - performAnalysisTest("test/Test.java",
8.206 - "package test; class Test {private void test(int i) {Object o2 = i < 1 ? null : \"\"; boolean b = true && o2 != null && o2.toString() != \"\";}}");
8.207 - }
8.208 -
8.209 - public void testWhileAndIf() throws Exception {
8.210 - performAnalysisTest("test/Test.java",
8.211 - "package test;\n" +
8.212 - "class Test {\n" +
8.213 - " private void test(int i, boolean b, Object o2) {\n" +
8.214 - " Object o = null;\n" +
8.215 - " while (--i > 0) {\n" +
8.216 - " if (b) {\n" +
8.217 - " o = o2;\n" +
8.218 - " }\n" +
8.219 - " }\n" +
8.220 - " o.toString();\n" +
8.221 - " }" +
8.222 - "}",
8.223 - "9:10-9:18:verifier:DN");
8.224 - }
8.225 -
8.226 - public void testWhile1() throws Exception {
8.227 - performAnalysisTest("test/Test.java",
8.228 - "package test;\n" +
8.229 - "class Test {\n" +
8.230 - " private void test(String o) {\n" +
8.231 - " while (o != null && o.length() > 1) {\n" +
8.232 - " o = o.substring(1);\n" +
8.233 - " }\n" +
8.234 - " o.toString();\n" +
8.235 - " }" +
8.236 - "}",
8.237 - "6:10-6:18:verifier:Possibly Dereferencing null");
8.238 - }
8.239 -
8.240 - public void testWhile2() throws Exception {
8.241 - performAnalysisTest("test/Test.java",
8.242 - "package test;\n" +
8.243 - "class Test {\n" +
8.244 - " private void test(@CheckForNull String o) {\n" +
8.245 - " while (o != null) {\n" +
8.246 - " o = o.substring(1);\n" +
8.247 - " }\n" +
8.248 - " }" +
8.249 - " @interface CheckForNull {}\n" +
8.250 - "}");
8.251 - }
8.252 -
8.253 - public void testParameter1() throws Exception {
8.254 - performAnalysisTest("test/Test.java",
8.255 - "package test; class Test {private void test(int i) {String s = null; ttt(s);} private void ttt(@NotNull String s){}} @interface NotNull {}",
8.256 - "0:73-0:74:verifier:ERR_NULL_TO_NON_NULL_ARG");
8.257 - }
8.258 -
8.259 - public void testParameter2() throws Exception {
8.260 - performAnalysisTest("test/Test.java",
8.261 - "package test; class Test {private void test(int i) {ttt(t());} private void ttt(@NotNull String s){} private @CheckForNull String t() {return \"\";} } @interface NotNull {} @interface CheckForNull {}",
8.262 - "0:56-0:59:verifier:ERR_POSSIBLENULL_TO_NON_NULL_ARG");
8.263 - }
8.264 -
8.265 - public void testNoMultipleReports1() throws Exception {
8.266 - performAnalysisTest("test/Test.java",
8.267 - "package test;\n" +
8.268 - "class Test {\n" +
8.269 - " private void test(@CheckForNull String o) {\n" +
8.270 - " o.toString();\n" +
8.271 - " o.toString();\n" +
8.272 - " o.toString();\n" +
8.273 - " }" +
8.274 - " @interface CheckForNull {}\n" +
8.275 - "}", "3:10-3:18:verifier:Possibly Dereferencing null");
8.276 - }
8.277 -
8.278 - public void testNoMultipleReports2() throws Exception {
8.279 - performAnalysisTest("test/Test.java",
8.280 - "package test;\n" +
8.281 - "class Test {\n" +
8.282 - " private void test(@CheckForNull String o) {\n" +
8.283 - " if (o.length() > 1 && o.length() > 2);\n" +
8.284 - " }" +
8.285 - " @interface CheckForNull {}\n" +
8.286 - "}", "3:14-3:20:verifier:Possibly Dereferencing null");
8.287 - }
8.288 -
8.289 - public void testNoMultipleReports3() throws Exception {
8.290 - performAnalysisTest("test/Test.java",
8.291 - "package test;\n" +
8.292 - "class Test {\n" +
8.293 - " private void test(@CheckForNull String o) {\n" +
8.294 - " boolean b = o.length() > 1 && o.length() > 2;\n" +
8.295 - " }" +
8.296 - " @interface CheckForNull {}\n" +
8.297 - "}", "3:22-3:28:verifier:Possibly Dereferencing null");
8.298 - }
8.299 -
8.300 - public void testNoMultipleReports4() throws Exception {
8.301 - performAnalysisTest("test/Test.java",
8.302 - "package test;\n" +
8.303 - "class Test {\n" +
8.304 - " private void test(@CheckForNull String o) {\n" +
8.305 - " boolean b = o.length() > 1 && o.length() > 2 ? false : true;\n" +
8.306 - " }" +
8.307 - " @interface CheckForNull {}\n" +
8.308 - "}", "3:22-3:28:verifier:Possibly Dereferencing null");
8.309 - }
8.310 -
8.311 - public void testCCE() throws Exception {
8.312 - HintTest.create()
8.313 - .input("package test;\n" +
8.314 - "class Test {\n" +
8.315 - " private void test() {\n" +
8.316 - " c.s Object method = new Object();\n" +
8.317 - " }" +
8.318 - "}", false)
8.319 - .run(NPECheck.class)
8.320 - .assertWarnings(/*"3:20-3:28:verifier:Possibly Dereferencing null"*/);
8.321 - }
8.322 -
8.323 - private void performAnalysisTest(String fileName, String code, String... golden) throws Exception {
8.324 - HintTest.create()
8.325 - .input(fileName, code)
8.326 - .run(NPECheck.class)
8.327 - .assertWarnings(golden);
8.328 - }
8.329 -}
9.1 --- a/javahints/test/unit/src/org/netbeans/modules/javahints/jdk5/IteratorToForTest.java Thu Jun 14 15:02:44 2012 +0200
9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
9.3 @@ -1,316 +0,0 @@
9.4 -/*
9.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9.6 - *
9.7 - * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
9.8 - *
9.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
9.10 - * Other names may be trademarks of their respective owners.
9.11 - *
9.12 - * The contents of this file are subject to the terms of either the GNU
9.13 - * General Public License Version 2 only ("GPL") or the Common
9.14 - * Development and Distribution License("CDDL") (collectively, the
9.15 - * "License"). You may not use this file except in compliance with the
9.16 - * License. You can obtain a copy of the License at
9.17 - * http://www.netbeans.org/cddl-gplv2.html
9.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
9.19 - * specific language governing permissions and limitations under the
9.20 - * License. When distributing the software, include this License Header
9.21 - * Notice in each file and include the License file at
9.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
9.23 - * particular file as subject to the "Classpath" exception as provided
9.24 - * by Oracle in the GPL Version 2 section of the License file that
9.25 - * accompanied this code. If applicable, add the following below the
9.26 - * License Header, with the fields enclosed by brackets [] replaced by
9.27 - * your own identifying information:
9.28 - * "Portions Copyrighted [year] [name of copyright owner]"
9.29 - *
9.30 - * If you wish your version of this file to be governed by only the CDDL
9.31 - * or only the GPL Version 2, indicate your decision by adding
9.32 - * "[Contributor] elects to include this software in this distribution
9.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
9.34 - * single choice of license, a recipient has the option to distribute
9.35 - * your version of this file under either the CDDL, the GPL Version 2 or
9.36 - * to extend the choice of license to its licensees as provided above.
9.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
9.38 - * Version 2 license, then the option applies only if the new code is
9.39 - * made subject to such option by the copyright holder.
9.40 - *
9.41 - * Contributor(s):
9.42 - *
9.43 - * Portions Copyrighted 2012 Sun Microsystems, Inc.
9.44 - */
9.45 -
9.46 -package org.netbeans.modules.javahints.jdk5;
9.47 -
9.48 -import org.junit.Test;
9.49 -import org.netbeans.modules.java.hints.test.api.HintTest;
9.50 -
9.51 -public class IteratorToForTest {
9.52 -
9.53 - @Test public void whileWarning() throws Exception {
9.54 - HintTest.create().input("package test;\n"
9.55 - + "import java.util.*;"
9.56 - + "public class Test {\n"
9.57 - + " void m(List<String> strings) {\n"
9.58 - + " Iterator it = strings.iterator();\n"
9.59 - + " while (it.hasNext()) {\n"
9.60 - + " String s = (String) it.next();\n"
9.61 - + " System.out.println(s);\n"
9.62 - + " System.err.println(s);\n"
9.63 - + " }\n"
9.64 - + " }\n"
9.65 - + "}\n").run(IteratorToFor.class).assertWarnings("2:33-9:5:verifier:" + Bundle.ERR_IteratorToFor());
9.66 - }
9.67 -
9.68 - @Test public void whileUsedSpecially() throws Exception {
9.69 - HintTest.create().input("package test;\n"
9.70 - + "import java.util.*;"
9.71 - + "public class Test {\n"
9.72 - + " void m(List<String> strings) {\n"
9.73 - + " Iterator it = strings.iterator();\n"
9.74 - + " while (it.hasNext()) {\n"
9.75 - + " String s = (String) it.next();\n"
9.76 - + " if (s.isEmpty()) {\n"
9.77 - + " it.remove();\n"
9.78 - + " } else {\n"
9.79 - + " System.out.println(s);\n"
9.80 - + " }\n"
9.81 - + " }\n"
9.82 - + " }\n"
9.83 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.84 - }
9.85 -
9.86 - @Test public void whileRaw() throws Exception {
9.87 - HintTest.create().input("package test;\n"
9.88 - + "import java.util.*;"
9.89 - + "public class Test {\n"
9.90 - + " void m(List strings) {\n"
9.91 - + " Iterator it = strings.iterator();\n"
9.92 - + " while (it.hasNext()) {\n"
9.93 - + " String s = (String) it.next();\n"
9.94 - + " System.out.println(s);\n"
9.95 - + " System.err.println(s);\n"
9.96 - + " }\n"
9.97 - + " }\n"
9.98 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.99 - }
9.100 -
9.101 - @Test public void whileWrongType() throws Exception {
9.102 - HintTest.create().input("package test;\n"
9.103 - + "import java.util.*;"
9.104 - + "public class Test {\n"
9.105 - + " void m(List<java.net.URL> strings) {\n"
9.106 - + " Iterator it = strings.iterator();\n"
9.107 - + " while (it.hasNext()) {\n"
9.108 - + " String s = (String) it.next();\n"
9.109 - + " System.out.println(s);\n"
9.110 - + " System.err.println(s);\n"
9.111 - + " }\n"
9.112 - + " }\n"
9.113 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.114 - }
9.115 -
9.116 - @Test public void whileNotRaw() throws Exception {
9.117 - HintTest.create().input("package test;\n"
9.118 - + "import java.util.*;"
9.119 - + "public class Test {\n"
9.120 - + " void m(MyList strings) {\n"
9.121 - + " Iterator it = strings.iterator();\n"
9.122 - + " while (it.hasNext()) {\n"
9.123 - + " String s = (String) it.next();\n"
9.124 - + " System.out.println(s);\n"
9.125 - + " System.err.println(s);\n"
9.126 - + " }\n"
9.127 - + " }\n"
9.128 - + " static class MyList extends ArrayList<String> {}\n"
9.129 - + "}\n").run(IteratorToFor.class).assertWarnings("2:27-9:5:verifier:" + Bundle.ERR_IteratorToFor());
9.130 - }
9.131 -
9.132 - @Test public void whileNotIterable() throws Exception {
9.133 - HintTest.create().input("package test;\n"
9.134 - + "import java.util.*;"
9.135 - + "public class Test {\n"
9.136 - + " void m(MyList strings) {\n"
9.137 - + " Iterator it = strings.iterator();\n"
9.138 - + " while (it.hasNext()) {\n"
9.139 - + " String s = (String) it.next();\n"
9.140 - + " System.out.println(s);\n"
9.141 - + " System.err.println(s);\n"
9.142 - + " }\n"
9.143 - + " }\n"
9.144 - + " interface MyList {\n"
9.145 - + " Iterator<String> iterator();\n"
9.146 - + " }\n"
9.147 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.148 - }
9.149 -
9.150 - @Test public void whileSubtype() throws Exception {
9.151 - HintTest.create().input("package test;\n"
9.152 - + "import java.util.*;"
9.153 - + "public class Test {\n"
9.154 - + " void m(List<PropertyResourceBundle> bundles) {\n"
9.155 - + " Iterator it = bundles.iterator();\n"
9.156 - + " while (it.hasNext()) {\n"
9.157 - + " ResourceBundle bundle = (ResourceBundle) it.next();\n"
9.158 - + " System.out.println(bundle);\n"
9.159 - + " System.err.println(bundle);\n"
9.160 - + " }\n"
9.161 - + " }\n"
9.162 - + "}\n").run(IteratorToFor.class).assertWarnings("2:49-9:5:verifier:" + Bundle.ERR_IteratorToFor());
9.163 - }
9.164 -
9.165 - @Test public void whileGenericSubtype() throws Exception {
9.166 - HintTest.create().input("package test;\n"
9.167 - + "import java.util.*;"
9.168 - + "public class Test {\n"
9.169 - + " void m(List<ArrayList<String>> lists) {\n"
9.170 - + " Iterator it = lists.iterator();\n"
9.171 - + " while (it.hasNext()) {\n"
9.172 - + " List<String> list = (List<String>) it.next();\n"
9.173 - + " System.out.println(list);\n"
9.174 - + " }\n"
9.175 - + " }\n"
9.176 - + "}\n").run(IteratorToFor.class).findWarning("2:42-8:5:verifier:" + Bundle.ERR_IteratorToFor()).
9.177 - applyFix().
9.178 - assertCompilable().
9.179 - assertOutput("package test;\n"
9.180 - + "import java.util.*;"
9.181 - + "public class Test {\n"
9.182 - + " void m(List<ArrayList<String>> lists) {\n"
9.183 - + " for (List<String> list : lists) {\n"
9.184 - + " System.out.println(list);\n"
9.185 - + " }\n"
9.186 - + " }\n"
9.187 - + "}\n");
9.188 - }
9.189 -
9.190 - @Test public void whileNotSubtype() throws Exception {
9.191 - HintTest.create().input("package test;\n"
9.192 - + "import java.util.*;"
9.193 - + "public class Test {\n"
9.194 - + " void m(List<ResourceBundle> bundles) {\n"
9.195 - + " Iterator it = bundles.iterator();\n"
9.196 - + " while (it.hasNext()) {\n"
9.197 - + " PropertyResourceBundle bundle = (PropertyResourceBundle) it.next();\n"
9.198 - + " System.out.println(bundle);\n"
9.199 - + " System.err.println(bundle);\n"
9.200 - + " }\n"
9.201 - + " }\n"
9.202 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.203 - }
9.204 -
9.205 - @Test public void whileFix() throws Exception {
9.206 - HintTest.create().input("package test;\n"
9.207 - + "import java.util.*;"
9.208 - + "public class Test {\n"
9.209 - + " void m(List<String> strings) {\n"
9.210 - + " Collections.reverse(strings);\n"
9.211 - + " Iterator it = strings.iterator();\n"
9.212 - + " while (it.hasNext()) {\n"
9.213 - + " String s = (String) it.next();\n"
9.214 - + " System.out.println(s);\n"
9.215 - + " // OK\n"
9.216 - + " System.err.println(s);\n"
9.217 - + " }\n"
9.218 - + " System.out.println();\n"
9.219 - + " }\n"
9.220 - + "}\n").run(IteratorToFor.class).findWarning("2:33-12:5:verifier:" + Bundle.ERR_IteratorToFor()).
9.221 - applyFix().
9.222 - assertCompilable()
9.223 - .assertOutput("package test;\n"
9.224 - + "import java.util.*;"
9.225 - + "public class Test {\n"
9.226 - + " void m(List<String> strings) {\n"
9.227 - + " Collections.reverse(strings);\n"
9.228 - + " for (String s : strings) {\n"
9.229 - + " System.out.println(s);\n"
9.230 - + " // OK\n"
9.231 - + " System.err.println(s);\n"
9.232 - + " }\n"
9.233 - + " System.out.println();\n"
9.234 - + " }\n"
9.235 - + "}\n");
9.236 - }
9.237 -
9.238 - @Test public void forWarning() throws Exception {
9.239 - HintTest.create().input("package test;\n"
9.240 - + "import java.util.*;"
9.241 - + "public class Test {\n"
9.242 - + " void m(List<String> strings) {\n"
9.243 - + " for (Iterator it = strings.iterator(); it.hasNext(); ) {\n"
9.244 - + " String s = (String) it.next();\n"
9.245 - + " System.out.println(s);\n"
9.246 - + " System.err.println(s);\n"
9.247 - + " }\n"
9.248 - + " }\n"
9.249 - + "}\n").run(IteratorToFor.class).assertWarnings("3:8-3:11:verifier:" + Bundle.ERR_IteratorToFor());
9.250 - }
9.251 -
9.252 - @Test public void forUsedSpecially() throws Exception {
9.253 - HintTest.create().input("package test;\n"
9.254 - + "import java.util.*;"
9.255 - + "public class Test {\n"
9.256 - + " void m(List<String> strings) {\n"
9.257 - + " for (Iterator it = strings.iterator(); it.hasNext(); ) {\n"
9.258 - + " String s = (String) it.next();\n"
9.259 - + " if (s.isEmpty()) {\n"
9.260 - + " it.remove();\n"
9.261 - + " } else {\n"
9.262 - + " System.out.println(s);\n"
9.263 - + " }\n"
9.264 - + " }\n"
9.265 - + " }\n"
9.266 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.267 - }
9.268 -
9.269 - @Test public void forRaw() throws Exception {
9.270 - HintTest.create().input("package test;\n"
9.271 - + "import java.util.*;"
9.272 - + "public class Test {\n"
9.273 - + " void m(List strings) {\n"
9.274 - + " for (Iterator it = strings.iterator(); it.hasNext(); ) {\n"
9.275 - + " String s = (String) it.next();\n"
9.276 - + " System.out.println(s);\n"
9.277 - + " System.err.println(s);\n"
9.278 - + " }\n"
9.279 - + " }\n"
9.280 - + "}\n").run(IteratorToFor.class).assertWarnings();
9.281 - }
9.282 -
9.283 - @Test public void forFix() throws Exception {
9.284 - HintTest.create().input("package test;\n"
9.285 - + "import java.util.*;"
9.286 - + "public class Test {\n"
9.287 - + " void m(List<String> strings) {\n"
9.288 - + " Collections.reverse(strings);\n"
9.289 - + " for (Iterator it = strings.iterator(); it.hasNext(); ) {\n"
9.290 - + " String s = (String) it.next();\n"
9.291 - + " System.out.println(s);\n"
9.292 - + " // OK\n"
9.293 - + " System.err.println(s);\n"
9.294 - + " }\n"
9.295 - + " System.out.println();\n"
9.296 - + " }\n"
9.297 - + "}\n").run(IteratorToFor.class).findWarning("4:8-4:11:verifier:" + Bundle.ERR_IteratorToFor()).
9.298 - applyFix().
9.299 - assertCompilable()
9.300 - .assertOutput("package test;\n"
9.301 - + "import java.util.*;"
9.302 - + "public class Test {\n"
9.303 - + " void m(List<String> strings) {\n"
9.304 - + " Collections.reverse(strings);\n"
9.305 - + " for (String s : strings) {\n"
9.306 - + " System.out.println(s);\n"
9.307 - + " // OK\n"
9.308 - + " System.err.println(s);\n"
9.309 - + " }\n"
9.310 - + " System.out.println();\n"
9.311 - + " }\n"
9.312 - + "}\n");
9.313 - }
9.314 -
9.315 - // XXX also ought to match: for (Iterator i = coll.iterator(); i.hasNext(); ) {use((Type) i.next());}
9.316 - // XXX match final modifiers on iterator and/or element vars
9.317 - // XXX remove import of java.util.Iterator if present
9.318 -
9.319 -}