Java Reference
In-Depth Information
Just how the sub-tree in Figure 4.11(b) is analyzed to produce (c) is discussed in the next
section.
4.5.4 Field Selection and Message Expressions
Reclassifying an Ambiguous Target
Both field selections and message expressions have targets. In a field selection, the target is
either an object or a class from which one wants to select a field. In a message expression,
the target is an object or class to which one is sending a message. Unfortunately, the parser
cannot always make out the syntactic structure of a target.
For example, consider the field selection
w.x.y.z
The parser knows this is a field selection of some sort and that z is the field. But, without
knowing the types of w , x , and y , the parser cannot know whether
w is a class name, x is a static field in w , and y is a field of x ;
w is a package containing class x , and y is a static field in x ; or
w.x.y is a fully qualified class name such as java.lang.System .
For this reason, the parser packages up the string "w.x.y" in an AmbiguousName object,
attached to either the JFieldSelection or JMessageExpression , so as to put o the
decision until analysis.
The first thing that analysis does in either sort of expression is to reclassify the ambigu-
ous target in the context in which it appears. The reclassify() method in AmbiguousName
is based on the rules in the Java Language Specification [Gosling et al., 2005] for reclassifying
an ambiguous name:
publicJExpressionreclassify(Contextcontext){
//Easierbecausewerequirealltypestobeimported.
JExpressionresult=null;
StringTokenizerst=newStringTokenizer(name,".");
//Firstly,findavariableorType.
StringnewName=st.nextToken();
IDefniDefn=null;
do{
iDefn=context.lookup(newName);
if(iDefn!=null){
result=newJVariable(line,newName);
break;
}elseif(!st.hasMoreTokens()){
//Nothingfound.:(
JAST.compilationUnit.reportSemanticError(line,
"Cannotfindname"+newName);
returnnull;
}else{
newName+="."+st.nextToken();
}
}while(true);
//Fornowwecanassumeeverythingelseisfields.
while(st.hasMoreTokens()){
result=newJFieldSelection(line,result,
st.nextToken());
 
Search WWH ::




Custom Search