| 1 |
package test0579; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.FileReader; |
| 5 |
import java.util.Iterator; |
| 6 |
import java.util.List; |
| 7 |
|
| 8 |
import org.apache.tools.ant.BuildException; |
| 9 |
import org.apache.tools.ant.Task; |
| 10 |
import org.eclipse.jdt.core.dom.AST; |
| 11 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
| 12 |
import org.eclipse.jdt.core.dom.ImportDeclaration; |
| 13 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 14 |
import org.eclipse.jdt.core.dom.Name; |
| 15 |
import org.eclipse.jdt.core.dom.PackageDeclaration; |
| 16 |
import org.eclipse.jdt.core.dom.TypeDeclaration; |
| 17 |
|
| 18 |
/** |
| 19 |
* |
| 20 |
*/ |
| 21 |
public class ParserTask extends Task { |
| 22 |
public void doNothingMethod() { |
| 23 |
// |
| 24 |
int x = 0; |
| 25 |
} |
| 26 |
|
| 27 |
public void execute() throws BuildException { |
| 28 |
try { |
| 29 |
String line = null; |
| 30 |
StringBuffer buffer = new StringBuffer(); |
| 31 |
|
| 32 |
/* Read in this source file */ |
| 33 |
BufferedReader bufferedReader = new BufferedReader(new FileReader( |
| 34 |
"C:\\Projects5.1\\ParserTask\\src\\ParserTask.java")); |
| 35 |
|
| 36 |
/* Put source file contents into a StringBuffer */ |
| 37 |
while ((line = bufferedReader.readLine()) != null) { |
| 38 |
buffer.append(line); |
| 39 |
} |
| 40 |
|
| 41 |
/* Parse the source code */ |
| 42 |
CompilationUnit compUnit = AST.parseCompilationUnit(buffer |
| 43 |
.toString().toCharArray()); |
| 44 |
|
| 45 |
System.out.println("\nPackage Declaration: "); |
| 46 |
PackageDeclaration packageDecl = compUnit.getPackage(); |
| 47 |
System.out.println(packageDecl == null ? "(default)" : packageDecl |
| 48 |
.getName().toString()); |
| 49 |
|
| 50 |
System.out.println("\nImports: "); |
| 51 |
List importList = compUnit.imports(); |
| 52 |
Iterator itr = importList.iterator(); |
| 53 |
while (itr.hasNext()) { |
| 54 |
ImportDeclaration importDecl = (ImportDeclaration) itr.next(); |
| 55 |
|
| 56 |
System.out.println(importDecl.getName()); |
| 57 |
} |
| 58 |
|
| 59 |
List typeList = compUnit.types(); |
| 60 |
itr = typeList.iterator(); |
| 61 |
while (itr.hasNext()) { |
| 62 |
TypeDeclaration typeDecl = (TypeDeclaration) itr.next(); |
| 63 |
|
| 64 |
System.out.println("\nClass: " + typeDecl.getName()); |
| 65 |
|
| 66 |
System.out.print("Superclass: "); |
| 67 |
Name superClassName = typeDecl.getSuperclass(); |
| 68 |
|
| 69 |
System.out.println(superClassName == null ? "(none)" |
| 70 |
: superClassName.toString()); |
| 71 |
|
| 72 |
MethodDeclaration[] methodDecls = typeDecl.getMethods(); |
| 73 |
System.out.println("Methods: "); |
| 74 |
|
| 75 |
for (int i = 0; i < methodDecls.length; i++) { |
| 76 |
System.out.println("\t\t" |
| 77 |
+ methodDecls[i].getName().toString()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
} catch (Exception e) { |
| 82 |
throw new BuildException(e); |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
void anotherDoNothingMethod() { |
| 88 |
int x = 0; |
| 89 |
} |
| 90 |
|
| 91 |
} |