Skip to content

Commit

Permalink
SemanticVersion: no exceptions, remove JacksonVersionMismatchError
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova committed Sep 13, 2021
1 parent 22e4691 commit 3a25b69
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ public final class SemanticVersion implements Comparable<SemanticVersion> {
private final String versionString;

/**
* Returns implementation version of the package for given class.
* Returns implementation version of the package for given class. If version can't be retrieved or parsed, returns invalid version.
*
* @param className - class name to get package version of.
* @return parsed {@link SemanticVersion} or invalid one.
*/
public static SemanticVersion getPackageVersionForClass(String className) throws ClassNotFoundException {
Objects.requireNonNull(className, "'className' cannot be null.");
return SemanticVersion.getPackageVersion(Class.forName(className));
public static SemanticVersion getPackageVersionForClass(String className) {
try {
return getPackageVersion(Class.forName(className));
} catch (Throwable e) {
return SemanticVersion.createInvalid();
}
}

/**
* Parses semver 2.0.0 string.
* Parses semver 2.0.0 string. If version can't be retrieved or parsed, returns invalid version.
*
* @param version to parse.
* @return parsed {@link SemanticVersion} or invalid one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ final class JacksonVersion {
private final ClientLogger logger = new ClientLogger(JacksonVersion.class);

private JacksonVersion() {
annotationsVersion = getVersion("com.fasterxml.jackson.annotation.JsonProperty");
coreVersion = getVersion("com.fasterxml.jackson.core.JsonGenerator");
databindVersion = getVersion("com.fasterxml.jackson.databind.ObjectMapper");
xmlVersion = getVersion("com.fasterxml.jackson.dataformat.xml.XmlMapper");
jsr310Version = getVersion("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule");
annotationsVersion = SemanticVersion.getPackageVersionForClass("com.fasterxml.jackson.annotation.JsonProperty");
coreVersion = SemanticVersion.getPackageVersionForClass("com.fasterxml.jackson.core.JsonGenerator");
databindVersion = SemanticVersion.getPackageVersionForClass("com.fasterxml.jackson.databind.ObjectMapper");
xmlVersion = SemanticVersion.getPackageVersionForClass("com.fasterxml.jackson.dataformat.xml.XmlMapper");
jsr310Version = SemanticVersion.getPackageVersionForClass("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule");
checkVersion(annotationsVersion, ANNOTATIONS_PACKAGE_NAME);
checkVersion(coreVersion, CORE_PACKAGE_NAME);
checkVersion(databindVersion, DATABIND_PACKAGE_NAME);
Expand All @@ -62,18 +62,6 @@ public String getHelpInfo() {
return helpString;
}

/**
* Gets {@code SemanticVersion} for given class name
*/
private SemanticVersion getVersion(String className) {
try {
return SemanticVersion.getPackageVersionForClass(className);
} catch (Throwable e) {
logger.warning("Failed to retrieve package version for class {}", className, e);
return SemanticVersion.createInvalid();
}
}

/**
* Gets {@code JacksonVersion} instance singleton.
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static ObjectMapperShim createJsonMapper(ObjectMapperShim innerMapperShim
ObjectMapper mapper = ObjectMapperFactory.INSTANCE.createJsonMapper(innerMapperShim);
return new ObjectMapperShim(mapper);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -66,7 +66,7 @@ public static ObjectMapperShim createXmlMapper() {
ObjectMapper mapper = ObjectMapperFactory.INSTANCE.createXmlMapper();
return new ObjectMapperShim(mapper);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -80,7 +80,7 @@ public static ObjectMapperShim createSimpleMapper() {
ObjectMapper mapper = ObjectMapperFactory.INSTANCE.createSimpleMapper();
return new ObjectMapperShim(mapper);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -94,7 +94,7 @@ public static ObjectMapperShim createDefaultMapper() {
ObjectMapper mapper = ObjectMapperFactory.INSTANCE.createDefaultMapper();
return new ObjectMapperShim(mapper);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -108,7 +108,7 @@ public static ObjectMapperShim createPrettyPrintMapper() {
ObjectMapper mapper = ObjectMapperFactory.INSTANCE.createPrettyPrintMapper();
return new ObjectMapperShim(mapper);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -122,7 +122,7 @@ public static ObjectMapperShim createHeaderMapper() {
ObjectMapper mapper = ObjectMapperFactory.INSTANCE.createHeaderMapper();
return new ObjectMapperShim(mapper);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public String writeValueAsString(Object value) throws IOException {
try {
return mapper.writeValueAsString(value);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -166,7 +166,7 @@ public byte[] writeValueAsBytes(Object value) throws IOException {
try {
return mapper.writeValueAsBytes(value);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -181,7 +181,7 @@ public void writeValue(OutputStream out, Object value) throws IOException {
try {
mapper.writeValue(out, value);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -198,7 +198,7 @@ public <T> T readValue(String content, final Type valueType) throws IOException
final JavaType javaType = createJavaType(valueType);
return mapper.readValue(content, javaType);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -215,7 +215,7 @@ public <T> T readValue(byte[] src, final Type valueType) throws IOException {
final JavaType javaType = createJavaType(valueType);
return mapper.readValue(src, javaType);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -232,7 +232,7 @@ public <T> T readValue(InputStream src, final Type valueType) throws IOException
final JavaType javaType = createJavaType(valueType);
return mapper.readValue(src, javaType);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -246,7 +246,7 @@ public JsonNode readTree(String content) throws IOException {
try {
return mapper.readTree(content);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand All @@ -260,7 +260,7 @@ public JsonNode readTree(byte[] content) throws IOException {
try {
return mapper.readTree(content);
} catch (LinkageError ex) {
throw LOGGER.logThrowableAsError(new JacksonVersionMismatchError(JACKSON_VERSION.getHelpInfo(), ex));
throw LOGGER.logThrowableAsError(new LinkageError(JACKSON_VERSION.getHelpInfo(), ex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SemanticVersionTests {
Expand Down Expand Up @@ -106,7 +105,7 @@ public void reasonablyBrokenSuppoertedVersion() {
}

@Test
public void classVersion() throws ClassNotFoundException {
public void classVersion() {
SemanticVersion version = SemanticVersion
.getPackageVersionForClass("com.fasterxml.jackson.databind.ObjectMapper");
assertTrue(version.isValid());
Expand All @@ -118,6 +117,7 @@ public void classVersion() throws ClassNotFoundException {
@ParameterizedTest
@ValueSource(strings = {"nonsense", ""})
public void malformedClassVersion(String className) {
assertThrows(ClassNotFoundException.class, () -> SemanticVersion.getPackageVersionForClass(className));
SemanticVersion version = SemanticVersion.getPackageVersionForClass(className);
assertFalse(version.isValid());
}
}

0 comments on commit 3a25b69

Please sign in to comment.