Skip to content

Commit

Permalink
Using Pattern Matching for instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Dec 23, 2024
1 parent 09a6b61 commit 95acea1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/beust/jcommander/ParameterDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/beust/jcommander/Parameterized.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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];
}
Expand Down

0 comments on commit 95acea1

Please sign in to comment.