Skip to content

Commit a75d201

Browse files
committed
Avoid using TypeConverter in ValidPortValidator
A handy new variant of Integer::parseInt helps here.
1 parent a0ba672 commit a75d201

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/validation/validators/ValidPortValidator.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package org.apache.logging.log4j.plugins.validation.validators;
1818

1919
import org.apache.logging.log4j.Logger;
20-
import org.apache.logging.log4j.plugins.Inject;
21-
import org.apache.logging.log4j.plugins.convert.TypeConverter;
22-
import org.apache.logging.log4j.plugins.convert.TypeConverterFactory;
2320
import org.apache.logging.log4j.plugins.validation.ConstraintValidator;
2421
import org.apache.logging.log4j.plugins.validation.constraints.ValidPort;
2522
import org.apache.logging.log4j.status.StatusLogger;
@@ -33,29 +30,29 @@ public class ValidPortValidator implements ConstraintValidator<ValidPort> {
3330

3431
private static final Logger LOGGER = StatusLogger.getLogger();
3532

36-
private final TypeConverter<Integer> converter;
3733
private ValidPort annotation;
3834

39-
@Inject
40-
public ValidPortValidator(final TypeConverterFactory factory) {
41-
converter = factory.getTypeConverter(Integer.class);
42-
}
43-
4435
@Override
4536
public void initialize(final ValidPort annotation) {
4637
this.annotation = annotation;
4738
}
4839

4940
@Override
5041
public boolean isValid(final String name, final Object value) {
51-
if (value instanceof CharSequence) {
52-
return isValid(name, converter.convert(value.toString(), -1));
53-
}
54-
if (!(value instanceof Integer)) {
42+
final int port;
43+
if (value instanceof Integer i) {
44+
port = i;
45+
} else if (value instanceof CharSequence cs) {
46+
try {
47+
port = Integer.parseInt(cs, 0, cs.length(), 10);
48+
} catch (final NumberFormatException ignored) {
49+
LOGGER.error(annotation.message());
50+
return false;
51+
}
52+
} else {
5553
LOGGER.error(annotation.message());
5654
return false;
5755
}
58-
final int port = (int) value;
5956
if (port < 0 || port > 65535) {
6057
LOGGER.error(annotation.message());
6158
return false;

0 commit comments

Comments
 (0)