提交 c99e16ec 编写于 作者: M martin

8064391: More thread safety problems in core reflection

Summary: Make fields final or volatile to ensure thread safety
Reviewed-by: jfranck
上级 32bd080d
......@@ -45,8 +45,8 @@ import sun.reflect.generics.tree.FieldTypeSignature;
* 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;
......
......@@ -40,7 +40,7 @@ import sun.reflect.generics.visitor.Reifier;
*
*/
public abstract class LazyReflectiveObjectGenerator {
private GenericsFactory factory; // cached factory
private final GenericsFactory factory; // cached factory
protected LazyReflectiveObjectGenerator(GenericsFactory f) {
factory = f;
......
......@@ -40,9 +40,9 @@ public abstract class AbstractRepository<T extends Tree> {
// 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;}
......
......@@ -42,8 +42,8 @@ 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
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 class ClassRepository extends GenericDeclRepository<ClassSignature> {
*/
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 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> {
sis[i] = r.getResult();
}
superInterfaces = sis; // cache overall result
this.superInterfaces = superInterfaces;
}
return superInterfaces.clone(); // return cached result
}
......
......@@ -42,7 +42,7 @@ import sun.reflect.generics.visitor.Reifier;
public abstract class GenericDeclRepository<S extends Signature>
extends AbstractRepository<S> {
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 @@ public abstract class GenericDeclRepository<S extends Signature>
* @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 @@ public abstract class GenericDeclRepository<S extends Signature>
tps[i] = (TypeVariable<?>) r.getResult();
}
typeParams = tps; // cache overall result
this.typeParams = typeParams;
}
return typeParams.clone(); // return cached result
}
......
......@@ -41,8 +41,8 @@ import java.lang.reflect.TypeVariable;
public abstract class AbstractScope<D extends GenericDeclaration>
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 @@ 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;
}
......
......@@ -28,9 +28,9 @@ package sun.reflect.generics.tree;
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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册