25874
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package com.sun.tools.jdeps;
|
|
26 |
|
|
27 |
import com.sun.tools.classfile.ClassFile;
|
|
28 |
import com.sun.tools.jdeps.PlatformClassPath.LegacyImageHelper;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.nio.file.Files;
|
|
32 |
import java.nio.file.Path;
|
|
33 |
import java.util.Collections;
|
|
34 |
import java.util.HashSet;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.Set;
|
|
37 |
import javax.xml.namespace.QName;
|
|
38 |
import javax.xml.stream.XMLEventReader;
|
|
39 |
import javax.xml.stream.XMLInputFactory;
|
|
40 |
import javax.xml.stream.XMLStreamException;
|
|
41 |
import javax.xml.stream.events.Attribute;
|
|
42 |
import javax.xml.stream.events.XMLEvent;
|
|
43 |
|
|
44 |
abstract class ModulesXmlReader {
|
|
45 |
abstract ClassFileReader getClassFileReader(String modulename, Set<String> packages)
|
|
46 |
throws IOException;
|
|
47 |
|
|
48 |
static class ImageReader extends ModulesXmlReader {
|
|
49 |
final LegacyImageHelper helper;
|
|
50 |
ImageReader(LegacyImageHelper helper) {
|
|
51 |
this.helper = helper;
|
|
52 |
}
|
|
53 |
ClassFileReader getClassFileReader(String modulename, Set<String> packages)
|
|
54 |
throws IOException
|
|
55 |
{
|
|
56 |
return helper.getClassReader(modulename, packages);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
static class ModulePathReader extends ModulesXmlReader {
|
|
61 |
final Path mpath;
|
|
62 |
final ClassFileReader defaultReader;
|
|
63 |
ModulePathReader(Path mp) throws IOException {
|
|
64 |
this.mpath = mp;
|
|
65 |
this.defaultReader = new NonExistModuleReader(mpath);
|
|
66 |
}
|
|
67 |
ClassFileReader getClassFileReader(String modulename, Set<String> packages)
|
|
68 |
throws IOException
|
|
69 |
{
|
|
70 |
Path mdir = mpath.resolve(modulename);
|
|
71 |
if (Files.exists(mdir) && Files.isDirectory(mdir)) {
|
|
72 |
return ClassFileReader.newInstance(mdir);
|
|
73 |
} else {
|
|
74 |
// aggregator module or os-specific module in modules.xml
|
|
75 |
// mdir not exist
|
|
76 |
return defaultReader;
|
|
77 |
}
|
|
78 |
}
|
|
79 |
class NonExistModuleReader extends ClassFileReader {
|
|
80 |
private final List<ClassFile> classes = Collections.emptyList();
|
|
81 |
private NonExistModuleReader(Path mpath) {
|
|
82 |
super(mpath);
|
|
83 |
}
|
|
84 |
|
|
85 |
public ClassFile getClassFile(String name) throws IOException {
|
|
86 |
return null;
|
|
87 |
}
|
|
88 |
public Iterable<ClassFile> getClassFiles() throws IOException {
|
|
89 |
return classes;
|
|
90 |
}
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
public static Set<Module> load(Path mpath, InputStream in)
|
|
95 |
throws IOException
|
|
96 |
{
|
|
97 |
try {
|
|
98 |
ModulePathReader reader = new ModulePathReader(mpath);
|
|
99 |
return reader.load(in);
|
|
100 |
} catch (XMLStreamException e) {
|
|
101 |
throw new RuntimeException(e);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
public static Set<Module> loadFromImage(LegacyImageHelper helper, InputStream in)
|
|
106 |
throws IOException
|
|
107 |
{
|
|
108 |
try {
|
|
109 |
ImageReader reader = new ImageReader(helper);
|
|
110 |
return reader.load(in);
|
|
111 |
} catch (XMLStreamException e) {
|
|
112 |
throw new RuntimeException(e);
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
private static final String MODULES = "modules";
|
|
117 |
private static final String MODULE = "module";
|
|
118 |
private static final String NAME = "name";
|
|
119 |
private static final String DEPEND = "depend";
|
|
120 |
private static final String EXPORT = "export";
|
|
121 |
private static final String TO = "to";
|
|
122 |
private static final String INCLUDE = "include";
|
|
123 |
private static final QName REEXPORTS = new QName("re-exports");
|
|
124 |
public Set<Module> load(InputStream in) throws XMLStreamException, IOException {
|
|
125 |
Set<Module> modules = new HashSet<>();
|
|
126 |
if (in == null) {
|
|
127 |
System.err.println("WARNING: modules.xml doesn't exist");
|
|
128 |
return modules;
|
|
129 |
}
|
|
130 |
XMLInputFactory factory = XMLInputFactory.newInstance();
|
|
131 |
XMLEventReader reader = factory.createXMLEventReader(in, "UTF-8");
|
|
132 |
Module.Builder mb = null;
|
|
133 |
String modulename = null;
|
|
134 |
String exportedPackage = null;
|
|
135 |
Set<String> permits = new HashSet<>();
|
|
136 |
while (reader.hasNext()) {
|
|
137 |
XMLEvent event = reader.nextEvent();
|
|
138 |
if (event.isStartElement()) {
|
|
139 |
String startTag = event.asStartElement().getName().getLocalPart();
|
|
140 |
switch (startTag) {
|
|
141 |
case MODULES:
|
|
142 |
break;
|
|
143 |
case MODULE:
|
|
144 |
if (mb != null) {
|
|
145 |
throw new RuntimeException("end tag for module is missing");
|
|
146 |
}
|
|
147 |
modulename = getNextTag(reader, NAME);
|
|
148 |
mb = new Module.Builder();
|
|
149 |
mb.name(modulename);
|
|
150 |
break;
|
|
151 |
case NAME:
|
|
152 |
throw new RuntimeException(event.toString());
|
|
153 |
case DEPEND:
|
|
154 |
boolean reexports = false;
|
|
155 |
Attribute attr = event.asStartElement().getAttributeByName(REEXPORTS);
|
|
156 |
if (attr != null) {
|
|
157 |
String value = attr.getValue();
|
|
158 |
if (value.equals("true") || value.equals("false")) {
|
|
159 |
reexports = Boolean.parseBoolean(value);
|
|
160 |
} else {
|
|
161 |
throw new RuntimeException("unexpected attribute " + attr.toString());
|
|
162 |
}
|
|
163 |
}
|
|
164 |
mb.require(getData(reader), reexports);
|
|
165 |
break;
|
|
166 |
case INCLUDE:
|
|
167 |
mb.include(getData(reader));
|
|
168 |
break;
|
|
169 |
case EXPORT:
|
|
170 |
exportedPackage = getNextTag(reader, NAME);
|
|
171 |
break;
|
|
172 |
case TO:
|
|
173 |
permits.add(getData(reader));
|
|
174 |
break;
|
|
175 |
default:
|
|
176 |
throw new RuntimeException("invalid element: " + event);
|
|
177 |
}
|
|
178 |
} else if (event.isEndElement()) {
|
|
179 |
String endTag = event.asEndElement().getName().getLocalPart();
|
|
180 |
switch (endTag) {
|
|
181 |
case MODULE:
|
|
182 |
ClassFileReader cfr = getClassFileReader(modulename, mb.packages);
|
|
183 |
mb.classes(cfr);
|
|
184 |
modules.add(mb.build());
|
|
185 |
mb = null;
|
|
186 |
break;
|
|
187 |
case EXPORT:
|
|
188 |
if (exportedPackage == null) {
|
|
189 |
throw new RuntimeException("export's name is missing");
|
|
190 |
}
|
|
191 |
mb.export(exportedPackage, permits);
|
|
192 |
exportedPackage = null;
|
|
193 |
permits.clear();
|
|
194 |
break;
|
|
195 |
default:
|
|
196 |
}
|
|
197 |
} else if (event.isCharacters()) {
|
|
198 |
String s = event.asCharacters().getData();
|
|
199 |
if (!s.trim().isEmpty()) {
|
|
200 |
throw new RuntimeException("export-to is malformed");
|
|
201 |
}
|
|
202 |
}
|
|
203 |
}
|
|
204 |
return modules;
|
|
205 |
}
|
|
206 |
private String getData(XMLEventReader reader) throws XMLStreamException {
|
|
207 |
XMLEvent e = reader.nextEvent();
|
|
208 |
if (e.isCharacters()) {
|
|
209 |
return e.asCharacters().getData();
|
|
210 |
}
|
|
211 |
throw new RuntimeException(e.toString());
|
|
212 |
}
|
|
213 |
|
|
214 |
private String getNextTag(XMLEventReader reader, String tag) throws XMLStreamException {
|
|
215 |
XMLEvent e = reader.nextTag();
|
|
216 |
if (e.isStartElement()) {
|
|
217 |
String t = e.asStartElement().getName().getLocalPart();
|
|
218 |
if (!tag.equals(t)) {
|
|
219 |
throw new RuntimeException(e + " expected: " + tag);
|
|
220 |
}
|
|
221 |
return getData(reader);
|
|
222 |
}
|
|
223 |
throw new RuntimeException("export-to name is missing:" + e);
|
|
224 |
}
|
|
225 |
}
|