23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.jdeps; |
26 package com.sun.tools.jdeps; |
27 |
27 |
28 import com.sun.tools.classfile.ClassFile; |
|
29 import com.sun.tools.classfile.Dependency.Location; |
28 import com.sun.tools.classfile.Dependency.Location; |
30 |
29 |
31 import java.io.IOException; |
30 import java.io.IOException; |
32 import java.io.UncheckedIOException; |
31 import java.io.UncheckedIOException; |
|
32 import java.net.URI; |
|
33 import java.nio.file.Files; |
33 import java.nio.file.Path; |
34 import java.nio.file.Path; |
|
35 import java.nio.file.Paths; |
34 import java.util.HashSet; |
36 import java.util.HashSet; |
35 import java.util.Map; |
37 import java.util.Map; |
|
38 import java.util.Objects; |
36 import java.util.Set; |
39 import java.util.Set; |
37 import java.util.concurrent.ConcurrentHashMap; |
40 import java.util.concurrent.ConcurrentHashMap; |
38 |
41 |
39 /** |
42 /** |
40 * Represents the source of the class files. |
43 * Represents the source of the class files. |
46 } catch (IOException e) { |
49 } catch (IOException e) { |
47 throw new UncheckedIOException(e); |
50 throw new UncheckedIOException(e); |
48 } |
51 } |
49 } |
52 } |
50 |
53 |
|
54 private final URI location; |
51 private final Path path; |
55 private final Path path; |
52 private final String filename; |
56 private final String filename; |
53 private final ClassFileReader reader; |
57 private final ClassFileReader reader; |
|
58 |
54 protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>(); |
59 protected Map<Location, Set<Location>> deps = new ConcurrentHashMap<>(); |
55 |
60 |
56 protected Archive(String name) { |
61 protected Archive(String name) { |
57 this(name, null); |
62 this(name, null, null); |
58 } |
63 } |
59 protected Archive(String name, ClassFileReader reader) { |
64 protected Archive(String name, URI location, ClassFileReader reader) { |
60 this.path = null; |
65 this.location = location; |
|
66 this.path = location != null ? Paths.get(location) : null; |
61 this.filename = name; |
67 this.filename = name; |
62 this.reader = reader; |
68 this.reader = reader; |
63 } |
69 } |
64 protected Archive(Path p, ClassFileReader reader) { |
70 protected Archive(Path p, ClassFileReader reader) { |
|
71 this.location = null; |
65 this.path = p; |
72 this.path = p; |
66 this.filename = path.getFileName().toString(); |
73 this.filename = path.getFileName().toString(); |
67 this.reader = reader; |
74 this.reader = reader; |
68 } |
75 } |
69 |
76 |
93 v.visit(e.getKey(), target); |
104 v.visit(e.getKey(), target); |
94 } |
105 } |
95 } |
106 } |
96 } |
107 } |
97 |
108 |
|
109 /** |
|
110 * Tests if any class has been parsed. |
|
111 */ |
98 public boolean isEmpty() { |
112 public boolean isEmpty() { |
99 return getClasses().isEmpty(); |
113 return getClasses().isEmpty(); |
100 } |
114 } |
101 |
115 |
102 public String getPathName() { |
116 public String getPathName() { |
103 return path != null ? path.toString() : filename; |
117 return path != null ? path.toString() : filename; |
104 } |
118 } |
105 |
119 |
|
120 @Override |
|
121 public int hashCode() { |
|
122 return Objects.hash(this.filename, this.path); |
|
123 } |
|
124 |
|
125 @Override |
|
126 public boolean equals(Object o) { |
|
127 if (o instanceof Archive) { |
|
128 Archive other = (Archive)o; |
|
129 if (path == other.path || isSameLocation(this, other)) |
|
130 return true; |
|
131 } |
|
132 return false; |
|
133 } |
|
134 |
|
135 @Override |
106 public String toString() { |
136 public String toString() { |
107 return filename; |
137 return filename; |
108 } |
138 } |
109 |
139 |
110 public Path path() { |
140 public Path path() { |
111 return path; |
141 return path; |
112 } |
142 } |
113 |
143 |
|
144 public static boolean isSameLocation(Archive archive, Archive other) { |
|
145 if (archive.path == null || other.path == null) |
|
146 return false; |
|
147 |
|
148 if (archive.location != null && other.location != null && |
|
149 archive.location.equals(other.location)) { |
|
150 return true; |
|
151 } |
|
152 |
|
153 if (archive.isJrt() || other.isJrt()) { |
|
154 return false; |
|
155 } |
|
156 |
|
157 try { |
|
158 return Files.isSameFile(archive.path, other.path); |
|
159 } catch (IOException e) { |
|
160 throw new UncheckedIOException(e); |
|
161 } |
|
162 } |
|
163 |
|
164 private boolean isJrt() { |
|
165 return location != null && location.getScheme().equals("jrt"); |
|
166 } |
114 interface Visitor { |
167 interface Visitor { |
115 void visit(Location origin, Location target); |
168 void visit(Location origin, Location target); |
116 } |
169 } |
117 } |
170 } |