8302822: Method/Field/Constructor/RecordComponent::getGenericInfo() is not thread safe

Reviewed-by: stsypanov, redestad
This commit is contained in:
Chen Liang 2023-06-01 15:31:51 +00:00 committed by Claes Redestad
parent c6f20db945
commit be36096a19
4 changed files with 16 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -73,7 +73,7 @@ public final class Constructor<T> extends Executable {
// Generics and annotations support
private final transient String signature;
// generic info repository; lazily initialized
private transient ConstructorRepository genericInfo;
private transient volatile ConstructorRepository genericInfo;
private final byte[] annotations;
private final byte[] parameterAnnotations;
@ -87,12 +87,14 @@ public final class Constructor<T> extends Executable {
// Accessor for generic info repository
@Override
ConstructorRepository getGenericInfo() {
var genericInfo = this.genericInfo;
// lazily initialize repository if necessary
if (genericInfo == null) {
// create and cache generic info repository
genericInfo =
ConstructorRepository.make(getSignature(),
getFactory());
this.genericInfo = genericInfo;
}
return genericInfo; //return cached repository
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -77,7 +77,7 @@ class Field extends AccessibleObject implements Member {
// Generics and annotations support
private final transient String signature;
// generic info repository; lazily initialized
private transient FieldRepository genericInfo;
private transient volatile FieldRepository genericInfo;
private final byte[] annotations;
// Cached field accessor created without override
@Stable
@ -106,11 +106,13 @@ class Field extends AccessibleObject implements Member {
// Accessor for generic info repository
private FieldRepository getGenericInfo() {
var genericInfo = this.genericInfo;
// lazily initialize repository if necessary
if (genericInfo == null) {
// create and cache generic info repository
genericInfo = FieldRepository.make(getGenericSignature(),
getFactory());
this.genericInfo = genericInfo;
}
return genericInfo; //return cached repository
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -82,7 +82,7 @@ public final class Method extends Executable {
// Generics and annotations support
private final transient String signature;
// generic info repository; lazily initialized
private transient MethodRepository genericInfo;
private transient volatile MethodRepository genericInfo;
private final byte[] annotations;
private final byte[] parameterAnnotations;
private final byte[] annotationDefault;
@ -108,11 +108,13 @@ public final class Method extends Executable {
// Accessor for generic info repository
@Override
MethodRepository getGenericInfo() {
var genericInfo = this.genericInfo;
// lazily initialize repository if necessary
if (genericInfo == null) {
// create and cache generic info repository
genericInfo = MethodRepository.make(getGenericSignature(),
getFactory());
this.genericInfo = genericInfo;
}
return genericInfo; //return cached repository
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -54,7 +54,7 @@ public final class RecordComponent implements AnnotatedElement {
private Method accessor;
private String signature;
// generic info repository; lazily initialized
private transient FieldRepository genericInfo;
private transient volatile FieldRepository genericInfo;
private byte[] annotations;
private byte[] typeAnnotations;
private RecordComponent root;
@ -127,10 +127,12 @@ public final class RecordComponent implements AnnotatedElement {
// Accessor for generic info repository
private FieldRepository getGenericInfo() {
var genericInfo = this.genericInfo;
// lazily initialize repository if necessary
if (genericInfo == null) {
// create and cache generic info repository
genericInfo = FieldRepository.make(getGenericSignature(), getFactory());
this.genericInfo = genericInfo;
}
return genericInfo; //return cached repository
}