/* * The contents of this file are subject to the terms of the Common Development * and Distribution License (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the License at http://www.netbeans.org/cddl.html * or http://www.netbeans.org/cddl.txt. * * When distributing Covered Code, include this CDDL Header Notice in each file * and include the License file at http://www.netbeans.org/cddl.txt. * If applicable, add the following below the CDDL Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Portions Copyrighted 2007 Sun Microsystems, Inc. */ package org.netbeans.api.java.source; import com.sun.source.tree.BlockTree; import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.StatementTree; import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; import com.sun.source.util.TreePath; import com.sun.source.util.TreePathScanner; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.List; import junit.framework.TestCase; import org.netbeans.api.java.source.CancellableTask; import org.netbeans.api.java.source.CompilationController; import org.netbeans.api.java.source.JavaSource.Phase; import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; import org.openide.filesystems.LocalFileSystem; /** * * @author Tim Boudreau */ public class TreePathHandleTest extends TestCase { private FileObject src; private FileObject data; public TreePathHandleTest(String nm) { super (nm); } private File dataDir; private File rootDir; private LocalFileSystem fs; protected void setUp() throws Exception { super.setUp(); File tmp = new File (System.getProperty("java.io.tmpdir")); String dirName = getClass().getName().replace(".", "_") + System.currentTimeMillis(); String dataFileName = "Test"; rootDir = new File (tmp, dirName); int ix = 0; while (rootDir.exists()) { String nm = dirName + (ix++); rootDir = new File (tmp, nm); } rootDir.mkdir(); dataDir = new File (rootDir, "src"); dataDir.mkdir(); File userDir = new File (rootDir, "userdir"); userDir.mkdir(); File javasource = new File (dataDir, dataFileName + ".java"); if (!javasource.createNewFile()) { fail ("Could not create " + javasource); } OutputStream out = new FileOutputStream (javasource); try { PrintWriter pw = new PrintWriter ( new OutputStreamWriter (out)); try { pw.println ("public class Test {\n" + " public int foo() {\n" + " int x = 0;" + " int y = 1;" + " int z = x + y;" + " return z;" + " }" + "}"); } finally { pw.close (); } } finally { out.close(); } assertTrue (dataDir.exists()); assertTrue (userDir.exists()); fs = new LocalFileSystem(); fs.setRootDirectory(dataDir); src = fs.getRoot().getFileObject(dataFileName + ".java"); //XXX otherwise error in TreePathHandle src = FileUtil.toFileObject (FileUtil.normalizeFile(javasource)); //A butt-ugly hack to fake out the parser System.setProperty ("netbeans.user", userDir.getPath()); } protected void tearDown() throws Exception { super.tearDown(); fs = null; recurseDel (rootDir); } private static void recurseDel (File f) { try { if (f.isDirectory()) { File[] ff = f.listFiles(); for (int i = 0; i < ff.length; i++) { recurseDel (ff[i]); } } } catch (Exception ioe) { ioe.printStackTrace(); } f.delete(); } public void testStatementHandles() throws Exception { System.out.println("Test Statement Handles"); final JavaSource js = JavaSource.forFileObject(src); CancellableTask task = new CancellableTask () { public void run(CompilationController info) throws Exception { info.toPhase(Phase.RESOLVED); CompilationUnitTree tree = info.getCompilationUnit(); assertEquals("Wrong file object returned: " + info.getFileObject(), src, info.getFileObject()); assertNotNull ("Compilation tree is null", tree); BlockTree bt = new BlockTreeFinder().scan(tree, null); assertNotNull ("Could not find block tree in method in " + tree, bt); List l = bt.getStatements(); TreePath parentPath = TreePath.getPath(tree, bt); System.out.println("Check " + l.size() + " statements " + l); for (StatementTree st : l) { TreePath path = TreePath.getPath(parentPath, st); assertNotNull (path.getLeaf()); assertEquals (st, path.getLeaf()); TreePathHandle handle = TreePathHandle.create(path, info); assertEquals (src, handle.getFileObject()); assertNotNull (handle.resolve(info)); assertNotNull (handle.resolve(info).getLeaf()); assertEquals (st, handle.resolve(info).getLeaf()); } } public void cancel() { } }; js.runUserActionTask(task, true); } private static class BlockTreeFinder extends TreePathScanner { private boolean inMethod; @Override public BlockTree scan(Tree tree, Void x) { if (tree != null && tree.getKind() == Kind.METHOD) { inMethod = true; try { return super.scan(tree, x); } finally { inMethod = false; } } if (inMethod && tree != null && tree.getKind() == Kind.BLOCK && tree instanceof BlockTree) { BlockTree bt = (BlockTree) tree; return bt; } return super.scan(tree, x); } } }