提交 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; ...@@ -45,8 +45,8 @@ import sun.reflect.generics.tree.FieldTypeSignature;
* core reflection (java.lang.reflect). * core reflection (java.lang.reflect).
*/ */
public class CoreReflectionFactory implements GenericsFactory { public class CoreReflectionFactory implements GenericsFactory {
private GenericDeclaration decl; private final GenericDeclaration decl;
private Scope scope; private final Scope scope;
private CoreReflectionFactory(GenericDeclaration d, Scope s) { private CoreReflectionFactory(GenericDeclaration d, Scope s) {
decl = d; decl = d;
......
...@@ -40,7 +40,7 @@ import sun.reflect.generics.visitor.Reifier; ...@@ -40,7 +40,7 @@ import sun.reflect.generics.visitor.Reifier;
* *
*/ */
public abstract class LazyReflectiveObjectGenerator { public abstract class LazyReflectiveObjectGenerator {
private GenericsFactory factory; // cached factory private final GenericsFactory factory; // cached factory
protected LazyReflectiveObjectGenerator(GenericsFactory f) { protected LazyReflectiveObjectGenerator(GenericsFactory f) {
factory = f; factory = f;
......
...@@ -40,9 +40,9 @@ public abstract class AbstractRepository<T extends Tree> { ...@@ -40,9 +40,9 @@ public abstract class AbstractRepository<T extends Tree> {
// A factory used to produce reflective objects. Provided when the // A factory used to produce reflective objects. Provided when the
//repository is created. Will vary across implementations. //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 //accessors
private GenericsFactory getFactory() { return factory;} private GenericsFactory getFactory() { return factory;}
......
...@@ -42,8 +42,8 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> { ...@@ -42,8 +42,8 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> {
public static final ClassRepository NONE = ClassRepository.make("Ljava/lang/Object;", null); public static final ClassRepository NONE = ClassRepository.make("Ljava/lang/Object;", null);
private Type superclass; // caches the generic superclass info private volatile Type superclass; // caches the generic superclass info
private Type[] superInterfaces; // caches the generic superinterface info private volatile Type[] superInterfaces; // caches the generic superinterface info
// private, to enforce use of static factory // private, to enforce use of static factory
private ClassRepository(String rawSig, GenericsFactory f) { private ClassRepository(String rawSig, GenericsFactory f) {
...@@ -80,17 +80,20 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> { ...@@ -80,17 +80,20 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> {
*/ */
public Type getSuperclass(){ public Type getSuperclass(){
Type superclass = this.superclass;
if (superclass == null) { // lazily initialize superclass if (superclass == null) { // lazily initialize superclass
Reifier r = getReifier(); // obtain visitor Reifier r = getReifier(); // obtain visitor
// Extract superclass subtree from AST and reify // Extract superclass subtree from AST and reify
getTree().getSuperclass().accept(r); getTree().getSuperclass().accept(r);
// extract result from visitor and cache it // extract result from visitor and cache it
superclass = r.getResult(); superclass = r.getResult();
this.superclass = superclass;
} }
return superclass; // return cached result return superclass; // return cached result
} }
public Type[] getSuperInterfaces(){ public Type[] getSuperInterfaces(){
Type[] superInterfaces = this.superInterfaces;
if (superInterfaces == null) { // lazily initialize super interfaces if (superInterfaces == null) { // lazily initialize super interfaces
// first, extract super interface subtree(s) from AST // first, extract super interface subtree(s) from AST
TypeTree[] ts = getTree().getSuperInterfaces(); TypeTree[] ts = getTree().getSuperInterfaces();
...@@ -104,6 +107,7 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> { ...@@ -104,6 +107,7 @@ public class ClassRepository extends GenericDeclRepository<ClassSignature> {
sis[i] = r.getResult(); sis[i] = r.getResult();
} }
superInterfaces = sis; // cache overall result superInterfaces = sis; // cache overall result
this.superInterfaces = superInterfaces;
} }
return superInterfaces.clone(); // return cached result return superInterfaces.clone(); // return cached result
} }
......
...@@ -42,7 +42,7 @@ import sun.reflect.generics.visitor.Reifier; ...@@ -42,7 +42,7 @@ import sun.reflect.generics.visitor.Reifier;
public abstract class GenericDeclRepository<S extends Signature> public abstract class GenericDeclRepository<S extends Signature>
extends AbstractRepository<S> { 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) { protected GenericDeclRepository(String rawSig, GenericsFactory f) {
super(rawSig, f); super(rawSig, f);
...@@ -65,6 +65,7 @@ public abstract class GenericDeclRepository<S extends Signature> ...@@ -65,6 +65,7 @@ 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 if (typeParams == null) { // lazily initialize type parameters
// first, extract type parameter subtree(s) from AST // first, extract type parameter subtree(s) from AST
FormalTypeParameter[] ftps = getTree().getFormalTypeParameters(); FormalTypeParameter[] ftps = getTree().getFormalTypeParameters();
...@@ -78,6 +79,7 @@ public abstract class GenericDeclRepository<S extends Signature> ...@@ -78,6 +79,7 @@ public abstract class GenericDeclRepository<S extends Signature>
tps[i] = (TypeVariable<?>) r.getResult(); tps[i] = (TypeVariable<?>) r.getResult();
} }
typeParams = tps; // cache overall result typeParams = tps; // cache overall result
this.typeParams = typeParams;
} }
return typeParams.clone(); // return cached result return typeParams.clone(); // return cached result
} }
......
...@@ -41,8 +41,8 @@ import java.lang.reflect.TypeVariable; ...@@ -41,8 +41,8 @@ import java.lang.reflect.TypeVariable;
public abstract class AbstractScope<D extends GenericDeclaration> public abstract class AbstractScope<D extends GenericDeclaration>
implements Scope { implements Scope {
private D recvr; // the declaration whose scope this instance represents private final D recvr; // the declaration whose scope this instance represents
private Scope enclosingScope; // the enclosing scope of this scope private volatile Scope enclosingScope; // the enclosing scope of this scope
/** /**
* Constructor. Takes a reflective object whose scope the newly * Constructor. Takes a reflective object whose scope the newly
...@@ -71,7 +71,11 @@ public abstract class AbstractScope<D extends GenericDeclaration> ...@@ -71,7 +71,11 @@ public abstract class AbstractScope<D extends GenericDeclaration>
* @return the enclosing scope * @return the enclosing scope
*/ */
protected Scope getEnclosingScope(){ protected Scope getEnclosingScope(){
if (enclosingScope == null) {enclosingScope = computeEnclosingScope();} Scope enclosingScope = this.enclosingScope;
if (enclosingScope == null) {
enclosingScope = computeEnclosingScope();
this.enclosingScope = enclosingScope;
}
return enclosingScope; return enclosingScope;
} }
......
...@@ -28,9 +28,9 @@ package sun.reflect.generics.tree; ...@@ -28,9 +28,9 @@ package sun.reflect.generics.tree;
import sun.reflect.generics.visitor.Visitor; import sun.reflect.generics.visitor.Visitor;
public class ClassSignature implements Signature { public class ClassSignature implements Signature {
private FormalTypeParameter[] formalTypeParams; private final FormalTypeParameter[] formalTypeParams;
private ClassTypeSignature superclass; private final ClassTypeSignature superclass;
private ClassTypeSignature[] superInterfaces; private final ClassTypeSignature[] superInterfaces;
private ClassSignature(FormalTypeParameter[] ftps, private ClassSignature(FormalTypeParameter[] ftps,
ClassTypeSignature sc, ClassTypeSignature sc,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册