001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.io.BufferedReader;
024: import java.io.File;
025: import java.io.FileReader;
026: import java.io.IOException;
027: import java.io.Reader;
028: import java.io.StreamTokenizer;
029:
030: import java.util.HashSet;
031: import java.util.Set;
032:
033: /**
034: * <a href="ClassUtil.java.html"><b><i>View Source</i></b></a>
035: *
036: * @author Brian Wing Shun Chan
037: *
038: */
039: public class ClassUtil {
040:
041: public static Set getClasses(File file) throws IOException {
042: String fileName = file.getName();
043:
044: if (fileName.endsWith(".java")) {
045: fileName = fileName.substring(0, fileName.length() - 5);
046: }
047:
048: return getClasses(new FileReader(file), fileName);
049: }
050:
051: public static Set getClasses(Reader reader, String className)
052: throws IOException {
053:
054: Set classes = new HashSet();
055:
056: StreamTokenizer st = new StreamTokenizer(new BufferedReader(
057: reader));
058:
059: st.resetSyntax();
060: st.slashSlashComments(true);
061: st.slashStarComments(true);
062: st.wordChars('a', 'z');
063: st.wordChars('A', 'Z');
064: st.wordChars('.', '.');
065: st.wordChars('0', '9');
066: st.wordChars('_', '_');
067: st.lowerCaseMode(false);
068: st.eolIsSignificant(false);
069: st.quoteChar('"');
070: st.quoteChar('\'');
071: st.parseNumbers();
072:
073: while (st.nextToken() != StreamTokenizer.TT_EOF) {
074: if (st.ttype == StreamTokenizer.TT_WORD) {
075: if (st.sval.equals("class")
076: || st.sval.equals("interface")) {
077: break;
078: }
079: }
080: }
081:
082: while (st.nextToken() != StreamTokenizer.TT_EOF) {
083: if (st.ttype == StreamTokenizer.TT_WORD) {
084: if (st.sval.indexOf('.') >= 0) {
085: classes.add(st.sval.substring(0, st.sval
086: .indexOf('.')));
087: } else {
088: classes.add(st.sval);
089: }
090: } else if (st.ttype != StreamTokenizer.TT_NUMBER
091: && st.ttype != StreamTokenizer.TT_EOL) {
092:
093: if (Character.isUpperCase((char) st.ttype)) {
094: classes.add(String.valueOf((char) st.ttype));
095: }
096: }
097: }
098:
099: classes.remove(className);
100:
101: return classes;
102: }
103:
104: public static boolean isSubclass(Class a, Class b) {
105: if (a == b) {
106: return true;
107: }
108:
109: if (a == null || b == null) {
110: return false;
111: }
112:
113: for (Class x = a; x != null; x = x.getSuperclass()) {
114: if (x == b) {
115: return true;
116: }
117:
118: if (b.isInterface()) {
119: Class[] interfaces = x.getInterfaces();
120:
121: for (int i = 0; i < interfaces.length; i++) {
122: if (isSubclass(interfaces[i], b)) {
123: return true;
124: }
125: }
126: }
127: }
128:
129: return false;
130: }
131:
132: public static boolean isSubclass(Class a, String s) {
133: if (a == null || s == null) {
134: return false;
135: }
136:
137: if (a.getName().equals(s)) {
138: return true;
139: }
140:
141: for (Class x = a; x != null; x = x.getSuperclass()) {
142: if (x.getName().equals(s)) {
143: return true;
144: }
145:
146: Class[] interfaces = x.getInterfaces();
147:
148: for (int i = 0; i < interfaces.length; i++) {
149: if (isSubclass(interfaces[i], s)) {
150: return true;
151: }
152: }
153: }
154:
155: return false;
156: }
157:
158: }
|