Skip to content

Commit

Permalink
#404 jsr305 -> JSpecify
Browse files Browse the repository at this point in the history
also, we replace jsr305 @CheckReturnValue annotation by `com.google.errorprone.annotations.CheckReturnValue` (because JSpecify doesn't yet have such annotation).

P.S. There is still multiple warnings about potential nullability problems all over the codebase. This is a sad reality, somebody needs to review them one day.
  • Loading branch information
asolntsev committed Oct 8, 2024
1 parent 33cbcc3 commit 18cd309
Show file tree
Hide file tree
Showing 172 changed files with 684 additions and 586 deletions.
1 change: 0 additions & 1 deletion .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.xhtmlrenderer.context;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.value.FontSpecification;
import org.xhtmlrenderer.extend.FontResolver;
Expand Down Expand Up @@ -63,7 +65,7 @@ public void flushCache() {
init();
}

public FSFont resolveFont(SharedContext ctx, String[] families, float size, IdentValue weight, IdentValue style, IdentValue variant) {
public FSFont resolveFont(SharedContext ctx, String @Nullable[] families, float size, IdentValue weight, IdentValue style, IdentValue variant) {
// for each font family
if (families != null) {
for (String family : families) {
Expand Down Expand Up @@ -95,7 +97,10 @@ public void setFontMapping(String name, Font font) {
availableFonts.put(name, font.deriveFont(1.0f));
}

protected static Font createFont(SharedContext ctx, Font root_font, float size, IdentValue weight, IdentValue style, IdentValue variant) {
protected static Font createFont(SharedContext ctx, Font root_font, float size,
@Nullable IdentValue weight,
@Nullable IdentValue style,
@Nullable IdentValue variant) {
int font_const = Font.PLAIN;
if (weight != null &&
(weight == IdentValue.BOLD ||
Expand All @@ -122,6 +127,7 @@ protected static Font createFont(SharedContext ctx, Font root_font, float size,
return fnt;
}

@Nullable
protected Font resolveFont(SharedContext ctx, String font, float size, IdentValue weight, IdentValue style, IdentValue variant) {
// strip off the "s if they are there
if (font.startsWith("\"")) {
Expand Down Expand Up @@ -179,6 +185,8 @@ protected static String getFontInstanceHashName(SharedContext ctx, String name,
return name + "-" + (size * ctx.getTextRenderer().getFontScale()) + "-" + weight + "-" + style + "-" + variant;
}

@Nullable
@CheckReturnValue
@Override
public FSFont resolveFont(SharedContext renderingContext, FontSpecification spec) {
return resolveFont(renderingContext, spec.families, spec.size, spec.fontWeight, spec.fontStyle, spec.variant);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/
package org.xhtmlrenderer.context;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xhtmlrenderer.css.extend.AttributeResolver;
import org.xhtmlrenderer.extend.NamespaceHandler;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.extend.UserInterface;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.IdentityHashMap;
import java.util.Map;

Expand All @@ -36,7 +36,6 @@
*
* @author Torbjoern Gannholm
*/
@ParametersAreNonnullByDefault
public class StandardAttributeResolver implements AttributeResolver {
private final NamespaceHandler nsh;
private final UserAgentCallback uac;
Expand Down Expand Up @@ -66,6 +65,7 @@ public String getAttributeValue(Node e, String namespaceURI, String attrName) {
* Gets the class attribute of the StandardAttributeResolver object
*/
@Override
@Nullable
public String getClass(Node e) {
return classAttributeCache.computeIfAbsent(e, (x) -> nsh.getClass((Element) e));
}
Expand All @@ -74,11 +74,13 @@ public String getClass(Node e) {
* Gets the iD attribute of the StandardAttributeResolver object
*/
@Override
@Nullable
public String getID(Node e) {
return nsh.getID((Element) e);
}

@Override
@Nullable
public String getNonCssStyling(Node e) {
return nsh.getNonCssStyling((Element) e);
}
Expand All @@ -87,6 +89,7 @@ public String getNonCssStyling(Node e) {
* Gets the elementStyling attribute of the StandardAttributeResolver object
*/
@Override
@Nullable
public String getElementStyling(Node e) {
return nsh.getElementStyling((Element) e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.context;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -149,6 +150,7 @@ public Map<String, CSSPrimitiveValue> getCascadedPropertiesMap(Element e) {
/**
* Gets the pseudoElementStyle attribute of the StyleReference object
*/
@Nullable
public CascadedStyle getPseudoElementStyle(Node node, String pseudoElement) {
Element e;
if (node.getNodeType() == Node.ELEMENT_NODE) {
Expand All @@ -163,7 +165,7 @@ public CascadedStyle getPseudoElementStyle(Node node, String pseudoElement) {
* Gets the CascadedStyle for an element. This must then be converted in the
* current context to a CalculatedStyle (use getDerivedStyle)
*/
public CascadedStyle getCascadedStyle(Element e, boolean restyle) {
public CascadedStyle getCascadedStyle(@Nullable Element e, boolean restyle) {
if (e == null) return CascadedStyle.emptyCascadedStyle;
return _matcher.getCascadedStyle(e, restyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.context;

import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.extend.StylesheetFactory;
import org.xhtmlrenderer.css.parser.CSSParser;
import org.xhtmlrenderer.css.sheet.Ruleset;
Expand All @@ -31,7 +32,6 @@
import org.xhtmlrenderer.util.XRLog;
import org.xml.sax.InputSource;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NullMarked
package org.xhtmlrenderer.context;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package org.xhtmlrenderer.css.constants;

import com.google.errorprone.annotations.CheckReturnValue;
import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.parser.CSSParser;
import org.xhtmlrenderer.css.parser.PropertyValue;
import org.xhtmlrenderer.css.parser.property.BackgroundPropertyBuilder;
Expand All @@ -39,9 +41,6 @@
import org.xhtmlrenderer.css.style.derived.DerivedValueFactory;
import org.xhtmlrenderer.util.XRLog;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -57,7 +56,6 @@
*
* @author Patrick Wright
*/
@ParametersAreNonnullByDefault
@CheckReturnValue
public final class CSSName implements Comparable<CSSName> {
/**
Expand Down Expand Up @@ -104,6 +102,7 @@ public final class CSSName implements Comparable<CSSName> {

private final boolean implemented;

@Nullable
private final PropertyBuilder builder;

/**
Expand Down Expand Up @@ -1667,7 +1666,7 @@ public final class CSSName implements Comparable<CSSName> {

private CSSName(
String propName, String initialValue, boolean inherits,
boolean implemented, PropertyBuilder builder) {
boolean implemented, @Nullable PropertyBuilder builder) {
this.propName = propName;
this.FS_ID = CSSName.maxAssigned++;
this.initialValue = initialValue;
Expand Down Expand Up @@ -1741,6 +1740,7 @@ public static boolean isImplemented(CSSName cssName) {
return cssName.implemented;
}

@Nullable
public static PropertyBuilder getPropertyBuilder(CSSName cssName) {
return cssName.builder;
}
Expand Down Expand Up @@ -1777,7 +1777,7 @@ private static synchronized CSSName addProperty(
String initialValue,
Object inherit,
boolean implemented,
PropertyBuilder builder
@Nullable PropertyBuilder builder
) {
CSSName cssName = new CSSName(
propName, initialValue, (inherit == INHERITS), implemented, builder);
Expand Down Expand Up @@ -1837,6 +1837,7 @@ public int hashCode() {
return FS_ID;
}

@CheckReturnValue
public record CSSSideProperties(CSSName top, CSSName right, CSSName bottom, CSSName left) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.xhtmlrenderer.css.constants;

import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.parser.FSColor;
import org.xhtmlrenderer.css.style.CssContext;
import org.xhtmlrenderer.css.style.FSDerivedValue;
Expand Down Expand Up @@ -252,6 +253,7 @@ public static boolean looksLikeIdent(String ident) {
return ALL_IDENT_VALUES.get(ident) != null;
}

@Nullable
public static IdentValue valueOf(String ident) {
return ALL_IDENT_VALUES.get(ident);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
*/
package org.xhtmlrenderer.css.constants;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.w3c.dom.css.CSSValue;
import org.xhtmlrenderer.util.GeneralUtil;
import org.xhtmlrenderer.util.XRLog;
import org.xhtmlrenderer.util.XRRuntimeException;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand All @@ -45,7 +44,6 @@
/**
* Utility class for working with {@code CSSValue} instances.
*/
@ParametersAreNonnullByDefault
public final class ValueConstants {
/**
* Type descriptions--a crude approximation taken by scanning CSSValue statics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NullMarked
package org.xhtmlrenderer.css.constants;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
*/
package org.xhtmlrenderer.css.extend;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.Node;

import javax.annotation.Nullable;

/**
* In XML, an application may or may not know how to find the ID and/or class
* and/or attribute defaults of an element. <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.xhtmlrenderer.css.newmatch;

import org.jspecify.annotations.Nullable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xhtmlrenderer.css.constants.MarginBoxName;
Expand Down Expand Up @@ -94,6 +95,7 @@ public CascadedStyle getCascadedStyle(Element e, boolean restyle) {
* May return null.
* We assume that restyle has already been done by a getCascadedStyle if necessary.
*/
@Nullable
public CascadedStyle getPECascadedStyle(Element e, String pseudoElement) {
synchronized (e) {
Mapper em = getMapper(e);
Expand Down Expand Up @@ -337,6 +339,7 @@ CascadedStyle getCascadedStyle(Node e) {
* May return null.
* We assume that restyle has already been done by a getCascadedStyle if necessary.
*/
@Nullable
public CascadedStyle getPECascadedStyle(Node e, String pseudoElement) {
Iterator<Map.Entry<String, List<Selector>>> si = pseudoSelectors.entrySet().iterator();
if (!si.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*/
package org.xhtmlrenderer.css.newmatch;

import org.jspecify.annotations.Nullable;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.constants.MarginBoxName;
import org.xhtmlrenderer.css.parser.PropertyValue;
import org.xhtmlrenderer.css.sheet.PropertyDeclaration;
import org.xhtmlrenderer.css.sheet.StylesheetInfo;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
*/
package org.xhtmlrenderer.css.parser;

import org.jspecify.annotations.Nullable;

public class CSSParseException extends RuntimeException {
@Nullable
private final Token _found;
@Nullable
private final Token[] _expected;
private int _line;

@Nullable
private final String _genericMessage;

private boolean _callerNotified;
Expand All @@ -32,7 +37,7 @@ public CSSParseException(String message, int line) {
this(message, line, null);
}

public CSSParseException(String message, int line, Throwable cause) {
public CSSParseException(String message, int line, @Nullable Throwable cause) {
super(message, cause);
_found = null;
_expected = null;
Expand All @@ -47,7 +52,7 @@ public CSSParseException(Token found, Token expected, int line) {
_genericMessage = null;
}

public CSSParseException(Token found, Token[] expected, int line) {
public CSSParseException(Token found, @Nullable Token[] expected, int line) {
_found = found;
_expected = expected == null ? new Token[]{} : expected.clone();
_line = line;
Expand Down
Loading

0 comments on commit 18cd309

Please sign in to comment.