001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    
022    import java.io.File;
023    import java.io.FileReader;
024    import java.io.IOException;
025    import java.io.Reader;
026    import java.io.StreamTokenizer;
027    
028    import java.net.URI;
029    import java.net.URISyntaxException;
030    import java.net.URL;
031    
032    import java.util.ArrayList;
033    import java.util.HashSet;
034    import java.util.List;
035    import java.util.Set;
036    import java.util.regex.Matcher;
037    import java.util.regex.Pattern;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Sandeep Soni
042     */
043    public class ClassUtil {
044    
045            public static Set<String> getClasses(File file) throws IOException {
046                    String fileName = file.getName();
047    
048                    if (fileName.endsWith(".java")) {
049                            fileName = fileName.substring(0, fileName.length() - 5);
050                    }
051    
052                    return getClasses(
053                            new UnsyncBufferedReader(new FileReader(file)), fileName);
054            }
055    
056            public static Set<String> getClasses(Reader reader, String className)
057                    throws IOException {
058    
059                    Set<String> classes = new HashSet<String>();
060    
061                    StreamTokenizer st = new StreamTokenizer(reader);
062    
063                    _setupParseTableForAnnotationProcessing(st);
064    
065                    while (st.nextToken() != StreamTokenizer.TT_EOF) {
066                            if (st.ttype == StreamTokenizer.TT_WORD) {
067                                    if (st.sval.equals("class") || st.sval.equals("enum") ||
068                                            st.sval.equals("interface") ||
069                                            st.sval.equals("@interface")) {
070    
071                                            break;
072                                    }
073                                    else if (st.sval.startsWith("@")) {
074                                            st.ordinaryChar(' ');
075                                            st.wordChars('=', '=');
076    
077                                            String[] annotationClasses = _processAnnotation(
078                                                    st.sval, st);
079    
080                                            for (String annotationClass : annotationClasses) {
081                                                    classes.add(annotationClass);
082                                            }
083    
084                                            _setupParseTableForAnnotationProcessing(st);
085                                    }
086                            }
087                    }
088    
089                    _setupParseTable(st);
090    
091                    while (st.nextToken() != StreamTokenizer.TT_EOF) {
092                            if (st.ttype == StreamTokenizer.TT_WORD) {
093                                    if (st.sval.indexOf('.') >= 0) {
094                                            classes.add(st.sval.substring(0, st.sval.indexOf('.')));
095                                    }
096                                    else {
097                                            classes.add(st.sval);
098                                    }
099                            }
100                            else if ((st.ttype != StreamTokenizer.TT_NUMBER) &&
101                                             (st.ttype != StreamTokenizer.TT_EOL)) {
102    
103                                    if (Character.isUpperCase((char)st.ttype)) {
104                                            classes.add(String.valueOf((char)st.ttype));
105                                    }
106                            }
107                    }
108    
109                    classes.remove(className);
110    
111                    return classes;
112            }
113    
114            public static String getClassName(Object object) {
115                    if (object == null) {
116                            return null;
117                    }
118    
119                    Class<?> clazz = object.getClass();
120    
121                    return clazz.getName();
122            }
123    
124            public static String getParentPath(
125                    ClassLoader classLoader, String className) {
126    
127                    if (_log.isDebugEnabled()) {
128                            _log.debug("Class name " + className);
129                    }
130    
131                    if (!className.endsWith(_CLASS_EXTENSION)) {
132                            className += _CLASS_EXTENSION;
133                    }
134    
135                    className = StringUtil.replace(
136                            className, CharPool.PERIOD, CharPool.SLASH);
137    
138                    className = StringUtil.replace(className, "/class", _CLASS_EXTENSION);
139    
140                    URL url = classLoader.getResource(className);
141    
142                    String path = null;
143    
144                    try {
145                            path = url.getPath();
146    
147                            URI uri = new URI(path);
148    
149                            String scheme = uri.getScheme();
150    
151                            if (path.contains(StringPool.EXCLAMATION) &&
152                                    ((scheme == null) || (scheme.length() <= 1))) {
153    
154                                    if (!path.startsWith(StringPool.SLASH)) {
155                                            path = StringPool.SLASH + path;
156                                    }
157                            }
158                            else {
159                                    path = uri.getPath();
160    
161                                    if (path == null) {
162                                            path = url.getFile();
163                                    }
164                            }
165                    }
166                    catch (URISyntaxException urise) {
167                            path = url.getFile();
168                    }
169    
170                    if (ServerDetector.isJBoss()) {
171                            if (path.startsWith("file:") && !path.startsWith("file:/")) {
172                                    path = path.substring(5);
173    
174                                    path = "file:/".concat(path);
175    
176                                    path = StringUtil.replace(path, "%5C", StringPool.SLASH);
177                            }
178                    }
179    
180                    if (_log.isDebugEnabled()) {
181                            _log.debug("Path " + path);
182                    }
183    
184                    int pos = path.indexOf(className);
185    
186                    String parentPath = path.substring(0, pos);
187    
188                    if (parentPath.startsWith("jar:")) {
189                            parentPath = parentPath.substring(4);
190                    }
191    
192                    if (parentPath.startsWith("file:/")) {
193                            parentPath = parentPath.substring(6);
194                    }
195    
196                    if (_log.isDebugEnabled()) {
197                            _log.debug("Parent path " + parentPath);
198                    }
199    
200                    return parentPath;
201            }
202    
203            public static boolean isSubclass(Class<?> a, Class<?> b) {
204                    if (a == b) {
205                            return true;
206                    }
207    
208                    if ((a == null) || (b == null)) {
209                            return false;
210                    }
211    
212                    for (Class<?> x = a; x != null; x = x.getSuperclass()) {
213                            if (x == b) {
214                                    return true;
215                            }
216    
217                            if (b.isInterface()) {
218                                    Class<?>[] interfaceClasses = x.getInterfaces();
219    
220                                    for (Class<?> interfaceClass : interfaceClasses) {
221                                            if (isSubclass(interfaceClass, b)) {
222                                                    return true;
223                                            }
224                                    }
225                            }
226                    }
227    
228                    return false;
229            }
230    
231            public static boolean isSubclass(Class<?> a, String s) {
232                    if ((a == null) || (s == null)) {
233                            return false;
234                    }
235    
236                    if (a.getName().equals(s)) {
237                            return true;
238                    }
239    
240                    for (Class<?> x = a; x != null; x = x.getSuperclass()) {
241                            if (x.getName().equals(s)) {
242                                    return true;
243                            }
244    
245                            Class<?>[] interfaceClasses = x.getInterfaces();
246    
247                            for (Class<?> interfaceClass : interfaceClasses) {
248                                    if (isSubclass(interfaceClass, s)) {
249                                            return true;
250                                    }
251                            }
252                    }
253    
254                    return false;
255            }
256    
257            private static String[] _processAnnotation(String s, StreamTokenizer st)
258                    throws IOException {
259    
260                    s = s.trim();
261    
262                    List<String> tokens = new ArrayList<String>();
263    
264                    Matcher annotationNameMatcher = _ANNOTATION_NAME_REGEXP.matcher(s);
265                    Matcher annotationParametersMatcher =
266                            _ANNOTATION_PARAMETERS_REGEXP.matcher(s);
267    
268                    if (annotationNameMatcher.matches()) {
269                            String annotationName = annotationNameMatcher.group();
270    
271                            tokens.add(annotationName.replace("@", ""));
272                    }
273                    else if (annotationParametersMatcher.matches()) {
274                            if (!s.trim().endsWith(")")) {
275                                    while (st.nextToken() != StreamTokenizer.TT_EOF) {
276                                            if (st.ttype == StreamTokenizer.TT_WORD) {
277                                                    s += st.sval;
278    
279                                                    if (s.trim().endsWith(")")) {
280                                                            break;
281                                                    }
282                                            }
283                                    }
284                            }
285    
286                            annotationParametersMatcher = _ANNOTATION_PARAMETERS_REGEXP.matcher(
287                                    s);
288    
289                            if (annotationParametersMatcher.matches()) {
290                                    String annotationName = annotationParametersMatcher.group(1);
291                                    String annotationParameters = annotationParametersMatcher.group(
292                                            2);
293    
294                                    tokens.add(annotationName.replace("@", ""));
295    
296                                    tokens = _processAnnotationParameters(
297                                            annotationParameters, tokens);
298                            }
299                    }
300    
301                    return tokens.toArray(new String[tokens.size()]);
302            }
303    
304            private static List<String> _processAnnotationParameters(
305                            String s, List<String> tokens)
306                    throws IOException {
307    
308                    StreamTokenizer st = new StreamTokenizer(new UnsyncStringReader(s));
309    
310                    _setupParseTable(st);
311    
312                    while (st.nextToken() != StreamTokenizer.TT_EOF) {
313                            if (st.ttype == StreamTokenizer.TT_WORD) {
314                                    if (st.sval.indexOf('.') >= 0) {
315                                            tokens.add(st.sval.substring(0, st.sval.indexOf('.')));
316                                    }
317                                    else {
318                                            tokens.add(st.sval);
319                                    }
320                            }
321                            else if ((st.ttype != StreamTokenizer.TT_NUMBER) &&
322                                             (st.ttype != StreamTokenizer.TT_EOL)) {
323    
324                                    if (Character.isUpperCase((char)st.ttype)) {
325                                            tokens.add(String.valueOf((char)st.ttype));
326                                    }
327                            }
328                    }
329    
330                    return tokens;
331            }
332    
333            private static void _setupParseTable(StreamTokenizer st) {
334                    st.resetSyntax();
335                    st.slashSlashComments(true);
336                    st.slashStarComments(true);
337                    st.wordChars('a', 'z');
338                    st.wordChars('A', 'Z');
339                    st.wordChars('.', '.');
340                    st.wordChars('0', '9');
341                    st.wordChars('_', '_');
342                    st.lowerCaseMode(false);
343                    st.eolIsSignificant(false);
344                    st.quoteChar('"');
345                    st.quoteChar('\'');
346                    st.parseNumbers();
347            }
348    
349            private static void _setupParseTableForAnnotationProcessing(
350                    StreamTokenizer st) {
351    
352                    _setupParseTable(st);
353    
354                    st.wordChars('@', '@');
355                    st.wordChars('(', '(');
356                    st.wordChars(')', ')');
357                    st.wordChars('{', '{');
358                    st.wordChars('}', '}');
359                    st.wordChars(',',',');
360            }
361    
362            private static final Pattern _ANNOTATION_NAME_REGEXP = Pattern.compile(
363                    "@(\\w+)$");
364    
365            private static final Pattern _ANNOTATION_PARAMETERS_REGEXP =
366                    Pattern.compile("@(\\w+)\\({0,1}\\{{0,1}([^)}]+)\\}{0,1}\\){0,1}");
367    
368            private static final String _CLASS_EXTENSION = ".class";
369    
370            private static Log _log = LogFactoryUtil.getLog(ClassUtil.class);
371    
372    }