1
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
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 }