This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 250702
Collapse All | Expand All

(-)a/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java (-14 / +44 lines)
Lines 37-42 Link Here
37
import java.util.Collections;
37
import java.util.Collections;
38
import java.util.EnumSet;
38
import java.util.EnumSet;
39
import java.util.HashMap;
39
import java.util.HashMap;
40
import java.util.HashSet;
40
import java.util.IdentityHashMap;
41
import java.util.IdentityHashMap;
41
import java.util.Iterator;
42
import java.util.Iterator;
42
import java.util.LinkedList;
43
import java.util.LinkedList;
Lines 364-399 Link Here
364
        return result;
365
        return result;
365
    }
366
    }
366
    
367
    
367
    private static State getStateFromAnnotations(CompilationInfo info, Element e) {
368
        return getStateFromAnnotations(info, e, State.POSSIBLE_NULL);
369
    }
370
371
    private static final AnnotationMirrorGetter OVERRIDE_ANNOTATIONS = Lookup.getDefault().lookup(AnnotationMirrorGetter.class);
368
    private static final AnnotationMirrorGetter OVERRIDE_ANNOTATIONS = Lookup.getDefault().lookup(AnnotationMirrorGetter.class);
372
    
369
    
373
    private static State getStateFromAnnotations(CompilationInfo info, Element e, State def) {
370
    private static State getStateFromAnnotations(CompilationInfo info, Element e) {
374
        if (e == null) return def;
371
        if (e == null) {
372
            return State.POSSIBLE_NULL;
373
        }
375
        
374
        
376
        Iterable<? extends AnnotationMirror> mirrors = OVERRIDE_ANNOTATIONS != null ? OVERRIDE_ANNOTATIONS.getAnnotationMirrors(info, e) : null;
375
        Iterable<? extends AnnotationMirror> mirrors = OVERRIDE_ANNOTATIONS != null ? OVERRIDE_ANNOTATIONS.getAnnotationMirrors(info, e) : null;
377
        
376
        
378
        if (mirrors == null) mirrors = e.getAnnotationMirrors();
377
        if (mirrors == null) {
378
            mirrors = e.getAnnotationMirrors();
379
        }
379
        
380
        
380
        for (AnnotationMirror am : mirrors) {
381
        Set<String> names = getSimpleNames(mirrors);
381
            String simpleName = ((TypeElement) am.getAnnotationType().asElement()).getSimpleName().toString();
382
382
383
            if ("Nullable".equals(simpleName) || "NullAllowed".equals(simpleName)) {
383
        if (containsAny(names, "Nullable", "NullAllowed", "CheckForNull")) {
384
                return State.POSSIBLE_NULL_REPORT;
384
                return State.POSSIBLE_NULL_REPORT;
385
            }
385
            }
386
386
387
            if ("CheckForNull".equals(simpleName)) {
387
        if (containsAny(names, "NotNull", "NonNull", "Nonnull")) {
388
            return State.NOT_NULL;
389
        }
390
391
        names = getSimpleNames(info.getElements().getPackageOf(e).getAnnotationMirrors());
392
393
        if (e.getKind() == ElementKind.PARAMETER) {
394
            if (names.contains("ParametersAreNonnullByDefault")) {
395
                return State.NOT_NULL;
396
            } else if (names.contains("ParametersAreNullableByDefault")) {
388
                return State.POSSIBLE_NULL_REPORT;
397
                return State.POSSIBLE_NULL_REPORT;
389
            }
398
            }
390
399
        } else if (e.getKind() == ElementKind.METHOD && containsAny(names, "ReturnValuesAreNonnullByDefault", "ReturnTypesAreNonnullByDefault")) {
391
            if ("NotNull".equals(simpleName) || "NonNull".equals(simpleName) || "Nonnull".equals(simpleName)) {
392
                return State.NOT_NULL;
400
                return State.NOT_NULL;
393
            }
401
            }
402
        return State.POSSIBLE_NULL;
394
        }
403
        }
395
404
396
        return def;
405
    private static Set<String> getSimpleNames(Iterable<? extends AnnotationMirror> mirrors) {
406
        Iterator<? extends AnnotationMirror> it = mirrors.iterator();
407
        if (!it.hasNext()) {
408
            return Collections.emptySet();
409
        }
410
        Set<String> names = new HashSet<String>();
411
        while (it.hasNext()) {
412
            names.add(((TypeElement) it.next().getAnnotationType().asElement()).getSimpleName().toString());
413
        }
414
        return names;
415
    }
416
417
    private static boolean containsAny(Set<String> set, String... values) {
418
        if (set.isEmpty()) {
419
            return false;
420
        }
421
        for (String v : values) {
422
            if (set.contains(v)) {
423
                return true;
424
            }
425
        }
426
        return false;
397
    }
427
    }
398
    
428
    
399
    public interface AnnotationMirrorGetter {
429
    public interface AnnotationMirrorGetter {
(-)a/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java (-1 / +66 lines)
Lines 28-34 Link Here
28
28
29
package org.netbeans.modules.java.hints.bugs;
29
package org.netbeans.modules.java.hints.bugs;
30
30
31
import junit.framework.TestSuite;
32
import org.netbeans.junit.NbTestCase;
31
import org.netbeans.junit.NbTestCase;
33
import org.netbeans.modules.java.hints.test.api.HintTest;
32
import org.netbeans.modules.java.hints.test.api.HintTest;
34
import org.openide.filesystems.FileUtil;
33
import org.openide.filesystems.FileUtil;
Lines 1476-1481 Link Here
1476
                .assertWarnings();
1475
                .assertWarnings();
1477
    }
1476
    }
1478
1477
1478
    public void testParametersAreNonnullByDefault() throws Exception {
1479
        createWithAnnotatedTestPackage("ParametersAreNonnullByDefault",
1480
                ""
1481
                + "package test;\n"
1482
                + "class Test {\n"
1483
                + "    public int getLength(String s) {\n"
1484
                + "        return s.length();\n"
1485
                + "    }\n"
1486
                + "}")
1487
                .run(NPECheck.class)
1488
                .assertWarnings();
1489
    }
1490
1491
    public void testParametersAreNonnullByDefaultUnnecessaryCheck() throws Exception {
1492
        createWithAnnotatedTestPackage("ParametersAreNonnullByDefault",
1493
                ""
1494
                + "package test;\n"
1495
                + "class Test {\n"
1496
                + "    public int getLength(String s) {\n"
1497
                + "        if (s == null) {\n"
1498
                + "            return 0;\n"
1499
                + "        }\n"
1500
                + "        return s.length();\n"
1501
                + "    }\n"
1502
                + "}")
1503
                .run(NPECheck.class)
1504
                .assertWarnings("3:12-3:21:verifier:ERR_NotNull");
1505
    }
1506
1507
    public void testParametersAreNullableByDefault() throws Exception {
1508
        createWithAnnotatedTestPackage("ParametersAreNullableByDefault",
1509
                ""
1510
                + "package test;\n"
1511
                + "class Test {\n"
1512
                + "    public int getLength(String s) {\n"
1513
                + "        return s.length();\n"
1514
                + "    }\n"
1515
                + "}")
1516
                .run(NPECheck.class)
1517
                .assertWarnings("3:17-3:23:verifier:Possibly Dereferencing null");
1518
    }
1519
1520
    public void testReturnValuesAreNonnullByDefault() throws Exception {
1521
        createWithAnnotatedTestPackage("ReturnValuesAreNonnullByDefault",
1522
                ""
1523
                + "package test;\n"
1524
                + "class Test {\n"
1525
                + "    public String returnNull() {\n"
1526
                + "        return null;\n"
1527
                + "    }\n"
1528
                + "}")
1529
                .run(NPECheck.class)
1530
                .assertWarnings("3:15-3:19:verifier:ERR_ReturningNullFromNonNull");
1531
    }
1532
1533
    private static HintTest createWithAnnotatedTestPackage(String annotation, String code) throws Exception {
1534
        return HintTest.create()
1535
                .input(code)
1536
                .input("test/package-info.java",
1537
                        "@" + annotation
1538
                        + "\n"
1539
                        + "package test;"
1540
                        + "\n"
1541
                        + "@interface " + annotation + " {}\n");
1542
    }
1543
1479
    private void performAnalysisTest(String fileName, String code, String... golden) throws Exception {
1544
    private void performAnalysisTest(String fileName, String code, String... golden) throws Exception {
1480
        HintTest.create()
1545
        HintTest.create()
1481
                .input(fileName, code)
1546
                .input(fileName, code)

Return to bug 250702