NAMES (3)
DETERMINING THE MEANING OF A NAME: the meaning of a name depends on the context of usage. Name meaning determination requires 3 steps:
First step: context causes a name to syntactically fall into 1 of 6 categories of:
- PackageName
- TypeName
- ExpressionName
- MethodName
- PackageOrTypeName
- AmbigousName
Second step: if a name is initially categorized as PackageOrTypeName or AmbigousName, it is then reclassified to be either a
- PackageName
- TypeName
- ExpressionName
Third step: the resulting category then dictates the final determination of the meaning of the name (or a compilation error if the name has no meaning.)
Why use contexts for name determination? To minimize name conflicts between entities of different kinds.
Syntactic classification of a name according to context:
Syntactic classification of PackageName: either in a package declaration or to the left of the dot ‘.’ in a qualified PackageName.
Syntactic classification of TypeName: either of the following contexts,
- in a single-type-import declaration e.g import java.util.Vector
- to the left of the dot ‘.’ in a single static import declaration,
e.g import static Loader.name;
- to the left of the dot ‘.’ in a static import-on-demand declaration,
e.g import static Loader.*;
- to the left of the “<” in a parameterized type.
- In an actual type argument list of a parameterized type.
- In an explicit actual type argument list in a generic method or constructor invocation
- In an extends clause in a type variable declaration.
- In an extends clause of a wildcard type argument.
- In a super clause of a wildcard type argument.
- In an extends clause in a class declaration.
- In an implements clause in a class declaration.
- In an extends clause in an interface declaration.
- After the “@” sign in an annotation
- As a Type in any of the following:
1. in a field declaration
2. as the result type of a method
3. as the type of a formal parameter of a method or constructor
4. as the type of an exception that can be thrown by a method or constructor
5. as the type of a local variable
6. as the type of an exception parameter in a catch clause of a try statement.
7. as the type in a class literal
8. as the qualifying type of a qualified this expression
9. as the class type which is to be instantiated in an unqualified class instance creation expression
10. as the direct superclass or direct superinterface of an anonymous class which is to be instantiated in an unqualified class instance creation expression.
11. as the element type of an array to be created in an array creation expression.
12. as the qualifying type of field access using the keyword super.
13. as the qualifying type of a method invocation using the keyword super.
14. as the type mentioned in the cast operator of a cast expression.
15. as the type that follows the instanceof relational operator
Syntactic classification of ExpressionName:
- as the qualifying expression in a qualified superclass constructor invocation.
- As the qualifying expression in a qualified class instance creation expression.
- As the array reference expression in an array access expression.
- As a PostfixExpression.
- As the left hand operand of an assignment operator.
Syntactic classification of a MethodName,
- before the “(“ in a method invocation expression.
- To the left of the “=” sign in annotation’s element value pair.
Syntactic classificatioin of a PackageOrTypeName,
- to the left of the “.” in a TypeName
- in a type-import-on-demand declaration e.g import PackageOrTypeName.*;
Syntactic classification of an AmbiguousName,
- to the left of the “.” in a qualified ExpressionName
- to the left of the “.” in a qualified MethodName
- to the left of the “.” in a qualified AmbiguousName
- in the default value clause of an annotation type element declaration.
- To the right of a “=” in an element value pair.
Reclassification of contextually ambiguous names:
- if the AmbiguousName is a simple name, i.e a single identifier
- if the appearance of the simple name is within the scope of a local variable, parameter declaration or field declaration, then reclassification as an ExpressionName.
- Otherwise, If a single-static-import declaration or static-import-on-demand declaration declares a field of that name, then reclassified as ExpressionName.
- Otherwise, if the identifier appears within the scope of a top-level class or interface type declaration, a local class declaration or member type declaration with that name, then reclassification as TypeName.
- Otherwise, if a type of that name is declared in a single-type-import declaration, type-import-on-demand declaration, single-static-import declaration or static-import-on-demand declaration, then reclassification of identifier as TypeName.
- Otherwise, reclassification as PackageName which is to be determined against existence.
- if AmbiguousName is a qualified name, the name to the left of the dot “.” is first reclassified,
- if reclassified as a PackageName, check if a type declaration exists in the package that is similar to the identifier to the right of the “.”, if yes, then qualified name a TypeName, else a PackageName. Check for package existence.
- If reclassified as a TypeName, check if a member or field declaration exists in the class that is similar to the identifier to the right of the “.”, if yes, then qualified name an ExpressionName, else a TypeName. Check for compile time error.
- If reclassified as an ExpressionName, note the type T of the ExpressionName. If the identifier denotes a member or field in the type T, then classify as ExpressionName, else as TypeName if the identifier is a member type of T. check for compile time error.
No comments:
Post a Comment