boolean isFinal = matches(keyword, Keyword.FINAL);
boolean hasInitializer = node.getInitializer() != null;
VariableElementImpl element;
if (inFieldContext) {
SimpleIdentifier fieldName = node.getName();
FieldElementImpl field;
if (isConst && hasInitializer) {
field = new ConstFieldElementImpl(fieldName);
} else {
field = new FieldElementImpl(fieldName);
}
element = field;
currentHolder.addField(field);
fieldName.setStaticElement(field);
} else if (inFunction) {
SimpleIdentifier variableName = node.getName();
LocalVariableElementImpl variable;
if (isConst && hasInitializer) {
variable = new ConstLocalVariableElementImpl(variableName);
} else {
variable = new LocalVariableElementImpl(variableName);
}
element = variable;
Block enclosingBlock = node.getAncestor(Block.class);
int functionEnd = node.getOffset() + node.getLength();
int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength();
// TODO(brianwilkerson) This isn't right for variables declared in a for loop.
variable.setVisibleRange(functionEnd, blockEnd - functionEnd - 1);
currentHolder.addLocalVariable(variable);
variableName.setStaticElement(element);
} else {
SimpleIdentifier variableName = node.getName();
TopLevelVariableElementImpl variable;
if (isConst && hasInitializer) {
variable = new ConstTopLevelVariableElementImpl(variableName);
} else {
variable = new TopLevelVariableElementImpl(variableName);
}
element = variable;
currentHolder.addTopLevelVariable(variable);
variableName.setStaticElement(element);
}
element.setConst(isConst);
element.setFinal(isFinal);
if (hasInitializer) {