package archmapper.stylespecific.spring.rules;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.tptp.platform.analysis.codereview.java.CodeReviewResource;
import org.eclipse.tptp.platform.analysis.core.history.AnalysisHistory;
import archmapper.main.conformance.rules.AbstractArchitectureConformanceRule;
import archmapper.main.model.architecture.Component;
import static archmapper.stylespecific.spring.SpringStyleTypeConstants.*;
public class NoCreationOfModelClassesInViewRule extends AbstractArchitectureConformanceRule {
@Override
public void analyze(AnalysisHistory history) {
CodeReviewResource resource = getCurrentResource(history);
List<?> list = resource.getTypedNodeList(resource.getResourceCompUnit(),
ASTNode.TYPE_DECLARATION, true);
for (Object obj : list) {
TypeDeclaration typeDec = (TypeDeclaration) obj;
ITypeBinding binding = typeDec.resolveBinding();
String compName = mappingHelper.getComponentName(binding);
if (compName != null) {
Component comp = arch.getComponentByName(compName);
if (comp != null && VIEW_COMPONENT_TYPE.equals(comp.getStyleType())) {
List<?> constructions = resource.getTypedNodeList(typeDec, ASTNode.CLASS_INSTANCE_CREATION, true);
for (Object constrObj : constructions) {
ClassInstanceCreation constrInv = (ClassInstanceCreation) constrObj;
Type type = constrInv.getType();
ITypeBinding classBinding = type.resolveBinding();
String createdClassCompName = mappingHelper.getComponentName(classBinding);
if (createdClassCompName != null) {
Component createdClassComp = arch.getComponentByName(createdClassCompName);
if (createdClassComp != null && MODEL_COMPONENT_TYPE.equals(createdClassComp.getStyleType())) {
generateResultsForASTNode(history, constrInv, resource,
"The class "+ classBinding.getQualifiedName() + " from the component "+
createdClassCompName + " may not be instantiated in a View component.");
}
}
}
}
}
}
}
}