/** * Parse the specified files returning a list of abstract syntax trees. * * @throws java.io.IOException TODO * @return a list of abstract syntax trees */ public Iterable<? extends CompilationUnitTree> parse() throws IOException { try { prepareCompiler(); List<JCCompilationUnit> units = compiler.parseFiles(fileObjects); for (JCCompilationUnit unit : units) { JavaFileObject file = unit.getSourceFile(); if (notYetEntered.containsKey(file)) notYetEntered.put(file, unit); } return units; } finally { parsed = true; if (compiler != null && compiler.log != null) compiler.log.flush(); } }
@NotNull public static Map<String, String> parseDirectives(String expectedText) { Map<String, String> directives = Maps.newHashMap(); Matcher directiveMatcher = DIRECTIVE_PATTERN.matcher(expectedText); int start = 0; while (directiveMatcher.find()) { if (directiveMatcher.start() != start) { Assert.fail( "Directives should only occur at the beginning of a file: " + directiveMatcher.group()); } String name = directiveMatcher.group(1); String value = directiveMatcher.group(3); String oldValue = directives.put(name, value); Assert.assertNull( "Directive overwritten: " + name + " old value: " + oldValue + " new value: " + value, oldValue); start = directiveMatcher.end() + 1; } return directives; }
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element annotatedElt : roundEnv.getElementsAnnotatedWith(CodeTranslate.class)) { ExecutableElement methodElt = (ExecutableElement) annotatedElt; TypeElement typeElt = (TypeElement) methodElt.getEnclosingElement(); if (typeElt.getQualifiedName().toString().equals(fqn) && methodElt.getSimpleName().toString().equals(method)) { for (Lang lang : langs) { Result result; try { String translation = translator.translate(methodElt, lang); result = new Result.Source(translation); } catch (Exception e) { result = new Result.Failure(e); } results.put(lang, result); } } } return false; }
private void prepareCompiler() throws IOException { if (used.getAndSet(true)) { if (compiler == null) throw new IllegalStateException(); } else { initContext(); compilerMain.setOptions(Options.instance(context)); compilerMain.filenames = new LinkedHashSet<File>(); Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args), classNames); if (!filenames.isEmpty()) throw new IllegalArgumentException("Malformed arguments " + toString(filenames, " ")); compiler = JavaCompiler.instance(context); compiler.keepComments = true; compiler.genEndPos = true; // NOTE: this value will be updated after annotation processing compiler.initProcessAnnotations(processors); notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>(); for (JavaFileObject file : fileObjects) notYetEntered.put(file, null); genList = new ListBuffer<Env<AttrContext>>(); // endContext will be called when all classes have been generated // TODO: should handle the case after each phase if errors have occurred args = null; classNames = null; } }