FormalParameter parameter = formalParameter;
if (parameter instanceof DefaultFormalParameter) {
parameter = ((DefaultFormalParameter) parameter).getParameter();
}
if (parameter instanceof FieldFormalParameter) {
FieldElement fieldElement = ((FieldFormalParameterElementImpl) parameter.getElement()).getField();
INIT_STATE state = fieldElementsMap.get(fieldElement);
if (state == INIT_STATE.NOT_INIT) {
fieldElementsMap.put(fieldElement, INIT_STATE.INIT_IN_FIELD_FORMAL);
} else if (state == INIT_STATE.INIT_IN_DECLARATION) {
if (fieldElement.isFinal() || fieldElement.isConst()) {
errorReporter.reportErrorForNode(
StaticWarningCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR,
formalParameter.getIdentifier(),
fieldElement.getDisplayName());
foundError = true;
}
} else if (state == INIT_STATE.INIT_IN_FIELD_FORMAL) {
if (fieldElement.isFinal() || fieldElement.isConst()) {
errorReporter.reportErrorForNode(
CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES,
formalParameter.getIdentifier(),
fieldElement.getDisplayName());
foundError = true;
}
}
}
}
// Visit all of the initializers
NodeList<ConstructorInitializer> initializers = node.getInitializers();
for (ConstructorInitializer constructorInitializer : initializers) {
if (constructorInitializer instanceof RedirectingConstructorInvocation) {
return false;
}
if (constructorInitializer instanceof ConstructorFieldInitializer) {
ConstructorFieldInitializer constructorFieldInitializer = (ConstructorFieldInitializer) constructorInitializer;
SimpleIdentifier fieldName = constructorFieldInitializer.getFieldName();
Element element = fieldName.getStaticElement();
if (element instanceof FieldElement) {
FieldElement fieldElement = (FieldElement) element;
INIT_STATE state = fieldElementsMap.get(fieldElement);
if (state == INIT_STATE.NOT_INIT) {
fieldElementsMap.put(fieldElement, INIT_STATE.INIT_IN_INITIALIZERS);
} else if (state == INIT_STATE.INIT_IN_DECLARATION) {
if (fieldElement.isFinal() || fieldElement.isConst()) {
errorReporter.reportErrorForNode(
StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION,
fieldName);
foundError = true;
}
} else if (state == INIT_STATE.INIT_IN_FIELD_FORMAL) {
errorReporter.reportErrorForNode(
CompileTimeErrorCode.FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER,
fieldName);
foundError = true;
} else if (state == INIT_STATE.INIT_IN_INITIALIZERS) {
errorReporter.reportErrorForNode(
CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS,
fieldName,
fieldElement.getDisplayName());
foundError = true;
}
}
}
}
// Visit all of the states in the map to ensure that none were never initialized.
for (Entry<FieldElement, INIT_STATE> entry : fieldElementsMap.entrySet()) {
if (entry.getValue() == INIT_STATE.NOT_INIT) {
FieldElement fieldElement = entry.getKey();
if (fieldElement.isConst()) {
errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_NOT_INITIALIZED,
node.getReturnType(),
fieldElement.getName());
foundError = true;
} else if (fieldElement.isFinal()) {
errorReporter.reportErrorForNode(
StaticWarningCode.FINAL_NOT_INITIALIZED,
node.getReturnType(),
fieldElement.getName());
foundError = true;
}
}
}
return foundError;