- private void processGeneratedCode(Collection<File> files,
- Map<String, ASTVisitor> verifiers) throws IOException {
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- Map<?, ?> options = JavaCore.getOptions();
- JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options);
- parser.setCompilerOptions(options);
-
- parser.setKind(ASTParser.K_COMPILATION_UNIT);
-
- for (File file : files) {
- char[] source = readFileAsChars(file);
- parser.setSource(source);
- parser.setResolveBindings(true);
-
- CompilationUnit cu = (CompilationUnit) parser.createAST(null);
- // Check for compilation problems in generated file
- for (IProblem c : cu.getProblems()) {
- // 1610613332 = Syntax error, annotations are only available if
- // source level is 5.0
- if (c.getID() == 1610613332)
- continue;
- // 1610613332 = Syntax error, parameterized types are only
- // available if source level is 5.0
- if (c.getID() == 1610613329)
- continue;
- if (c.getID() == 1610613328) // 'for each' statements are only available if source level is 5.0
- continue;
- fail("Error in generated source code " + file + ":"
- + c.getSourceLineNumber() + " id: " + c.getID() + " message:" + c.toString());
- }
-
- ASTVisitor visitor = verifiers.get(file.getName());
- if (visitor == null)
- fail("Unknown generated file " + file.getName());
- cu.accept(visitor);
-
- }
- }
-
- public static char[] readFileAsChars(File file) throws IOException {
- List<String> readLines = Files
- .readLines(file, Charset.forName("utf-8"));
- char[] retVal = new char[0];
- for (String string : readLines) {
- char[] line = string.toCharArray();
- int beforeLength = retVal.length;
- retVal = Arrays.copyOf(retVal, retVal.length + line.length + 1);
- System.arraycopy(line, 0, retVal, beforeLength, line.length);
- retVal[retVal.length - 1] = '\n';
- }
- return retVal;