class CheckAccessControls extends java.lang.Object implements NodeTraversal.ScopedCallback, HotSwapCompilerPass
@private
and @deprecated
.
Because access control restrictions are attached to type information,
it's important that TypedScopeCreator, TypeInference, and InferJSDocInfo
all run before this pass. TypedScopeCreator creates and resolves types,
TypeInference propagates those types across the AST, and InferJSDocInfo
propagates JSDoc across the types.Constructor and Description |
---|
CheckAccessControls(AbstractCompiler compiler,
boolean enforceCodingConventions) |
Modifier and Type | Method and Description |
---|---|
private boolean |
canAccessDeprecatedTypes(NodeTraversal t)
Returns whether it's currently OK to access deprecated names and
properties.
|
private void |
checkConstantProperty(NodeTraversal t,
Node getprop)
Determines whether the given constant property got reassigned
|
private void |
checkConstructorDeprecation(NodeTraversal t,
Node n,
Node parent)
Checks the given NEW node to ensure that access restrictions are obeyed.
|
private void |
checkFinalClassOverrides(NodeTraversal t,
Node fn,
Node parent)
Checks if a constructor is trying to override a final class.
|
private void |
checkKeyVisibilityConvention(NodeTraversal t,
Node key,
Node parent)
Determines whether the given OBJECTLIT property visibility
violates the coding convention.
|
private void |
checkNameDeprecation(NodeTraversal t,
Node n,
Node parent)
Checks the given NAME node to ensure that access restrictions are obeyed.
|
private void |
checkNameVisibility(NodeTraversal t,
Node name,
Node parent)
Reports an error if the given name is not visible in the current context.
|
private void |
checkNonOverriddenPropertyVisibility(NodeTraversal t,
Node getprop,
Node parent,
JSDocInfo.Visibility visibility,
boolean isClassType,
JSType objectType,
StaticSourceFile referenceSource,
StaticSourceFile definingSource) |
private void |
checkOverriddenPropertyVisibility(NodeTraversal t,
Node getprop,
Node parent,
JSDocInfo.Visibility visibility,
JSDocInfo.Visibility fileOverviewVisibility,
ObjectType objectType,
boolean sameInput) |
private void |
checkOverriddenPropertyVisibilityMismatch(JSDocInfo.Visibility overriding,
JSDocInfo.Visibility overridden,
JSDocInfo.Visibility fileOverview,
NodeTraversal t,
Node getprop) |
private void |
checkPackagePropertyVisibility(NodeTraversal t,
Node getprop,
StaticSourceFile referenceSource,
StaticSourceFile definingSource) |
private JSDocInfo.Visibility |
checkPrivateNameConvention(JSDocInfo.Visibility v,
Node name)
Returns the effective visibility of the given name, reporting an error
if there is a contradiction in the various sources of visibility
(example: a variable with a trailing underscore that is declared
@public ). |
private void |
checkPrivatePropertyVisibility(NodeTraversal t,
Node getprop,
Node parent,
boolean isClassType,
JSType ownerType) |
private void |
checkPropertyDeprecation(NodeTraversal t,
Node n,
Node parent)
Checks the given GETPROP node to ensure that access restrictions are
obeyed.
|
private void |
checkPropertyVisibility(NodeTraversal t,
Node getprop,
Node parent)
Reports an error if the given property is not visible in the current
context.
|
private void |
checkProtectedPropertyVisibility(NodeTraversal t,
Node getprop,
JSType ownerType) |
private static JSType |
dereference(JSType type)
Dereference a type, autoboxing it and filtering out null.
|
void |
enterScope(NodeTraversal t)
Called immediately after entering a new scope.
|
void |
exitScope(NodeTraversal t)
Called immediately before exiting a scope.
|
private JSType |
getClassOfMethod(Node n,
Node parent)
Gets the type of the class that "owns" a method, or null if
we know that its un-owned.
|
private JSType |
getCurrentClass() |
private static StaticSourceFile |
getDefiningSource(Node getprop,
ObjectType referenceType,
java.lang.String propertyName) |
private static JSType |
getFinalParentClass(JSType type)
Returns the super class of the given type that has a constructor.
|
private static ObjectType |
getObjectType(ObjectType referenceType,
boolean isOverride,
java.lang.String propertyName) |
private static JSDocInfo.Visibility |
getOverridingPropertyVisibility(Node parent) |
private static java.lang.String |
getPropertyDeprecationInfo(ObjectType type,
java.lang.String prop)
Returns the deprecation reason for the property if it is marked
as being deprecated.
|
private static java.lang.String |
getTypeDeprecationInfo(JSType type)
Returns the deprecation reason for the type if it is marked
as being deprecated.
|
void |
hotSwapScript(Node scriptRoot,
Node originalRoot)
Process the JS with root node root.
|
private static boolean |
isDeprecatedFunction(Node n)
Returns whether this is a function node annotated as deprecated.
|
private boolean |
isPackageAccessAllowed(TypedVar var,
Node name) |
private static boolean |
isPrivateAccessAllowed(TypedVar var,
Node name,
Node parent) |
private boolean |
isPrivateByConvention(java.lang.String name) |
private boolean |
isPropertyDeclaredConstant(ObjectType objectType,
java.lang.String prop)
Returns if a property is declared constant.
|
private static boolean |
isValidPrivateConstructorAccess(Node parent)
Whether the given access of a private constructor is legal.
|
private static JSType |
normalizeClassType(JSType type)
Normalize the type of a constructor, its instance, and its prototype
all down to the same type (the instance type).
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private static boolean |
propertyIsDeclaredButNotPrivate(Node getprop,
Node parent) |
private boolean |
shouldEmitDeprecationWarning(NodeTraversal t,
Node n,
Node parent)
Determines whether a deprecation warning should be emitted.
|
boolean |
shouldTraverse(NodeTraversal t,
Node n,
Node parent)
Visits a node in pre order (before visiting its children) and decides
whether this node's children should be traversed.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
static final DiagnosticType DEPRECATED_NAME
static final DiagnosticType DEPRECATED_NAME_REASON
static final DiagnosticType DEPRECATED_PROP
static final DiagnosticType DEPRECATED_PROP_REASON
static final DiagnosticType DEPRECATED_CLASS
static final DiagnosticType DEPRECATED_CLASS_REASON
static final DiagnosticType BAD_PACKAGE_PROPERTY_ACCESS
static final DiagnosticType BAD_PRIVATE_GLOBAL_ACCESS
static final DiagnosticType BAD_PRIVATE_PROPERTY_ACCESS
static final DiagnosticType BAD_PROTECTED_PROPERTY_ACCESS
static final DiagnosticType BAD_PROPERTY_OVERRIDE_IN_FILE_WITH_FILEOVERVIEW_VISIBILITY
static final DiagnosticType PRIVATE_OVERRIDE
static final DiagnosticType EXTEND_FINAL_CLASS
static final DiagnosticType VISIBILITY_MISMATCH
static final DiagnosticType CONST_PROPERTY_REASSIGNED_VALUE
static final DiagnosticType CONST_PROPERTY_DELETED
static final DiagnosticType CONVENTION_MISMATCH
private final AbstractCompiler compiler
private final TypeIRegistry typeRegistry
private final boolean enforceCodingConventions
private int deprecatedDepth
private final ArrayDeque<JSType> currentClassStack
private final JSType noTypeSentinel
private com.google.common.collect.ImmutableMap<StaticSourceFile,JSDocInfo.Visibility> defaultVisibilityForFiles
private final com.google.common.collect.Multimap<JSType,java.lang.String> initializedConstantProperties
CheckAccessControls(AbstractCompiler compiler, boolean enforceCodingConventions)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void hotSwapScript(Node scriptRoot, Node originalRoot)
HotSwapCompilerPass
hotSwapScript
in interface HotSwapCompilerPass
scriptRoot
- Root node corresponding to the file that is modified,
should be of type Token.SCRIPT
.originalRoot
- Root node corresponding to the original version of the
file that is modified. Should be of type token.SCRIPT
.public void enterScope(NodeTraversal t)
NodeTraversal.ScopedCallback
enterScope
in interface NodeTraversal.ScopedCallback
public void exitScope(NodeTraversal t)
NodeTraversal.ScopedCallback
exitScope
in interface NodeTraversal.ScopedCallback
private JSType getClassOfMethod(Node n, Node parent)
private static JSType normalizeClassType(JSType type)
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in pre order (before visiting its children) and decides
whether this node's children should be traversed. If children are
traversed, they will be visited by
NodeTraversal.Callback.visit(NodeTraversal, Node, Node)
in postorder.
Implementations can have side effects (e.g. modifying the parse tree).
shouldTraverse
in interface NodeTraversal.Callback
public void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private void checkConstructorDeprecation(NodeTraversal t, Node n, Node parent)
private void checkNameDeprecation(NodeTraversal t, Node n, Node parent)
private void checkPropertyDeprecation(NodeTraversal t, Node n, Node parent)
private boolean isPrivateByConvention(java.lang.String name)
private void checkKeyVisibilityConvention(NodeTraversal t, Node key, Node parent)
t
- The current traversal.key
- The objectlit key node (STRING_KEY, GETTER_DEF, SETTER_DEF).private void checkNameVisibility(NodeTraversal t, Node name, Node parent)
t
- The current traversal.name
- The name node.private JSDocInfo.Visibility checkPrivateNameConvention(JSDocInfo.Visibility v, Node name)
@public
).private static boolean isPrivateAccessAllowed(TypedVar var, Node name, Node parent)
private void checkOverriddenPropertyVisibilityMismatch(JSDocInfo.Visibility overriding, JSDocInfo.Visibility overridden, @Nullable JSDocInfo.Visibility fileOverview, NodeTraversal t, Node getprop)
@Nullable private static JSDocInfo.Visibility getOverridingPropertyVisibility(Node parent)
private void checkFinalClassOverrides(NodeTraversal t, Node fn, Node parent)
private void checkConstantProperty(NodeTraversal t, Node getprop)
t
- The current traversal.getprop
- The getprop node.private void checkPropertyVisibility(NodeTraversal t, Node getprop, Node parent)
t
- The current traversal.getprop
- The getprop node.private static boolean propertyIsDeclaredButNotPrivate(Node getprop, Node parent)
@Nullable private static StaticSourceFile getDefiningSource(Node getprop, @Nullable ObjectType referenceType, java.lang.String propertyName)
@Nullable private static ObjectType getObjectType(@Nullable ObjectType referenceType, boolean isOverride, java.lang.String propertyName)
private void checkOverriddenPropertyVisibility(NodeTraversal t, Node getprop, Node parent, JSDocInfo.Visibility visibility, JSDocInfo.Visibility fileOverviewVisibility, ObjectType objectType, boolean sameInput)
private void checkNonOverriddenPropertyVisibility(NodeTraversal t, Node getprop, Node parent, JSDocInfo.Visibility visibility, boolean isClassType, JSType objectType, StaticSourceFile referenceSource, StaticSourceFile definingSource)
private void checkPackagePropertyVisibility(NodeTraversal t, Node getprop, StaticSourceFile referenceSource, StaticSourceFile definingSource)
@Nullable private JSType getCurrentClass()
private void checkPrivatePropertyVisibility(NodeTraversal t, Node getprop, Node parent, boolean isClassType, JSType ownerType)
private void checkProtectedPropertyVisibility(NodeTraversal t, Node getprop, JSType ownerType)
private static boolean isValidPrivateConstructorAccess(Node parent)
private boolean shouldEmitDeprecationWarning(NodeTraversal t, Node n, Node parent)
t
- The current traversal.n
- The node which we are checking.parent
- The parent of the node which we are checking.private boolean canAccessDeprecatedTypes(NodeTraversal t)
private static boolean isDeprecatedFunction(Node n)
private static java.lang.String getTypeDeprecationInfo(JSType type)
private boolean isPropertyDeclaredConstant(ObjectType objectType, java.lang.String prop)
private static java.lang.String getPropertyDeprecationInfo(ObjectType type, java.lang.String prop)
private static JSType dereference(JSType type)