// parse test file into a tree, and write it out to a stringbuffer using Pretty
JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Iterable<? extends CompilationUnitTree> trees = t1.parse(); for (CompilationUnitTree tree: trees) { new Pretty(pw, true).printExpr((JCTree) tree);
}
pw.close();
final String out = sw.toString();
System.err.println("generated code:\n" + out + "\n");
// verify the generated code is valid Java by compiling it
JavacTool tool2 = JavacTool.create();
JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
@Override public CharSequence getCharContent(boolean ignoreEncodingErrors) { return out;
}
};
JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo)); boolean ok = t2.call(); if (!ok) thrownew Exception("compilation of generated code failed");
File expectedClass = new File(test.getName().replace(".java", ".class")); if (!expectedClass.exists()) thrownew Exception(expectedClass + " not found");
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.