# HG changeset patch # User martin # Date 1415886498 -3600 # Node ID e141fdeb9ff330b11d5e33b81370116e69b4063f # Parent dc4b0e9ce63f6e3f228b57ff5d6b015df559bcb9 8064391: More thread safety problems in core reflection Summary: Make fields final or volatile to ensure thread safety Reviewed-by: jfranck diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/factory/CoreReflectionFactory.java --- a/src/share/classes/sun/reflect/generics/factory/CoreReflectionFactory.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/factory/CoreReflectionFactory.java Thu Nov 13 14:48:18 2014 +0100 @@ -45,8 +45,8 @@ * core reflection (java.lang.reflect). */ public class CoreReflectionFactory implements GenericsFactory { - private GenericDeclaration decl; - private Scope scope; + private final GenericDeclaration decl; + private final Scope scope; private CoreReflectionFactory(GenericDeclaration d, Scope s) { decl = d; diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/reflectiveObjects/LazyReflectiveObjectGenerator.java --- a/src/share/classes/sun/reflect/generics/reflectiveObjects/LazyReflectiveObjectGenerator.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/reflectiveObjects/LazyReflectiveObjectGenerator.java Thu Nov 13 14:48:18 2014 +0100 @@ -40,7 +40,7 @@ * */ public abstract class LazyReflectiveObjectGenerator { - private GenericsFactory factory; // cached factory + private final GenericsFactory factory; // cached factory protected LazyReflectiveObjectGenerator(GenericsFactory f) { factory = f; diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/repository/AbstractRepository.java --- a/src/share/classes/sun/reflect/generics/repository/AbstractRepository.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/repository/AbstractRepository.java Thu Nov 13 14:48:18 2014 +0100 @@ -40,9 +40,9 @@ // A factory used to produce reflective objects. Provided when the //repository is created. Will vary across implementations. - private GenericsFactory factory; + private final GenericsFactory factory; - private T tree; // the AST for the generic type info + private final T tree; // the AST for the generic type info //accessors private GenericsFactory getFactory() { return factory;} diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/repository/ClassRepository.java --- a/src/share/classes/sun/reflect/generics/repository/ClassRepository.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/repository/ClassRepository.java Thu Nov 13 14:48:18 2014 +0100 @@ -42,8 +42,8 @@ public static final ClassRepository NONE = ClassRepository.make("Ljava/lang/Object;", null); - private Type superclass; // caches the generic superclass info - private Type[] superInterfaces; // caches the generic superinterface info + private volatile Type superclass; // caches the generic superclass info + private volatile Type[] superInterfaces; // caches the generic superinterface info // private, to enforce use of static factory private ClassRepository(String rawSig, GenericsFactory f) { @@ -80,17 +80,20 @@ */ public Type getSuperclass(){ + Type superclass = this.superclass; if (superclass == null) { // lazily initialize superclass Reifier r = getReifier(); // obtain visitor // Extract superclass subtree from AST and reify getTree().getSuperclass().accept(r); // extract result from visitor and cache it superclass = r.getResult(); + this.superclass = superclass; } return superclass; // return cached result } public Type[] getSuperInterfaces(){ + Type[] superInterfaces = this.superInterfaces; if (superInterfaces == null) { // lazily initialize super interfaces // first, extract super interface subtree(s) from AST TypeTree[] ts = getTree().getSuperInterfaces(); @@ -104,6 +107,7 @@ sis[i] = r.getResult(); } superInterfaces = sis; // cache overall result + this.superInterfaces = superInterfaces; } return superInterfaces.clone(); // return cached result } diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java --- a/src/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/repository/GenericDeclRepository.java Thu Nov 13 14:48:18 2014 +0100 @@ -42,7 +42,7 @@ public abstract class GenericDeclRepository extends AbstractRepository { - private TypeVariable[] typeParams; // caches the formal type parameters + private volatile TypeVariable[] typeParams; // caches the formal type parameters protected GenericDeclRepository(String rawSig, GenericsFactory f) { super(rawSig, f); @@ -65,6 +65,7 @@ * @return the formal type parameters of this generic declaration */ public TypeVariable[] getTypeParameters(){ + TypeVariable[] typeParams = this.typeParams; if (typeParams == null) { // lazily initialize type parameters // first, extract type parameter subtree(s) from AST FormalTypeParameter[] ftps = getTree().getFormalTypeParameters(); @@ -78,6 +79,7 @@ tps[i] = (TypeVariable) r.getResult(); } typeParams = tps; // cache overall result + this.typeParams = typeParams; } return typeParams.clone(); // return cached result } diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/scope/AbstractScope.java --- a/src/share/classes/sun/reflect/generics/scope/AbstractScope.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/scope/AbstractScope.java Thu Nov 13 14:48:18 2014 +0100 @@ -41,8 +41,8 @@ public abstract class AbstractScope implements Scope { - private D recvr; // the declaration whose scope this instance represents - private Scope enclosingScope; // the enclosing scope of this scope + private final D recvr; // the declaration whose scope this instance represents + private volatile Scope enclosingScope; // the enclosing scope of this scope /** * Constructor. Takes a reflective object whose scope the newly @@ -71,7 +71,11 @@ * @return the enclosing scope */ protected Scope getEnclosingScope(){ - if (enclosingScope == null) {enclosingScope = computeEnclosingScope();} + Scope enclosingScope = this.enclosingScope; + if (enclosingScope == null) { + enclosingScope = computeEnclosingScope(); + this.enclosingScope = enclosingScope; + } return enclosingScope; } diff -r dc4b0e9ce63f -r e141fdeb9ff3 src/share/classes/sun/reflect/generics/tree/ClassSignature.java --- a/src/share/classes/sun/reflect/generics/tree/ClassSignature.java Fri Nov 14 12:00:07 2014 +0300 +++ b/src/share/classes/sun/reflect/generics/tree/ClassSignature.java Thu Nov 13 14:48:18 2014 +0100 @@ -28,9 +28,9 @@ import sun.reflect.generics.visitor.Visitor; public class ClassSignature implements Signature { - private FormalTypeParameter[] formalTypeParams; - private ClassTypeSignature superclass; - private ClassTypeSignature[] superInterfaces; + private final FormalTypeParameter[] formalTypeParams; + private final ClassTypeSignature superclass; + private final ClassTypeSignature[] superInterfaces; private ClassSignature(FormalTypeParameter[] ftps, ClassTypeSignature sc,