1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.FileReader;
28  import java.io.IOException;
29  import java.io.StreamTokenizer;
30  
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  /**
35   * <a href="ClassUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class ClassUtil {
41  
42      public static Set getClasses(File file) throws IOException {
43          Set classes = new HashSet();
44  
45          StreamTokenizer st = new StreamTokenizer(
46              new BufferedReader(new FileReader(file)));
47  
48          st.resetSyntax();
49          st.slashSlashComments(true);
50          st.slashStarComments(true);
51          st.wordChars('a', 'z');
52          st.wordChars('A', 'Z');
53          st.wordChars('.', '.');
54          st.wordChars('0', '9');
55          st.wordChars('_', '_');
56          st.lowerCaseMode(false);
57          st.eolIsSignificant(false);
58          st.quoteChar('"');
59          st.quoteChar('\'');
60          st.parseNumbers();
61  
62          while (st.nextToken() != StreamTokenizer.TT_EOF) {
63              if (st.ttype == StreamTokenizer.TT_WORD) {
64                  if (st.sval.equals("class") || st.sval.equals("interface")) {
65                      break;
66                  }
67              }
68          }
69  
70          while (st.nextToken() != StreamTokenizer.TT_EOF) {
71              if (st.ttype == StreamTokenizer.TT_WORD) {
72                  if (Character.isUpperCase(st.sval.charAt(0))) {
73                      if (st.sval.indexOf('.') >= 0) {
74                          classes.add(st.sval.substring(0, st.sval.indexOf('.')));
75                      }
76                      else {
77                          classes.add(st.sval);
78                      }
79                  }
80              }
81              else if (st.ttype != StreamTokenizer.TT_NUMBER &&
82                       st.ttype != StreamTokenizer.TT_EOL) {
83  
84                  if (Character.isUpperCase((char)st.ttype)) {
85                      classes.add(String.valueOf((char)st.ttype));
86                  }
87              }
88          }
89  
90          String fileName = file.getName();
91  
92          if (fileName.endsWith(".java")) {
93              fileName = fileName.substring(0, fileName.length() - 5);
94          }
95  
96          classes.remove(fileName);
97  
98          return classes;
99      }
100 
101     public static boolean isSubclass(Class a, Class b) {
102         if (a == b) {
103             return true;
104         }
105 
106         if (a == null || b == null) {
107             return false;
108         }
109 
110         for (Class x = a; x != null; x = x.getSuperclass()) {
111             if (x == b) {
112                 return true;
113             }
114 
115             if (b.isInterface()) {
116                 Class[] interfaces = x.getInterfaces();
117 
118                 for (int i = 0; i < interfaces.length; i++) {
119                     if (isSubclass(interfaces[i], b)) {
120                         return true;
121                     }
122                 }
123             }
124         }
125 
126         return false;
127     }
128 
129     public static boolean isSubclass(Class a, String s) {
130         if (a == null || s == null) {
131             return false;
132         }
133 
134         if (a.getName().equals(s)) {
135             return true;
136         }
137 
138         for (Class x = a; x != null; x = x.getSuperclass()) {
139             if (x.getName().equals(s)) {
140                 return true;
141             }
142 
143             Class[] interfaces = x.getInterfaces();
144 
145             for (int i = 0; i < interfaces.length; i++) {
146                 if (isSubclass(interfaces[i], s)) {
147                     return true;
148                 }
149             }
150         }
151 
152         return false;
153     }
154 
155 }