Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/Archive.java
41161 views
1
/*
2
* Copyright (c) 2012, 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
26
package com.sun.tools.jdeps;
27
28
import com.sun.tools.classfile.Dependency.Location;
29
30
import java.io.Closeable;
31
import java.io.IOException;
32
import java.io.UncheckedIOException;
33
import java.net.URI;
34
import java.nio.file.Files;
35
import java.nio.file.Path;
36
import java.nio.file.Paths;
37
import java.util.HashSet;
38
import java.util.Map;
39
import java.util.Objects;
40
import java.util.Optional;
41
import java.util.Set;
42
import java.util.concurrent.ConcurrentHashMap;
43
import java.util.stream.Stream;
44
45
import static com.sun.tools.jdeps.Module.trace;
46
47
/**
48
* Represents the source of the class files.
49
*/
50
public class Archive implements Closeable {
51
public static Archive getInstance(Path p, Runtime.Version version) {
52
try {
53
return new Archive(p, ClassFileReader.newInstance(p, version));
54
} catch (IOException e) {
55
throw new UncheckedIOException(e);
56
}
57
}
58
59
private final URI location;
60
private final Path path;
61
private final String filename;
62
private final ClassFileReader reader;
63
64
protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>();
65
66
protected Archive(String name) {
67
this(name, null, null);
68
}
69
protected Archive(String name, URI location, ClassFileReader reader) {
70
this.location = location;
71
this.path = location != null ? Paths.get(location) : null;
72
this.filename = name;
73
this.reader = reader;
74
}
75
protected Archive(Path p, ClassFileReader reader) {
76
this.location = null;
77
this.path = p;
78
this.filename = path.getFileName().toString();
79
this.reader = reader;
80
}
81
82
public ClassFileReader reader() {
83
return reader;
84
}
85
86
public String getName() {
87
return filename;
88
}
89
90
public Module getModule() {
91
return Module.UNNAMED_MODULE;
92
}
93
94
public boolean contains(String entry) {
95
return reader.entries().contains(entry);
96
}
97
98
public void addClass(Location origin) {
99
deps.computeIfAbsent(origin, _k -> new HashSet<>());
100
}
101
102
public void addClass(Location origin, Location target) {
103
deps.computeIfAbsent(origin, _k -> new HashSet<>()).add(target);
104
}
105
106
public Set<Location> getClasses() {
107
return deps.keySet();
108
}
109
110
public Stream<Location> getDependencies() {
111
return deps.values().stream()
112
.flatMap(Set::stream);
113
}
114
115
public boolean hasDependences() {
116
return getDependencies().count() > 0;
117
}
118
119
public void visitDependences(Visitor v) {
120
for (Map.Entry<Location,Set<Location>> e: deps.entrySet()) {
121
for (Location target : e.getValue()) {
122
v.visit(e.getKey(), target);
123
}
124
}
125
}
126
127
/**
128
* Tests if any class has been parsed.
129
*/
130
public boolean isEmpty() {
131
return getClasses().isEmpty();
132
}
133
134
public String getPathName() {
135
return path != null ? path.toString() : filename;
136
}
137
138
public Optional<Path> path() {
139
return Optional.ofNullable(path);
140
}
141
142
@Override
143
public int hashCode() {
144
return Objects.hash(this.filename, this.path);
145
}
146
147
@Override
148
public boolean equals(Object o) {
149
if (o instanceof Archive) {
150
Archive other = (Archive)o;
151
if (path == other.path || isSameLocation(this, other))
152
return true;
153
}
154
return false;
155
}
156
157
@Override
158
public String toString() {
159
return filename;
160
}
161
162
163
public static boolean isSameLocation(Archive archive, Archive other) {
164
if (archive.path == null || other.path == null)
165
return false;
166
167
if (archive.location != null && other.location != null &&
168
archive.location.equals(other.location)) {
169
return true;
170
}
171
172
if (archive.isJrt() || other.isJrt()) {
173
return false;
174
}
175
176
try {
177
return Files.isSameFile(archive.path, other.path);
178
} catch (IOException e) {
179
throw new UncheckedIOException(e);
180
}
181
}
182
183
private boolean isJrt() {
184
return location != null && location.getScheme().equals("jrt");
185
}
186
187
@Override
188
public void close() throws IOException {
189
trace("closing %s %n", getPathName());
190
if (reader != null)
191
reader.close();
192
}
193
194
interface Visitor {
195
void visit(Location origin, Location target);
196
}
197
}
198
199