001    /**
002     * Copyright (c) 2000-present 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.tools.sourceformatter;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    /**
024     * @author Hugo Huijser
025     */
026    public class JavaTerm {
027    
028            public static final int TYPE_CLASS_PRIVATE = 24;
029    
030            public static final int TYPE_CLASS_PRIVATE_STATIC = 23;
031    
032            public static final int TYPE_CLASS_PROTECTED = 16;
033    
034            public static final int TYPE_CLASS_PROTECTED_STATIC = 15;
035    
036            public static final int TYPE_CLASS_PUBLIC = 8;
037    
038            public static final int TYPE_CLASS_PUBLIC_STATIC = 7;
039    
040            public static final int TYPE_CONSTRUCTOR_PRIVATE = 18;
041    
042            public static final int TYPE_CONSTRUCTOR_PROTECTED = 10;
043    
044            public static final int TYPE_CONSTRUCTOR_PUBLIC = 4;
045    
046            public static final int TYPE_METHOD_PRIVATE = 19;
047    
048            public static final int TYPE_METHOD_PRIVATE_STATIC = 17;
049    
050            public static final int TYPE_METHOD_PROTECTED = 11;
051    
052            public static final int TYPE_METHOD_PROTECTED_STATIC = 9;
053    
054            public static final int TYPE_METHOD_PUBLIC = 5;
055    
056            public static final int TYPE_METHOD_PUBLIC_STATIC = 3;
057    
058            public static final int TYPE_STATIC_BLOCK = 21;
059    
060            public static final int TYPE_VARIABLE_PRIVATE = 22;
061    
062            public static final int TYPE_VARIABLE_PRIVATE_STATIC = 20;
063    
064            public static final int TYPE_VARIABLE_PROTECTED = 14;
065    
066            public static final int TYPE_VARIABLE_PROTECTED_STATIC = 12;
067    
068            public static final int TYPE_VARIABLE_PUBLIC = 6;
069    
070            public static final int TYPE_VARIABLE_PUBLIC_STATIC = 1;
071    
072            public JavaTerm(String name, int type, String content, int lineCount) {
073                    _name = name;
074                    _type = type;
075                    _content = content;
076                    _lineCount = lineCount;
077            }
078    
079            public String getContent() {
080                    return _content;
081            }
082    
083            public int getLineCount() {
084                    return _lineCount;
085            }
086    
087            public String getName() {
088                    return _name;
089            }
090    
091            public List<String> getParameterNames() {
092                    if (_parameterNames == null) {
093                            readParameterNamesAndTypes();
094                    }
095    
096                    return _parameterNames;
097            }
098    
099            public List<String> getParameterTypes() {
100                    if (_parameterTypes == null) {
101                            readParameterNamesAndTypes();
102                    }
103    
104                    return _parameterTypes;
105            }
106    
107            public int getType() {
108                    return _type;
109            }
110    
111            public boolean isClass() {
112                    if ((_type == TYPE_CLASS_PRIVATE) ||
113                            (_type == TYPE_CLASS_PRIVATE_STATIC) ||
114                            (_type == TYPE_CLASS_PROTECTED) ||
115                            (_type == TYPE_CLASS_PROTECTED_STATIC) ||
116                            (_type == TYPE_CLASS_PUBLIC) ||
117                            (_type == TYPE_CLASS_PUBLIC_STATIC)) {
118    
119                            return true;
120                    }
121                    else {
122                            return false;
123                    }
124            }
125    
126            public boolean isConstructor() {
127                    if ((_type == TYPE_CONSTRUCTOR_PRIVATE) ||
128                            (_type == TYPE_CONSTRUCTOR_PROTECTED) ||
129                            (_type == TYPE_CONSTRUCTOR_PUBLIC)) {
130    
131                            return true;
132                    }
133                    else {
134                            return false;
135                    }
136            }
137    
138            public boolean isMethod() {
139                    if ((_type == TYPE_METHOD_PRIVATE) ||
140                            (_type == TYPE_METHOD_PRIVATE_STATIC) ||
141                            (_type == TYPE_METHOD_PROTECTED) ||
142                            (_type == TYPE_METHOD_PROTECTED_STATIC) ||
143                            (_type == TYPE_METHOD_PUBLIC) ||
144                            (_type == TYPE_METHOD_PUBLIC_STATIC)) {
145    
146                            return true;
147                    }
148                    else {
149                            return false;
150                    }
151            }
152    
153            public boolean isPrivate() {
154                    if ((_type == TYPE_CLASS_PRIVATE) ||
155                            (_type == TYPE_CLASS_PRIVATE_STATIC) ||
156                            (_type == TYPE_CONSTRUCTOR_PRIVATE) ||
157                            (_type == TYPE_METHOD_PRIVATE) ||
158                            (_type == TYPE_METHOD_PRIVATE_STATIC) ||
159                            (_type == TYPE_VARIABLE_PRIVATE) ||
160                            (_type == TYPE_VARIABLE_PRIVATE_STATIC)) {
161    
162                            return true;
163                    }
164                    else {
165                            return false;
166                    }
167            }
168    
169            public boolean isProtected() {
170                    if ((_type == TYPE_CLASS_PROTECTED) ||
171                            (_type == TYPE_CLASS_PROTECTED_STATIC) ||
172                            (_type == TYPE_CONSTRUCTOR_PROTECTED) ||
173                            (_type == TYPE_METHOD_PROTECTED) ||
174                            (_type == TYPE_METHOD_PROTECTED_STATIC) ||
175                            (_type == TYPE_VARIABLE_PROTECTED) ||
176                            (_type == TYPE_VARIABLE_PROTECTED_STATIC)) {
177    
178                            return true;
179                    }
180                    else {
181                            return false;
182                    }
183            }
184    
185            public boolean isPublic() {
186                    if ((_type == TYPE_CLASS_PUBLIC) ||
187                            (_type == TYPE_CLASS_PUBLIC_STATIC) ||
188                            (_type == TYPE_CONSTRUCTOR_PUBLIC) ||
189                            (_type == TYPE_METHOD_PUBLIC) ||
190                            (_type == TYPE_METHOD_PUBLIC_STATIC) ||
191                            (_type == TYPE_VARIABLE_PUBLIC) ||
192                            (_type == TYPE_VARIABLE_PUBLIC_STATIC)) {
193    
194                            return true;
195                    }
196                    else {
197                            return false;
198                    }
199            }
200    
201            public boolean isStatic() {
202                    if ((_type == TYPE_CLASS_PRIVATE_STATIC) ||
203                            (_type == TYPE_CLASS_PROTECTED_STATIC) ||
204                            (_type == TYPE_CLASS_PUBLIC_STATIC) ||
205                            (_type == TYPE_METHOD_PRIVATE_STATIC) ||
206                            (_type == TYPE_METHOD_PROTECTED_STATIC) ||
207                            (_type == TYPE_METHOD_PUBLIC_STATIC) ||
208                            (_type == TYPE_VARIABLE_PRIVATE_STATIC) ||
209                            (_type == TYPE_VARIABLE_PROTECTED_STATIC) ||
210                            (_type == TYPE_VARIABLE_PUBLIC_STATIC)) {
211    
212                            return true;
213                    }
214                    else {
215                            return false;
216                    }
217            }
218    
219            public boolean isVariable() {
220                    if ((_type == TYPE_VARIABLE_PRIVATE) ||
221                            (_type == TYPE_VARIABLE_PRIVATE_STATIC) ||
222                            (_type == TYPE_VARIABLE_PROTECTED) ||
223                            (_type == TYPE_VARIABLE_PROTECTED_STATIC) ||
224                            (_type == TYPE_VARIABLE_PUBLIC) ||
225                            (_type == TYPE_VARIABLE_PUBLIC_STATIC)) {
226    
227                            return true;
228                    }
229                    else {
230                            return false;
231                    }
232            }
233    
234            public void setContent(String content) {
235                    _content = content;
236            }
237    
238            public void setLineCount(int lineCount) {
239                    _lineCount = lineCount;
240            }
241    
242            public void setName(String name) {
243                    _name = name;
244            }
245    
246            public void setParameterTypes(List<String> parameterTypes) {
247                    _parameterTypes = parameterTypes;
248            }
249    
250            public void setType(int type) {
251                    _type = type;
252            }
253    
254            protected void readParameterNamesAndTypes() {
255                    _parameterNames = new ArrayList<String>();
256                    _parameterTypes = new ArrayList<String>();
257    
258                    if (!isConstructor() && !isMethod()) {
259                            return;
260                    }
261    
262                    int x = -1;
263    
264                    if (isPrivate()) {
265                            x = _content.indexOf("\tprivate ");
266                    }
267                    else if (isProtected()) {
268                            x = _content.indexOf("\tprotected ");
269                    }
270                    else if (isPublic()) {
271                            x = _content.indexOf("\tpublic ");
272                    }
273    
274                    if (x == -1) {
275                            return;
276                    }
277    
278                    x = _content.indexOf(StringPool.OPEN_PARENTHESIS, x);
279    
280                    int y = x;
281    
282                    String parameters = StringPool.BLANK;
283    
284                    while (true) {
285                            y = _content.indexOf(StringPool.CLOSE_PARENTHESIS, y + 1);
286    
287                            if (y == -1) {
288                                    return;
289                            }
290    
291                            parameters = _content.substring(x + 1, y);
292    
293                            int closeParenthesesCount = StringUtil.count(
294                                    parameters, StringPool.CLOSE_PARENTHESIS);
295                            int openParenthesesCount = StringUtil.count(
296                                    parameters, StringPool.OPEN_PARENTHESIS);
297    
298                            if (closeParenthesesCount == openParenthesesCount) {
299                                    break;
300                            }
301                    }
302    
303                    parameters = StringUtil.replace(
304                            parameters, new String[] {StringPool.TAB, StringPool.NEW_LINE},
305                            new String[] {StringPool.BLANK, StringPool.SPACE});
306    
307                    for (x = 0;;) {
308                            parameters = StringUtil.trim(parameters);
309    
310                            if (parameters.startsWith(StringPool.AT)) {
311                                    parameters = stripAnnotation(parameters);
312                            }
313    
314                            if (parameters.startsWith("final ")) {
315                                    parameters = parameters.substring(6);
316                            }
317    
318                            x = parameters.indexOf(StringPool.SPACE, x + 1);
319    
320                            if (x == -1) {
321                                    return;
322                            }
323    
324                            String parameterType = parameters.substring(0, x);
325    
326                            int greaterThanCount = StringUtil.count(
327                                    parameterType, StringPool.GREATER_THAN);
328                            int lessThanCount = StringUtil.count(
329                                    parameterType, StringPool.LESS_THAN);
330    
331                            if (greaterThanCount != lessThanCount) {
332                                    continue;
333                            }
334    
335                            _parameterTypes.add(parameterType);
336    
337                            y = parameters.indexOf(StringPool.COMMA, x);
338    
339                            if (y == -1) {
340                                    _parameterNames.add(parameters.substring(x + 1));
341    
342                                    return;
343                            }
344    
345                            _parameterNames.add(parameters.substring(x + 1, y));
346    
347                            parameters = parameters.substring(y + 1);
348    
349                            x = 0;
350                    }
351            }
352    
353            protected String stripAnnotation(String parameters) {
354                    int pos = -1;
355    
356                    while (true) {
357                            pos = parameters.indexOf(StringPool.SPACE, pos + 1);
358    
359                            if (pos == -1) {
360                                    return parameters;
361                            }
362    
363                            String annotation = parameters.substring(0, pos);
364    
365                            int closeParenthesesCount = StringUtil.count(
366                                    annotation, StringPool.CLOSE_PARENTHESIS);
367                            int greaterThanCount = StringUtil.count(
368                                    annotation, StringPool.GREATER_THAN);
369                            int lessThanCount = StringUtil.count(
370                                    annotation, StringPool.LESS_THAN);
371                            int openParenthesesCount = StringUtil.count(
372                                    annotation, StringPool.OPEN_PARENTHESIS);
373    
374                            if ((closeParenthesesCount == openParenthesesCount) &&
375                                    (greaterThanCount == lessThanCount)) {
376    
377                                    return parameters.substring(pos + 1);
378                            }
379                    }
380            }
381    
382            private String _content;
383            private int _lineCount;
384            private String _name;
385            private List<String> _parameterNames;
386            private List<String> _parameterTypes;
387            private int _type;
388    
389    }