4524
|
1 |
/*
|
|
2 |
* Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
package com.sun.classanalyzer;
|
|
24 |
|
|
25 |
import java.io.*;
|
|
26 |
import java.util.*;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* A simple tool to check module dependencies against a known list of
|
|
30 |
* dependencies. The tool fails (by throwing a RuntimeException) is an
|
|
31 |
* unexpected dependency is detected.
|
|
32 |
*/
|
|
33 |
|
|
34 |
public class CheckDeps {
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Represents a dependency from one module to another module. The dependency
|
|
38 |
* may be optional.
|
|
39 |
*/
|
|
40 |
static class Dependency {
|
|
41 |
private final String module;
|
|
42 |
private final String other;
|
|
43 |
private final boolean optional;
|
|
44 |
|
|
45 |
private Dependency(String module, String other, boolean optional) {
|
|
46 |
this.module = module;
|
|
47 |
this.other = other;
|
|
48 |
this.optional = optional;
|
|
49 |
}
|
|
50 |
|
|
51 |
String module() { return module; }
|
|
52 |
String other() { return other; }
|
|
53 |
boolean isOptional() { return optional; }
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Parses a dependency in one of the following forms:
|
|
57 |
* a -> b
|
|
58 |
* [optional] a -> b
|
|
59 |
*/
|
|
60 |
static Dependency fromString(String s) {
|
|
61 |
String[] components = s.split(" ");
|
|
62 |
int count = components.length;
|
|
63 |
if (count != 3 && count != 4)
|
|
64 |
throw new IllegalArgumentException(s);
|
|
65 |
boolean optional = (count == 4);
|
|
66 |
if (optional && !components[0].equals("[optional]"))
|
|
67 |
throw new IllegalArgumentException(s);
|
|
68 |
String arrow = optional ? components[2] : components[1];
|
|
69 |
if (!arrow.equals("->"))
|
|
70 |
throw new IllegalArgumentException(s);
|
|
71 |
String module = optional ? components[1] : components[0];
|
|
72 |
String other = optional ? components[3] : components[2];
|
|
73 |
return new Dependency(module, other, optional);
|
|
74 |
}
|
|
75 |
|
|
76 |
@Override public String toString() {
|
|
77 |
StringBuilder sb = new StringBuilder();
|
|
78 |
if (optional)
|
|
79 |
sb.append("[optional] ");
|
|
80 |
sb.append(module);
|
|
81 |
sb.append(" -> ");
|
|
82 |
sb.append(other);
|
|
83 |
return sb.toString();
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Represents the "tail"
|
|
89 |
*/
|
|
90 |
static class DependencyTail {
|
|
91 |
private final String module;
|
|
92 |
private final boolean optional;
|
|
93 |
|
|
94 |
DependencyTail(String module, boolean optional) {
|
|
95 |
this.module = module;
|
|
96 |
this.optional = optional;
|
|
97 |
}
|
|
98 |
String module() { return module; }
|
|
99 |
boolean isOptional() { return optional; }
|
|
100 |
}
|
|
101 |
|
|
102 |
static void usage() {
|
|
103 |
System.out.println("java CheckDeps file1 file2");
|
|
104 |
System.out.println(" where file1 is the expected dependencies and file2 is");
|
|
105 |
System.out.println(" the actual dependencies. Both files are assumed to be");
|
|
106 |
System.out.println(" in modules.summary format (see ClassAnalyzer tool).");
|
|
107 |
System.out.println();
|
|
108 |
System.out.println("Example usages:");
|
|
109 |
System.out.println(" java CheckDeps make/modules/modules.summary " +
|
|
110 |
"$(OUTPUTDIR)/modules.summary");
|
|
111 |
System.exit(-1);
|
|
112 |
}
|
|
113 |
|
|
114 |
public static void main(String[] args) throws IOException {
|
|
115 |
if (args.length != 2)
|
|
116 |
usage();
|
|
117 |
|
|
118 |
// maps a module to the list of modules that it depends on
|
|
119 |
Map<String,List<DependencyTail>> expected =
|
|
120 |
new HashMap<String,List<DependencyTail>>();
|
|
121 |
|
|
122 |
// parse the expected dependencies file
|
|
123 |
Scanner s;
|
|
124 |
s = new Scanner(new FileInputStream(args[0]));
|
|
125 |
try {
|
|
126 |
while (s.hasNextLine()) {
|
|
127 |
Dependency ref = Dependency.fromString(s.nextLine());
|
|
128 |
if (ref != null) {
|
|
129 |
String module = ref.module();
|
|
130 |
List<DependencyTail> list = expected.get(module);
|
|
131 |
if (list == null) {
|
|
132 |
list = new ArrayList<DependencyTail>();
|
|
133 |
expected.put(module, list);
|
|
134 |
}
|
|
135 |
list.add(new DependencyTail(ref.other(), ref.isOptional()));
|
|
136 |
}
|
|
137 |
}
|
|
138 |
} finally {
|
|
139 |
s.close();
|
|
140 |
}
|
|
141 |
|
|
142 |
// parse the actual dependencies file, checking each dependency
|
|
143 |
// against the expected list.
|
|
144 |
boolean fail = false;
|
|
145 |
s = new Scanner(new FileInputStream(args[1]));
|
|
146 |
try {
|
|
147 |
while (s.hasNextLine()) {
|
|
148 |
Dependency dep = Dependency.fromString(s.nextLine());
|
|
149 |
|
|
150 |
// check if this dependency is expected
|
|
151 |
List<DependencyTail> list = expected.get(dep.module());
|
|
152 |
DependencyTail tail = null;
|
|
153 |
if (list != null) {
|
|
154 |
for (DependencyTail t: list) {
|
|
155 |
if (t.module().equals(dep.other())) {
|
|
156 |
tail = t;
|
|
157 |
break;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|
|
161 |
if (tail == null) {
|
|
162 |
System.err.println("Unexpected dependency: " + dep);
|
|
163 |
fail = true;
|
|
164 |
} else {
|
|
165 |
// hard dependency when optional dependency is expected
|
|
166 |
if (tail.isOptional() != dep.isOptional()) {
|
|
167 |
if (tail.isOptional()) {
|
|
168 |
System.err.println("Unexpected dependency: " + dep);
|
|
169 |
fail = true;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
} finally {
|
|
175 |
s.close();
|
|
176 |
}
|
|
177 |
|
|
178 |
if (fail)
|
|
179 |
throw new RuntimeException("Unexpected dependencies found");
|
|
180 |
}
|
|
181 |
}
|