🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8017

Re: Gettging all classes in a package ?

#8017
From: Mikael Fransson
Date: Thu, 26 Apr 2001 15:01
184 lines
6219 bytes
now i done it thanks for your help !!!!

code below:
imports needed:

import java.util.Enumeration;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.zip.*;
import java.util.jar.*;
import java.io.File;


public static HashSet getclassesInPackage( String pkg )
 {
  HashSet v = new HashSet();
  try
  {
   char fileseparator = java.io.File.separator.charAt( 0 );
   String pkgfs = pkg.replace( '.', '/' );
   String pkgbs = pkg.replace( '.', '\\' );
   pkg = pkg.replace( '.', fileseparator );

   java.util.Properties prop = System.getProperties();

   String str = prop.getProperty( "java.class.path" );
   StringTokenizer steTok = new StringTokenizer( str, ";" );
   while ( steTok.hasMoreElements() )
   {
    String tempStr = steTok.nextToken();
    if ( tempStr.toLowerCase().trim().endsWith( ".zip" ) )
    {
     ZipFile file = null;
     try
     {
      file = new ZipFile( tempStr );
     }
     catch ( Exception ex )
     {
      System.out.println( "faild:" + tempStr );
     }
     if ( file != null )
     {
      Enumeration enum = file.entries();
      while ( enum.hasMoreElements() )
      {
       ZipEntry filefound = ( ZipEntry ) enum.nextElement();
       String foundClassName = filefound.getName();
       if ( foundClassName.startsWith( pkgbs ) ||
foundClassName.startsWith( pkgfs ) )
       {
        if ( foundClassName.toLowerCase().endsWith( ".class" ) )
        {
         int start = foundClassName.lastIndexOf( "/" );
         if ( start == -1 )
         {
          start = foundClassName.lastIndexOf( "\\" );
         }
         foundClassName = foundClassName.substring( start + 1,
foundClassName.length() - 6 );
         v.add( foundClassName );
        }
       }
      }
     }
    }
    else if ( tempStr.toLowerCase().trim().endsWith( ".jar" ) )
    {
     JarFile file = null;
     try
     {
      file = new JarFile( tempStr );
     }
     catch ( Exception ex )
     {
      System.out.println( "faild:" + tempStr );
     }
     if ( file != null )
     {
      Enumeration enum = file.entries();
      while ( enum.hasMoreElements() )
      {
       JarEntry filefound = ( JarEntry ) enum.nextElement();
       String foundClassName = filefound.getName();
       if ( foundClassName.startsWith( pkgbs ) ||
foundClassName.startsWith( pkgfs ) )
       {
        if ( foundClassName.toLowerCase().endsWith( ".class" ) )
        {
         int start = foundClassName.lastIndexOf( "/" );
         if ( start == -1 )
         {
          start = foundClassName.lastIndexOf( "\\" );
         }
         foundClassName = foundClassName.substring( start + 1,
foundClassName.length() - 6 );
         v.add( foundClassName );
        }
       }
      }
     }
    }
    else
    {
     File file = new File( tempStr + fileseparator + pkg );
     if ( file.exists() )
     {
      if ( file.isDirectory() )
      {
       String[] classnames = file.list();
       for ( int i = 0; i < classnames.length; i++ )
       {
        String foundClassName = classnames[i];
        if ( foundClassName.toLowerCase().endsWith( ".class" ) )
        {
         int start = foundClassName.lastIndexOf( "/" );
         if ( start == -1 )
         {
          start = foundClassName.lastIndexOf( "\\" );
         }
         foundClassName = foundClassName.substring( start + 1,
foundClassName.length() - 6 );
         v.add( foundClassName );
        }
       }
      }
     }
    }
   }
  }
  catch ( Exception ex )
  {
   ex.printStackTrace( System.out );
  }
  return v;
 }




"Joi Ellis [TeamB]" <joi@aravox.com> wrote in message
news:3ae82f86_1@dnews...
> Mikael Fransson <mikael.fransson@aspiro.com> wrote:
> > I think you missed my point...
>
> > I'm going to write a class tester for JUnit where the only thing I have
is a
> > package name
> > and I know that there are a lot of classes with only setters and getters
and
> > ofcorse a constructor.
>
> > I have finished the test code for all the setters and getters but now I
need
> > some thing to collect the classes for me.
> > its not necessary that I get them as Class, if I get the names I can
create
> > the Class object my self.
>
>
> Didn't someone already just announce this support for JUnit?  I know there
is
> one that works based on a specified source tree of tests.
>
> Keep in mind that when you're querying the JVM, you're seeing only the
classes
> which the JVM itself has loaded.  You aren't seeing everything that
possibly
> exists out in the CLASSPATH but which hasn't been used.
>
> Anyway, what you need to do is loop through the CLASSPATH looking for
> directories and jars which contain that package name.  You have to look at
the
> disk for directories, and open the jar files yourself.  Once you find the
package
> in something, read in its contents.  You have to do this for every item in
the
> CLASSPATH because a package can exist in multiple jars and/or directories.
>
> --
> Joi Ellis (TeamB)
> No direct email, please.
> For best results, post on news server newsgroups.borland.com!
> Newsgroup Info: http://www.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <3ae8386e$2_2@dnews>
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!newsfeed.cwix.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3ae69dfa$1_2@dnews> <3ae7c298$1_2@dnews> <3ae8066d$1_2@dnews> <3ae82f86_1@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,e2562564d66f3419
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-04-26 08:20:04 PST
From: "Mikael Fransson" <mikael.fransson@aspiro.com>
Newsgroups: borland.public.jbuilder.java.language
References: <3ae69dfa$1_2@dnews>  <3ae7c298$1_2@dnews> <3ae8066d$1_2@dnews> <3ae82f86_1@dnews>
Subject: Re: Gettging all classes in a package ?
Date: Thu, 26 Apr 2001 17:01:15 +0200
Lines: 186
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 193.15.113.253
Message-ID: <3ae8386e$2_2@dnews>
X-Trace: dnews 988297326 193.15.113.253 (26 Apr 2001 08:02:06 -0700)
Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!newsswitch.lcs.mit.edu!newsfeed.cwix.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews