author | attila |
Wed, 01 Oct 2014 10:26:25 +0200 | |
changeset 26888 | 2722a9c501a5 |
parent 26276 | 0dca2378aa34 |
child 27579 | d1a63c99cdd5 |
permissions | -rw-r--r-- |
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 { |
|
26276
0dca2378aa34
8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents:
25874
diff
changeset
|
74 |
// aggregator module or os-specific module in jdeps-modules.xml |
25874 | 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) { |
|
26276
0dca2378aa34
8055856: checkdeps build target doesn't work for cross-compilation builds
mchung
parents:
25874
diff
changeset
|
127 |
throw new RuntimeException("jdeps-modules.xml doesn't exist"); |
25874 | 128 |
} |
129 |
XMLInputFactory factory = XMLInputFactory.newInstance(); |
|
130 |
XMLEventReader reader = factory.createXMLEventReader(in, "UTF-8"); |
|
131 |
Module.Builder mb = null; |
|
132 |
String modulename = null; |
|
133 |
String exportedPackage = null; |
|
134 |
Set<String> permits = new HashSet<>(); |
|
135 |
while (reader.hasNext()) { |
|
136 |
XMLEvent event = reader.nextEvent(); |
|
137 |
if (event.isStartElement()) { |
|
138 |
String startTag = event.asStartElement().getName().getLocalPart(); |
|
139 |
switch (startTag) { |
|
140 |
case MODULES: |
|
141 |
break; |
|
142 |
case MODULE: |
|
143 |
if (mb != null) { |
|
144 |
throw new RuntimeException("end tag for module is missing"); |
|
145 |
} |
|
146 |
modulename = getNextTag(reader, NAME); |
|
147 |
mb = new Module.Builder(); |
|
148 |
mb.name(modulename); |
|
149 |
break; |
|
150 |
case NAME: |
|
151 |
throw new RuntimeException(event.toString()); |
|
152 |
case DEPEND: |
|
153 |
boolean reexports = false; |
|
154 |
Attribute attr = event.asStartElement().getAttributeByName(REEXPORTS); |
|
155 |
if (attr != null) { |
|
156 |
String value = attr.getValue(); |
|
157 |
if (value.equals("true") || value.equals("false")) { |
|
158 |
reexports = Boolean.parseBoolean(value); |
|
159 |
} else { |
|
160 |
throw new RuntimeException("unexpected attribute " + attr.toString()); |
|
161 |
} |
|
162 |
} |
|
163 |
mb.require(getData(reader), reexports); |
|
164 |
break; |
|
165 |
case INCLUDE: |
|
166 |
mb.include(getData(reader)); |
|
167 |
break; |
|
168 |
case EXPORT: |
|
169 |
exportedPackage = getNextTag(reader, NAME); |
|
170 |
break; |
|
171 |
case TO: |
|
172 |
permits.add(getData(reader)); |
|
173 |
break; |
|
174 |
default: |
|
175 |
throw new RuntimeException("invalid element: " + event); |
|
176 |
} |
|
177 |
} else if (event.isEndElement()) { |
|
178 |
String endTag = event.asEndElement().getName().getLocalPart(); |
|
179 |
switch (endTag) { |
|
180 |
case MODULE: |
|
181 |
ClassFileReader cfr = getClassFileReader(modulename, mb.packages); |
|
182 |
mb.classes(cfr); |
|
183 |
modules.add(mb.build()); |
|
184 |
mb = null; |
|
185 |
break; |
|
186 |
case EXPORT: |
|
187 |
if (exportedPackage == null) { |
|
188 |
throw new RuntimeException("export's name is missing"); |
|
189 |
} |
|
190 |
mb.export(exportedPackage, permits); |
|
191 |
exportedPackage = null; |
|
192 |
permits.clear(); |
|
193 |
break; |
|
194 |
default: |
|
195 |
} |
|
196 |
} else if (event.isCharacters()) { |
|
197 |
String s = event.asCharacters().getData(); |
|
198 |
if (!s.trim().isEmpty()) { |
|
199 |
throw new RuntimeException("export-to is malformed"); |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
return modules; |
|
204 |
} |
|
205 |
private String getData(XMLEventReader reader) throws XMLStreamException { |
|
206 |
XMLEvent e = reader.nextEvent(); |
|
207 |
if (e.isCharacters()) { |
|
208 |
return e.asCharacters().getData(); |
|
209 |
} |
|
210 |
throw new RuntimeException(e.toString()); |
|
211 |
} |
|
212 |
||
213 |
private String getNextTag(XMLEventReader reader, String tag) throws XMLStreamException { |
|
214 |
XMLEvent e = reader.nextTag(); |
|
215 |
if (e.isStartElement()) { |
|
216 |
String t = e.asStartElement().getName().getLocalPart(); |
|
217 |
if (!tag.equals(t)) { |
|
218 |
throw new RuntimeException(e + " expected: " + tag); |
|
219 |
} |
|
220 |
return getData(reader); |
|
221 |
} |
|
222 |
throw new RuntimeException("export-to name is missing:" + e); |
|
223 |
} |
|
224 |
} |