litesoft
@ 950
litesoft / trunk / Java / JavaAST / src / org / litesoft / javaparser / MyMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package org.litesoft.javaparser; import java.io.*; import org.litesoft.core.typeutils.Objects; import java.util.*; import org.eclipse.jdt.core.*; import org.eclipse.jdt.core.dom.*; public class MyMain { public static void main(String[] args) throws Exception { System.out.println("Main: " + Arrays.asList(args)); if (args.length < 1) { System.err.println("At least one file needed..."); System.exit( 1 ); } File file = new File(args[0]); BufferedReader in = new BufferedReader(new FileReader(file)); StringBuffer buffer = new StringBuffer(); for (String line; null != (line = in.readLine());) { buffer.append(line).append("\n"); } String text = buffer.toString(); ASTParser parser = ASTParser.newParser(AST.JLS3); Map<String, String> options = new HashMap<String, String>(); String zVersion15 = JavaCore.VERSION_1_5; options.put(JavaCore.COMPILER_SOURCE, zVersion15); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, zVersion15); parser.setCompilerOptions(options); parser.setResolveBindings(false); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(text.toCharArray()); CompilationUnit node = (CompilationUnit) parser.createAST(null); PackageDeclaration zPackage = node.getPackage(); System.out.println("Package: " + zPackage.getName()); List<ImportDeclaration> zImports = (List<ImportDeclaration>) node.imports(); if ((zImports != null) && !zImports.isEmpty()) { System.out.println("Imports:"); for (ImportDeclaration zDeclaration : zImports) { System.out.print(" "); if (zDeclaration.isStatic()) { System.out.print("static "); } System.out.print(zDeclaration.getName()); if (zDeclaration.isOnDemand()) { System.out.print(".*"); } System.out.println(); } } List<AbstractTypeDeclaration> zTypes = (List<AbstractTypeDeclaration>) node.types(); if ((zTypes != null) && !zTypes.isEmpty()) { System.out.println("Types: " + zTypes.size()); for (AbstractTypeDeclaration zDeclaration : zTypes) { if (!(zDeclaration instanceof TypeDeclaration)) { System.out.println(" Not processing: " + ((zDeclaration == null) ? "null" : zDeclaration.getClass().getSimpleName())); } else { TypeDeclaration zType = (TypeDeclaration) zDeclaration; MarkerAnnotation ma = null; // zType.toString(); System.out.println( " extends: " + zType.getSuperclassType() ); System.out.println( " impliments: " + zType.superInterfaceTypes() ); } } } // IPackageDeclaration, IImportContainer, and IType, and appear in the order in which they are declared in the source. If a source file cannot be parsed, its structure remains unknown. Use IJavaElement.isStructureKnown() } } |