001: package spoon.vsuite.common;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005: import java.util.Set;
006: import java.util.TreeSet;
007: import java.util.Map.Entry;
008:
009: import spoon.processing.AbstractProcessor;
010: import spoon.processing.Property;
011: import spoon.processing.Severity;
012: import spoon.reflect.code.CtAssignment;
013: import spoon.reflect.code.CtOperatorAssignment;
014: import spoon.reflect.code.CtVariableAccess;
015: import spoon.reflect.declaration.CtSimpleType;
016: import spoon.reflect.declaration.CtVariable;
017: import spoon.reflect.visitor.Query;
018: import spoon.reflect.visitor.filter.TypeFilter;
019:
020: public class VariableProcessor extends
021: AbstractProcessor<CtSimpleType<?>> {
022:
023: @Property
024: Severity level = Severity.WARNING;
025:
026: public static Map<CtSimpleType<?>, Set<CtVariable<?>>> variables = new HashMap<CtSimpleType<?>, Set<CtVariable<?>>>();
027: public static Map<CtSimpleType<?>, Set<CtVariable<?>>> readVariables = new HashMap<CtSimpleType<?>, Set<CtVariable<?>>>();
028: public static Map<CtSimpleType<?>, Set<CtVariable<?>>> writtenVariables = new HashMap<CtSimpleType<?>, Set<CtVariable<?>>>();
029:
030: public static Set<CtVariable<?>> getVariables() {
031: Set<CtVariable<?>> vars = new TreeSet<CtVariable<?>>();
032: for (Entry<CtSimpleType<?>, Set<CtVariable<?>>> e : variables
033: .entrySet()) {
034: vars.addAll(e.getValue());
035: }
036: return vars;
037: }
038:
039: public static Set<CtVariable<?>> getReadVariables() {
040: Set<CtVariable<?>> vars = new TreeSet<CtVariable<?>>();
041: for (Entry<CtSimpleType<?>, Set<CtVariable<?>>> e : readVariables
042: .entrySet()) {
043: vars.addAll(e.getValue());
044: }
045: return vars;
046: }
047:
048: public static Set<CtVariable<?>> getWrittenVariables() {
049: Set<CtVariable<?>> vars = new TreeSet<CtVariable<?>>();
050: for (Entry<CtSimpleType<?>, Set<CtVariable<?>>> e : writtenVariables
051: .entrySet()) {
052: vars.addAll(e.getValue());
053: }
054: return vars;
055: }
056:
057: public void process(CtSimpleType<?> type) {
058: if (type.isTopLevel()) {
059: variables.remove(type);
060: readVariables.remove(type);
061: writtenVariables.remove(type);
062: Set<CtVariable<?>> vars = new TreeSet<CtVariable<?>>();
063: for (CtVariable<?> v : Query.getElements(type,
064: new TypeFilter<CtVariable<?>>(CtVariable.class))) {
065: vars.add(v);
066: }
067: variables.put(type, vars);
068: vars = new TreeSet<CtVariable<?>>();
069: for (CtVariableAccess<?> va : Query.getElements(type,
070: new TypeFilter<CtVariableAccess<?>>(
071: CtVariableAccess.class))) {
072: CtVariable<?> v = va.getVariable().getDeclaration();
073: if (v == null)
074: continue;
075: vars.add(v);
076: }
077: readVariables.put(type, vars);
078: vars = new TreeSet<CtVariable<?>>();
079: for (CtAssignment<?, ?> assignment : Query.getElements(
080: type, new TypeFilter<CtAssignment<?, ?>>(
081: CtAssignment.class))) {
082: if (!(assignment.getAssigned() instanceof CtVariableAccess))
083: continue;
084: CtVariableAccess<?> va = (CtVariableAccess<?>) assignment
085: .getAssigned();
086: CtVariable<?> v = va.getVariable().getDeclaration();
087: if (v == null)
088: continue;
089: vars.add(v);
090: }
091: for (CtOperatorAssignment<?, ?> assignment : Query
092: .getElements(type,
093: new TypeFilter<CtOperatorAssignment<?, ?>>(
094: CtOperatorAssignment.class))) {
095: if (!(assignment.getAssigned() instanceof CtVariableAccess))
096: continue;
097: CtVariableAccess<?> va = (CtVariableAccess<?>) assignment
098: .getAssigned();
099: CtVariable<?> v = va.getVariable().getDeclaration();
100: if (v == null)
101: continue;
102: vars.add(v);
103: }
104: writtenVariables.put(type, vars);
105: }
106: }
107:
108: }
|