JavaSourceFile implements SourceFile { @Override public Nulls nulls() throws NullfreeException { try (final InputStream stream = source.execute()) { final List<Null> result = new ArrayList<>(); final ParseResult<CompilationUnit> parsed = parser.parse(stream); if (parsed.isSuccessful()) { final Optional<CompilationUnit> optionalUnit = parsed.getResult(); if (optionalUnit.isPresent()) { for (NullLiteralExpr nullLiteralExpr : optionalUnit.get().findAll(NullLiteralExpr.class)) { result.add(new JavaNull(nullLiteralExpr)); } } } else { final StringBuilder problems = new StringBuilder(); for (final Problem problem : parsed.getProblems()) { problems.append(problem.toString()); problems.append("\n"); } throw new NullfreeException( String.format( "Can not count nulls in: '%s'. \nPlease, fix java syntax errors: \n%s", descriptor, problems.toString() ) ); } return new SimpleNulls(result); } catch (final IOException e) { throw new NullfreeException( String.format( "Can not count nulls in: '%s'.", descriptor ), e ); } catch (ParseProblemException e) { throw new NullfreeException( String.format( "Can not count nulls in: '%s'. \nPlease, fix java syntax errors and try again.", descriptor ), e ); } } JavaSourceFile(final String... lines);  JavaSourceFile(final ParserConfiguration.LanguageLevel level, final String... lines);  JavaSourceFile(final JavaParser parser, final String... lines);  JavaSourceFile(final String source);  JavaSourceFile(final ParserConfiguration.LanguageLevel level, final String source);  JavaSourceFile(final JavaParser parser, final String source);  JavaSourceFile(final File file);  JavaSourceFile(final ParserConfiguration.LanguageLevel level, final File file);  JavaSourceFile(final JavaParser parser, final File file);  JavaSourceFile(
        final JavaParser parser,
        final Function0<InputStream, NullfreeException> source,
        final String descriptor
    ); @Override Nulls nulls();  }
@Test public void nullNotInComparision() throws Exception { final List<Null> nulls = new JavaSourceFile( "class A {\n", " void a() {\n", " String name = \"Some name\";", " if (name.equals(null)) {}", " }\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isInComparision(), IsEqual.equalTo(false) ); }
@Test public void nullsWhenParsingError() throws Exception { try { new JavaSourceFile( "class A {\n", " void int a() {\n", " }\n", "}\n" ).nulls().asList(); } catch (final NullfreeException e) { } catch (Throwable e) { Assert.fail(); } }
@Test public void nullsWhenParsingErrorInFile() throws Exception { final File source = folder.newFile("Main.java"); try (final PrintWriter writer = new PrintWriter(source)) { writer.println("class A {"); writer.println(" private public final String a = null;"); writer.println("}"); } try { new JavaSourceFile(source).nulls().asList(); } catch (final NullfreeException e) { Assert.assertThat(e.getMessage(), StringContains.containsString("Main.java")); } catch (Throwable e) { Assert.fail(); } }
@Test public void nullsInPrivateInterfaceMethod() throws Exception { Assert.assertThat( new JavaSourceFile( "interface A {\n", " private void a() {\n", " String a = null;\n", " }\n", "}\n" ).nulls().asList().size(), IsEqual.equalTo(1) ); }
@Test public void nullsInJava12SyntaxFile() throws Exception { Assert.assertThat( new JavaSourceFile( "public class Test {\n", " enum Day {\n", " MON, TUE, WED, THUR, FRI, SAT, SUN\n", " };\n", " @SuppressWarnings(\"preview\")\n", " public String getDay_1 (Day today) {\n", " String day = switch(today) {\n", " case MON, TUE, WED, THUR, FRI -> \"Weekday\";\n", " case SAT, SUN -> \"Weekend\";\n", " };\n", " return day;\n", " }\n", "}" ).nulls().asList().size(), IsEqual.equalTo(0) ); }
@Test public void oneNullLiteral() throws NullfreeException { Assert.assertThat( new JavaSourceFile( "class A {\n", " private final String a = null;\n", "}\n" ).nulls().asList().size(), IsEqual.equalTo(1) ); }
@Test public void oneNullLiteralFromFile() throws Exception { final File source = folder.newFile(); try (final PrintWriter writer = new PrintWriter(source)) { writer.println("class A {"); writer.println(" private final String a = null;"); writer.println("}"); } Assert.assertThat( new JavaSourceFile(source).nulls().asList().size(), IsEqual.equalTo(1) ); }
@Test public void classSuppressedNull() throws Exception { final List<Null> nulls = new JavaSourceFile( "@SuppressWarnings(\"nullfree\")\n", "class A {\n", " private final String a = null;\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isSuppressed(), IsEqual.equalTo(true) ); }
@Test public void methodSuppressedNull() throws Exception { final List<Null> nulls = new JavaSourceFile( "class A {\n", " @SuppressWarnings(\"nullfree\")\n", " void method() {\n", " String name = null;\n", " }\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isSuppressed(), IsEqual.equalTo(true) ); }
@Test public void assignSuppressedNull() throws Exception { final List<Null> nulls = new JavaSourceFile( "class A {\n", " void method() {\n", " @SuppressWarnings(\"nullfree\")\n", " String name = null;\n", " }\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isSuppressed(), IsEqual.equalTo(true) ); }
@Test public void fieldSuppressedNull() throws Exception { final List<Null> nulls = new JavaSourceFile( "class A {\n", " @SuppressWarnings(\"nullfree\")\n", " String name = null;\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isSuppressed(), IsEqual.equalTo(true) ); }
@Test public void fieldMultipleSuppressedNull() throws Exception { final List<Null> nulls = new JavaSourceFile( "class A {\n", " @SuppressWarnings(value = {\"nullfree\", \"something_else\"})\n", " String name = null;\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isSuppressed(), IsEqual.equalTo(true) ); }
@Test public void nullInComparision() throws Exception { final List<Null> nulls = new JavaSourceFile( "class A {\n", " void a() {\n", " String name = \"Some name\";", " if (name != null) {}", " }\n", "}\n" ).nulls().asList(); Assert.assertThat( nulls.size(), IsEqual.equalTo(1) ); Assert.assertThat( nulls.get(0).isInComparision(), IsEqual.equalTo(true) ); }