Java Reference
In-Depth Information
}
returnresult;
}
For example, consider the following message expression:
java.lang.System.out.println(...);
The parser will have encapsulated the target
java.lang.System.out
in an
AmbiguousName
object. The first thing
analyze()
does for a
JMessageExpression
is to reclassify this
AmbiguousName
to determine the structure of the expression that it denotes. It does this by
looking at the ambiguous
java.lang.System.out
from left to right.
1. First,
reclassify(
) looks up the simple name
java
in the symbol table.
2. Not finding that, it looks up
java.lang
.
3. Not finding that, it looks up
java.lang.System
, which (assuming
java.lang.System
has been properly imported) it finds to be a class.
4. It then assumes that the rest of the ambiguous part, that is
out
, is a field.
5. Thus, the target is a field selection whose target is
java.lang.System
and whose field
name is
out
.
Analyzing a Field Selection
The code for analyzing a field is as follows:
publicJExpressionanalyze(Contextcontext){
//Reclassifytheambiguouspart.
target=(JExpression)target.analyze(context);
TypetargetType=target.type();
//Weuseaworkaroundforthe"length"fieldofarrays.
if((targetTypeinstanceofArrayTypeName)
&&fieldName.equals("length")){
type=Type.INT;
}else{
//Otherthanthat,targetTypehastobea
//ReferenceType
if(targetType.isPrimitive()){
JAST.compilationUnit.reportSemanticError(line(),
"Targetofafieldselectionmust"
+"beadefinedtype");
type=Type.ANY;
returnthis;
}
field=targetType.fieldFor(fieldName);
if(field==null){
JAST.compilationUnit.reportSemanticError(line(),
"Cannotfindafield:"+fieldName);
type=Type.ANY;
}else{
context.definingType().checkAccess(line,
(Member)field);
type=field.type();
//Non-staticfieldcannotbereferencedfroma
//staticcontext.
if(!field.isStatic()){
Search WWH ::
Custom Search