Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial nullability annotations #1467

Merged
merged 4 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ jsoup changelog

* Build Improvement: updated Jetty (used for integration tests; not bundled) to 9.4.35.v2020112.

* Build Improvement: added nullability annotations and initial settings.
<https:/jhy/jsoup/pull/1467>

* Bugfix: when parsing HTML, could throw NPEs on some tags (isindex or table>input).
<https:/jhy/jsoup/issues/1404>

Expand Down
17 changes: 12 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<url>https://jhy.io/</url>
</organization>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -311,7 +315,6 @@
<scope>test</scope>
</dependency>


<dependency>
<!-- jetty for webserver integration tests -->
<groupId>org.eclipse.jetty</groupId>
Expand All @@ -320,17 +323,21 @@
<scope>test</scope>
</dependency>

<dependency>
<!-- javax.annotations.nonnull, with Apache 2 (not GPL) license. Not distributed. -->
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<developers>
<developer>
<id>jhy</id>
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/jsoup/Jsoup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import org.jsoup.helper.DataUtil;
import org.jsoup.helper.HttpConnection;
import org.jsoup.internal.ReturnsAreNonnullByDefault;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Safelist;
import org.jsoup.safety.Whitelist;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -17,6 +20,8 @@
The core public access point to the jsoup functionality.

@author Jonathan Hedley */

@ParametersAreNonnullByDefault @ReturnsAreNonnullByDefault
public class Jsoup {
private Jsoup() {}

Expand Down Expand Up @@ -85,7 +90,7 @@ public static Connection connect(String url) {

@throws IOException if the file could not be found, or read, or if the charsetName is invalid.
*/
public static Document parse(File in, String charsetName, String baseUri) throws IOException {
public static Document parse(File in, @Nullable String charsetName, String baseUri) throws IOException {
return DataUtil.load(in, charsetName, baseUri);
}

Expand All @@ -100,7 +105,7 @@ public static Document parse(File in, String charsetName, String baseUri) throws
@throws IOException if the file could not be found, or read, or if the charsetName is invalid.
@see #parse(File, String, String)
*/
public static Document parse(File in, String charsetName) throws IOException {
public static Document parse(File in, @Nullable String charsetName) throws IOException {
return DataUtil.load(in, charsetName, in.getAbsolutePath());
}

Expand All @@ -115,7 +120,7 @@ public static Document parse(File in, String charsetName) throws IOException {

@throws IOException if the file could not be found, or read, or if the charsetName is invalid.
*/
public static Document parse(InputStream in, String charsetName, String baseUri) throws IOException {
public static Document parse(InputStream in, @Nullable String charsetName, String baseUri) throws IOException {
return DataUtil.load(in, charsetName, baseUri);
}

Expand All @@ -132,7 +137,7 @@ public static Document parse(InputStream in, String charsetName, String baseUri)

@throws IOException if the file could not be found, or read, or if the charsetName is invalid.
*/
public static Document parse(InputStream in, String charsetName, String baseUri, Parser parser) throws IOException {
public static Document parse(InputStream in, @Nullable String charsetName, String baseUri, Parser parser) throws IOException {
return DataUtil.load(in, charsetName, baseUri, parser);
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/jsoup/internal/ReturnsAreNonnullByDefault.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jsoup.internal;

import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Documented
@Nonnull
@TypeQualifierDefault(ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)

/**
Indicates return types are not nullable, unless otherwise specified by @Nullable.
@see javax.annotation.ParametersAreNonnullByDefault
*/
public @interface ReturnsAreNonnullByDefault {
}
3 changes: 2 additions & 1 deletion src/main/java/org/jsoup/parser/HtmlTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
Expand Down Expand Up @@ -60,7 +61,7 @@ ParseSettings defaultSettings() {
return ParseSettings.htmlDefault;
}

@Override
@Override @ParametersAreNonnullByDefault
protected void initialiseParse(Reader input, String baseUri, Parser parser) {
super.initialiseParse(input, baseUri, parser);

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jsoup/parser/TreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,9 +28,11 @@ abstract class TreeBuilder {
private Token.EndTag end = new Token.EndTag();
abstract ParseSettings defaultSettings();

@ParametersAreNonnullByDefault
protected void initialiseParse(Reader input, String baseUri, Parser parser) {
Validate.notNull(input, "String input must not be null");
Validate.notNull(baseUri, "BaseURI must not be null");
Validate.notNull(parser);

doc = new Document(baseUri);
doc.parser(parser);
Expand All @@ -42,6 +45,7 @@ protected void initialiseParse(Reader input, String baseUri, Parser parser) {
this.baseUri = baseUri;
}

@ParametersAreNonnullByDefault
Document parse(Reader input, String baseUri, Parser parser) {
initialiseParse(input, baseUri, parser);
runParser();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jsoup/parser/XmlTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jsoup.nodes.TextNode;
import org.jsoup.nodes.XmlDeclaration;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
Expand All @@ -26,7 +27,7 @@ ParseSettings defaultSettings() {
return ParseSettings.preserveCase;
}

@Override
@Override @ParametersAreNonnullByDefault
protected void initialiseParse(Reader input, String baseUri, Parser parser) {
super.initialiseParse(input, baseUri, parser);
stack.add(doc); // place the document onto the stack. differs from HtmlTreeBuilder (not on stack)
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/jsoup/helper/ValidateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jsoup.helper;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ValidateTest {

@Test
public void testNotNull() {
Validate.notNull("foo");
boolean threw = false;
try {
Validate.notNull(null);
} catch (IllegalArgumentException e) {
threw = true;
}
Assertions.assertTrue(threw);
}
}
31 changes: 30 additions & 1 deletion src/test/java/org/jsoup/parser/HtmlTreeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

import org.junit.jupiter.api.Test;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.*;

public class HtmlTreeBuilderTest {
@Test
Expand All @@ -26,4 +31,28 @@ public void ensureSearchArraysAreSorted() {
assertArrayEquals(array, copy);
}
}

@Test
public void nonnull() {
assertThrows(IllegalArgumentException.class, () -> {
HtmlTreeBuilder treeBuilder = new HtmlTreeBuilder();
treeBuilder.parse(null, null, null); // not sure how to test that these visual warnings actually appear! - test below checks for method annotation
}
); // I'm not convinced that this lambda is easier to read than the old Junit 4 @Test(expected=IEA.class)...
}

@Test public void nonnullAssertions() throws NoSuchMethodException {
Method parseMethod = TreeBuilder.class.getDeclaredMethod("parse", Reader.class, String.class, Parser.class);
assertNotNull(parseMethod);
Annotation[] declaredAnnotations = parseMethod.getDeclaredAnnotations();
boolean seen = false;
for (Annotation annotation : declaredAnnotations) {
if (annotation.annotationType().isAssignableFrom(ParametersAreNonnullByDefault.class))
seen = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: a break can be added here

}

// would need to rework this if/when that annotation moves from the method to the class / package.
assertTrue(seen);

}
}