001: package javaparser;
002:
003: import javaparser.javacc_gen.Token;
004: import javaparser.javacc_gen.JavaParserConstants;
005: import java.io.*;
006: import java.util.*;
007: import javax.swing.text.*;
008: import javax.swing.tree.*;
009: import javax.swing.JTree;
010:
011: /** Mainly tree utils.
012: */
013: public final class Utils {
014: private Utils() {
015: }
016:
017: public static int getLineNumber(Document doc, int offset) {
018: //int offset = textPane.getCaretPosition();
019: Element map = doc.getDefaultRootElement();
020: int line = map.getElementIndex(offset);
021: return line;
022: }
023:
024: public static Element getLine(Document doc, int line) {
025: Element map = doc.getDefaultRootElement();
026: return map.getElement(line);
027: }
028:
029: public static String getFileContent(File f) throws Exception {
030: FileReader fr = null;
031: try {
032: fr = new FileReader(f);
033: char[] buf = new char[1024];
034: StringBuilder sb = new StringBuilder(Math.max((int) f
035: .length(), 100));
036: int read = 0;
037: while ((read = fr.read(buf)) != -1) {
038: sb.append(buf, 0, read);
039: }
040: return sb.toString();
041: } finally {
042: fr.close();
043: }
044: }
045:
046: /** depth first. used
047: */
048: static Token getFirstSubchild(ParserTreeNode tn) {
049: if (tn.getUserObject() instanceof Token) // first at first level = first
050: {
051: return (Token) tn.getUserObject();
052: }
053:
054: for (int i = 0; i < tn.getChildCount(); i++) {
055: ParserTreeNode ci = tn.getChildNodeAt(i);
056: Token t = getFirstSubchild(ci);
057: if (t != null)
058: return t;
059: }
060: // none found
061: return null;
062: }
063:
064: /** @param line 1 for the first
065: */
066: public static boolean isInto(int line, int col, Token from, Token to) {
067: if (from == null)
068: return false;
069: if (to == null)
070: return false;
071: if (line < from.beginLine)
072: return false;
073: if (line > to.endLine)
074: return false;
075: // TODO: look at columns
076:
077: return true;
078: }
079:
080: /** @param node pass the public, private of package scope or protected node as argument.
081: */
082: @tide.annotations.Recurse
083: public static void getAllMethods(ParserTreeNode node,
084: List<MethodNode> found) {
085: for (int i = 0; i < node.getChildCount(); i++) {
086: ParserTreeNode ci = node.getChildNodeAt(i);
087: if (ci instanceof MethodNode) {
088: found.add((MethodNode) ci);
089: }
090:
091: // recurse
092: getAllMethods(ci, found);
093: }
094: }
095:
096: /** @param node pass the public, private of package scope or protected node as argument.
097: */
098: @tide.annotations.Recurse
099: public static void getAllFields(ParserTreeNode node,
100: List<FieldNode> found) {
101: for (int i = 0; i < node.getChildCount(); i++) {
102: ParserTreeNode ci = node.getChildNodeAt(i);
103: if (ci instanceof FieldNode) {
104: found.add((FieldNode) ci);
105: }
106:
107: // recurse
108: getAllFields(ci, found);
109: }
110: }
111:
112: /** null if none, null if tn is null. used
113: */
114: @edu.umd.cs.findbugs.annotations.CheckForNull
115: static Token getFirstToken_ONLYInDirectChilds(ParserTreeNode tn,
116: int kind) {
117: if (tn == null)
118: return null;
119: for (int i = 0; i < tn.getChildCount(); i++) {
120: ParserTreeNode ci = tn.getChildNodeAt(i);
121: if (ci.isToken() && ci.getToken().kind == kind)
122: return ci.getToken();
123: }
124: // none found
125: return null;
126: }
127:
128: /** depth first. used
129: */
130: @tide.annotations.Recurse
131: static Token getLastSubchild(ParserTreeNode tn) {
132: if (tn.getUserObject() instanceof Token) // last at first level = last
133: {
134: return (Token) tn.getUserObject();
135: }
136:
137: // if(tn.tokens!=null && tn.tokens.size()>0) return tn.tokens.get(tn.tokens.size()-1);
138:
139: for (int i = tn.getChildCount() - 1; i >= 0; i--) // reverse search
140: {
141: ParserTreeNode ci = tn.getChildNodeAt(i);
142:
143: Token t = getLastSubchild(ci);
144: if (t != null)
145: return t;
146: }
147: // none found
148: return null;
149: }
150:
151: /** depth first. used
152: */
153: @tide.annotations.Recurse
154: static void collectChilds(ParserTreeNode tn, List<Token> tokens) {
155: if (tn.getUserObject() instanceof Token) {
156: tokens.add((Token) tn.getUserObject());
157: return; // a token is terminal
158: }
159:
160: for (int i = 0; i < tn.getChildCount(); i++) {
161: ParserTreeNode ci = tn.getChildNodeAt(i);
162: collectChilds(ci, tokens);
163: }
164: }
165:
166: static String getImageOfAllSubElements(ParserTreeNode tn) {
167: ArrayList<Token> tokens = new ArrayList<Token>();
168: collectChilds(tn, tokens);
169: StringBuilder sb = new StringBuilder();
170: for (int i = 0; i < tokens.size(); i++) {
171: //if(i>0) sb.append(" ");
172: sb.append(tokens.get(i).image);
173:
174: }
175: return sb.toString();
176: }
177:
178: /** makes the given number of levels childs expanded.
179: */
180: public static void makeChildsExpanded(ParserTreeNode node,
181: JTree tree, int numberOfLevels) {
182: for (int i = 0; i < node.getChildCount(); i++) {
183: ParserTreeNode ni = node.getChildNodeAt(i);
184: if (i == 0) {
185: tree.makeVisible(new TreePath(ni.getPath())); // out of bounds here !! ((Make visible ??)
186: }
187:
188: // recurse
189: if (numberOfLevels > 0) {
190: makeChildsExpanded(ni, tree, numberOfLevels - 1);
191: }
192: }
193: }
194:
195: /** if any mod found, a space after is present
196: Annotations are ignored
197: *
198: static String getModifiers(ParserTreeNode mods)
199: {
200: if(mods==null) return "<ERR: no modifiers>"; // ERROR
201: if(mods.getChildCount()==0) return "";
202: StringBuilder mod = new StringBuilder();
203: for(int i=0; i<mods.getChildCount(); i++)
204: {
205: ParserTreeNode ci = mods.getChildNodeAt(i);
206: if(!ci.toString().equals("Annotation"))
207: {
208: mod.append(ci);
209: mod.append(" ");
210: }
211: }
212: return mod.toString();
213: }
214:
215: /** Only "s f a t y n v" ignores access modifiers as {public protected packagescope private}. Is used.
216: */
217: static String getModifiersShortString(int[] mods) {
218: StringBuilder mod = new StringBuilder();
219: for (int mi : mods) {
220: switch (mi) {
221: case -1:
222: break;
223: case JavaParserConstants.PUBLIC:
224: break; // ignore, because already present in the parent node that collect them.
225: case JavaParserConstants.PRIVATE:
226: break;
227: case JavaParserConstants.PROTECTED:
228: break;
229: case JavaParserConstants.FINAL:
230: mod.append("f");
231: break;
232: case JavaParserConstants.STATIC:
233: mod.append("s");
234: break;
235: case JavaParserConstants.ABSTRACT:
236: mod.append("a");
237: break;
238: case JavaParserConstants.STRICTFP:
239: mod.append("t");
240: break;
241: case JavaParserConstants.SYNCHRONIZED:
242: mod.append("y");
243: break;
244: case JavaParserConstants.NATIVE:
245: mod.append("n");
246: break;
247: case JavaParserConstants.TRANSIENT:
248: mod.append("r");
249: break;
250: case JavaParserConstants.VOLATILE:
251: mod.append("v");
252: break;
253: default:
254: mod.append(" ?ERROR? " + mi); // unknow !! => add it
255: }
256: }
257: return mod.toString();
258: }
259:
260: /*
261: public static String getModifiersFullString(int[] mods)
262: {
263: if(mods.length==0) return "";
264: final StringBuilder mod = new StringBuilder();
265: for(int mi : mods)
266: {
267: if(mod.length()>0) mod.append(" ");
268:
269: if(mi==-1)
270: {
271: mod.append("ERROR");
272: }
273: else
274: {
275: mod.append(JavaParserConstants.tokenImage[mi]);
276: }
277: }
278:
279: return mod.toString();
280: }*/
281:
282: public static String getModifiersWithoutAccessModifiersFullString(
283: int[] mods) {
284: if (mods.length == 0)
285: return "";
286: final StringBuilder mod = new StringBuilder();
287: for (int mi : mods) {
288: if (isAccessModifier(mi))
289: continue;
290:
291: if (mod.length() > 0)
292: mod.append(" ");
293:
294: if (mi == -1) {
295: mod.append("ERROR");
296: } else {
297: String tim = JavaParserConstants.tokenImage[mi];
298: if (tim.startsWith("\""))
299: tim = tim.substring(1, tim.length() - 1);
300: mod.append(tim);
301: }
302: }
303:
304: return mod.toString();
305: }
306:
307: private static boolean contains(int[] mods, int mod) {
308: if (mods == null)
309: return false; // Warn ??
310: for (int modi : mods) {
311: if (modi == mod)
312: return true;
313: }
314: return false;
315: }
316:
317: public static boolean isAccessModifier(int mod) {
318: if (mod == JavaParserConstants.PUBLIC)
319: return true;
320: if (mod == JavaParserConstants.PRIVATE)
321: return true;
322: if (mod == JavaParserConstants.PROTECTED)
323: return true;
324:
325: return false;
326: }
327:
328: /** looks if JavaParserConstants.PUBLIC is present.
329: */
330: public static boolean isPublic(int[] mods) {
331: return contains(mods, JavaParserConstants.PUBLIC);
332: }
333:
334: /** looks if JavaParserConstants.PRIVATE is present.
335: */
336: public static boolean isPrivate(int[] mods) {
337: return contains(mods, JavaParserConstants.PRIVATE);
338: }
339:
340: /** looks if JavaParserConstants.PROTECTED is present.
341: */
342: public static boolean isProtected(int[] mods) {
343: return contains(mods, JavaParserConstants.PROTECTED);
344: }
345:
346: /** looks if JavaParserConstants.PUBLIC is present.
347: */
348: public static boolean isStatic(int[] mods) {
349: return contains(mods, JavaParserConstants.STATIC);
350: }
351:
352: /** Can be used to compare accorting ordering {public, pscope, protected, private}
353: * pscope araises when no other modifier (pub, pri, pro) are declared.
354: */
355: public static int compareModifiers(int[] m1, int[] m2) {
356: int o1 = getOrderForSort(m1);
357: int o2 = getOrderForSort(m2);
358: if (o1 == o2)
359: return 0;
360: if (o1 < o2)
361: return 1;
362: return -1;
363: }
364:
365: /** Can be used to compare accorting ordering {1=public, 2=pscope, 3=protected, 4=private}
366: * pscope araises when no other modifier (pub, pri, pro) are declared.
367: *
368: */
369: public static int getOrderForSort(int[] mods) {
370: for (int mi : mods) {
371: if (mi == JavaParserConstants.PUBLIC)
372: return 1;
373: if (mi == JavaParserConstants.PROTECTED)
374: return 3;
375: if (mi == JavaParserConstants.PRIVATE)
376: return 4;
377: }
378: return 2;
379: }
380:
381: /* Collects the paramters.
382: *
383: public static List<Parameter> getParameters(RAWParserTreeNode formalParametersNode)
384: {
385: List<Parameter> params = new ArrayList<Parameter>();
386: for(int i=0; i<formalParametersNode.getChildCount(); i++)
387: {
388: RAWParserTreeNode ci = formalParametersNode.getChildNodeAt(i);
389: if(ci.toString().equals("FormalParameter"))
390: {
391: params.add( Parameter.parseFromFormalParamNode(ci) );
392: }
393: }
394: return params;
395: }*/
396:
397: /** Used for fields and methods and constructors.
398: * Uses node's sort order provided by the comparable interface.
399: */
400: public static void sortedInsert(final ParserTreeNode node,
401: final ParserTreeNode destination) {
402: for (int i = 0; i < destination.getChildCount(); i++) {
403: ParserTreeNode ci = destination.getChildNodeAt(i);
404: if (node.compareTo(ci) > 0) {
405: destination.insert(node, i);
406: return;
407: }
408: }
409: // append at end
410: destination.add(node);
411: }
412:
413: /* used for extends, implements, ...
414: *
415: static List<String> collectIdentifiers(ParserTreeNode node, String name)
416: {
417: List<String> ids = new ArrayList<String>();
418: for(int i=0; i<node.getChildCount(); i++)
419: {
420: ParserTreeNode ci = node.getChildNodeAt(i);
421: if(ci.toString().equals(name))
422: {
423: ids.add( Utils.getImageOfAllSubElements(ci) );
424: }
425: }
426: return ids;
427: }*/
428:
429: /** Simple helper.
430: */
431: public static String toStringWithoutBraces(List<?> v) {
432: StringBuilder sb = new StringBuilder();
433: for (int i = 0; i < v.size(); i++) {
434: if (i > 0)
435: sb.append(", ");
436: sb.append("" + v.get(i));
437: }
438: return sb.toString();
439: }
440:
441: }
|