1 /* |
|
2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 package sun.jvm.hotspot.bugspot; |
|
26 |
|
27 import java.io.*; |
|
28 |
|
29 /** Scans a .java file for the package that it is in. */ |
|
30 |
|
31 public class PackageScanner { |
|
32 |
|
33 public PackageScanner() { |
|
34 } |
|
35 |
|
36 public String scan(String filename) { |
|
37 return scan(new File(filename)); |
|
38 } |
|
39 |
|
40 /** Returns the String comprising the package name of the classes in |
|
41 this .java file. Returns the (non-null) empty string if any |
|
42 error occurs or if the classes are in the unnamed package. */ |
|
43 public String scan(File file) { |
|
44 BufferedReader buf = null; |
|
45 String res = ""; |
|
46 try { |
|
47 buf = new BufferedReader(new FileReader(file)); |
|
48 StreamTokenizer tok = new StreamTokenizer(buf); |
|
49 tok.slashStarComments(true); |
|
50 tok.slashSlashComments(true); |
|
51 if (tok.nextToken() != StreamTokenizer.TT_WORD) { |
|
52 return res; |
|
53 } |
|
54 if (!tok.sval.equals("package")) { |
|
55 return res; |
|
56 } |
|
57 if (tok.nextToken() != StreamTokenizer.TT_WORD) { |
|
58 return res; |
|
59 } |
|
60 res = tok.sval; |
|
61 return res; |
|
62 } catch (FileNotFoundException e) { |
|
63 return res; |
|
64 } catch (IOException e) { |
|
65 return res; |
|
66 } finally { |
|
67 try { |
|
68 if (buf != null) { |
|
69 buf.close(); |
|
70 } |
|
71 } catch (IOException e) { |
|
72 } |
|
73 } |
|
74 } |
|
75 |
|
76 public static void main(String[] args) { |
|
77 if (args.length != 1) { |
|
78 usage(); |
|
79 } |
|
80 |
|
81 System.out.println(new PackageScanner().scan(args[0])); |
|
82 } |
|
83 |
|
84 private static void usage() { |
|
85 System.err.println("Usage: java PackageScanner <.java file name>"); |
|
86 System.err.println("Prints package the .java file is in to stdout."); |
|
87 System.exit(1); |
|
88 } |
|
89 } |
|