Skip to content

Commit 9248388

Browse files
Replace clone() with copy constructor (#1496)
1 parent 5dd8324 commit 9248388

28 files changed

+337
-385
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Feat: Add secondary constructor to SentryOkHttpInterceptor (#1491)
44
* Feat: Add option to enable debug mode in Log4j2 integration (#1492)
5+
* Ref: Replace clone() with copy constructor (#1496)
56

67
# 5.0.0-beta.5
78

sentry/api/sentry.api

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ public final class io/sentry/Attachment {
1717
public fun getPathname ()Ljava/lang/String;
1818
}
1919

20-
public final class io/sentry/Breadcrumb : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
20+
public final class io/sentry/Breadcrumb : io/sentry/IUnknownPropertiesConsumer {
2121
public fun <init> ()V
2222
public fun <init> (Ljava/lang/String;)V
2323
public fun <init> (Ljava/util/Date;)V
2424
public fun acceptUnknownProperties (Ljava/util/Map;)V
25-
public fun clone ()Lio/sentry/Breadcrumb;
26-
public synthetic fun clone ()Ljava/lang/Object;
2725
public fun getCategory ()Ljava/lang/String;
2826
public fun getData ()Ljava/util/Map;
2927
public fun getData (Ljava/lang/String;)Ljava/lang/Object;
@@ -467,7 +465,7 @@ public final class io/sentry/SamplingContext {
467465
public fun getTransactionContext ()Lio/sentry/TransactionContext;
468466
}
469467

470-
public final class io/sentry/Scope : java/lang/Cloneable {
468+
public final class io/sentry/Scope {
471469
public fun <init> (Lio/sentry/SentryOptions;)V
472470
public fun addAttachment (Lio/sentry/Attachment;)V
473471
public fun addBreadcrumb (Lio/sentry/Breadcrumb;)V
@@ -476,8 +474,6 @@ public final class io/sentry/Scope : java/lang/Cloneable {
476474
public fun clear ()V
477475
public fun clearBreadcrumbs ()V
478476
public fun clearTransaction ()V
479-
public fun clone ()Lio/sentry/Scope;
480-
public synthetic fun clone ()Ljava/lang/Object;
481477
public fun getContexts ()Lio/sentry/protocol/Contexts;
482478
public fun getLevel ()Lio/sentry/SentryLevel;
483479
public fun getRequest ()Lio/sentry/protocol/Request;
@@ -1017,17 +1013,16 @@ public final class io/sentry/Span : io/sentry/ISpan {
10171013
public fun toSentryTrace ()Lio/sentry/SentryTraceHeader;
10181014
}
10191015

1020-
public class io/sentry/SpanContext : java/lang/Cloneable {
1016+
public class io/sentry/SpanContext {
10211017
public static final field TYPE Ljava/lang/String;
10221018
protected field description Ljava/lang/String;
10231019
protected field op Ljava/lang/String;
10241020
protected field status Lio/sentry/SpanStatus;
10251021
protected field tags Ljava/util/Map;
1022+
public fun <init> (Lio/sentry/SpanContext;)V
10261023
public fun <init> (Lio/sentry/protocol/SentryId;Lio/sentry/SpanId;Ljava/lang/String;Lio/sentry/SpanId;Ljava/lang/Boolean;)V
10271024
public fun <init> (Ljava/lang/String;)V
10281025
public fun <init> (Ljava/lang/String;Ljava/lang/Boolean;)V
1029-
public fun clone ()Lio/sentry/SpanContext;
1030-
public synthetic fun clone ()Ljava/lang/Object;
10311026
public fun getDescription ()Ljava/lang/String;
10321027
public fun getOperation ()Ljava/lang/String;
10331028
public fun getParentSpanId ()Lio/sentry/SpanId;
@@ -1311,12 +1306,10 @@ public abstract interface class io/sentry/hints/SubmissionResult {
13111306
public abstract fun setResult (Z)V
13121307
}
13131308

1314-
public final class io/sentry/protocol/App : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1309+
public final class io/sentry/protocol/App : io/sentry/IUnknownPropertiesConsumer {
13151310
public static final field TYPE Ljava/lang/String;
13161311
public fun <init> ()V
13171312
public fun acceptUnknownProperties (Ljava/util/Map;)V
1318-
public fun clone ()Lio/sentry/protocol/App;
1319-
public synthetic fun clone ()Ljava/lang/Object;
13201313
public fun getAppBuild ()Ljava/lang/String;
13211314
public fun getAppIdentifier ()Ljava/lang/String;
13221315
public fun getAppName ()Ljava/lang/String;
@@ -1333,22 +1326,19 @@ public final class io/sentry/protocol/App : io/sentry/IUnknownPropertiesConsumer
13331326
public fun setDeviceAppHash (Ljava/lang/String;)V
13341327
}
13351328

1336-
public final class io/sentry/protocol/Browser : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1329+
public final class io/sentry/protocol/Browser : io/sentry/IUnknownPropertiesConsumer {
13371330
public static final field TYPE Ljava/lang/String;
13381331
public fun <init> ()V
13391332
public fun acceptUnknownProperties (Ljava/util/Map;)V
1340-
public fun clone ()Lio/sentry/protocol/Browser;
1341-
public synthetic fun clone ()Ljava/lang/Object;
13421333
public fun getName ()Ljava/lang/String;
13431334
public fun getVersion ()Ljava/lang/String;
13441335
public fun setName (Ljava/lang/String;)V
13451336
public fun setVersion (Ljava/lang/String;)V
13461337
}
13471338

1348-
public final class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMap, java/lang/Cloneable {
1339+
public final class io/sentry/protocol/Contexts : java/util/concurrent/ConcurrentHashMap {
13491340
public fun <init> ()V
1350-
public fun clone ()Lio/sentry/protocol/Contexts;
1351-
public synthetic fun clone ()Ljava/lang/Object;
1341+
public fun <init> (Lio/sentry/protocol/Contexts;)V
13521342
public fun getApp ()Lio/sentry/protocol/App;
13531343
public fun getBrowser ()Lio/sentry/protocol/Browser;
13541344
public fun getDevice ()Lio/sentry/protocol/Device;
@@ -1398,12 +1388,10 @@ public final class io/sentry/protocol/DebugMeta : io/sentry/IUnknownPropertiesCo
13981388
public fun setSdkInfo (Lio/sentry/protocol/SdkInfo;)V
13991389
}
14001390

1401-
public final class io/sentry/protocol/Device : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1391+
public final class io/sentry/protocol/Device : io/sentry/IUnknownPropertiesConsumer {
14021392
public static final field TYPE Ljava/lang/String;
14031393
public fun <init> ()V
14041394
public fun acceptUnknownProperties (Ljava/util/Map;)V
1405-
public fun clone ()Lio/sentry/protocol/Device;
1406-
public synthetic fun clone ()Ljava/lang/Object;
14071395
public fun getArchs ()[Ljava/lang/String;
14081396
public fun getBatteryLevel ()Ljava/lang/Float;
14091397
public fun getBatteryTemperature ()Ljava/lang/Float;
@@ -1473,12 +1461,10 @@ public final class io/sentry/protocol/Device$DeviceOrientation : java/lang/Enum
14731461
public static fun values ()[Lio/sentry/protocol/Device$DeviceOrientation;
14741462
}
14751463

1476-
public final class io/sentry/protocol/Gpu : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1464+
public final class io/sentry/protocol/Gpu : io/sentry/IUnknownPropertiesConsumer {
14771465
public static final field TYPE Ljava/lang/String;
14781466
public fun <init> ()V
14791467
public fun acceptUnknownProperties (Ljava/util/Map;)V
1480-
public fun clone ()Lio/sentry/protocol/Gpu;
1481-
public synthetic fun clone ()Ljava/lang/Object;
14821468
public fun getApiType ()Ljava/lang/String;
14831469
public fun getId ()Ljava/lang/Integer;
14841470
public fun getMemorySize ()Ljava/lang/Integer;
@@ -1530,12 +1516,10 @@ public final class io/sentry/protocol/Message : io/sentry/IUnknownPropertiesCons
15301516
public fun setParams (Ljava/util/List;)V
15311517
}
15321518

1533-
public final class io/sentry/protocol/OperatingSystem : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1519+
public final class io/sentry/protocol/OperatingSystem : io/sentry/IUnknownPropertiesConsumer {
15341520
public static final field TYPE Ljava/lang/String;
15351521
public fun <init> ()V
15361522
public fun acceptUnknownProperties (Ljava/util/Map;)V
1537-
public fun clone ()Lio/sentry/protocol/OperatingSystem;
1538-
public synthetic fun clone ()Ljava/lang/Object;
15391523
public fun getBuild ()Ljava/lang/String;
15401524
public fun getKernelVersion ()Ljava/lang/String;
15411525
public fun getName ()Ljava/lang/String;
@@ -1550,11 +1534,10 @@ public final class io/sentry/protocol/OperatingSystem : io/sentry/IUnknownProper
15501534
public fun setVersion (Ljava/lang/String;)V
15511535
}
15521536

1553-
public final class io/sentry/protocol/Request : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1537+
public final class io/sentry/protocol/Request : io/sentry/IUnknownPropertiesConsumer {
15541538
public fun <init> ()V
1539+
public fun <init> (Lio/sentry/protocol/Request;)V
15551540
public fun acceptUnknownProperties (Ljava/util/Map;)V
1556-
public fun clone ()Lio/sentry/protocol/Request;
1557-
public synthetic fun clone ()Ljava/lang/Object;
15581541
public fun getCookies ()Ljava/lang/String;
15591542
public fun getData ()Ljava/lang/Object;
15601543
public fun getEnvs ()Ljava/util/Map;
@@ -1638,12 +1621,10 @@ public final class io/sentry/protocol/SentryPackage : io/sentry/IUnknownProperti
16381621
public fun setVersion (Ljava/lang/String;)V
16391622
}
16401623

1641-
public final class io/sentry/protocol/SentryRuntime : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1624+
public final class io/sentry/protocol/SentryRuntime : io/sentry/IUnknownPropertiesConsumer {
16421625
public static final field TYPE Ljava/lang/String;
16431626
public fun <init> ()V
16441627
public fun acceptUnknownProperties (Ljava/util/Map;)V
1645-
public fun clone ()Lio/sentry/protocol/SentryRuntime;
1646-
public synthetic fun clone ()Ljava/lang/Object;
16471628
public fun getName ()Ljava/lang/String;
16481629
public fun getRawDescription ()Ljava/lang/String;
16491630
public fun getVersion ()Ljava/lang/String;
@@ -1754,11 +1735,10 @@ public final class io/sentry/protocol/SentryTransaction : io/sentry/SentryBaseEv
17541735
public fun isSampled ()Z
17551736
}
17561737

1757-
public final class io/sentry/protocol/User : io/sentry/IUnknownPropertiesConsumer, java/lang/Cloneable {
1738+
public final class io/sentry/protocol/User : io/sentry/IUnknownPropertiesConsumer {
17581739
public fun <init> ()V
1740+
public fun <init> (Lio/sentry/protocol/User;)V
17591741
public fun acceptUnknownProperties (Ljava/util/Map;)V
1760-
public fun clone ()Lio/sentry/protocol/User;
1761-
public synthetic fun clone ()Ljava/lang/Object;
17621742
public fun getEmail ()Ljava/lang/String;
17631743
public fun getId ()Ljava/lang/String;
17641744
public fun getIpAddress ()Ljava/lang/String;

sentry/src/main/java/io/sentry/Breadcrumb.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.jetbrains.annotations.TestOnly;
1212

1313
/** Series of application events */
14-
public final class Breadcrumb implements Cloneable, IUnknownPropertiesConsumer {
14+
public final class Breadcrumb implements IUnknownPropertiesConsumer {
1515

1616
/** A timestamp representing when the breadcrumb occurred. */
1717
private final @NotNull Date timestamp;
@@ -43,6 +43,19 @@ public Breadcrumb(final @NotNull Date timestamp) {
4343
this.timestamp = timestamp;
4444
}
4545

46+
Breadcrumb(final @NotNull Breadcrumb breadcrumb) {
47+
this.timestamp = breadcrumb.timestamp;
48+
this.message = breadcrumb.message;
49+
this.type = breadcrumb.type;
50+
this.category = breadcrumb.category;
51+
final Map<String, Object> dataClone = CollectionUtils.newConcurrentHashMap(breadcrumb.data);
52+
if (dataClone != null) {
53+
this.data = dataClone;
54+
}
55+
this.unknown = CollectionUtils.newConcurrentHashMap(breadcrumb.unknown);
56+
this.level = breadcrumb.level;
57+
}
58+
4659
/**
4760
* Creates HTTP breadcrumb.
4861
*
@@ -236,27 +249,4 @@ public void acceptUnknownProperties(@NotNull Map<String, Object> unknown) {
236249
Map<String, Object> getUnknown() {
237250
return unknown;
238251
}
239-
240-
/**
241-
* Clones the breadcrumb aka deep copy
242-
*
243-
* @return the cloned breadcrumb
244-
* @throws CloneNotSupportedException if a breadcrumb is not cloneable
245-
*/
246-
@Override
247-
public @NotNull Breadcrumb clone() throws CloneNotSupportedException {
248-
final Breadcrumb clone = (Breadcrumb) super.clone();
249-
250-
final Map<String, Object> dataCopy = CollectionUtils.newConcurrentHashMap(this.data);
251-
if (dataCopy != null) {
252-
clone.data = dataCopy;
253-
}
254-
clone.unknown = CollectionUtils.newConcurrentHashMap(unknown);
255-
256-
final SentryLevel levelRef = level;
257-
clone.level =
258-
levelRef != null ? SentryLevel.valueOf(levelRef.name().toUpperCase(Locale.ROOT)) : null;
259-
260-
return clone;
261-
}
262252
}

sentry/src/main/java/io/sentry/Hub.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,9 @@ public void pushScope() {
429429
.log(SentryLevel.WARNING, "Instance is disabled and this 'pushScope' call is a no-op.");
430430
} else {
431431
final StackItem item = stack.peek();
432-
Scope clone = null;
433-
try {
434-
clone = item.getScope().clone();
435-
} catch (CloneNotSupportedException e) {
436-
options.getLogger().log(SentryLevel.ERROR, "An error has occurred when cloning a Scope", e);
437-
}
438-
if (clone != null) {
439-
final StackItem newItem = new StackItem(options, item.getClient(), clone);
440-
stack.push(newItem);
441-
}
432+
final StackItem newItem =
433+
new StackItem(options, item.getClient(), new Scope(item.getScope()));
434+
stack.push(newItem);
442435
}
443436
}
444437

0 commit comments

Comments
 (0)