Skip to content

Commit d117e6a

Browse files
committed
Add JSpecify to spring-shell-table
Signed-off-by: Piotr Olaszewski <[email protected]>
1 parent 773e576 commit d117e6a

File tree

10 files changed

+54
-13
lines changed

10 files changed

+54
-13
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package org.springframework.shell;
3+
4+
import org.jspecify.annotations.NullMarked;

spring-shell-table/src/main/java/org/springframework/shell/table/ArrayTableModel.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package org.springframework.shell.table;
1818

19+
import org.jspecify.annotations.Nullable;
1920
import org.springframework.util.Assert;
2021

2122
/**
2223
* A TableModel backed by a row-first array.
2324
*
2425
* @author Eric Bottard
26+
* @author Piotr Olaszewski
2527
*/
2628
public class ArrayTableModel extends TableModel {
2729

@@ -43,7 +45,7 @@ public int getColumnCount() {
4345
return data.length > 0 ? data[0].length : 0;
4446
}
4547

46-
public Object getValue(int row, int column) {
48+
public @Nullable Object getValue(int row, int column) {
4749
return data[row][column];
4850
}
4951
}

spring-shell-table/src/main/java/org/springframework/shell/table/BeanListTableModel.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.LinkedHashMap;
2323
import java.util.List;
2424

25+
import org.jspecify.annotations.Nullable;
2526
import org.springframework.beans.BeanUtils;
2627
import org.springframework.beans.BeanWrapper;
2728
import org.springframework.beans.BeanWrapperImpl;
29+
import org.springframework.util.Assert;
2830

2931
/**
3032
* A table model that is backed by a list of beans.
@@ -33,14 +35,15 @@
3335
* a convenience constructor for adding a special header row.</p>
3436
*
3537
* @author Eric Bottard
38+
* @author Piotr Olaszewski
3639
*/
3740
public class BeanListTableModel<T> extends TableModel {
3841

3942
private final List<BeanWrapper> data;
4043

4144
private final List<String> propertyNames;
4245

43-
private final List<Object> headerRow;
46+
private final @Nullable List<Object> headerRow;
4447

4548
public BeanListTableModel(Class<T> clazz, Iterable<T> list) {
4649
this.data = new ArrayList<BeanWrapper>();
@@ -86,14 +89,16 @@ public int getColumnCount() {
8689
}
8790

8891
@Override
89-
public Object getValue(int row, int column) {
92+
public @Nullable Object getValue(int row, int column) {
9093
if (headerRow != null && row == 0) {
9194
return headerRow.get(column);
9295
}
9396
else {
9497
int rowToUse = headerRow == null ? row : row - 1;
9598
String propertyName = propertyNames.get(column);
96-
return data.get(rowToUse).getPropertyValue(propertyName);
99+
BeanWrapper beanWrapper = data.get(rowToUse);
100+
Assert.notNull(beanWrapper, "'beanWrapper' must not be null");
101+
return beanWrapper.getPropertyValue(propertyName);
97102
}
98103
}
99104
}

spring-shell-table/src/main/java/org/springframework/shell/table/BorderSpecification.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.shell.table;
1818

19+
import java.lang.reflect.Field;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -27,6 +28,7 @@
2728
* Multiple specifications can be combined on a single table.
2829
*
2930
* @author Eric Bottard
31+
* @author Piotr Olaszewski
3032
*/
3133
public class BorderSpecification {
3234

@@ -102,14 +104,22 @@ public String toString() {
102104
private String matchConstants() {
103105
try {
104106
for (String field : new String[] {"NONE", "INNER", "FULL", "OUTLINE"}) {
105-
int value = ReflectionUtils.findField(getClass(), field).getInt(null);
107+
Field reflectedField = ReflectionUtils.findField(getClass(), field);
108+
if (reflectedField == null) {
109+
continue;
110+
}
111+
int value = reflectedField.getInt(null);
106112
if (match == value) {
107113
return field;
108114
}
109115
}
110116
List<String> constants = new ArrayList<String>();
111117
for (String field : new String[] {"TOP", "BOTTOM", "LEFT", "RIGHT", "INNER_HORIZONTAL", "INNER_VERTICAL"}) {
112-
int value = ReflectionUtils.findField(getClass(), field).getInt(null);
118+
Field reflectedField = ReflectionUtils.findField(getClass(), field);
119+
if (reflectedField == null) {
120+
continue;
121+
}
122+
int value = reflectedField.getInt(null);
113123
if ((match & value) == value) {
114124
constants.add(field);
115125
}

spring-shell-table/src/main/java/org/springframework/shell/table/DefaultFormatter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package org.springframework.shell.table;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* A very simple formatter that uses {@link Object#toString()} and splits on newlines.
2123
*
2224
* @author Eric Bottard
25+
* @author Piotr Olaszewski
2326
*/
2427
public class DefaultFormatter implements Formatter {
2528

26-
public String[] format(Object value) {
29+
public String[] format(@Nullable Object value) {
2730
return value == null ? new String[] {""} : value.toString().split("\n");
2831
}
2932

spring-shell-table/src/main/java/org/springframework/shell/table/Formatter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.shell.table;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* A Formatter is responsible for the initial rendering of a value to lines of text.
2123
*
@@ -24,8 +26,9 @@
2426
* raw text representation (<i>e.g.</i> format numbers).</p>
2527
*
2628
* @author Eric Bottard
29+
* @author Piotr Olaszewski
2730
*/
2831
public interface Formatter {
2932

30-
public String[] format(Object value);
33+
public String[] format(@Nullable Object value);
3134
}

spring-shell-table/src/main/java/org/springframework/shell/table/MapFormatter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
package org.springframework.shell.table;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import java.util.Map;
2022

2123
/**
2224
* A formatter suited for key-value pairs, that renders each mapping on a new line.
2325
*
2426
* @author Eric Bottard
27+
* @author Piotr Olaszewski
2528
*/
2629
public class MapFormatter implements Formatter {
2730

@@ -32,7 +35,10 @@ public MapFormatter(String separator) {
3235
}
3336

3437
@Override
35-
public String[] format(Object value) {
38+
public String[] format(@Nullable Object value) {
39+
if (value == null) {
40+
return new String[]{""};
41+
}
3642
Map<?, ?> map = (Map<?, ?>) value;
3743
String[] result = new String[map.size()];
3844
int i = 0;

spring-shell-table/src/main/java/org/springframework/shell/table/TableModel.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.shell.table;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* Abstracts away the contract a {@link Table} will use to retrieve tabular data.
2123
*
2224
* @author Eric Bottard
25+
* @author Piotr Olaszewski
2326
*/
2427
public abstract class TableModel {
2528

@@ -40,7 +43,7 @@ public abstract class TableModel {
4043
* @param row the row that is being queried
4144
* @param column the column that is being queried
4245
*/
43-
public abstract Object getValue(int row, int column);
46+
public abstract @Nullable Object getValue(int row, int column);
4447

4548
/**
4649
* @return a transposed view of this model, where rows become columns and vice-versa.
@@ -58,7 +61,7 @@ public int getColumnCount() {
5861
}
5962

6063
@Override
61-
public Object getValue(int row, int column) {
64+
public @Nullable Object getValue(int row, int column) {
6265
return TableModel.this.getValue(column, row);
6366
}
6467
};

spring-shell-table/src/main/java/org/springframework/shell/table/TableModelBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.jspecify.annotations.Nullable;
2223
import org.springframework.util.Assert;
2324

2425
/**
2526
* Helper class to build a TableModel incrementally.
2627
*
2728
* @author Eric Bottard
29+
* @author Piotr Olaszewski
2830
*/
2931
public class TableModelBuilder<T> {
3032

@@ -74,7 +76,7 @@ public int getColumnCount() {
7476
}
7577

7678
@Override
77-
public Object getValue(int row, int column) {
79+
public @Nullable Object getValue(int row, int column) {
7880
return rows.get(row).get(column);
7981
}
8082
};

spring-shell-table/src/main/java/org/springframework/shell/table/package-info.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
*
2020
* @author Eric Bottard
2121
*/
22-
package org.springframework.shell.table;
22+
@NullMarked
23+
package org.springframework.shell.table;
24+
25+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)