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.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.archmapping.ComponentMapping;
import archmapper.main.model.archmapping.ImplementationArtifactDefinition;
import archmapper.main.model.stylemapping.ClassType;
import archmapper.main.model.stylemapping.ComponentTypeMapping;
public class NoCreationOfSingletonsRule extends
AbstractArchitectureConformanceRule {
@Override
public void analyze(AnalysisHistory history) {
CodeReviewResource resource = getCurrentResource(history);
List<?> list = resource.getTypedNodeList(resource.getResourceCompUnit(),
ASTNode.CLASS_INSTANCE_CREATION, true);
for (Object obj : list) {
ClassInstanceCreation creation = (ClassInstanceCreation) obj;
Type type = creation.getType();
if (type != null) {
ITypeBinding binding = type.resolveBinding();
String compName = mappingHelper.getComponentName(binding);
if (compName != null) {
ComponentMapping compMapping = archMapping.getComponentMapping(compName);
if (compMapping != null) {
ImplementationArtifactDefinition classDef = mappingHelper.getImplementationArtifactDefinitionByClassname(compMapping, binding.getQualifiedName());
if (classDef != null) {
String classTypeName = classDef.getType();
if (classTypeName != null) {
ComponentTypeMapping typeMapping = mappingHelper.getComponentTypeMapping(compName);
if (typeMapping != null) {
ClassType classType = typeMapping.getClassType(classTypeName);
if (classType != null) {
if (classType.isSingleton()) {
generateResultsForASTNode(history, creation, resource, "The singleton "+
binding.getName() + " should only be created by the Spring configuration");
}
}
}
}
}
}
}
}
}
}
}