Skip to content

Commit d941c18

Browse files
committed
[java] Fixing nullable issues
1 parent 4558a24 commit d941c18

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

java/src/org/openqa/selenium/grid/jmx/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//java:defs.bzl", "java_library")
1+
load("//java:defs.bzl", "artifact", "java_library")
22

33
java_library(
44
name = "jmx",
@@ -8,4 +8,7 @@ java_library(
88
"//java/src/org/openqa/selenium/grid/session:__pkg__",
99
"//java/test/org/openqa/selenium/grid:__subpackages__",
1010
],
11+
deps = [
12+
artifact("org.jspecify:jspecify"),
13+
],
1114
)

java/src/org/openqa/selenium/grid/jmx/MBean.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,27 @@ private void collectAttributeInfo(Object bean) {
118118
.forEach(ai -> attributeMap.put(ai.name, ai));
119119
}
120120

121+
@Nullable
121122
private AttributeInfo getAttributeInfo(Method m) {
122123
ManagedAttribute ma = m.getAnnotation(ManagedAttribute.class);
123124
if (ma == null) {
124125
return null;
125126
}
126127
try {
127128
String name = "".equals(ma.name()) ? m.getName() : ma.name();
128-
return new AttributeInfo(name, ma.description(), findGetter(m), findSetter(m));
129+
Method getter = findGetter(m);
130+
Method setter = findSetter(m);
131+
if (getter == null || setter == null) {
132+
return null;
133+
}
134+
return new AttributeInfo(name, ma.description(), getter, setter);
129135
} catch (Throwable t) {
130136
LOG.severe("Error during execution: " + t.getMessage());
131137
return null;
132138
}
133139
}
134140

141+
@Nullable
135142
private Method findGetter(Method annotatedMethod) {
136143
ManagedAttribute ma = annotatedMethod.getAnnotation(ManagedAttribute.class);
137144
try {
@@ -153,6 +160,7 @@ private Method findGetter(Method annotatedMethod) {
153160
}
154161
}
155162

163+
@Nullable
156164
private Method findSetter(Method annotatedMethod) {
157165
ManagedAttribute ma = annotatedMethod.getAnnotation(ManagedAttribute.class);
158166
if (!"".equals(ma.setter())) {
@@ -172,6 +180,7 @@ private Method findSetter(Method annotatedMethod) {
172180
return null;
173181
}
174182

183+
@Nullable
175184
private Method findMethod(Class<?> cls, String name) {
176185
return Stream.of(cls.getMethods())
177186
.filter(m -> m.getName().equals(name))
@@ -186,6 +195,7 @@ private void collectOperationInfo(Object bean) {
186195
.forEach(oi -> operationMap.put(oi.name, oi));
187196
}
188197

198+
@Nullable
189199
private OperationInfo getOperationInfo(Method m) {
190200
ManagedOperation mo = m.getAnnotation(ManagedOperation.class);
191201
if (mo == null) {
@@ -215,10 +225,15 @@ private ObjectName generateObjectName(Object bean) {
215225
}
216226
}
217227

228+
@Nullable
218229
@Override
219230
public Object getAttribute(String attribute) {
231+
AttributeInfo attributeInfo = attributeMap.get(attribute);
232+
if (attributeInfo == null) {
233+
return null;
234+
}
220235
try {
221-
Object res = attributeMap.get(attribute).getter.invoke(bean);
236+
Object res = attributeInfo.getter.invoke(bean);
222237
if (res instanceof Map<?, ?>) {
223238
return ((Map<?, ?>) res)
224239
.entrySet().stream()
@@ -236,8 +251,12 @@ public Object getAttribute(String attribute) {
236251

237252
@Override
238253
public void setAttribute(Attribute attribute) {
254+
AttributeInfo attributeInfo = attributeMap.get(attribute.getName());
255+
if (attributeInfo == null) {
256+
return;
257+
}
239258
try {
240-
attributeMap.get(attribute.getName()).setter.invoke(bean, attribute.getValue());
259+
attributeInfo.setter.invoke(bean, attribute.getValue());
241260
} catch (IllegalAccessException | InvocationTargetException e) {
242261
LOG.severe("Error during execution: " + e.getMessage());
243262
}
@@ -258,15 +277,21 @@ public AttributeList getAttributes(String[] attributes) {
258277
return resultList;
259278
}
260279

280+
@Nullable
261281
@Override
262282
public AttributeList setAttributes(AttributeList attributes) {
263283
return null;
264284
}
265285

286+
@Nullable
266287
@Override
267288
public Object invoke(String actionName, Object[] params, String[] signature) {
289+
OperationInfo operationInfo = operationMap.get(actionName);
290+
if (operationInfo == null) {
291+
return null;
292+
}
268293
try {
269-
return operationMap.get(actionName).method.invoke(bean, params);
294+
return operationInfo.method.invoke(bean, params);
270295
} catch (IllegalAccessException | InvocationTargetException e) {
271296
LOG.severe("Error during execution: " + e.getMessage());
272297
return null;

0 commit comments

Comments
 (0)