Package: Vdm2JavaMojo$1

Vdm2JavaMojo$1

nameinstructionbranchcomplexitylinemethod
error(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
errorln(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
print(String)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
println(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package org.overture.codegen.mojocg;
2:
3: import com.google.common.hash.HashCode;
4: import com.google.common.hash.Hashing;
5: import com.google.common.io.Files;
6: import org.apache.commons.io.FileUtils;
7: import org.apache.maven.plugin.MojoExecutionException;
8: import org.apache.maven.plugin.MojoFailureException;
9: import org.overture.ast.analysis.AnalysisException;
10: import org.overture.ast.definitions.SClassDefinition;
11: import org.overture.ast.lex.Dialect;
12: import org.overture.ast.modules.AModuleModules;
13: import org.overture.codegen.ir.CodeGenBase;
14: import org.overture.codegen.ir.IRSettings;
15: import org.overture.codegen.mojocg.util.DelegateTrans;
16: import org.overture.codegen.printer.AbstractPrinter;
17: import org.overture.codegen.printer.MsgPrinter;
18: import org.overture.codegen.utils.GeneralCodeGenUtils;
19: import org.overture.codegen.utils.GeneralUtils;
20: import org.overture.codegen.utils.GeneratedData;
21: import org.overture.codegen.vdm2java.JavaCodeGen;
22: import org.overture.codegen.vdm2java.JavaCodeGenMain;
23: import org.overture.codegen.vdm2java.JavaCodeGenUtil;
24: import org.overture.codegen.vdm2java.JavaSettings;
25: import org.overture.config.Release;
26: import org.overture.config.Settings;
27: import org.overture.typechecker.util.TypeCheckerUtil;
28: import org.overture.typechecker.util.TypeCheckerUtil.TypeCheckResult;
29:
30: import java.io.File;
31: import java.io.IOException;
32: import java.util.*;
33:
34: /**
35: * VDM-to-Java code generator mojo
36: *
37: * @goal generate
38: * @phase generate-sources
39: * @requiresDependencyResolution compile
40: */
41: public class Vdm2JavaMojo extends Vdm2JavaBaseMojo {
42: public static final String VDM_PP = "pp";
43: public static final String VDM_SL = "sl";
44: public static final String VDM_RT = "rt";
45:
46: public static final String VDM_10 = "vdm10";
47: public static final String VDM_CLASSIC = "classic";
48:
49: @Override
50: public void execute() throws MojoExecutionException, MojoFailureException {
51: MsgPrinter.setPrinter(new AbstractPrinter() {
52:
53: @Override
54: public void println(String msg) {
55:
56: print(msg);
57: }
58:
59: @Override
60: public void print(String msg) {
61:
62:• if (!silent) {
63: getLog().info(msg);
64: }
65: }
66:
67: @Override
68: public void errorln(String msg) {
69:
70: error(msg);
71: }
72:
73: @Override
74: public void error(String msg) {
75:
76: getLog().error(msg);
77: }
78: });
79:
80: if (!verbose) {
81: MsgPrinter.getPrinter().setSilent(true);
82: }
83:
84: getLog().info("Starting the VDM-to-Java code generator...");
85:
86: List<File> files = new LinkedList<File>();
87:
88: if (specificationDir != null && specificationDir.isDirectory()) {
89: findVdmSources(files, specificationDir);
90: }
91:
92: if (files == null || files.isEmpty()) {
93: throw new MojoFailureException("Nothing to generate, no specification files.");
94: }
95:
96: if (!isReGenerationRequired(files)) {
97: getLog().info("No change in specification files. Skipping generating.");
98: return;
99: }
100:
101: if (outputDirectory != null && outputDirectory.exists()) {
102: getLog().debug("Deleting Java code generation output folder: " + outputDirectory.getAbsolutePath());
103: try {
104: FileUtils.deleteDirectory(outputDirectory);
105: } catch (IOException e) {
106: getLog().error(e);
107: throw new MojoFailureException("Failed to delete Java Generation output folder: " + outputDirectory.getAbsolutePath());
108:
109: }
110: }
111:
112: // Let's make sure that maven knows to look in the output directory
113: project.addCompileSourceRoot(outputDirectory.getPath());
114:
115: IRSettings irSettings = new IRSettings();
116: irSettings.setCharSeqAsString(true);
117: irSettings.setGeneratePreConds(false);
118: irSettings.setGeneratePreCondChecks(false);
119: irSettings.setGeneratePostConds(false);
120: irSettings.setGeneratePostCondChecks(false);
121: irSettings.setGenerateConc(genConcurrency);
122:
123: JavaSettings javaSettings = new JavaSettings();
124: javaSettings.setDisableCloning(false);
125: javaSettings.setFormatCode(formatCode);
126: javaSettings.setGenJUnit4tests(genJUnit4Tests);
127: javaSettings.setPrintVdmLocations(printVdmLocations);
128: javaSettings.setGenSystemClass(genSystemClass);
129:
130: if (modulesToSkip != null && !modulesToSkip.isEmpty()) {
131: javaSettings.setModulesToSkip(modulesToSkip);
132: }
133:
134: if (JavaCodeGenUtil.isValidJavaPackage(packageName)) {
135: javaSettings.setJavaRootPackage(packageName);
136: } else {
137: if (packageName != null) {
138: getLog().warn(String.format("%s is not a valid Java package.", packageName));
139: }
140: }
141:
142: outputDirectory.mkdirs();
143:
144: if (release.equals(VDM_10)) {
145: Settings.release = Release.VDM_10;
146: } else if (release.equals(VDM_CLASSIC)) {
147: Settings.release = Release.CLASSIC;
148: } else {
149: throw new MojoFailureException(String.format("Expected VDM version to be '%s' or '%s'", VDM_10, VDM_CLASSIC));
150: }
151:
152: JavaCodeGen javaCodeGen = new JavaCodeGen();
153: javaCodeGen.setSettings(irSettings);
154: javaCodeGen.setJavaSettings(javaSettings);
155:
156: addDelegateTrans(javaCodeGen);
157:
158: GeneratedData genData = null;
159:
160: if (dialect.equals(VDM_PP)) {
161: Settings.dialect = Dialect.VDM_PP;
162: TypeCheckResult<List<SClassDefinition>> tcResult = TypeCheckerUtil.typeCheckPp(files);
163:
164: validateTcResult(tcResult);
165:
166: try {
167: genData = javaCodeGen.generate(CodeGenBase.getNodes(tcResult.result));
168: } catch (AnalysisException e) {
169: e.printStackTrace();
170: throw new MojoExecutionException("Got unexpected error when trying to code generate VDM++ model: "
171: + e.getMessage());
172: }
173: } else if (dialect.equals(VDM_SL)) {
174: Settings.dialect = Dialect.VDM_SL;
175: TypeCheckResult<List<AModuleModules>> tcResult = TypeCheckerUtil.typeCheckSl(files);
176:
177: validateTcResult(tcResult);
178:
179: try {
180: genData = javaCodeGen.generate(CodeGenBase.getNodes(tcResult.result));
181:
182: } catch (AnalysisException e) {
183: e.printStackTrace();
184: throw new MojoExecutionException("Got unexpected error when trying to code generate VDM-SL model: "
185: + e.getMessage());
186: }
187:
188: } else if (dialect.equals(VDM_RT)) {
189: try {
190: Settings.dialect = Dialect.VDM_RT;
191: TypeCheckResult<List<SClassDefinition>> tcResult = TypeCheckerUtil.typeCheckRt(files);
192:
193: validateTcResult(tcResult);
194:
195: javaSettings.setMakeClassesSerializable(true);
196:
197: genData = javaCodeGen.generate(CodeGenBase.getNodes(tcResult.result));
198:
199: } catch (AnalysisException e) {
200: e.printStackTrace();
201: throw new MojoExecutionException("Got unexpected error when trying to code generate VDM-RT model: "
202: + e.getMessage());
203: }
204: } else {
205: throw new MojoExecutionException(String.format("Expected dialect to be '%s' or '%s'", VDM_SL, VDM_PP));
206: }
207:
208: if (genData != null) {
209: JavaCodeGenMain.processData(false, outputDirectory, javaCodeGen, genData, separateTestCode, true);
210:
211: if (genData.hasErrors()) {
212: throw new MojoExecutionException("Could not code generate model.");
213: }
214: }
215:
216: getLog().info("Code generation completed.");
217: }
218:
219: private boolean isReGenerationRequired(List<File> files) throws MojoFailureException {
220: File crcFile = new File(getProjectOutputDirectory(), "javagen.crc");
221:
222: Set<String> crcs = new HashSet<>();
223: if (crcFile.exists()) {
224: try {
225: for (Object l : FileUtils.readLines(crcFile, "UTF-8")) {
226: String line = l + "";
227: if (!line.trim().isEmpty()) {
228: crcs.add(line);
229: }
230: }
231: } catch (IOException e) {
232: getLog().error(e);
233: throw new MojoFailureException("Failed to read CRC lines for Java Generation from: " + crcFile.getAbsolutePath());
234: }
235: }
236:
237:
238: Set<String> inputCrcs = new HashSet<>();
239: for (File f : files) {
240: HashCode hc = null;
241: try {
242: hc = Files.asByteSource(f).hash(Hashing.sha1());
243: String crc = "SHA-1: " + hc.toString();
244: inputCrcs.add(crc);
245: getLog().debug("Calculating CRC: " + crc + " " + f.getPath());
246: } catch (IOException e) {
247: getLog().error(e);
248: throw new MojoFailureException("Failed to calculate CRC for Java Generation from: " + f.getAbsolutePath());
249: }
250: }
251:
252: if (crcs.size() == inputCrcs.size() && crcs.equals(inputCrcs)) {
253: //no changes
254: return false;
255: }
256:
257: try {
258: FileUtils.writeLines(crcFile, "UTF-8", inputCrcs, "\n");
259: } catch (IOException e) {
260: getLog().error(e);
261: throw new MojoFailureException("Failed to write CRC lines for Java Generation to: " + crcFile.getAbsolutePath());
262: }
263: return true;
264:
265: }
266:
267: private Map<String, String> buidDelegateMap() {
268: if (delegates == null) {
269: return new HashMap<>();
270: }
271:
272: Map<String, String> map = new HashMap<String, String>();
273: for (String key : delegates.stringPropertyNames()) {
274: map.put(key, delegates.getProperty(key));
275: }
276:
277: return map;
278: }
279:
280: private void addDelegateTrans(JavaCodeGen javaCodeGen) {
281: if (delegates != null && !delegates.isEmpty()) {
282: Map<String, String> delegateMap = buidDelegateMap();
283:
284: getLog().debug("Found following bridge/delegate pairs:");
285:
286: for (String entry : delegateMap.keySet()) {
287: getLog().debug(" Bridge class: " + entry + ". Delegate class: "
288: + delegateMap.get(entry));
289: }
290:
291: javaCodeGen.getTransSeries().getSeries().add(new DelegateTrans(delegateMap, javaCodeGen.getTransAssistant(), getLog()));
292: }
293: }
294:
295: private void findVdmSources(List<File> files, File specificationRoot) {
296: for (File f : GeneralUtils.getFilesRecursively(specificationRoot)) {
297: if (GeneralCodeGenUtils.isVdmSourceFile(f)) {
298: files.add(f);
299: }
300: }
301: }
302:
303: private void validateTcResult(TypeCheckResult<?> tcResult)
304: throws MojoExecutionException {
305: if (!tcResult.parserResult.errors.isEmpty()
306: || !tcResult.errors.isEmpty()) {
307: String PARSE_TYPE_CHECK_ERR_MSG = "Could not parse or type check VDM model";
308: getLog().error(PARSE_TYPE_CHECK_ERR_MSG + ":\n"
309: + GeneralCodeGenUtils.errorStr(tcResult));
310:
311: throw new MojoExecutionException(PARSE_TYPE_CHECK_ERR_MSG);
312: }
313:
314: // No type errors
315: }
316: }