Method: main(String[])

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.utils;
23:
24: import java.io.FileInputStream;
25: import java.io.IOException;
26: import java.util.ArrayList;
27: import java.util.List;
28: import java.util.jar.JarEntry;
29: import java.util.jar.JarInputStream;
30:
31: public class PackageUtils
32: {
33:
34:         private static boolean debug = false;
35:
36:         public static List<String> getClasseNamesInPackage(String jarName,
37:                         String packageName)
38:         {
39:                 ArrayList<String> classes = new ArrayList<String>();
40:
41:                 if (packageName != null)
42:                 {
43:                         packageName = packageName.replaceAll("\\.", "/");
44:                 }
45:                 if (debug)
46:                 {
47:                         System.out.println("Jar " + jarName + " looking for " + packageName);
48:                 }
49:
50:                 JarInputStream jarFile = null;
51:                 try
52:                 {
53:                         jarFile = new JarInputStream(new FileInputStream(jarName));
54:                         JarEntry jarEntry;
55:
56:                         while (true)
57:                         {
58:                                 jarEntry = jarFile.getNextJarEntry();
59:                                 if (jarEntry == null)
60:                                 {
61:                                         break;
62:                                 }
63:                                 if ((packageName == null || jarEntry.getName().startsWith(packageName))
64:                                                 && jarEntry.getName().endsWith(".class"))
65:                                 {
66:
67:                                         String className = jarEntry.getName().replaceAll("/", "\\.");
68:                                         className = className.substring(0, className.length()
69:                                                         - ".class".length());
70:
71:                                         if (className.endsWith("$1"))
72:                                         {
73:                                                 continue;
74:                                         }
75:
76:                                         className = className.replace('$', '.');
77:
78:                                         if (debug)
79:                                         {
80:                                                 System.out.println("Found " + className);
81:                                         }
82:
83:                                         classes.add(className);
84:                                 }
85:                         }
86:                 } catch (Exception e)
87:                 {
88:                         e.printStackTrace();
89:                 } finally
90:                 {
91:                         try
92:                         {
93:                                 jarFile.close();
94:                         } catch (IOException e)
95:                         {
96:                         }
97:                 }
98:                 return classes;
99:         }
100:
101:         /**
102: *
103: */
104:         public static void main(String[] args)
105:         {
106:                 List<String> list = PackageUtils.getClasseNamesInPackage("C:\\overture\\overture_gitAST\\core\\parser\\target\\parser-2.0.1-SNAPSHOT.jar", null);
107:                 System.out.println(list);
108:                 /*
109:                  * output : Jar C:/j2sdk1.4.1_02/lib/mail.jar looking for com/sun/mail/handlers Found
110:                  * com.sun.mail.handlers.text_html.class Found com.sun.mail.handlers.text_plain.class Found
111:                  * com.sun.mail.handlers.text_xml.class Found com.sun.mail.handlers.image_gif.class Found
112:                  * com.sun.mail.handlers.image_jpeg.class Found com.sun.mail.handlers.multipart_mixed.class Found
113:                  * com.sun.mail.handlers.message_rfc822.class [com.sun.mail.handlers.text_html.class,
114:                  * com.sun.mail.handlers.text_xml.class, com .sun.mail.handlers.image_jpeg.class, ,
115:                  * com.sun.mail.handlers.message_rfc822.class]
116:                  */
117:         }
118: }