Commit 1f70ad14 authored by Kristof Szabados's avatar Kristof Szabados Committed by GitHub
Browse files

Merge pull request #253 from izaingrid/master

UnusedImportsProject class implementation
parents ae6eaeee e274f24b
......@@ -112,6 +112,7 @@ public final class StaticData {
final Map<CodeSmellType, BaseProjectCodeSmellSpotter[]> pm = new HashMap<CodeSmellType, BaseProjectCodeSmellSpotter[]>();
pm.put(CodeSmellType.CIRCULAR_IMPORTATION, new BaseProjectCodeSmellSpotter[] { new CircularImportation() });
pm.put(CodeSmellType.UNUSED_IMPORTS_PROJECT, new BaseProjectCodeSmellSpotter[] { new UnusedImportsProject() });
pm.put(CodeSmellType.LANGUAGE_MIXING, new BaseProjectCodeSmellSpotter[]{ new LanguageMixing() });
return Collections.unmodifiableMap(pm);
......
/******************************************************************************
* Copyright (c) 2000-2017 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/
package org.eclipse.titanium.markers.spotters.implementation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.core.resources.IProject;
import org.eclipse.titan.designer.AST.ASTVisitor;
import org.eclipse.titan.designer.AST.Assignment;
import org.eclipse.titan.designer.AST.IVisitableNode;
import org.eclipse.titan.designer.AST.Module;
import org.eclipse.titan.designer.AST.Reference;
import org.eclipse.titan.designer.AST.Scope;
import org.eclipse.titan.designer.AST.TTCN3.definitions.ImportModule;
import org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module;
import org.eclipse.titan.designer.consoles.TITANDebugConsole;
import org.eclipse.titan.designer.parsers.CompilationTimeStamp;
import org.eclipse.titan.designer.parsers.GlobalParser;
import org.eclipse.titan.designer.parsers.ProjectSourceParser;
import org.eclipse.titanium.markers.spotters.BaseProjectCodeSmellSpotter;
import org.eclipse.titanium.markers.types.CodeSmellType;
/**
*
* @author Farkas Izabella Ingrid
*/
public class UnusedImportsProject extends BaseProjectCodeSmellSpotter{
private Set<TTCN3Module> setOfModules = new HashSet<TTCN3Module>();
private Set<TTCN3Module> setOfImportModules = new HashSet<TTCN3Module>();
public UnusedImportsProject() {
super(CodeSmellType.UNUSED_IMPORTS_PROJECT);
}
@Override
protected void process(IProject project, Problems problems) {
TITANDebugConsole.print("Unused import in project called");
final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(project);
final Set<String> knownModuleNames = projectSourceParser.getKnownModuleNames();
final List<TTCN3Module> modules = new ArrayList<TTCN3Module>();
for (final String moduleName : new TreeSet<String>(knownModuleNames)) {
Module module = projectSourceParser.getModuleByName(moduleName);
if (module instanceof TTCN3Module) {
modules.add((TTCN3Module) module);
}
}
for (TTCN3Module module : modules) {
setOfModules.clear();
setOfImportModules.clear();
for (Module impMod : module.getImportedModules()) {
if (impMod instanceof TTCN3Module) {
setOfImportModules.add((TTCN3Module)impMod);
}
}
ImportsCheck check = new ImportsCheck();
module.accept(check);
setOfImportModules.removeAll(setOfModules);
for (ImportModule mod : module.getImports()){
for (TTCN3Module m : setOfImportModules) {
if(m.getIdentifier().equals(mod.getIdentifier())) {
problems.report(mod.getLocation(), "Possibly unused importation");
}
}
}
}
}
class ImportsCheck extends ASTVisitor {
public int visit(final IVisitableNode node){
if (node instanceof Reference) {
if(((Reference) node).getIsErroneous(CompilationTimeStamp.getBaseTimestamp())) {
return V_CONTINUE;
}
final Assignment assignment = ((Reference) node).getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false, null);
if(assignment != null ) {
final Scope scope = assignment.getMyScope();
if (scope != null) {
final Module module = scope.getModuleScope();
if (module instanceof TTCN3Module){
TTCN3Module mod = (TTCN3Module)module;
setOfModules.add(mod);
}
}
return V_CONTINUE;
}
}
return V_CONTINUE;
}
}
}
......@@ -19,6 +19,7 @@ import org.eclipse.titanium.Activator;
public enum CodeSmellType implements ProblemType{
ALTSTEP_COVERAGE("Altstep coverage", 1.0, 5.0, 76.0),
CIRCULAR_IMPORTATION("Circular importation", 2.0, 12.0, 80.0),
UNUSED_IMPORTS_PROJECT("Unused import in project", 0.0, 0.5, 1.0),
LANGUAGE_MIXING("Language mixing", 2.0, 12.0, 80.0),
CONSECUTIVE_ASSIGNMENTS("Consecutive assignments", 0.0, 1.0, 6.0),
CONVERT_TO_ENUM("Convert to enumeration", 0.5, 3.0, 8.0),
......
......@@ -28,6 +28,7 @@ public class RiskFactorCalculator {
TaskType.FIXME,
TaskType.TODO,
CodeSmellType.CIRCULAR_IMPORTATION,
CodeSmellType.UNUSED_IMPORTS_PROJECT,
CodeSmellType.LANGUAGE_MIXING,
CodeSmellType.TOO_MANY_STATEMENTS,
CodeSmellType.TOO_MANY_PARAMETERS,
......
......@@ -26,6 +26,7 @@ import org.eclipse.titanium.markers.types.CodeSmellType;
public enum ProblemTypePreference {
ALTSTEP_COVERAGE("Report insufficient altstep coverage", EnumSet.of(CodeSmellType.ALTSTEP_COVERAGE)),
CIRCULAR_IMPORTATION("Report circular module dependencies", EnumSet.of(CodeSmellType.CIRCULAR_IMPORTATION)),
UNUSED_IMPORTS_PROJECT("Report unused module importation in project", EnumSet.of(CodeSmellType.UNUSED_IMPORTS_PROJECT)),
LANGUAGE_MIXING("Report modules with different language restrictions", EnumSet.of(CodeSmellType.LANGUAGE_MIXING)),
CONSECUTIVE_ASSIGNMENTS("Report consecutive assignments", EnumSet.of(CodeSmellType.CONSECUTIVE_ASSIGNMENTS)),
CONVERT_TO_ENUM("Report usage of non-enumeration types in select statements", EnumSet.of(CodeSmellType.CONVERT_TO_ENUM)),
......
......@@ -80,6 +80,7 @@ public final class MarkersPreferencePage extends FieldEditorPreferencePage imple
+ " it recommendable for an altstep to handle any possible incoming message types");
m.put(ProblemTypePreference.CIRCULAR_IMPORTATION, "Circular importation is not prohibited in TTCN-3,"
+ " but it is also not recommended.");
m.put(ProblemTypePreference.UNUSED_IMPORTS_PROJECT, "popup text");
m.put(ProblemTypePreference.LANGUAGE_MIXING, "Mixing modules with different language versions in the same project "
+ "is a sign of architectural problems as it can lead to missunderstandings/unexpected behaviours/aged design.\n"
+ "it is always recommended to adapt TTCN-3 source code to the newest version of the standard.");
......@@ -215,6 +216,7 @@ public final class MarkersPreferencePage extends FieldEditorPreferencePage imple
comp.setLayout(new FillLayout(SWT.VERTICAL));
createField(comp, ProblemTypePreference.GOTO);
createField(comp, ProblemTypePreference.CIRCULAR_IMPORTATION);
createField(comp, ProblemTypePreference.UNUSED_IMPORTS_PROJECT);
createField(comp, ProblemTypePreference.LANGUAGE_MIXING);
createField(comp, ProblemTypePreference.MISSING_IMPORT);
createField(comp, ProblemTypePreference.UNUSED_IMPORT);
......
......@@ -40,6 +40,7 @@ public class RiskFactorPreferencePage extends PreferencePage implements IWorkben
insertTask(TaskType.FIXME);
insertTask(TaskType.TODO);
insertSP(CodeSmellType.CIRCULAR_IMPORTATION);
insertSP(CodeSmellType.UNUSED_IMPORTS_PROJECT);
insertSP(CodeSmellType.LANGUAGE_MIXING);
insertSP(CodeSmellType.TOO_MANY_STATEMENTS);
insertSP(CodeSmellType.TOO_MANY_PARAMETERS);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment