Package: JmlGenMain

JmlGenMain

nameinstructionbranchcomplexitylinemethod
JmlGenMain()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
main(String[])
M: 87 C: 161
65%
M: 16 C: 22
58%
M: 14 C: 6
30%
M: 20 C: 43
68%
M: 0 C: 1
100%
usage(String)
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.codegen.vdm2jml;
2:
3: import java.io.File;
4: import java.util.Arrays;
5: import java.util.Iterator;
6: import java.util.LinkedList;
7: import java.util.List;
8:
9: import org.overture.ast.analysis.AnalysisException;
10: import org.overture.ast.lex.Dialect;
11: import org.overture.ast.modules.AModuleModules;
12: import org.overture.codegen.printer.MsgPrinter;
13: import org.overture.codegen.utils.GeneralCodeGenUtils;
14: import org.overture.codegen.utils.GeneralUtils;
15: import org.overture.codegen.utils.GeneratedData;
16: import org.overture.codegen.vdm2java.JavaCodeGenMain;
17: import org.overture.codegen.vdm2java.JavaCodeGenUtil;
18: import org.overture.config.Release;
19: import org.overture.config.Settings;
20: import org.overture.typechecker.util.TypeCheckerUtil;
21: import org.overture.typechecker.util.TypeCheckerUtil.TypeCheckResult;
22:
23: public class JmlGenMain
24: {
25:         public static final String OUTPUT_ARG = "-output";
26:         public static final String PRINT_ARG = "-print";
27:         public static final String FOLDER_ARG = "-folder";
28:         public static final String INVARIANT_FOR = "-invariant_for";
29:         public static final String NO_TRACE = "-notrace";
30:         public static final String NO_CLONING = "-nocloning";
31:         public static final String PACKAGE_ARG = "-package";
32:
33:         public static void main(String[] args)
34:         {
35:•                if (args == null || args.length < 1)
36:                 {
37:                         usage("Expected one or more arguments");
38:                         return;
39:                 }
40:
41:                 Settings.release = Release.VDM_10;
42:                 Settings.dialect = Dialect.VDM_SL;
43:
44:                 List<String> listArgs = Arrays.asList(args);
45:
46:                 List<File> files = new LinkedList<File>();
47:
48:                 File outputDir = null;
49:
50:                 boolean print = false;
51:
52:                 JmlGenerator jmlGen = new JmlGenerator();
53:                 jmlGen.getIrSettings().setCharSeqAsString(true);
54:
55:•                for (Iterator<String> i = listArgs.iterator(); i.hasNext();)
56:                 {
57:                         String arg = i.next();
58:
59:•                        if (arg.equals(OUTPUT_ARG))
60:                         {
61:•                                if (i.hasNext())
62:                                 {
63:                                         outputDir = new File(i.next());
64:                                         outputDir.mkdirs();
65:
66:•                                        if (!outputDir.isDirectory())
67:                                         {
68:                                                 usage(outputDir + " is not a directory");
69:                                         }
70:
71:                                 } else
72:                                 {
73:                                         usage(OUTPUT_ARG + " requires a directory");
74:                                 }
75:•                        } else if (arg.equals(PRINT_ARG))
76:                         {
77:                                 print = true;
78:•                        } else if (arg.equals(PACKAGE_ARG))
79:                         {
80:•                                if (i.hasNext())
81:                                 {
82:                                         String javaPackage = i.next();
83:
84:•                                        if (JavaCodeGenUtil.isValidJavaPackage(javaPackage))
85:                                         {
86:                                                 jmlGen.getJavaSettings().setJavaRootPackage(javaPackage);
87:                                         } else
88:                                         {
89:                                                 MsgPrinter.getPrinter().errorln("Not a valid java package. Using the default java package instead..\n");
90:                                         }
91:                                 }
92:•                        } else if (arg.equals(FOLDER_ARG))
93:                         {
94:•                                if (i.hasNext())
95:                                 {
96:                                         File path = new File(i.next());
97:
98:•                                        if (path.isDirectory())
99:                                         {
100:                                                 files.addAll(JavaCodeGenMain.filterFiles(GeneralUtils.getFiles(path)));
101:                                         } else
102:                                         {
103:                                                 usage("Could not find path: " + path);
104:                                         }
105:                                 } else
106:                                 {
107:                                         usage(FOLDER_ARG + " requires a directory");
108:                                 }
109:•                        } else if (arg.equals(INVARIANT_FOR))
110:                         {
111:                                 jmlGen.getJmlSettings().setGenInvariantFor(true);
112:•                        } else if (arg.equals(NO_TRACE))
113:                         {
114:                                 jmlGen.getIrSettings().setGenerateTraces(false);
115:                                 jmlGen.getJavaSettings().setMakeClassesSerializable(false);
116:•                        } else if (arg.equals(NO_CLONING))
117:                         {
118:                                 jmlGen.getJavaSettings().setDisableCloning(true);
119:                         } else
120:                         {
121:                                 // It's a file or a directory
122:                                 File file = new File(arg);
123:
124:•                                if (file.isFile())
125:                                 {
126:•                                        if (GeneralCodeGenUtils.isVdmSourceFile(file))
127:                                         {
128:                                                 files.add(file);
129:                                         }
130:                                 } else
131:                                 {
132:                                         usage("Not a file: " + file);
133:                                 }
134:                         }
135:                 }
136:
137:                 try
138:                 {
139:                         MsgPrinter.getPrinter().println("Starting the VDM to JML generator...");
140:
141:                         TypeCheckResult<List<AModuleModules>> tcResult = TypeCheckerUtil.typeCheckSl(files);
142:
143:•                        if (!GeneralCodeGenUtils.hasErrors(tcResult))
144:                         {
145:                                 GeneratedData data = jmlGen.generateJml(tcResult.result);
146:                                 JavaCodeGenMain.processData(print, outputDir, jmlGen.getJavaGen(), data, false, true);
147:                         } else
148:                         {
149:                                 MsgPrinter.getPrinter().errorln("Could not parse/type check VDM model:\n"
150:                                                 + GeneralCodeGenUtils.errorStr(tcResult));
151:                         }
152:
153:                 } catch (AnalysisException e)
154:                 {
155:                         MsgPrinter.getPrinter().println("Could not code generate model: "
156:                                         + e.getMessage());
157:                 }
158:         }
159:
160:         private static void usage(String msg)
161:         {
162:                 MsgPrinter.getPrinter().errorln("VDMSL to JML/Java generator: " + msg
163:                                 + "\n");
164:                 MsgPrinter.getPrinter().errorln("Usage: vdm2jml [<options>] [<VDM SL files>]");
165:                 MsgPrinter.getPrinter().errorln(PRINT_ARG
166:                                 + ": print the generated code to the console");
167:                 MsgPrinter.getPrinter().errorln(OUTPUT_ARG
168:                                 + " <folder path>: the output folder of the generated code");
169:                 MsgPrinter.getPrinter().errorln(FOLDER_ARG
170:                                 + " <folder path>: a folder containing input .vdmsl files");
171:                 MsgPrinter.getPrinter().errorln(INVARIANT_FOR
172:                                 + ": to check record invariants explicitly using JML's invariant_for");
173:                 MsgPrinter.getPrinter().errorln(NO_CLONING
174:                                 + ": To disable deep cloning of value types");
175:                 System.exit(1);
176:         }
177: }