8064846: Lazy-init thread safety problems in core reflection

Make several fields in core reflection volatile

Reviewed-by: jfranck, shade, plevart
This commit is contained in:
Martin Buchholz 2014-11-14 11:23:07 -08:00
parent e1f99ed979
commit 7c51cce1ad
3 changed files with 30 additions and 17 deletions

View File

@ -42,8 +42,11 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> {
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
/** The generic superclass info. Lazily initialized. */
private volatile Type superclass;
/** The generic superinterface info. Lazily initialized. */
private volatile Type[] superInterfaces;
// private, to enforce use of static factory
private ClassRepository(String rawSig, GenericsFactory f) {
@ -79,31 +82,34 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> {
* with which the repository was created.
*/
public Type getSuperclass(){
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(){
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();
// create array to store reified subtree(s)
Type[] sis = new Type[ts.length];
superInterfaces = new Type[ts.length];
// reify all subtrees
for (int i = 0; i < ts.length; i++) {
Reifier r = getReifier(); // obtain visitor
ts[i].accept(r);// reify subtree
// extract result from visitor and store it
sis[i] = r.getResult();
superInterfaces[i] = r.getResult();
}
superInterfaces = sis; // cache overall result
this.superInterfaces = superInterfaces;
}
return superInterfaces.clone(); // return cached result
}

View File

@ -42,7 +42,8 @@ import sun.reflect.generics.visitor.Reifier;
public abstract class GenericDeclRepository<S extends Signature>
extends AbstractRepository<S> {
private TypeVariable<?>[] typeParams; // caches the formal type parameters
/** The formal type parameters. Lazily initialized. */
private volatile TypeVariable<?>[] typeParams;
protected GenericDeclRepository(String rawSig, GenericsFactory f) {
super(rawSig, f);
@ -55,8 +56,7 @@ public abstract class GenericDeclRepository<S extends Signature>
* If the corresponding field is non-null, it is returned.
* If not, it is created lazily. This is done by selecting the appropriate
* part of the tree and transforming it into a reflective object
* using a visitor.
* a visitor, which is created by feeding it the factory
* using a visitor, which is created by feeding it the factory
* with which the repository was created.
*/
@ -64,20 +64,21 @@ public abstract class GenericDeclRepository<S extends Signature>
* Return the formal type parameters of this generic declaration.
* @return the formal type parameters of this generic declaration
*/
public TypeVariable<?>[] getTypeParameters(){
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();
// create array to store reified subtree(s)
TypeVariable<?>[] tps = new TypeVariable<?>[ftps.length];
typeParams = new TypeVariable<?>[ftps.length];
// reify all subtrees
for (int i = 0; i < ftps.length; i++) {
Reifier r = getReifier(); // obtain visitor
ftps[i].accept(r); // reify subtree
// extract result from visitor and store it
tps[i] = (TypeVariable<?>) r.getResult();
typeParams[i] = (TypeVariable<?>) r.getResult();
}
typeParams = tps; // cache overall result
this.typeParams = typeParams; // cache overall result
}
return typeParams.clone(); // return cached result
}

View File

@ -42,7 +42,9 @@ public abstract class AbstractScope<D extends GenericDeclaration>
implements Scope {
private final D recvr; // the declaration whose scope this instance represents
private Scope enclosingScope; // the enclosing scope of this scope
/** The enclosing scope of this scope. Lazily initialized. */
private volatile Scope enclosingScope;
/**
* Constructor. Takes a reflective object whose scope the newly
@ -71,7 +73,11 @@ public abstract class AbstractScope<D extends GenericDeclaration>
* @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;
}