Menu

getCompilationUnit(i) fails

Developers
2004-03-09
2012-10-08
  • deepak_advani

    deepak_advani - 2004-03-09

    Hi,
    I want to parse all files in a folder, for which i written following code but it seems to fail particularly the getCompilationUnit (i). Any ideas what am i doing wrong?

    import recoder.io.SourceFileRepository;
    import recoder.convenience.Format;
    import recoder.java.CompilationUnit;
    import recoder.ParserException;
    import recoder.CrossReferenceServiceConfiguration;
    import recoder.ServiceConfiguration;
    //import recoder.service.DefaultNameInfo;
    import recoder.list.CompilationUnitList;
    import java.io.FilenameFilter;
    import java.io.File;
    import java.io.IOException;
    //import recoder.list.PackageList;

    public class Main {

            public static void main(String args[]) {

            CrossReferenceServiceConfiguration xref = new CrossReferenceServiceConfiguration();
           
            ServiceConfiguration serviceConfig = xref;
           
            SourceFileRepository sfr = serviceConfig.getSourceFileRepository();

            File dir = new File (args[0]);
            FilenameFilter filenamefilter = new JavaSrcFilter();
         
            System.out.println(args[0]);
           
            String files[] = dir.list(filenamefilter);
            CompilationUnitList cuList = null;
            try {
                    cuList = sfr.getCompilationUnitsFromFiles(files);
                   
                    sfr.getAllCompilationUnitsFromPath();       
            } catch (ParserException e) {
                    // TODO Auto-generated catch block
                    System.out.println("dfd");
                    e.printStackTrace();
                    //System.exit(999);
            }

            for (int i = 0; i < cuList.size(); i++)
             {
                    CompilationUnit cu = cuList.getCompilationUnit(i);
                    try {
                            System.out.println(Format.toString("%u [%f]", cu));
                            serviceConfig.getSourceFileRepository().print(cu);
                } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                }
            }
        }
    }

    class JavaSrcFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".java"));
        }
    }

    Thanks,

    Deepak

     
    • Angelo Schneider

      I think the compilation simply fails, because the packages likely do not fit.

      Suppose you want to compile a java class called "som.sun.Test", then this class is in the file "Test.java", down in the directory ./com/sun/

      To compile it you say: javac com/sun/Test.java

      RECODER has the same behaviour. CDing to ./com/sun and saying javac Test.java will give an error. The happends for recoder.

      Just go the appropriated directories up, and fix the file names in a way that the extra path info like "C:\myfiles" is removed.

      Hope that helps,
         Angelo

       
      • deepak_advani

        deepak_advani - 2004-03-21

        Thanks Angelo. Already figured that bit out though i have new problem. Is there a way to determine which units (packages,classes, methods, and so on) refer to or depend on, if u will, a particular class? For example, suppose we want to determine which units are dependent on class ClassMain. This class may be extended as well as internally referred to by other classes say for defining fields or may be within a particular method.

        I know of methods such as getSuperTypes, getSubTypes, getTypes. Of which, none seems to be promising for getting "units that depend on our target class".

        The other thing that is a bit effy is dynamic navigation of AST Trees built of the source code using recoder. Is there a manual on navigation and manipulation of ast trees?

        Thanks.

        Deepak

         
    • deepak_advani

      deepak_advani - 2004-03-21

      Angelo,
      Please let me know if you would accept any help in regards to recoder documentation. IMO, recoder is brilliant peace work but needs more explanation via short examples on the forum. If at all i could help in anyway to promote its use or in development, please do let me know.

      I want to use recoder to build refactoring plugin.   

      Deepak Advani

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.