@Override public String getMethodBody(ProcessingEnvironment env, ExecutableElement method) { try { /* * if (false) { Pair<JCTree, JCCompilationUnit> treeAndTopLevel = ((JavacElements) * env.getElementUtils()).getTreeAndTopLevel(method, null, null); JCBlock block = * ((JCMethodDecl) treeAndTopLevel.fst).getBody(); int startPos = block.pos; int endPos * = block.endpos; String methodBody = * treeAndTopLevel.snd.getSourceFile().getCharContent(true).subSequence(startPos + 1, * endPos).toString(); return methodBody; } */ Object treeAndTopLevel = getTreeAndTopLevel(env, method); Object block = method(field(treeAndTopLevel, "fst"), "getBody"); int startPos = (int) field(block, "pos"); int endPos = (int) field(block, "endpos"); return getContent(treeAndTopLevel).subSequence(startPos + 1, endPos).toString(); } catch (Exception e) { return ElementUtils.printException(e); } }
private static CharSequence getContent(Object treeAndTopLevel) throws Exception { /* * CharSequence content = treeAndTopLevel.snd.getSourceFile().getCharContent(true); */ return (CharSequence) method(method(field(treeAndTopLevel, "snd"), "getSourceFile"), "getCharContent", getCharContentSignature, true); }