aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKolin Lu <kolinlu@google.com>2023-05-01 16:50:20 -0700
committerGitHub <noreply@github.com>2023-05-01 16:50:20 -0700
commit67e3941ddeccf87fed519e0f9cd8679c826888f2 (patch)
tree17c974033b4866151af25c714d1c888c50dd175c
parent52ea7ce5d0b68560fecf876c3eb73da2f33eb41f (diff)
downloadmobly-snippet-lib-67e3941ddeccf87fed519e0f9cd8679c826888f2.tar.gz
Fix Java style, lint, and best practices warnings. (#124)
1. Java Style - MixedArrayDimensions. C-style array declarations should not be used 2. Java Lint - UnusedException. New exception to be thrown should include the caught exception as its cause. 3. Java Api Best Practices - AvoidObjectArrays. Avoid accepting a Annotation[]; consider an Iterable<Annotation> instead
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java24
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/RpcError.java4
2 files changed, 18 insertions, 10 deletions
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java
index 55a73ce..467053d 100644
--- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java
+++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/MethodDescriptor.java
@@ -27,6 +27,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -74,7 +75,7 @@ public final class MethodDescriptor {
* @throws Throwable the exception raised from executing the RPC method.
*/
public Object invoke(SnippetManager manager, final JSONArray parameters) throws Throwable {
- final Annotation annotations[][] = getParameterAnnotations();
+ final Annotation[][] annotations = getParameterAnnotations();
final Type[] parameterTypes = getGenericParameterTypes();
final Object[] args = new Object[parameterTypes.length];
@@ -86,10 +87,12 @@ public final class MethodDescriptor {
final Type parameterType = parameterTypes[i];
if (i < parameters.length()) {
args[i] = convertParameter(parameters, i, parameterType);
- } else if (MethodDescriptor.hasDefaultValue(annotations[i])) {
- args[i] = MethodDescriptor.getDefaultValue(parameterType, annotations[i]);
- } else if (MethodDescriptor.isOptional(annotations[i])) {
- args[i] = MethodDescriptor.getOptionalValue(parameterType, annotations[i]);
+ } else if (MethodDescriptor.hasDefaultValue(Arrays.asList(annotations[i]))) {
+ args[i] = MethodDescriptor.getDefaultValue(
+ parameterType, Arrays.asList(annotations[i]));
+ } else if (MethodDescriptor.isOptional(Arrays.asList(annotations[i]))) {
+ args[i] = MethodDescriptor.getOptionalValue(
+ parameterType, Arrays.asList(annotations[i]));
} else {
throw new RpcError("Argument " + (i + 1) + " is not present");
}
@@ -176,7 +179,8 @@ public final class MethodDescriptor {
+ " should be of type "
+ ((Class<?>) type).getSimpleName()
+ ", but is of type "
- + parameters.get(index).getClass().getSimpleName());
+ + parameters.get(index).getClass().getSimpleName(),
+ e);
}
}
@@ -272,7 +276,7 @@ public final class MethodDescriptor {
* @param parameterType parameterType
* @param annotations annotations of the parameter
*/
- public static Object getDefaultValue(Type parameterType, Annotation[] annotations) {
+ public static Object getDefaultValue(Type parameterType, Iterable<Annotation> annotations) {
for (Annotation a : annotations) {
if (a instanceof RpcDefault) {
RpcDefault defaultAnnotation = (RpcDefault) a;
@@ -290,7 +294,7 @@ public final class MethodDescriptor {
* @param parameterType parameterType
* @param annotations annotations of the parameter
*/
- public static Object getOptionalValue(Type parameterType, Annotation[] annotations) {
+ public static Object getOptionalValue(Type parameterType, Iterable<Annotation> annotations) {
for (Annotation a : annotations) {
if (a instanceof RpcOptional) {
return null;
@@ -326,7 +330,7 @@ public final class MethodDescriptor {
*
* @param annotations annotations of the parameter
*/
- public static boolean hasDefaultValue(Annotation[] annotations) {
+ public static boolean hasDefaultValue(Iterable<Annotation> annotations) {
for (Annotation a : annotations) {
if (a instanceof RpcDefault) {
return true;
@@ -340,7 +344,7 @@ public final class MethodDescriptor {
*
* @param annotations annotations of the parameter
*/
- public static boolean isOptional(Annotation[] annotations) {
+ public static boolean isOptional(Iterable<Annotation> annotations) {
for (Annotation a : annotations) {
if (a instanceof RpcOptional) {
return true;
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/RpcError.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/RpcError.java
index 0862673..03038ee 100644
--- a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/RpcError.java
+++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/rpc/RpcError.java
@@ -22,4 +22,8 @@ public class RpcError extends Exception {
public RpcError(String message) {
super(message);
}
+
+ public RpcError(String message, Throwable cause) {
+ super(message, cause);
+ }
}