From c912501a25e5e9c004c8ce6a27802f61df0d1cd7 Mon Sep 17 00:00:00 2001 From: atshaw43 <108552302+atshaw43@users.noreply.github.com> Date: Mon, 12 Dec 2022 11:04:34 -0800 Subject: [PATCH] Fixing NoClassDefFoundError (#366) * Fixing NoClassDefFoundError --- .../amazonaws/xray/AWSXRayObjectMapper.java | 72 +++++++++++++++++++ .../amazonaws/xray/entities/EntityImpl.java | 58 +-------------- .../xray/entities/SubsegmentImpl.java | 8 ++- 3 files changed, 80 insertions(+), 58 deletions(-) create mode 100644 aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java new file mode 100644 index 00000000..7cb5eeb8 --- /dev/null +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayObjectMapper.java @@ -0,0 +1,72 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.amazonaws.xray; + +import com.amazonaws.xray.entities.Cause; +import com.amazonaws.xray.serializers.CauseSerializer; +import com.amazonaws.xray.serializers.StackTraceElementSerializer; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; + +public class AWSXRayObjectMapper extends ObjectMapper { + + private static AWSXRayObjectMapper instance; + + public static AWSXRayObjectMapper getInstance() { + return instance; + } + + static { + instance = new AWSXRayObjectMapper(); + + instance.findAndRegisterModules(); + instance.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); + instance.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); + instance.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + + instance.registerModule(new SimpleModule() { + private static final long serialVersionUID = 545800949242254918L; + + @Override + public void setupModule(SetupContext setupContext) { + super.setupModule(setupContext); + setupContext.addBeanSerializerModifier(new BeanSerializerModifier() { + @SuppressWarnings("unchecked") + @Override + public JsonSerializer modifySerializer( + SerializationConfig serializationConfig, + BeanDescription beanDescription, + JsonSerializer jsonSerializer) { + Class beanClass = beanDescription.getBeanClass(); + if (Cause.class.isAssignableFrom(beanClass)) { + return new CauseSerializer((JsonSerializer) jsonSerializer); + } else if (StackTraceElement.class.isAssignableFrom(beanClass)) { + return new StackTraceElementSerializer(); + } + return jsonSerializer; + } + }); + } + }); + } +} diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java index 7591deec..d71a04b5 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java @@ -15,24 +15,15 @@ package com.amazonaws.xray.entities; +import com.amazonaws.xray.AWSXRayObjectMapper; import com.amazonaws.xray.AWSXRayRecorder; import com.amazonaws.xray.exceptions.AlreadyEmittedException; -import com.amazonaws.xray.serializers.CauseSerializer; -import com.amazonaws.xray.serializers.StackTraceElementSerializer; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.BeanDescription; -import com.fasterxml.jackson.databind.JsonSerializer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.SerializationConfig; -import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.node.NullNode; -import com.fasterxml.jackson.databind.ser.BeanSerializerModifier; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import java.util.ArrayList; import java.util.List; @@ -50,18 +41,6 @@ * */ public abstract class EntityImpl implements Entity { - - /** - * @deprecated For internal use only. - */ - @SuppressWarnings("checkstyle:ConstantName") - @Deprecated - protected static final ObjectMapper mapper = new ObjectMapper() - .findAndRegisterModules() - .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) - .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) - .setSerializationInclusion(JsonInclude.Include.NON_EMPTY); - private static final Log logger = LogFactory.getLog(EntityImpl.class); private static final String DEFAULT_METADATA_NAMESPACE = "default"; @@ -121,37 +100,6 @@ public abstract class EntityImpl implements Entity { @JsonIgnore private boolean emitted = false; - static { - /* - * Inject the CauseSerializer and StackTraceElementSerializer classes into the local mapper such that they will serialize - * their respective object types. - */ - mapper.registerModule(new SimpleModule() { - private static final long serialVersionUID = 545800949242254918L; - - @Override - public void setupModule(SetupContext setupContext) { - super.setupModule(setupContext); - setupContext.addBeanSerializerModifier(new BeanSerializerModifier() { - @SuppressWarnings("unchecked") - @Override - public JsonSerializer modifySerializer( - SerializationConfig serializationConfig, - BeanDescription beanDescription, - JsonSerializer jsonSerializer) { - Class beanClass = beanDescription.getBeanClass(); - if (Cause.class.isAssignableFrom(beanClass)) { - return new CauseSerializer((JsonSerializer) jsonSerializer); - } else if (StackTraceElement.class.isAssignableFrom(beanClass)) { - return new StackTraceElementSerializer(); - } - return jsonSerializer; - } - }); - } - }); - } - // default constructor for Jackson, so it can understand the default values to compare when using the Include.NON_DEFAULT // annotation. @SuppressWarnings("nullness") @@ -612,7 +560,7 @@ public boolean compareAndSetEmitted(boolean current, boolean next) { @Override public String serialize() { try { - return mapper.writeValueAsString(this); + return AWSXRayObjectMapper.getInstance().writeValueAsString(this); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing entity.", jpe); } @@ -622,7 +570,7 @@ public String serialize() { @Override public String prettySerialize() { try { - return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); + return AWSXRayObjectMapper.getInstance().writerWithDefaultPrettyPrinter().writeValueAsString(this); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing segment.", jpe); } diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java index f17fddc2..4477eea1 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java @@ -15,6 +15,7 @@ package com.amazonaws.xray.entities; +import com.amazonaws.xray.AWSXRayObjectMapper; import com.amazonaws.xray.AWSXRayRecorder; import com.amazonaws.xray.internal.SamplingStrategyOverride; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -145,7 +146,7 @@ public boolean shouldPropagate() { } private ObjectNode getStreamSerializeObjectNode() { - ObjectNode obj = (ObjectNode) mapper.valueToTree(this); + ObjectNode obj = (ObjectNode) AWSXRayObjectMapper.getInstance().valueToTree(this); obj.put("type", "subsegment"); obj.put("parent_id", getParent().getId()); obj.put("trace_id", parentSegment.getTraceId().toString()); @@ -156,7 +157,7 @@ private ObjectNode getStreamSerializeObjectNode() { public String streamSerialize() { String ret = ""; try { - ret = mapper.writeValueAsString(getStreamSerializeObjectNode()); + ret = AWSXRayObjectMapper.getInstance().writeValueAsString(getStreamSerializeObjectNode()); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing entity.", jpe); } @@ -167,7 +168,8 @@ public String streamSerialize() { public String prettyStreamSerialize() { String ret = ""; try { - ret = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(getStreamSerializeObjectNode()); + ret = AWSXRayObjectMapper.getInstance().writerWithDefaultPrettyPrinter() + .writeValueAsString(getStreamSerializeObjectNode()); } catch (JsonProcessingException jpe) { logger.error("Exception while serializing entity.", jpe); }