Skip to content

Commit

Permalink
refact: improve null analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jul 19, 2024
1 parent d773164 commit 37f10de
Show file tree
Hide file tree
Showing 28 changed files with 109 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.eclipse.tm4e.core.internal.parser;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.assertNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.castNonNull;

import java.io.BufferedReader;
import java.io.Reader;
Expand Down Expand Up @@ -53,7 +53,7 @@ protected TMParserJSON() {
protected Map<String, Object> loadRaw(final Reader source) {
// GSON does not support trailing commas so we have to manually remove them -> maybe better switch to jackson json parser?
final var jsonString = removeTrailingCommas(new BufferedReader(source).lines().collect(Collectors.joining("\n")));
return assertNonNull(LOADER.fromJson(jsonString, Map.class));
return castNonNull(LOADER.fromJson(jsonString, Map.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@

public final class NullSafetyHelper {

public static <T> @NonNull T assertNonNull(final @Nullable T value) {
if (value == null)
throw new IllegalStateException("Null value!");
return value;
}

/**
* Casts non-null value marked as {@link Nullable} to {@link NonNull}.
* Casts a non-null value marked as {@link Nullable} to {@link NonNull}.
* <p>
* Only use if you are sure the value is non-null but annotation-based null analysis was not able to determine it.
* <p>
* This method is not meant for non-null input validation.
*
* @throws AssertionError if JVM assertions are enabled and the given value is null
* @throws IllegalStateException if the given value is null
*/
public static <T> @NonNull T castNonNull(final @Nullable T value) {
assert value != null;
if (value == null)
throw new IllegalStateException("Unexpected null value present!");
return value;
}

/**
* Casts the elements of given array to {@link NonNull} without any validation.
* <p>
* Only use if you are sure the value is non-null but annotation-based null analysis was not able to determine it.
*/
@SuppressWarnings("null")
private static <T> @NonNull T castNonNullUnsafe(final T value) {
public static <T> @NonNull T castNonNullUnsafe(final T value) {
return value;
}

Expand All @@ -58,18 +58,19 @@ public static <T> T defaultIfNull(final @Nullable T object, final T defaultValue
return object;
}

public static <T> T defaultIfNull(@Nullable final T object, final Supplier<T> defaultValue) {
public static <T> T defaultIfNull(final @Nullable T object, final Supplier<T> defaultValue) {
if (object == null) {
return defaultValue.get();
}
return object;
}

/**
* Allows to initializes a @NonNull field with <code>null</code> that is lazy initialized
* Allows to initializes a @NonNull field with <code>null</code> that is
* initialized later.
*/
@SuppressWarnings("unchecked")
public static <T> @NonNull T lazyNonNull() {
public static <T> @NonNull T lateNonNull() {
return (T) castNonNullUnsafe(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package org.eclipse.tm4e.core.model;

import static java.lang.System.Logger.Level.*;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lateNonNull;

import java.lang.System.Logger;
import java.time.Duration;
Expand Down Expand Up @@ -99,7 +99,7 @@ public String toString() {
/** The background thread performing async line tokenizations */
private volatile @Nullable TokenizerThread tokenizerThread;
private volatile boolean tokenizerThreadHasWork;
private TMTokenizationSupport tokenizer = lazyNonNull();
private TMTokenizationSupport tokenizer = lateNonNull();

/** package visibility for tests **/
final ArrayList<LineTokens> lines;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private static String removeTrailingCommas(final String jsonString) {
.create();

try {
final var langCfg = assertNonNull(gsonBuilder.fromJson(jsonString, LanguageConfiguration.class));
final var langCfg = castNonNull(gsonBuilder.fromJson(jsonString, LanguageConfiguration.class));
if (castNullable(langCfg.autoClosingPairs) == null) {
langCfg.autoClosingPairs = Collections.emptyList();
} else {
Expand Down Expand Up @@ -364,7 +364,7 @@ private static boolean getAsBoolean(final @Nullable JsonElement element, final b
return comments;
}

private List<CharacterPair> brackets = lazyNonNull();
private List<CharacterPair> brackets = lateNonNull();

/**
* Returns the language's brackets. This configuration implicitly affects pressing Enter around these brackets.
Expand Down Expand Up @@ -395,7 +395,7 @@ public List<CharacterPair> getBrackets() {
return indentationRules;
}

private List<OnEnterRule> onEnterRules = lazyNonNull();
private List<OnEnterRule> onEnterRules = lateNonNull();

/**
* Returns the language's rules to be evaluated when pressing Enter.
Expand All @@ -406,7 +406,7 @@ public List<OnEnterRule> getOnEnterRules() {
return onEnterRules;
}

private List<AutoClosingPairConditional> autoClosingPairs = lazyNonNull();
private List<AutoClosingPairConditional> autoClosingPairs = lateNonNull();

/**
* Returns the language's auto closing pairs. The 'close' character is automatically inserted with the 'open'
Expand All @@ -418,7 +418,7 @@ public List<AutoClosingPairConditional> getAutoClosingPairs() {
return autoClosingPairs;
}

private List<AutoClosingPair> surroundingPairs = lazyNonNull();
private List<AutoClosingPair> surroundingPairs = lateNonNull();

/**
* Returns the language's surrounding pairs. When the 'open' character is typed on a selection, the selected string
Expand All @@ -430,7 +430,7 @@ public List<AutoClosingPair> getSurroundingPairs() {
return surroundingPairs;
}

private List<CharacterPair> colorizedBracketPairs = lazyNonNull();
private List<CharacterPair> colorizedBracketPairs = lateNonNull();

/**
* Defines a list of bracket pairs that are colorized depending on their nesting level.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.preferences;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lateNonNull;
import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -46,7 +46,7 @@ public final class LanguageConfigurationPreferencePage extends AbstractPreferenc
static final String PAGE_ID = "org.eclipse.tm4e.languageconfiguration.preferences.LanguageConfigurationPreferencePage"; //$NON-NLS-1$

private ILanguageConfigurationRegistryManager.EditSession manager = LanguageConfigurationRegistryManager.getInstance().newEditSession();
private TableWidget<ILanguageConfigurationDefinition> langCfgsTable = lazyNonNull();
private TableWidget<ILanguageConfigurationDefinition> langCfgsTable = lateNonNull();

public LanguageConfigurationPreferencePage() {
super(LanguageConfigurationPreferencePage_title, LanguageConfigurationPreferencePage_description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lateNonNull;
import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;

import java.util.List;
Expand Down Expand Up @@ -52,32 +52,32 @@ public class LanguageConfigurationInfoWidget extends Composite {
LanguageConfigurationInfoWidget_colorizedBracketPairs_title,
};

private Text lineCommentText = lazyNonNull();
private Text blockCommentStartText = lazyNonNull();
private Text blockCommentEndText = lazyNonNull();
private Text lineCommentText = lateNonNull();
private Text blockCommentStartText = lateNonNull();
private Text blockCommentEndText = lateNonNull();

private CharacterPairsTableWidget bracketsTable = lazyNonNull();
private CharacterPairsTableWidget bracketsTable = lateNonNull();

private AutoClosingPairConditionalTableWidget autoClosingPairsTable = lazyNonNull();
private AutoClosingPairConditionalTableWidget autoClosingPairsTable = lateNonNull();

private Text autoCloseBeforeText = lazyNonNull();
private Text autoCloseBeforeText = lateNonNull();

private CharacterPairsTableWidget surroundingPairsTable = lazyNonNull();
private CharacterPairsTableWidget surroundingPairsTable = lateNonNull();

private Text foldingOffsideText = lazyNonNull();
private Text foldingMarkersStartText = lazyNonNull();
private Text foldingMarkersEndText = lazyNonNull();
private Text foldingOffsideText = lateNonNull();
private Text foldingMarkersStartText = lateNonNull();
private Text foldingMarkersEndText = lateNonNull();

private Text wordPatternText = lazyNonNull();
private Text wordPatternText = lateNonNull();

private OnEnterRuleTableWidget onEnterRuleTable = lazyNonNull();
private OnEnterRuleTableWidget onEnterRuleTable = lateNonNull();

private Text indentationDecreaseIndentPattern = lazyNonNull();
private Text indentationIncreaseIndentPattern = lazyNonNull();
private Text indentationIndentNextLinePattern = lazyNonNull();
private Text indentationUnIndentedLinePattern = lazyNonNull();
private Text indentationDecreaseIndentPattern = lateNonNull();
private Text indentationIncreaseIndentPattern = lateNonNull();
private Text indentationIndentNextLinePattern = lateNonNull();
private Text indentationUnIndentedLinePattern = lateNonNull();

private CharacterPairsTableWidget colorizedBracketPairsTable = lazyNonNull();
private CharacterPairsTableWidget colorizedBracketPairsTable = lateNonNull();

public LanguageConfigurationInfoWidget(final Composite parent, final int style) {
super(parent, style);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
package org.eclipse.tm4e.languageconfiguration.internal.widgets;

import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lateNonNull;
import static org.eclipse.tm4e.languageconfiguration.internal.LanguageConfigurationMessages.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -31,8 +31,8 @@ public final class LanguageConfigurationPreferencesWidget extends LanguageConfig
private @NonNullByDefault({}) Button toggleBracketAutoClosingButton;
private @NonNullByDefault({}) Button toggleMatchingPairsButton;

private ILanguageConfigurationDefinition definition = lazyNonNull();
private ILanguageConfigurationRegistryManager.EditSession manager = lazyNonNull();
private ILanguageConfigurationDefinition definition = lateNonNull();
private ILanguageConfigurationRegistryManager.EditSession manager = lateNonNull();

public LanguageConfigurationPreferencesWidget(final Composite parent, final int style) {
super(parent, style);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.wizards;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lateNonNull;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.viewers.IStructuredSelection;
Expand All @@ -31,8 +31,8 @@ public final class LanguageConfigurationImportWizard extends Wizard implements I
private final ILanguageConfigurationRegistryManager.EditSession manager;
private final boolean saveOnFinish;

private SelectLanguageConfigurationWizardPage mainPage = lazyNonNull();
private ILanguageConfigurationDefinition createdDefinition = lazyNonNull();
private SelectLanguageConfigurationWizardPage mainPage = lateNonNull();
private ILanguageConfigurationDefinition createdDefinition = lateNonNull();

public LanguageConfigurationImportWizard(final ILanguageConfigurationRegistryManager.EditSession manager, final boolean saveOnFinish) {
this.manager = manager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ final class SelectLanguageConfigurationWizardPage extends WizardPage implements

private static final String[] TEXTMATE_EXTENSIONS = { "*language-configuration.json" }; //$NON-NLS-1$

private Text fileText = lazyNonNull();
private Text contentTypeText = lazyNonNull();
private LanguageConfigurationInfoWidget infoWidget = lazyNonNull();
private Text fileText = lateNonNull();
private Text contentTypeText = lateNonNull();
private LanguageConfigurationInfoWidget infoWidget = lateNonNull();

private final ILanguageConfigurationRegistryManager registryManager;

Expand Down Expand Up @@ -306,7 +306,7 @@ ILanguageConfigurationDefinition getDefinition() {
path = castNonNull(ResourcesPlugin.getWorkspace().getRoot().getFile(path).getLocation());
}

final var contentType = assertNonNull(ContentTypeHelper.getContentTypeById(contentTypeText.getText()));
final var contentType = castNonNull(ContentTypeHelper.getContentTypeById(contentTypeText.getText()));
return new LanguageConfigurationDefinition(contentType, path.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.io.LineNumberReader;
import java.io.StringReader;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultLineTracker;
import org.eclipse.jface.text.IDocument;
Expand All @@ -33,7 +33,6 @@
* Partially implemented {@link IDocument}. Especially all methods related to {@link IDocumentListener}, {@link IDocumentPartitioner},
* {@link Position} are not implemented.
*/
@NonNullByDefault({})
public class MockDocument implements IDocument {

private String contentType;
Expand Down Expand Up @@ -164,7 +163,7 @@ public int getLength() {
}

@Override
public String getLineDelimiter(final int lineIndex) throws BadLocationException {
public @Nullable String getLineDelimiter(final int lineIndex) throws BadLocationException {
final String[] lines = text.split("\n");
if (lines.length <= lineIndex)
throw new BadLocationException("Line " + lineIndex + " not present.");
Expand Down Expand Up @@ -305,7 +304,7 @@ public void set(final String text) {
}

@Override
public void setDocumentPartitioner(IDocumentPartitioner partitioner) {
public void setDocumentPartitioner(@Nullable IDocumentPartitioner partitioner) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static final class RegionWithTMToken extends Region {
}

@Override
public IInformationControlCreator getHoverControlCreator() {
public @Nullable IInformationControlCreator getHoverControlCreator() {
// setup a hover control that interprets basic HTML input
return new AbstractReusableInformationControlCreator() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*******************************************************************************/
package org.eclipse.tm4e.ui.internal.preferences;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lazyNonNull;
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.lateNonNull;

import java.util.stream.Collectors;

Expand Down Expand Up @@ -65,14 +65,14 @@ public final class GrammarPreferencePage extends AbstractPreferencePage {
private IThemeManager.EditSession themeManager = ThemeManager.getInstance().newEditSession();
private final ISnippetManager snippetManager = TMUIPlugin.getSnippetManager();

private TableWidget<IGrammarDefinition> grammarsTable = lazyNonNull();
private TableWidget<IGrammarDefinition> grammarsTable = lateNonNull();

// Grammar info tabs
private GrammarInfoWidget grammarInfoWidget = lazyNonNull();
private TableWithControlsWidget<IContentType> contentTypesWidget = lazyNonNull();
private ThemeAssociationsWidget themeAssociationsWidget = lazyNonNull();
private GrammarInfoWidget grammarInfoWidget = lateNonNull();
private TableWithControlsWidget<IContentType> contentTypesWidget = lateNonNull();
private ThemeAssociationsWidget themeAssociationsWidget = lateNonNull();

private TMViewer grammarPreview = lazyNonNull();
private TMViewer grammarPreview = lateNonNull();

public GrammarPreferencePage() {
super(TMUIMessages.GrammarPreferencePage_title, TMUIMessages.GrammarPreferencePage_description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void write(final JsonWriter out, final @Nullable MarkerConfig value) thro

@Override
public @Nullable MarkerConfig read(final JsonReader in) throws IOException {
final var objectJson = assertNonNull(jsonElementAdapter.read(in)).getAsJsonObject();
final var objectJson = castNonNull(jsonElementAdapter.read(in)).getAsJsonObject();
return switch (MarkerConfig.Type.valueOf(objectJson.get("type").getAsString())) {
case PROBLEM -> problemAdapter.fromJsonTree(objectJson);
case TASK -> taskAdapter.fromJsonTree(objectJson);
Expand All @@ -77,7 +77,7 @@ public void write(final JsonWriter out, final @Nullable MarkerConfig value) thro
}).create();

public static IThemeAssociation[] loadThemeAssociations(final String json) {
return assertNonNull(DEFAULT_GSON.fromJson(json, ThemeAssociation[].class));
return castNonNull(DEFAULT_GSON.fromJson(json, ThemeAssociation[].class));
}

public static String toJsonThemeAssociations(final Collection<IThemeAssociation> themeAssociations) {
Expand All @@ -97,7 +97,7 @@ public static Set<MarkerConfig> loadMarkerConfigs() {
}

public static Set<MarkerConfig> loadMarkerConfigs(final String json) {
return assertNonNull(DEFAULT_GSON.fromJson(json, new TypeToken<Set<MarkerConfig>>() {
return castNonNull(DEFAULT_GSON.fromJson(json, new TypeToken<Set<MarkerConfig>>() {
}.getType()));
}

Expand Down
Loading

0 comments on commit 37f10de

Please sign in to comment.