Jdt: get comments for each method of the Java class

Source: Internet
Author: User
// Step 1: Obtain icompilationunit iworkspaceroot root = resourcesplugin. getworkspace (). getroot (); iproject project = root. getproject ("test"); try {project. open (null/* iprogressmonitor */);} catch (coreexception e) {// todo auto-generated catch blocke. printstacktrace ();} ijavaproject javaproject = javacore. create (project); itype lwtype = NULL; try {lwtype = javaproject. findtype ("net. chenxs. test ");} catch (Jav Amodelexception e) {e. printstacktrace ();} // Step 2: extract javadocicompilationunit lwcompilationunit = lwtype. getcompilationunit (); try {sourcetype type = (sourcetype) lwcompilationunit. getalltypes () [0]; ibuffer buffer = lwcompilationunit. getbuffer (); imethod [] Methods = type. getmethods (); ibuffer buffer1 = lwcompilationunit. getbuffer (); For (INT I = 0; I <methods. length; I ++) {imethod method = methods [I]; isourceran GE range = method. getjavadocrange (); // obtain the javadoc range if (range! = NULL) {system. out. println (range. getoffset () + "" + range. getlength (); system. out. println (buffer1.gettext (range. getoffset (), range. getlength () ;}}} catch (javamodelexception e) {e. printstacktrace ();} Note: it is also possible to use AST; astparser = astparser. newparser (AST. jls3); astparser. setkind (astparser. k_compilation_unit); astparser. setsource (lwcompilationunit); compilationunit = (compilationunit) astpars Er. createast (null); typedeclaration type = (typedeclaration) unit. types (). get (0); methoddeclaration [] Methods = type. getmethods (); For (INT I = 0; I <methods. length; I ++) {methoddeclaration method = methods [I]; javadoc Doc = method. getjavadoc (); If (Doc! = NULL) {system. Out. println (DOC );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.