diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index 4d7e8c06..e3912197 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -319,9 +319,9 @@ public synchronized Console getConsole() { */ // declared final since this is invoked from constructors public final void addObject(Object object) { - if (object instanceof Iterable) { + if (object instanceof Iterable i) { // Iterable - for (Object o : (Iterable) object) { + for (Object o : i) { objects.add(o); } } else if (object.getClass().isArray()) { @@ -835,11 +835,10 @@ else if (commands.isEmpty()) { } Type genericType = mainParameter.parameterized.getGenericType(); - if (genericType instanceof ParameterizedType) { - ParameterizedType p = (ParameterizedType) genericType; + if (genericType instanceof ParameterizedType p) { Type cls = p.getActualTypeArguments()[0]; - if (cls instanceof Class) { - convertedValue = convertValue(mainParameter.parameterized, (Class) cls, null, value); + if (cls instanceof Class c) { + convertedValue = convertValue(mainParameter.parameterized, c, null, value); } } @@ -947,10 +946,10 @@ private int processPassword(String[] args, int index, ParameterDescription pd, b private int processVariableArity(String[] args, int index, ParameterDescription pd, boolean validate) { Object arg = pd.getObject(); IVariableArity va; - if (!(arg instanceof IVariableArity)) { + if (!(arg instanceof IVariableArity iva)) { va = DEFAULT_VARIABLE_ARITY; } else { - va = (IVariableArity) arg; + va = iva; } int arity = determineArity(args, index, pd, va); @@ -1423,7 +1422,7 @@ public Object convertValue(final Parameterized parameterized, Class type, String @Override public Object convert(String value) { final Type genericType = parameterized.findFieldGenericType(); - return convertValue(parameterized, genericType instanceof Class ? (Class) genericType : String.class, null, value); + return convertValue(parameterized, genericType instanceof Class c ? c : String.class, null, value); } }); } diff --git a/src/main/java/com/beust/jcommander/ParameterDescription.java b/src/main/java/com/beust/jcommander/ParameterDescription.java index 652c1122..89ddbfdd 100644 --- a/src/main/java/com/beust/jcommander/ParameterDescription.java +++ b/src/main/java/com/beust/jcommander/ParameterDescription.java @@ -263,8 +263,8 @@ Object addValue(String name, String value, boolean isDefault, boolean validate, l = newCollection(type); parameterized.set(object, l); } - if (convertedValue instanceof Collection) { - l.addAll((Collection) convertedValue); + if (convertedValue instanceof Collection c) { + l.addAll(c); } else { l.add(convertedValue); } diff --git a/src/main/java/com/beust/jcommander/Parameterized.java b/src/main/java/com/beust/jcommander/Parameterized.java index ba92301b..1ae2ad2f 100644 --- a/src/main/java/com/beust/jcommander/Parameterized.java +++ b/src/main/java/com/beust/jcommander/Parameterized.java @@ -303,8 +303,8 @@ public void set(Object object, Object value) { throw new ParameterException(errorMessage(method, ex)); } catch (InvocationTargetException ex) { // If a ParameterException was thrown, don't wrap it into another one - if (ex.getTargetException() instanceof ParameterException) { - throw (ParameterException) ex.getTargetException(); + if (ex.getTargetException() instanceof ParameterException pe) { + throw pe; } else { throw new ParameterException(errorMessage(method, ex), ex.getTargetException()); } @@ -334,13 +334,11 @@ public Type findFieldGenericType() { if (method != null) { return null; } else { - if (field.getGenericType() instanceof ParameterizedType) { - ParameterizedType p = (ParameterizedType) field.getGenericType(); + if (field.getGenericType() instanceof ParameterizedType p) { Type cls = p.getActualTypeArguments()[0]; - if (cls instanceof Class) { - return cls; - } else if ( cls instanceof WildcardType) { - WildcardType wildcardType = (WildcardType)cls; + if (cls instanceof Class c) { + return c; + } else if (cls instanceof WildcardType wildcardType) { if (wildcardType.getLowerBounds().length > 0) { return wildcardType.getLowerBounds()[0]; }