257
258 /**
259 * Get the set of fields for a tree node that may contain child tree nodes.
260 * These are the fields that are subtypes of JCTree or List.
261 * The results are cached, based on the tree's tag.
262 */
263 Set<Field> getFields(JCTree tree) {
264 Set<Field> fields = map.get(tree.getTag());
265 if (fields == null) {
266 fields = new HashSet<Field>();
267 for (Field f: tree.getClass().getFields()) {
268 Class<?> fc = f.getType();
269 if (JCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
270 fields.add(f);
271 }
272 map.put(tree.getTag(), fields);
273 }
274 return fields;
275 }
276 // where
277 Map<Integer, Set<Field>> map = new HashMap<Integer,Set<Field>>();
278
279 /** Get the line number for the primary position for a tree.
280 * The code is intended to be simple, although not necessarily efficient.
281 * However, note that a file manager such as JavacFileManager is likely
282 * to cache the results of file.getCharContent, avoiding the need to read
283 * the bits from disk each time this method is called.
284 */
285 int getLine(JavaFileObject file, JCTree tree) {
286 try {
287 CharSequence cs = file.getCharContent(true);
288 int line = 1;
289 for (int i = 0; i < tree.pos; i++) {
290 if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
291 line++;
292 }
293 return line;
294 } catch (IOException e) {
295 return -1;
296 }
297 }
|
257
258 /**
259 * Get the set of fields for a tree node that may contain child tree nodes.
260 * These are the fields that are subtypes of JCTree or List.
261 * The results are cached, based on the tree's tag.
262 */
263 Set<Field> getFields(JCTree tree) {
264 Set<Field> fields = map.get(tree.getTag());
265 if (fields == null) {
266 fields = new HashSet<Field>();
267 for (Field f: tree.getClass().getFields()) {
268 Class<?> fc = f.getType();
269 if (JCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
270 fields.add(f);
271 }
272 map.put(tree.getTag(), fields);
273 }
274 return fields;
275 }
276 // where
277 Map<JCTree.Tag, Set<Field>> map = new HashMap<JCTree.Tag,Set<Field>>();
278
279 /** Get the line number for the primary position for a tree.
280 * The code is intended to be simple, although not necessarily efficient.
281 * However, note that a file manager such as JavacFileManager is likely
282 * to cache the results of file.getCharContent, avoiding the need to read
283 * the bits from disk each time this method is called.
284 */
285 int getLine(JavaFileObject file, JCTree tree) {
286 try {
287 CharSequence cs = file.getCharContent(true);
288 int line = 1;
289 for (int i = 0; i < tree.pos; i++) {
290 if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
291 line++;
292 }
293 return line;
294 } catch (IOException e) {
295 return -1;
296 }
297 }
|