001: package japa.dev;
002:
003: import snow.utils.gui.GUIUtils;
004: import tide.editor.MainEditorFrame;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import tide.syntaxtree.ASTUtils;
008: import java.awt.EventQueue; //import tide.editor.UIConstants;
009: import java.awt.Component;
010: import tide.syntaxtree.SyntaxTreeIcon; //import java.awt.Color;
011: import snow.utils.storage.FileUtils;
012: import java.util.*;
013: import java.awt.BorderLayout;
014: import javax.swing.*;
015: import javax.swing.tree.*;
016: import japa.parser.ast.body.*;
017: import java.io.*;
018: import japa.parser.ast.*;
019: import japa.parser.*;
020: import japa.parser.ast.type.*;
021:
022: public final class Test<T> {
023: public <B> Test() {
024: }
025:
026: @tide.annotations.Implements("Comparable")
027: // lies!
028: @edu.umd.cs.findbugs.annotations.CheckForNull
029: public <A, B, C> A make(A a) throws IOException {
030: return a;
031: }
032:
033: private void test(String a) {
034: ;
035: }
036:
037: protected String Hello() {
038: return "";
039: }
040:
041: int a = 2;
042: protected int b = 2;
043: static final private int u = 12 - 2;
044:
045: /** [Oct2007]: PIV2.8: 3.7sec for 1000 files, 3.9 with Stree!
046: */
047: public static void scanAllSrcsRecurse(final File root)
048: throws Exception {
049: long t0 = System.currentTimeMillis();
050: final List<File> allFiles = new ArrayList<File>();
051: FileUtils.getAllJavaFilesRecurse(root, allFiles, false, true); // ignore dirs starting with "."
052: long t1 = System.currentTimeMillis();
053: System.out.println("Collected " + allFiles.size()
054: + " files in " + (t1 - t0) + " ms");
055:
056: int i = 0;
057: // WARM - UP for the JVM
058: t0 = System.currentTimeMillis();
059: for (File fi : allFiles) // trick to allow jvm to optimize
060: {
061: final CompilationUnit cun = JavaParser.parse(fi);
062: TNode tr = createSimplifiedTree(cun);
063: if (i == 100)
064: break;
065: }
066: t1 = System.currentTimeMillis();
067: System.out.println("Scanned 100 files in " + (t1 - t0)
068: + " ms (first VM preparation step to allow optims)");
069: System.out.println("maxm=" + Runtime.getRuntime().maxMemory()); // 65M if not more set with -Xmx
070: System.out.println("free=" + Runtime.getRuntime().freeMemory());
071: System.out
072: .println("tota=" + Runtime.getRuntime().totalMemory());
073:
074: // Measurement:
075: //
076: i = 0;
077: t0 = System.currentTimeMillis();
078: for (File fi : allFiles) {
079: i++;
080: //if(i==10) break; // when using -agentlib:hprof=cpu=times,lineno=y,file=../.tide/profiler/japa.dev.Test.cpu_times.hprof,depth=8,cutoff=0.0001
081:
082: if (i % 100 == 0)
083: System.out.println("" + i + " parsed so far");
084: final CompilationUnit cun = JavaParser.parse(fi);
085: TNode tr = createSimplifiedTree(cun);
086: }
087: t1 = System.currentTimeMillis();
088: System.out.println("\nScanned " + allFiles.size()
089: + " files in " + (t1 - t0) + " ms");
090: System.out.println("maxm=" + Runtime.getRuntime().maxMemory());
091: System.out.println("free=" + Runtime.getRuntime().freeMemory());
092: System.out
093: .println("tota=" + Runtime.getRuntime().totalMemory());
094:
095: }
096:
097: public static void main(final String[] args) throws Exception {
098: //scanAllSrcsRecurse(new File("c:/sources/other/script/src"));
099: final CompilationUnit cun = JavaParser.parse(new File(
100: "c:/sources/other/script/src/japa/dev/Test.java"));
101: //new File("c:/projects/tide/src/japa/dev/Test.java"));
102: /*
103: if(cun.pakage==null) System.out.println("pack: root package");
104: else System.out.println("pack: "+ cun.pakage.name);
105: //System.out.println("imp: "+cun.imports);
106:
107: for(final TypeDeclaration ti : cun.types)
108: {
109: System.out.println("ti: "+ti.name);
110: for(final BodyDeclaration bi : ti.members) {
111: if(bi instanceof AnnotationMemberDeclaration) {}
112: else if(bi instanceof ConstructorDeclaration) {}
113: else if(bi instanceof EmptyMemberDeclaration) {}
114: else if(bi instanceof EnumConstantDeclaration) {}
115: else if(bi instanceof FieldDeclaration) {}
116: else if(bi instanceof InitializerDeclaration) {}
117: else if(bi instanceof MethodDeclaration) {}
118: else if(bi instanceof TypeDeclaration)
119: {
120: System.out.println(" type: "+bi);
121: }
122: }
123: }*/
124:
125: ASTUtils.getDeepestTypeAt(cun, 0, 0);
126:
127: EventQueue.invokeLater(new Runnable() {
128: public void run() {
129:
130: TNode root = createSimplifiedTree(cun);
131: JTree tree = new JTree(root);
132: makeFirstLevelVisible(root, tree);
133: tree.setCellRenderer(new SyntaxTreeRenderer());
134: tree.setRootVisible(false);
135: JFrame fr = new JFrame("test");
136: fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
137: fr.add(new JScrollPane(tree), BorderLayout.CENTER);
138: fr.setSize(400, 700);
139: fr.setVisible(true);
140: }
141: });
142:
143: }
144:
145: public static void makeFirstLevelVisible(final TNode root,
146: final JTree tree) {
147: for (int i = 0; i < root.getChildCount(); i++) {
148: final TNode tn = (TNode) root.getChildAt(i);
149: if (tn.getChildCount() > 0) {
150: EventQueue.invokeLater(new Runnable() {
151: public void run() {
152: tree.expandPath(new TreePath(tn.getPath())); // Trick: expand the "parent" of the node to be visible...
153: }
154: });
155: }
156: }
157: }
158:
159: /** For the view.
160: */
161: public static TNode createSimplifiedTree(final CompilationUnit cun) {
162: TNode root = new TNode("root", null, 0, "", "", 0);
163: if (cun.types == null)
164: return root; // can occur.
165:
166: for (final TypeDeclaration ti : cun.types) {
167: addType(root, root, "", ti);
168: }
169: return root;
170: }
171:
172: public static void addType(final TNode root, final TNode parent,
173: final String parentTypeName, final TypeDeclaration ti) {
174: if (ti instanceof AnnotationDeclaration) {
175: AnnotationDeclaration ade = (AnnotationDeclaration) ti;
176: String name = (parentTypeName.length() > 0 ? parentTypeName
177: + "." + ade.name : ade.name);
178: //System.out.println("ADE "+name);
179:
180: TNode ni = TNode.createForClass(appendModShort(
181: new StringBuilder("A"), ade.modifiers).toString(),
182: name, ade, ade.modifiers);
183: parent.add(ni);
184: // members
185: createMembersSimplifiedTree(root, ni, name, ade);
186: } else if (ti instanceof ClassOrInterfaceDeclaration) {
187:
188: ClassOrInterfaceDeclaration cid = (ClassOrInterfaceDeclaration) ti;
189: String name = (parentTypeName.length() > 0 ? parentTypeName
190: + "." + cid.name : cid.name);
191: if (cid.isInterface) {
192: StringBuilder iname = new StringBuilder();
193: //appendMainModLong(iname, ti.modifiers);
194: iname.append(name);
195: if (cid.typeParameters != null) {
196: toStringTypeParams(iname, cid.typeParameters);
197: }
198: appendExtendsImplementsStr(iname, cid);
199: TNode ni = TNode.createForClass(appendModShort(
200: new StringBuilder("I"), cid.modifiers)
201: .toString(), iname.toString(), cid,
202: cid.modifiers); //
203: parent.add(ni);
204: createMembersSimplifiedTree(root, ni, name, cid);
205: } else {
206: StringBuilder cname = new StringBuilder();
207: //appendMainModLong(cname, ti.modifiers);
208: cname.append(name);
209: if (cid.typeParameters != null) {
210: toStringTypeParams(cname, cid.typeParameters);
211: }
212: appendExtendsImplementsStr(cname, cid);
213: TNode ni = TNode.createForClass(appendModShort(
214: new StringBuilder("K"), cid.modifiers)
215: .toString(), cname.toString(), cid,
216: cid.modifiers); // TODO: mark public classes specially.
217: parent.add(ni);
218: createMembersSimplifiedTree(root, ni, name, cid);
219: }
220: } else if (ti instanceof EmptyTypeDeclaration) {
221: //System.out.println("ETD"); // For example when a semicolon ends a file after the last "}"
222: // => Unuseful semicolon ! (TODO)
223: } else if (ti instanceof EnumDeclaration) {
224: EnumDeclaration end = (EnumDeclaration) ti;
225: String name = (parentTypeName.length() > 0 ? parentTypeName
226: + "." + end.name : end.name);
227: //System.out.println("END");
228: TNode ni = new TNode(name, end, end.modifiers,
229: appendModShort(new StringBuilder("E"),
230: end.modifiers).toString(), name, 0); //
231: parent.add(ni);
232: createMembersSimplifiedTree(root, ni, name, end);
233:
234: for (final EnumConstantDeclaration ei : end.entries) {
235: TNode eni = new TNode(ei.name, ei, 0, "F", ei.name, 0); //
236: ni.add(eni); // keep decl order !
237: }
238: }
239: }
240:
241: public static StringBuilder appendExtendsImplementsStr(
242: StringBuilder sb, final ClassOrInterfaceDeclaration cid) {
243: if (cid.extendsList != null) {
244: sb.append(" extends ");
245: boolean has = false;
246: for (ClassOrInterfaceType ei : cid.extendsList) {
247: if (has)
248: sb.append(", ");
249: sb.append(ei.name);
250: has = true;
251: }
252: }
253: if (cid.implements List != null) {
254: sb.append(" implements ");
255: boolean has = false;
256: for (ClassOrInterfaceType ei : cid.implements List) {
257: if (has)
258: sb.append(", ");
259: sb.append(ei.name);
260: has = true;
261: }
262: }
263: return sb;
264: }
265:
266: /** First sort key: access mod (pub, pro, ps, pri).
267: */
268: public static void insertMemberSorted(final TNode parent,
269: final TNode child) {
270: // without sort: parent.add(child);
271: for (int i = 0; i < parent.getChildCount(); i++) {
272: TNode ci = (TNode) parent.getChildAt(i);
273: if (ci.compareTo(child) < 0) {
274: parent.insert(child, i);
275: return;
276: }
277: }
278:
279: // append at end
280: parent.add(child);
281:
282: }
283:
284: public int abc = 12;
285:
286: static final int aaaa = 1;
287:
288: /** Fields methods and recursively inner classes.
289: */
290: public static void createMembersSimplifiedTree(final TNode root,
291: final TNode parent, final String parentTypeName,
292: final TypeDeclaration td) {
293: if (td.members == null)
294: return; // occurs
295:
296: for (final BodyDeclaration bi : td.members) {
297: if (bi instanceof AnnotationMemberDeclaration) {
298: AnnotationMemberDeclaration amd = (AnnotationMemberDeclaration) bi;
299: StringBuilder fname = new StringBuilder();
300: fname.append("F"); // ?
301: //appendModShort(fname, cd.modifiers); // fys
302:
303: StringBuilder name = new StringBuilder();
304: name.append(amd.name);
305:
306: TNode ni = new TNode(name.toString(), amd,
307: amd.modifiers, fname.toString(), name
308: .toString(), 1);
309:
310: insertMemberSorted(parent, ni);
311:
312: //System.out.println("AMD");
313: } else if (bi instanceof ConstructorDeclaration) {
314: ConstructorDeclaration cd = (ConstructorDeclaration) bi;
315:
316: StringBuilder fname = new StringBuilder();
317: fname.append("C");
318: appendModShort(fname, cd.modifiers); // fys
319:
320: StringBuilder name = new StringBuilder();
321: name.append(cd.name);
322: name.append("(");
323: if (cd.parameters != null) {
324: name.append(toStringParams(cd.parameters));
325: }
326: name.append(")");
327:
328: if (cd.typeParameters != null) {
329: toStringTypeParams(name, cd.typeParameters);
330: }
331:
332: TNode ni = new TNode(name.toString(), cd, cd.modifiers,
333: fname.toString(), name.toString(), 3);
334:
335: insertMemberSorted(parent, ni);
336: } else if (bi instanceof EmptyMemberDeclaration) {
337: System.out.println("EMD in " + parent);
338: } else if (bi instanceof EnumConstantDeclaration) {
339: System.out.println("ECD");
340: } else if (bi instanceof FieldDeclaration) {
341: FieldDeclaration fi = (FieldDeclaration) bi;
342: StringBuilder fname = new StringBuilder();
343: fname.append("F");
344: appendModShort(fname, fi.modifiers); // fys
345: StringBuilder name = new StringBuilder();
346: name.append(fi.variables.get(0).id.name); // tricky... ?? has no direct name ?
347: name.append(" ");
348: name.append(fi.type);
349: TNode ni = new TNode(name.toString(), fi, fi.modifiers,
350: fname.toString(), name.toString(), 1);
351: insertMemberSorted(parent, ni);
352: } else if (bi instanceof InitializerDeclaration) {
353: //System.out.println("InitDE");
354: } else if (bi instanceof MethodDeclaration) {
355: MethodDeclaration mi = (MethodDeclaration) bi;
356:
357: StringBuilder fname = new StringBuilder();
358: fname.append("M");
359: appendModShort(fname, mi.modifiers); // fys
360:
361: StringBuilder name = new StringBuilder();
362: name.append(mi.name);
363:
364: name.append("(");
365: name.append(toStringParams(mi.parameters));
366: name.append(")");
367: if (mi.typeParameters != null) {
368: toStringTypeParams(name, mi.typeParameters);
369: }
370: if (!(mi.type instanceof VoidType)) {
371: name.append(" returns " + mi.type);
372: }
373: TNode ni = new TNode(name.toString(), mi, mi.modifiers,
374: fname.toString(), name.toString(), 2);
375: insertMemberSorted(parent, ni);
376: } else if (bi instanceof TypeDeclaration) {
377: //System.out.println(" type: "+bi);
378: //TNode tn = new TNode();
379: addType(root, root, parentTypeName,
380: (TypeDeclaration) bi);
381: }
382:
383: }
384: }
385:
386: public static String toStringParams(final List<Parameter> params) {
387: if (params == null || params.isEmpty())
388: return "";
389: final StringBuilder sb = new StringBuilder();
390: for (final Parameter pi : params) {
391: sb.append(pi.type);
392: sb.append(" ");
393: sb.append(pi.id.name);
394: sb.append(", ");
395: }
396: if (sb.length() > 2)
397: sb.setLength(sb.length() - 2);
398: return sb.toString();
399: }
400:
401: public static void toStringTypeParams(final StringBuilder sb,
402: final List<TypeParameter> params) {
403: if (params == null || params.isEmpty())
404: return;
405: sb.append("<");
406: for (final TypeParameter it : params) {
407: sb.append(it.name);
408: sb.append(", ");
409: }
410: if (!params.isEmpty())
411: sb.setLength(sb.length() - 2);
412: sb.append(">");
413: }
414:
415: /** f for final y for synchr, s for static.
416: * Used for Fields, Methods and Constructors.
417: * And types.
418: * @return the passed reference.
419: */
420: public static StringBuilder appendModShort(final StringBuilder sb,
421: final int mods) {
422: if (ModifierSet.isFinal(mods))
423: sb.append("f");
424: if (ModifierSet.isSynchronized(mods))
425: sb.append("y");
426: if (ModifierSet.isStatic(mods))
427: sb.append("s");
428: return sb;
429: }
430:
431: /** Used for toplevel classes. With space at end.
432: */
433: public static void appendMainModLong(final StringBuilder sb,
434: final int mods) {
435: if (ModifierSet.isPublic(mods))
436: sb.append("public ");
437: if (ModifierSet.isFinal(mods))
438: sb.append("final ");
439: if (ModifierSet.isStatic(mods))
440: sb.append("static ");
441: }
442:
443: static class SyntaxTreeRenderer extends DefaultTreeCellRenderer {
444: final SyntaxTreeIcon icon = new SyntaxTreeIcon();
445:
446: public SyntaxTreeRenderer() {
447: super ();
448: this .setIcon(icon);
449: }
450:
451: /*
452: public void updateUI()
453: {
454: super.updateUI();
455: // JDK bug ??
456: // not working well on theme change, the events come, but are not reflected well to parent
457: // 1) the DefaultTreeCellRenderer is not really in the tree, only his paint is called
458: // 2) on updateUI, DefaultTreeCellRenderer seems to ignore the new values
459: // (only set in the constructor, from UIManager ...
460: }*/
461:
462: @Override
463: public Component getTreeCellRendererComponent(JTree tree,
464: Object value, boolean selected, boolean expanded,
465: boolean leaf, int row, boolean hasFocus) {
466: // call super
467: JLabel comp = (JLabel) super .getTreeCellRendererComponent(
468: tree, value, selected, expanded, leaf, row,
469: hasFocus);
470: comp.setIcon(icon);
471:
472: TNode node = (TNode) value;
473:
474: icon.text = node.fatName;
475: icon.width = node.fatWidth;
476: icon.letterColor = node.color;
477: comp.setText(node.displayName);
478:
479: return comp;
480: }
481:
482: }
483:
484: final private class InnerClass {
485: public void publicMet() {
486: }
487:
488: void pascopMet() {
489: }
490:
491: private void privateMet() {
492: }
493:
494: protected void protecMet() {
495: }
496:
497: }
498:
499: /*ok
500: @interface HelloAnnot
501: {
502: String value();
503: int level();
504: java.lang.annotation.ElementType levele();
505: }*/
506:
507: interface Hello2 {
508: public void myInterfageMethod(final int aaa, final int bbb);
509:
510: int aConstant = 2;
511: }
512:
513: } // end of test
514:
515: @java.lang.SuppressWarnings("unchecked")
516: final class InnerClass22 // private not allowed here !!
517: {
518: public void publicMet() {
519: }
520:
521: void pascopMet() {
522: }
523:
524: private void privateMet() {
525: }
526:
527: protected void protecMet() {
528: final int a;
529: ActionListener acli = new ActionListener() {
530: public void actionPerformed(ActionEvent ae) {
531: System.out.println("Hello");
532: }
533: };
534:
535: final int b = 12;
536:
537: java.lang.StringBuilder sb = new java.lang.StringBuilder();
538: sb.append("Hello");
539: sb.toString();
540:
541: int[] aaa = new int[12];
542: ArrayList<String>[] abc = new ArrayList[Test.aaaa];
543:
544: // Dep: should see SimpleDocument
545: MainEditorFrame.instance.outputPanels.getExecutionOutputDoc()
546: .append("Hello");
547:
548: List<JInternalFrame> ll;
549:
550: double azt = Math.PI;
551: double azt2 = java.lang.Math.PI;
552:
553: GUIUtils.displayInDialog(null, null, null);
554:
555: new Throwable() {
556: }.printStackTrace(); // Anonymous class
557: new Exception().printStackTrace();
558: }
559:
560: }; // NULL TYPE !
561:
562: enum HelloEnum {
563: AA(1), BB(1), CC(2);
564: HelloEnum(int a) {
565: }
566:
567: int a = 17;
568: } //
569:
570: class SomeStupidInnerClass {
571: class AnotherStupidOne {
572: class AntLastButNotLeast {
573: }
574: }
575: }
576:
577: class TheEnd {
578: }
|