Package: NameGen

NameGen

nameinstructionbranchcomplexitylinemethod
NameGen()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
NameGen(INode)
M: 14 C: 19
58%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 7
70%
M: 0 C: 1
100%
NameGen(SClassDeclIR)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addName(String)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getName(String)
M: 0 C: 55
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 11
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.vdm2jml.util;
2:
3: import java.util.HashSet;
4: import java.util.Set;
5:
6: import org.apache.log4j.Logger;
7: import org.overture.ast.analysis.AnalysisException;
8: import org.overture.ast.node.INode;
9: import org.overture.codegen.analysis.vdm.NameCollector;
10: import org.overture.codegen.assistant.AssistantBase;
11: import org.overture.codegen.ir.declarations.SClassDeclIR;
12:
13: /**
14: * Convenience class for generating parameter names that do not collide with other names used in a given class.
15: *
16: * @author pvj
17: */
18: public class NameGen
19: {
20:         private Set<String> toAvoid;
21:
22:         private Logger log = Logger.getLogger(this.getClass().getName());
23:
24:         public NameGen()
25:         {
26:                 this.toAvoid = new HashSet<String>();
27:         }
28:
29:         public NameGen(INode vdmNode)
30:         {
31:                 this();
32:
33:•                if (vdmNode != null)
34:                 {
35:                         NameCollector collector = new NameCollector();
36:
37:                         try
38:                         {
39:                                 vdmNode.apply(collector);
40:                                 this.toAvoid.addAll(collector.namesToAvoid());
41:                         } catch (AnalysisException e)
42:                         {
43:                                 log.error("Problems encountered when trying to collect names from "
44:                                                 + vdmNode);
45:                                 e.printStackTrace();
46:                         }
47:                 }
48:         }
49:
50:         public NameGen(SClassDeclIR classDecl)
51:         {
52:                 this(AssistantBase.getVdmNode(classDecl));
53:
54:         }
55:
56:         public void addName(String name)
57:         {
58:                 toAvoid.add(name);
59:         }
60:
61:         public String getName(String suggestion)
62:         {
63:•                if (!toAvoid.contains(suggestion))
64:                 {
65:                         toAvoid.add(suggestion);
66:                         return suggestion;
67:                 } else
68:                 {
69:                         int counter = 1;
70:
71:                         String prefix = suggestion + "_";
72:
73:                         String newSuggestion = prefix + counter;
74:
75:•                        while (toAvoid.contains(newSuggestion))
76:                         {
77:                                 counter++;
78:                                 newSuggestion = prefix + counter;
79:                         }
80:
81:                         toAvoid.add(newSuggestion);
82:                         return newSuggestion;
83:                 }
84:         }
85: }