001: /*
002: * Main.java
003: *
004: * Created on 28-Aug-2007, 12:11:26
005: *
006: * To change this template, choose Tools | Templates
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.e2e.wsdl.wsdl2java;
011:
012: import com.sun.source.tree.CompilationUnitTree;
013: import com.sun.source.tree.Tree;
014: import com.sun.source.util.TreePath;
015: import com.sun.source.util.Trees;
016: import java.io.File;
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Set;
023: import java.util.concurrent.Future;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026: import javax.lang.model.element.Element;
027: import javax.lang.model.element.ElementKind;
028: import javax.lang.model.element.ExecutableElement;
029: import javax.lang.model.element.TypeElement;
030: import javax.lang.model.element.VariableElement;
031: import javax.lang.model.type.TypeMirror;
032: import org.netbeans.api.java.source.CancellableTask;
033: import org.netbeans.api.java.source.CompilationController;
034: import org.netbeans.api.java.source.ElementHandle;
035: import org.netbeans.api.java.source.JavaSource;
036: import org.netbeans.api.java.source.JavaSource.Phase;
037: import org.openide.filesystems.FileObject;
038: import org.openide.filesystems.FileUtil;
039:
040: /**
041: *
042: * @author luky
043: */
044: public class ClassExplorer {
045: static {
046: System.setProperty("netbeans.user",
047: "C:\\Documents and Settings\\luky\\.netbeans\\dev");
048: }
049:
050: private CompilationUnitTree compilationUnit;
051: private Trees trees;
052: private boolean initialized = false;
053:
054: private class ClassFinderTask implements
055: CancellableTask<CompilationController> {
056:
057: private volatile boolean cancelled;
058: private HashMap<ElementHandle<TypeElement>, List<ExecutableElement>> topClassMap = new HashMap<ElementHandle<TypeElement>, List<ExecutableElement>>();
059:
060: public void cancel() {
061: this .cancelled = true;
062: }
063:
064: public void run(CompilationController parameter)
065: throws Exception {
066: if (!initialized) {
067: parameter.toPhase(Phase.ELEMENTS_RESOLVED);
068: if (cancelled) {
069: return;
070: }
071: ClassExplorer.this .compilationUnit = parameter
072: .getCompilationUnit();
073: ClassExplorer.this .trees = parameter.getTrees();
074: initialized = true;
075: }
076: }
077: }
078:
079: public ClassExplorer(FileObject fobj) {
080: try {
081: JavaSource javaSource = JavaSource.forFileObject(fobj);
082: ClassFinderTask task = new ClassFinderTask();
083: Future fin = javaSource.runWhenScanFinished(task, true);
084: //Wait till task is finished
085: fin.get();
086: } catch (Exception ex) {
087: Logger.getLogger(ClassExplorer.class.getName()).log(
088: Level.SEVERE, null, ex);
089: }
090: }
091:
092: public List<TypeElement> getClasses() {
093: List<TypeElement> result = new ArrayList<TypeElement>();
094: if (initialized) {
095: List<? extends Tree> typeDecls = compilationUnit
096: .getTypeDecls();
097: if ((typeDecls == null) || typeDecls.isEmpty()) {
098: return Collections.<TypeElement> emptyList();
099: }
100:
101: TreePath tPath = new TreePath(compilationUnit);
102: for (Tree typeDecl : typeDecls) {
103: if (typeDecl.getKind() == Tree.Kind.CLASS) {
104: TypeElement elem = (TypeElement) trees
105: .getElement(new TreePath(tPath, typeDecl));
106: result.add(elem);
107: }
108: }
109: }
110: return result;
111: }
112:
113: public List<ExecutableElement> getMethods(TypeElement elem) {
114: List<? extends Element> elems = elem.getEnclosedElements();
115: List<ExecutableElement> exElems = new ArrayList<ExecutableElement>(
116: elems.size());
117: for (Element el : elems) {
118: if (el.getKind().equals(ElementKind.METHOD)
119: || el.getKind().equals(ElementKind.CONSTRUCTOR)) {
120: exElems.add((ExecutableElement) el);
121: }
122: }
123: return exElems;
124: }
125:
126: public TypeMirror getReturnType(ExecutableElement elem) {
127: return elem.getReturnType();
128: }
129:
130: public List<TypeMirror> getParameters(ExecutableElement elem) {
131: List<? extends VariableElement> varElems = elem.getParameters();
132: List<TypeMirror> paramList = new ArrayList<TypeMirror>(varElems
133: .size());
134: for (VariableElement vElem : varElems) {
135: paramList.add(vElem.asType());
136: }
137: return paramList;
138: }
139:
140: public boolean compareClasses(Set<String> classes,
141: List<TypeElement> classElems) {
142: if (classes.size() == classElems.size()) {
143: HashSet<String> set = new HashSet<String>();
144: for (TypeElement elem : classElems) {
145: set.add(elem.getQualifiedName().toString());
146: }
147: for (String className : classes) {
148: if (!set.contains(className)) {
149: System.err.println("Class: " + className
150: + " not found in generated file");
151: break;
152: }
153: }
154: return true;
155: }
156: return false;
157: }
158:
159: public boolean compareMethods(Set<String> methods,
160: List<ExecutableElement> methodElems) {
161: if (methods.size() == methodElems.size()) {
162: HashSet<String> set = new HashSet<String>();
163: for (ExecutableElement elem : methodElems) {
164: set.add(elem.getSimpleName().toString());
165: }
166: for (String methodName : methods) {
167: if (!set.contains(methodName)) {
168: System.err.println("Method: " + methodName
169: + " not found in generated file");
170: break;
171: }
172: }
173: return true;
174: }
175: return false;
176: }
177:
178: public boolean compareParameters(Set<String> params,
179: List<TypeMirror> variableElems) {
180: if (params.size() == variableElems.size()) {
181: HashSet<String> set = new HashSet<String>();
182: for (TypeMirror elem : variableElems) {
183: set.add(elem.toString());
184: }
185: for (String variableType : params) {
186: if (!set.contains(variableType.toString())) {
187: System.err.println("Parameter: " + variableType
188: + " not found in generated file");
189: break;
190: }
191: }
192: return true;
193: }
194: return false;
195: }
196:
197: /**
198: * @param args the command line arguments
199: */
200: public static void main(String[] args) {
201: // TODO code application logic here
202: ClassExplorer expl = new ClassExplorer(FileUtil
203: .toFileObject(new File("C:\\test.java")));
204: List<TypeElement> tel = expl.getClasses();
205: for (TypeElement te : tel) {
206: System.out.println(te.toString());
207: List<ExecutableElement> mel = expl.getMethods(te);
208: for (ExecutableElement me : mel) {
209: System.out
210: .println("\t" + me.getSimpleName().toString());
211: List<TypeMirror> vel = expl.getParameters(me);
212: for (TypeMirror var : vel) {
213: System.out.println("\t\t" + var.toString());
214: }
215: System.out.println("\t\treturns:"
216: + me.getReturnType().toString());
217: }
218: }
219: System.exit(-1);
220: }
221: }
|